Skip to content

Commit d6fa56e

Browse files
committed
wip
1 parent e0100ac commit d6fa56e

File tree

5 files changed

+82
-20
lines changed

5 files changed

+82
-20
lines changed

src/Compiler/Driver/CompilerDiagnostics.fs

Lines changed: 19 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,20 @@ 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+
match e with
824+
| ConstraintSolverTypesNotInEqualityRelation(env, ty1b, ty2b, m, m2, contextInfo) when typeEquiv g ty2 ty2b ->
825+
ConstraintSolverTypesNotInEqualityRelation(env, ty2b, ty1b, m, m2, contextInfo)
826+
| _ -> e
824827

825828
e.Output(os, suggestNames)
826829

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 ty1 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

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

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ let inline checkDiagnosticData
1212
(check: 'a -> unit)
1313
(checkResults: 'b when 'b: (member Diagnostics: FSharpDiagnostic[])) =
1414
match checkResults.Diagnostics |> Array.tryFind (fun d -> d.ErrorNumber = diagnosticNumber) with
15-
| None -> failwith "Expected diagnostic not found"
15+
| None -> failwith $"Expected diagnostic (number {diagnosticNumber}) not found"
1616
| Some diagnostic ->
1717

1818
Assert.Equal(message, diagnostic.Message)
1919
match diagnostic.ExtendedData with
2020
| Some(:? 'a as data) -> check data
2121
| _ -> failwith "Expected diagnostic extended data not found"
2222

23+
checkResults
2324

2425
[<Fact>]
2526
let ``TypeMismatchDiagnosticExtendedData 01`` () =
@@ -187,7 +188,62 @@ let f2 (x: inref<'T>) = f1 &x
187188
Assert.Equal("outref<'T>", typeMismatch.ExpectedType.Format(displayContext))
188189
Assert.Equal("inref<'T>", typeMismatch.ActualType.Format(displayContext)))
189190

190-
[<Theory>]
191+
[<Fact>]
192+
let ``TypeMismatchDiagnosticExtendedData 11`` () =
193+
FSharp """
194+
type T() =
195+
static member P1 = T.P2 + 1
196+
static member P2 = ""
197+
"""
198+
|> typecheckResults
199+
// static member P1 = T.P2 ->+<- 1
200+
|> checkDiagnosticData
201+
(43, "The type 'int' does not match the type 'string'")
202+
(fun (typeMismatch: TypeMismatchDiagnosticExtendedData) ->
203+
let displayContext = typeMismatch.DisplayContext
204+
Assert.Equal(DiagnosticContextInfo.NoContext, typeMismatch.ContextInfo)
205+
Assert.Equal("string", typeMismatch.ExpectedType.Format(displayContext))
206+
Assert.Equal("int", typeMismatch.ActualType.Format(displayContext)))
207+
208+
// static member P2 = ->""<-
209+
|> checkDiagnosticData
210+
(1, "The type 'string' does not match the type 'int'")
211+
(fun (typeMismatch: TypeMismatchDiagnosticExtendedData) ->
212+
let displayContext = typeMismatch.DisplayContext
213+
Assert.Equal(DiagnosticContextInfo.NoContext, typeMismatch.ContextInfo)
214+
Assert.Equal("int", typeMismatch.ExpectedType.Format(displayContext))
215+
Assert.Equal("string", typeMismatch.ActualType.Format(displayContext)))
216+
217+
[<Fact>]
218+
let ``TypeMismatchDiagnosticExtendedData 12`` () =
219+
FSharp """
220+
let x: string = 1 + 1
221+
"""
222+
|> typecheckResults
223+
|> checkDiagnosticData
224+
(1, "The type 'int' does not match the type 'string'")
225+
(fun (typeMismatch: TypeMismatchDiagnosticExtendedData) ->
226+
let displayContext = typeMismatch.DisplayContext
227+
Assert.Equal(DiagnosticContextInfo.NoContext, typeMismatch.ContextInfo)
228+
Assert.Equal("string", typeMismatch.ExpectedType.Format(displayContext))
229+
Assert.Equal("int", typeMismatch.ActualType.Format(displayContext)))
230+
231+
[<Fact>]
232+
let ``TypeMismatchDiagnosticExtendedData 13`` () =
233+
FSharp """
234+
let x: string -> string = id
235+
let y: unit -> string = x
236+
"""
237+
|> typecheckResults
238+
|> checkDiagnosticData
239+
(1, "Type mismatch. Expecting a\n 'unit -> string' \nbut given a\n 'string -> string' \nThe type 'string' does not match the type 'unit'")
240+
(fun (typeMismatch: TypeMismatchDiagnosticExtendedData) ->
241+
let displayContext = typeMismatch.DisplayContext
242+
Assert.Equal(DiagnosticContextInfo.NoContext, typeMismatch.ContextInfo)
243+
Assert.Equal("unit -> string", typeMismatch.ExpectedType.Format(displayContext))
244+
Assert.Equal("string -> string", typeMismatch.ActualType.Format(displayContext)))
245+
246+
[<Theory>]
191247
[<InlineData true>]
192248
[<InlineData false>]
193249
let ``ArgumentsInSigAndImplMismatchExtendedData 01`` useTransparentCompiler =

0 commit comments

Comments
 (0)