Skip to content

Commit 0482af7

Browse files
committed
wip
1 parent 7fd419a commit 0482af7

File tree

4 files changed

+46
-30
lines changed

4 files changed

+46
-30
lines changed

src/Compiler/Checking/ConstraintSolver.fs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3184,6 +3184,13 @@ and SolveTypeEqualsTypeWithReport (csenv: ConstraintSolverEnv) ndeep m trace cxs
31843184
(fun () -> SolveTypeEqualsTypeKeepAbbrevsWithCxsln csenv ndeep m trace cxsln expectedTy actualTy)
31853185
(function
31863186
| AbortForFailedMemberConstraintResolution as err -> ErrorD err
3187+
| ConstraintSolverTypesNotInEqualityRelation (_, ty1, ty2, _, _, _) as err ->
3188+
let isEqual = typeEquiv csenv.g expectedTy ty1 && typeEquiv csenv.g actualTy ty2
3189+
let expectedTy, actualTy =
3190+
if isEqual then expectedTy, actualTy
3191+
else ty2, ty1
3192+
3193+
ErrorD (ErrorFromAddingTypeEquation(csenv.g, csenv.DisplayEnv, expectedTy, actualTy, err, m))
31873194
| res -> ErrorD (ErrorFromAddingTypeEquation(csenv.g, csenv.DisplayEnv, expectedTy, actualTy, res, m)))
31883195

31893196
and ArgsMustSubsumeOrConvert

src/Compiler/Driver/CompilerDiagnostics.fs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -771,27 +771,27 @@ type Exception with
771771
if m.StartLine <> m2.StartLine then
772772
os.AppendString(SeeAlsoE().Format(stringOfRange m2))
773773

774-
| ErrorFromAddingTypeEquation(g, denv, ty1, ty2, ConstraintSolverTypesNotInEqualityRelation(_, ty1b, ty2b, m, _, contextInfo), _) when
775-
typeEquiv g ty1 ty1b && typeEquiv g ty2 ty2b
776-
->
777-
let ty1, ty2, tpcs = NicePrint.minimalStringsOfTwoTypes denv ty1 ty2
774+
| ErrorFromAddingTypeEquation(g,
775+
denv,
776+
ty1,
777+
ty2,
778+
(ConstraintSolverTypesNotInEqualityRelation(_, ty1b, ty2b, m, _, contextInfo) as e),
779+
_) ->
780+
if not (typeEquiv g ty1 ty1b && typeEquiv g ty2 ty2b) then
781+
e.Output(os, suggestNames)
782+
else
778783

779-
OutputTypesNotInEqualityRelationContextInfo contextInfo ty1 ty2 m os (fun contextInfo ->
780-
match contextInfo with
781-
| ContextInfo.TupleInRecordFields ->
782-
os.AppendString(ErrorFromAddingTypeEquation1E().Format ty2 ty1 tpcs)
783-
os.AppendString(Environment.NewLine + FSComp.SR.commaInsteadOfSemicolonInRecord ())
784-
| _ when ty2 = "bool" && ty1.EndsWithOrdinal(" ref") ->
785-
os.AppendString(ErrorFromAddingTypeEquation1E().Format ty2 ty1 tpcs)
786-
os.AppendString(Environment.NewLine + FSComp.SR.derefInsteadOfNot ())
787-
| _ -> os.AppendString(ErrorFromAddingTypeEquation1E().Format ty2 ty1 tpcs))
788-
789-
| ErrorFromAddingTypeEquation(_, _, _, _, (ConstraintSolverTypesNotInEqualityRelation(_, _, _, _, _, contextInfo) as e), _) when
790-
(match contextInfo with
791-
| ContextInfo.NoContext -> false
792-
| _ -> true)
793-
->
794-
e.Output(os, suggestNames)
784+
let ty1, ty2, tpcs = NicePrint.minimalStringsOfTwoTypes denv ty1 ty2
785+
786+
OutputTypesNotInEqualityRelationContextInfo contextInfo ty1 ty2 m os (fun contextInfo ->
787+
match contextInfo with
788+
| ContextInfo.TupleInRecordFields ->
789+
os.AppendString(ErrorFromAddingTypeEquation1E().Format ty2 ty1 tpcs)
790+
os.AppendString(Environment.NewLine + FSComp.SR.commaInsteadOfSemicolonInRecord ())
791+
| _ when ty2 = "bool" && ty1.EndsWithOrdinal(" ref") ->
792+
os.AppendString(ErrorFromAddingTypeEquation1E().Format ty2 ty1 tpcs)
793+
os.AppendString(Environment.NewLine + FSComp.SR.derefInsteadOfNot ())
794+
| _ -> os.AppendString(ErrorFromAddingTypeEquation1E().Format ty2 ty1 tpcs))
795795

