Skip to content

TypeMismatchDiagnosticExtendedData: fix expected type calculation - #18851

Merged
T-Gro merged 7 commits into
dotnet:mainfrom
DedSec256:ber.a/fixTypeEquals
Aug 26, 2025
Merged

TypeMismatchDiagnosticExtendedData: fix expected type calculation#18851
T-Gro merged 7 commits into
dotnet:mainfrom
DedSec256:ber.a/fixTypeEquals

Conversation

@DedSec256

@DedSec256DedSec256 commented Aug 14, 2025

Copy link
Copy Markdown
Contributor

TypeMismatchDiagnosticExtendedData provides additional data for FSharpDiagnostic that the IDE needs to suggest appropriate quick fixes, such as replacing the expected type with the actual one.
It can be created from an ErrorFromAddingTypeEquation exception, which contains the expected and actual types:

exception ErrorFromAddingTypeEquation of
tcGlobals: TcGlobals *
displayEnv: DisplayEnv *
expectedTy: TType *
actualTy: TType *
error: exn *
range: range

Unfortunately, what is referred to here as the “expected” type is not always the real “expected” type.

For example, in the following case:

""+1

The SolveTypeEqualsTypeWithReport function will create an ErrorFromAddingTypeEquation exception where, after type resolution, the expected type will equal the actual type — both being int:

andSolveTypeEqualsTypeWithReport(csenv:ConstraintSolverEnv)ndeep m trace cxsln expectedTy actualTy =
TryD
(fun()-> SolveTypeEqualsTypeKeepAbbrevsWithCxsln csenv ndeep m trace cxsln expectedTy actualTy)
(function
| AbortForFailedMemberConstraintResolution as err -> ErrorD err
| res -> ErrorD (ErrorFromAddingTypeEquation(csenv.g, csenv.DisplayEnv, expectedTy, actualTy, res, m)))

This, although unexpected from the point of view of naming, is a valid workflow for solving the type equation for the generic + operator.

As a result, TypeMismatchDiagnosticExtendedData will also contain int instead of string as the expected type.

image

The good news is that, to clarify the error in type resolution, ErrorFromAddingTypeEquation contains a nested exception, ConstraintSolverTypesNotInEqualityRelation, which also contains two TType values that can be treated as the expected and actual types:

exception ConstraintSolverTypesNotInEqualityRelation ofdisplayEnv:DisplayEnv*TType*TType*range*range*ContextInfo

Unfortunately, this structure also does not guarantee which TType corresponds to the actual type and which to the expected one. This depends on the order in which they are passed in one of many different calls to the type-checking logic.

Despite this, FCS still needs to correctly identify the expected and actual types to produce an accurate type mismatch error. This logic is implemented here:

typeExceptionwith
memberexn.Output(os:StringBuilder,suggestNames)=
match exn with

This PR implements this logic when creating TypeMismatchDiagnosticExtendedData to correctly identify the expected and actual types.

@github-actions

github-actionsBot commented Aug 14, 2025

Copy link
Copy Markdown
Contributor

❗ Release notes required


✅ Found changes and release notes in following paths:

Change pathRelease notes pathDescription
src/Compilerdocs/release-notes/.FSharp.Compiler.Service/10.0.100.md

@DedSec256DedSec256 changed the title ErrorFromAddingTypeEquation: fix for expected type [WIP]TypeMismatchDiagnosticExtendedData: fix expected and actual types calculation [WIP]Aug 14, 2025
@DedSec256DedSec256 changed the title TypeMismatchDiagnosticExtendedData: fix expected and actual types calculation [WIP]TypeMismatchDiagnosticExtendedData: fix expected and actual types calculationAug 14, 2025
@DedSec256DedSec256 changed the title TypeMismatchDiagnosticExtendedData: fix expected and actual types calculationTypeMismatchDiagnosticExtendedData: fix expected type calculationAug 15, 2025
Comment threaddocs/release-notes/.FSharp.Compiler.Service/10.0.100.md

@auduchinokauduchinok left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DedSec256 This is very good work, thanks!

This reverts commit 01192b1.
@DedSec256
DedSec256 marked this pull request as ready for review August 15, 2025 12:12
@DedSec256
DedSec256 requested a review from a team as a code ownerAugust 15, 2025 12:12
@T-Gro
T-Gro merged commit 6a1e4af into dotnet:mainAug 26, 2025
38 checks passed
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

4 participants

@DedSec256@auduchinok@edgarfgp@T-Gro