-
Notifications
You must be signed in to change notification settings - Fork 824
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
base: main
Are you sure you want to change the base?
Conversation
❗ Release notes required
|
0482af7
to
abced93
Compare
@@ -21,6 +21,7 @@ | |||
|
|||
* Fix SRTP nullness constraint resolution for types imported from older assemblies. AmbivalentToNull types now use legacy F# nullness rules instead of always satisfying `'T : null` constraints. ([Issue #18390](https://github.com/dotnet/fsharp/issues/18390), [Issue #18344](https://github.com/dotnet/fsharp/issues/18344)) | |||
* Fix Show XML doc for enum fields in external metadata ([Issue #17939](https://github.com/dotnet/fsharp/issues/17939#issuecomment-3137410105), [PR #18800](https://github.com/dotnet/fsharp/pull/18800)) | |||
* TypeMismatchDiagnosticExtendedData: fix expected and actual types calculation. ([Issue ](https://github.com/dotnet/fsharp/pull/18851)) |
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.
Should this PR fix #18849 ?
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.
@edgarfgp No, why? 🙂
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.
Just out of curiosity. If not I might give it a try once I finish my open PR's :)
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.
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.