796796
| ErrorFromAddingTypeEquation(error = ConstraintSolverTypesNotInSubsumptionRelation _ as e) -> e.Output(os, suggestNames)
797797

src/Compiler/Symbols/FSharpDiagnostic.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ type FSharpDiagnostic(m: range, severity: FSharpDiagnosticSeverity, message: str
199199

200200
match diagnostic.Exception with
201201
| ErrorFromAddingTypeEquation(_, displayEnv, expectedType, actualType, ConstraintSolverTupleDiffLengths(contextInfo = contextInfo), _)
202-
| ErrorFromAddingTypeEquation(_, displayEnv, _, _, ConstraintSolverTypesNotInEqualityRelation(_, expectedType, actualType, _, _, contextInfo), _)
202+
| ErrorFromAddingTypeEquation(_, displayEnv, expectedType, actualType, ConstraintSolverTypesNotInEqualityRelation(_, _, _, _, _, contextInfo), _)
203203
| ErrorsFromAddingSubsumptionConstraint(_, displayEnv, expectedType, actualType, _, contextInfo, _) ->
204204
let context = DiagnosticContextInfo.From(contextInfo)
205205
Some(TypeMismatchDiagnosticExtendedData(symbolEnv, displayEnv, expectedType, actualType, context))

tests/FSharp.Compiler.ComponentTests/ErrorMessages/ExtendedDiagnosticDataTests.fs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
open FSharp.Compiler.Text
55
open FSharp.Compiler.Diagnostics
66
open FSharp.Compiler.Diagnostics.ExtendedData
7-
open FSharp.Test
87
open FSharp.Test.Compiler
98
open Xunit
109

@@ -144,16 +143,12 @@ if true then 1 else "a"
144143
Assert.Equal("int", typeMismatch.ExpectedType.Format(displayContext))
145144
Assert.Equal("string", typeMismatch.ActualType.Format(displayContext)))
146145

147-
[<Theory>]
148-
[<InlineData("""
146+
[<Fact>]
147+
let ``TypeMismatchDiagnosticExtendedData 08`` () =
148+
FSharp """
149149
type R = { Field1: int }
150150
let f (x: R) = "" + x.Field1
151-
""")>]
152-
[<InlineData("""
153-
let x: string = 1
154-
""")>]
155-
let ``TypeMismatchDiagnosticExtendedData 08`` code =
156-
FSharp code
151+
"""
157152
|> typecheckResults
158153
|> checkDiagnostic
159154
(1, "The type 'int' does not match the type 'string'")
@@ -163,6 +158,20 @@ let ``TypeMismatchDiagnosticExtendedData 08`` code =
163158
Assert.Equal("string", typeMismatch.ExpectedType.Format(displayContext))
164159
Assert.Equal("int", typeMismatch.ActualType.Format(displayContext)))
165160

161+
[<Fact>]
162+
let ``TypeMismatchDiagnosticExtendedData 09`` () =
163+
FSharp """
164+
let x: string = 1
165+
"""
166+
|> typecheckResults
167+
|> checkDiagnostic
168+
(1, "This expression was expected to have type\n 'string' \nbut here has type\n 'int' ")
169+
(fun (typeMismatch: TypeMismatchDiagnosticExtendedData) ->
170+
let displayContext = typeMismatch.DisplayContext
171+
Assert.Equal(DiagnosticContextInfo.NoContext, typeMismatch.ContextInfo)
172+
Assert.Equal("string", typeMismatch.ExpectedType.Format(displayContext))
173+
Assert.Equal("int", typeMismatch.ActualType.Format(displayContext)))
174+
166175
[<Theory>]
167176
[<InlineData true>]
168177
[<InlineData false>]

0 commit comments

Comments
 (0)