Skip to content

Commit bf88205

Browse files
authored
Fix expected and actual types in ErrorFromAddingTypeEquation message and extended diagnostic data (#18915)
1 parent 92ff3eb commit bf88205

File tree

6 files changed

+156
-97
lines changed

6 files changed

+156
-97
lines changed

docs/release-notes/.FSharp.Compiler.Service/10.0.100.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
* Checker: fix declaring type for abbreviated types extensions ([PR #18909](https://github.com/dotnet/fsharp/pull/18909))
3636
* Caches: type subsumption cache key perf regression ([Issue #18925](https://github.com/dotnet/fsharp/issues/18925) [PR #18926](https://github.com/dotnet/fsharp/pull/18926))
3737
* Ensure that line directives are applied to source identifiers (issue [#18908](https://github.com/dotnet/fsharp/issues/18908), PR [#18918](https://github.com/dotnet/fsharp/pull/18918))
38+
* Fix expected and actual types in ErrorFromAddingTypeEquation message and extended diagnostic data. ([PR #18915](https://github.com/dotnet/fsharp/pull/18915))
3839

3940
### Changed
4041
* Use `errorR` instead of `error` in `CheckDeclarations.fs` when possible. ([PR #18645](https://github.com/dotnet/fsharp/pull/18645))

src/Compiler/Driver/CompilerDiagnostics.fs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -733,21 +733,15 @@ type Exception with
733733
if m.StartLine <> m2.StartLine then
734734
os.AppendString(SeeAlsoE().Format(stringOfRange m))
735735

736-
| ConstraintSolverTypesNotInEqualityRelation(denv, (TType_measure _ as ty1), (TType_measure _ as ty2), m, m2, _) ->
737-
// REVIEW: consider if we need to show _cxs (the type parameter constraints)
738-
let ty1, ty2, _cxs = NicePrint.minimalStringsOfTwoTypes denv ty1 ty2
739-
740-
os.AppendString(ConstraintSolverTypesNotInEqualityRelation1E().Format ty1 ty2)
741-
742-
if m.StartLine <> m2.StartLine then
743-
os.AppendString(SeeAlsoE().Format(stringOfRange m))
744-
745736
| ConstraintSolverTypesNotInEqualityRelation(denv, ty1, ty2, m, m2, contextInfo) ->
746737
// REVIEW: consider if we need to show _cxs (the type parameter constraints)
747-
let ty1, ty2, _cxs = NicePrint.minimalStringsOfTwoTypes denv ty1 ty2
738+
let ty1str, ty2str, _cxs = NicePrint.minimalStringsOfTwoTypes denv ty1 ty2
748739

749-
OutputTypesNotInEqualityRelationContextInfo contextInfo ty1 ty2 m os (fun _ ->
750-
os.AppendString(ConstraintSolverTypesNotInEqualityRelation2E().Format ty1 ty2))
740+
match ty1, ty2 with
741+
| TType_measure _, TType_measure _ -> os.AppendString(ConstraintSolverTypesNotInEqualityRelation1E().Format ty1str ty2str)
742+
| _ ->
743+
OutputTypesNotInEqualityRelationContextInfo contextInfo ty1str ty2str m os (fun _ ->
744+
os.AppendString(ConstraintSolverTypesNotInEqualityRelation2E().Format ty1str ty2str))
751745

752746
if m.StartLine <> m2.StartLine then
753747
os.AppendString(SeeAlsoE().Format(stringOfRange m))
@@ -816,11 +810,26 @@ type Exception with
816810
os.AppendString(SeeAlsoE().Format(stringOfRange m1))
817811

818812
| ErrorFromAddingTypeEquation(g, denv, ty1, ty2, e, _) ->
819-
if not (typeEquiv g ty1 ty2) then
820-
let ty1, ty2, tpcs = NicePrint.minimalStringsOfTwoTypes denv ty1 ty2
813+
let e =
814+
if not (typeEquiv g ty1 ty2) then
815+
let ty1, ty2, tpcs = NicePrint.minimalStringsOfTwoTypes denv ty1 ty2
821816

822-
if ty1 <> ty2 + tpcs then
823-
os.AppendString(ErrorFromAddingTypeEquation2E().Format ty1 ty2 tpcs)
817+
if ty1 <> ty2 + tpcs then
818+
os.AppendString(ErrorFromAddingTypeEquation2E().Format ty1 ty2 tpcs)
819+
820+
e
821+
822+
else
823+
// Fix for https://github.com/dotnet/fsharp/issues/18905
824+
// If ty1 = ty2 after the type solving, then ty2 holds an actual type.
825+
// The order of expected and actual types in ConstraintSolverTypesNotInEqualityRelation can be arbitrary
826+
// due to type solving logic.
827+
// If ty1 = ty2 = ty2b, it means ty2b is also an actual type, and it needs to be swapped with ty1b
828+
// to be correctly used in the type mismatch error message based on ConstraintSolverTypesNotInEqualityRelation
829+
match e with
830+
| ConstraintSolverTypesNotInEqualityRelation(env, ty1b, ty2b, m, m2, contextInfo) when typeEquiv g ty2 ty2b ->
831+
ConstraintSolverTypesNotInEqualityRelation(env, ty2b, ty1b, m, m2, contextInfo)
832+
| _ -> e
824833

825834
e.Output(os, suggestNames)
826835

src/Compiler/Symbols/FSharpDiagnostic.fs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ type FSharpDiagnostic(m: range, severity: FSharpDiagnosticSeverity, message: str
176176
| Some symbolEnv ->
177177

178178
match diagnostic.Exception with
179+
| ErrorFromAddingConstraint(displayEnv, ConstraintSolverTypesNotInEqualityRelation(_, actualType, expectedType, _, _, contextInfo), _)
179180
| ErrorFromAddingTypeEquation(_, displayEnv, expectedType, actualType, ConstraintSolverTupleDiffLengths(contextInfo = contextInfo), _)
180181
| ErrorsFromAddingSubsumptionConstraint(_, displayEnv, expectedType, actualType, _, contextInfo, _) ->
181182
let context = DiagnosticContextInfo.From(contextInfo)
@@ -187,6 +188,8 @@ type FSharpDiagnostic(m: range, severity: FSharpDiagnosticSeverity, message: str
187188
ty1, ty2
188189
elif not (typeEquiv g ty1 ty2) then
189190
ty1, ty2
191+
elif typeEquiv g ty2 ty2b then
192+
ty1b, ty2b
190193
else ty2b, ty1b
191194

192195
let context = DiagnosticContextInfo.From(contextInfo)

tests/FSharp.Compiler.ComponentTests/Conformance/Types/RecordTypes/RecordTypes.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ module RecordTypes =
116116
(Warning 464, Line 15, Col 22, Line 15, Col 28, "This code is less generic than indicated by its annotations. A unit-of-measure specified using '_' has been determined to be '1', i.e. dimensionless. Consider making the code generic, or removing the use of '_'.")
117117
(Warning 464, Line 15, Col 35, Line 15, Col 42, "This code is less generic than indicated by its annotations. A unit-of-measure specified using '_' has been determined to be '1', i.e. dimensionless. Consider making the code generic, or removing the use of '_'.")
118118
(Error 5, Line 17, Col 1, Line 17, Col 5, "This field is not mutable")
119-
(Error 1, Line 17, Col 16, Line 17, Col 22, "The type 'decimal<Kg>' does not match the type 'float<Kg>'")
119+
(Error 1, Line 17, Col 16, Line 17, Col 22, "The type 'float<Kg>' does not match the type 'decimal<Kg>'")
120120
(Error 5, Line 18, Col 1, Line 18, Col 5, "This field is not mutable")
121121
(Error 1, Line 18, Col 16, Line 18, Col 21, "This expression was expected to have type\n 'float' \nbut here has type\n 'decimal' ")
122122
]

tests/FSharp.Compiler.ComponentTests/Conformance/Types/TypeConstraints/IWSAMsAndSRTPs/IWSAMsAndSRTPsTests.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1145,7 +1145,7 @@ module StaticAbstractBug =
11451145
|> compile
11461146
|> shouldFail
11471147
|> withDiagnostics [
1148-
(Error 1, Line 14, Col 41, Line 14, Col 42, "The type 'bool' does not match the type 'int'")
1148+
(Error 1, Line 14, Col 41, Line 14, Col 42, "The type 'int' does not match the type 'bool'")
11491149
(Error 1, Line 16, Col 32, Line 16, Col 33, "This expression was expected to have type
11501150
'bool'
11511151
but here has type

0 commit comments

Comments
 (0)