Skip to content

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

DedSec256
Copy link
Contributor

@DedSec256 DedSec256 commented Aug 14, 2025

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:

and SolveTypeEqualsTypeWithReport (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 of displayEnv: 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:

type Exception with
member exn.Output(os: StringBuilder, suggestNames) =
match exn with

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

Copy link
Contributor

github-actions bot commented Aug 14, 2025

❗ Release notes required


✅ Found changes and release notes in following paths:

Change path Release notes path Description
src/Compiler docs/release-notes/.FSharp.Compiler.Service/10.0.100.md

@DedSec256 DedSec256 closed this Aug 14, 2025
@DedSec256 DedSec256 reopened this Aug 14, 2025
@DedSec256 DedSec256 closed this Aug 14, 2025
@DedSec256 DedSec256 reopened this Aug 14, 2025
@DedSec256 DedSec256 closed this Aug 14, 2025
@DedSec256 DedSec256 reopened this Aug 14, 2025
@DedSec256 DedSec256 force-pushed the ber.a/fixTypeEquals branch from 0482af7 to abced93 Compare August 14, 2025 22:33
@DedSec256 DedSec256 changed the title ErrorFromAddingTypeEquation: fix for expected type [WIP] TypeMismatchDiagnosticExtendedData: fix expected and actual types calculation [WIP] Aug 14, 2025
@DedSec256 DedSec256 changed the title TypeMismatchDiagnosticExtendedData: fix expected and actual types calculation [WIP] TypeMismatchDiagnosticExtendedData: fix expected and actual types calculation Aug 14, 2025
@DedSec256 DedSec256 changed the title TypeMismatchDiagnosticExtendedData: fix expected and actual types calculation TypeMismatchDiagnosticExtendedData: fix expected type calculation Aug 15, 2025
@@ -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))
Copy link
Contributor

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 ?

Copy link
Member

Choose a reason for hiding this comment

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

@edgarfgp No, why? 🙂

Copy link
Contributor

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 :)

Copy link
Member

@auduchinok auduchinok left a 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.
@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 owner August 15, 2025 12:12
Sign up for free to 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.

3 participants