-
Notifications
You must be signed in to change notification settings - Fork 826
TypeMismatchDiagnosticExtendedData: fix expected type calculation #18851
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
❗ Release notes required
|
0482af7
to
abced93
Compare
edgarfgp
reviewed
Aug 15, 2025
auduchinok
approved these changes
Aug 15, 2025
There was a problem hiding this comment.
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.
edgarfgp
approved these changes
Aug 15, 2025
T-Gro
approved these changes
Aug 26, 2025
This was referenced Aug 29, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:fsharp/src/Compiler/Checking/ConstraintSolver.fsi
Lines 154 to 160 in 616530c
Unfortunately, what is referred to here as the “expected” type is not always the real “expected” type.
For example, in the following case:
The
SolveTypeEqualsTypeWithReport
function will create anErrorFromAddingTypeEquation
exception where, after type resolution, the expected type will equal the actual type — both beingint
:fsharp/src/Compiler/Checking/ConstraintSolver.fs
Lines 3182 to 3187 in 616530c
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 containint
instead ofstring
as the expected type.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:fsharp/src/Compiler/Checking/ConstraintSolver.fs
Line 219 in 616530c
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:
fsharp/src/Compiler/Driver/CompilerDiagnostics.fs
Lines 660 to 664 in 616530c
This PR implements this logic when creating
TypeMismatchDiagnosticExtendedData
to correctly identify the expected and actual types.