Skip to content

Commit ee0f8e4

Browse files
authored
Better error message when using wrong anon record (#15732)
* Better error message when using wrong anon record
1 parent df92217 commit ee0f8e4

19 files changed

+438
-166
lines changed

src/Compiler/Checking/ConstraintSolver.fs

+24-2
Original file line numberDiff line numberDiff line change
@@ -1078,7 +1078,9 @@ and SolveAnonInfoEqualsAnonInfo (csenv: ConstraintSolverEnv) m2 (anonInfo1: Anon
10781078
elif Set.intersect first second <> Set.empty then
10791079
Overlap(firstOnly, secondOnly)
10801080
else
1081-
CompletelyDifferent(Seq.toList first)
1081+
let first = Set.toList first
1082+
let second = Set.toList second
1083+
CompletelyDifferent(first, second)
10821084

10831085
let message =
10841086
match anonInfo1.SortedNames, anonInfo2.SortedNames with
@@ -1087,19 +1089,39 @@ and SolveAnonInfoEqualsAnonInfo (csenv: ConstraintSolverEnv) m2 (anonInfo1: Anon
10871089
| [missingField] ->
10881090
FSComp.SR.tcAnonRecdSingleFieldNameSubset(string missingField)
10891091
| _ ->
1092+
let missingFields = missingFields |> List.map(sprintf "'%s'")
10901093
let missingFields = String.concat ", " missingFields
10911094
FSComp.SR.tcAnonRecdMultipleFieldsNameSubset(string missingFields)
10921095
| Superset extraFields ->
10931096
match extraFields with
10941097
| [extraField] ->
10951098
FSComp.SR.tcAnonRecdSingleFieldNameSuperset(string extraField)
10961099
| _ ->
1100+
let extraFields = extraFields |> List.map(sprintf "'%s'")
10971101
let extraFields = String.concat ", " extraFields
10981102
FSComp.SR.tcAnonRecdMultipleFieldsNameSuperset(string extraFields)
10991103
| Overlap (missingFields, extraFields) ->
11001104
FSComp.SR.tcAnonRecdFieldNameMismatch(string missingFields, string extraFields)
11011105
| CompletelyDifferent missingFields ->
1102-
FSComp.SR.tcAnonRecdFieldNameDifferent(string missingFields)
1106+
let missingFields, usedFields = missingFields
1107+
match missingFields, usedFields with
1108+
| [ missingField ], [ usedField ] ->
1109+
FSComp.SR.tcAnonRecdSingleFieldNameSingleDifferent(missingField, usedField)
1110+
| [ missingField ], usedFields ->
1111+
let usedFields = usedFields |> List.map(sprintf "'%s'")
1112+
let usedFields = String.concat ", " usedFields
1113+
FSComp.SR.tcAnonRecdSingleFieldNameMultipleDifferent(missingField, usedFields)
1114+
| missingFields, [ usedField ] ->
1115+
let missingFields = missingFields |> List.map(sprintf "'%s'")
1116+
let missingFields = String.concat ", " missingFields
1117+
FSComp.SR.tcAnonRecdMultipleFieldNameSingleDifferent(missingFields, usedField)
1118+
1119+
| missingFields, usedFields ->
1120+
let missingFields = missingFields |> List.map(sprintf "'%s'")
1121+
let missingFields = String.concat ", " missingFields
1122+
let usedFields = usedFields |> List.map(sprintf "'%s'")
1123+
let usedFields = String.concat ", " usedFields
1124+
FSComp.SR.tcAnonRecdMultipleFieldNameMultipleDifferent(missingFields, usedFields)
11031125

11041126
ErrorD (ConstraintSolverError(message, csenv.m,m2))
11051127
else

src/Compiler/FSComp.txt

+6-3
Original file line numberDiff line numberDiff line change
@@ -1396,10 +1396,13 @@ tcAnonRecdInvalid,"Invalid Anonymous Record type declaration."
13961396
tcAnonRecdCcuMismatch,"Two anonymous record types are from different assemblies '%s' and '%s'"
13971397
tcAnonRecdFieldNameMismatch,"This anonymous record does not exactly match the expected shape. Add the missing fields %s and remove the extra fields %s."
13981398
tcAnonRecdSingleFieldNameSubset,"This anonymous record is missing field '%s'."
1399-
tcAnonRecdMultipleFieldsNameSubset,"This anonymous record is missing fields '%s'."
1399+
tcAnonRecdMultipleFieldsNameSubset,"This anonymous record is missing fields %s."
14001400
tcAnonRecdSingleFieldNameSuperset,"This anonymous record has an extra field. Remove field '%s'."
1401-
tcAnonRecdMultipleFieldsNameSuperset,"This anonymous record has extra fields. Remove fields '%s'."
1402-
tcAnonRecdFieldNameDifferent,"This is the wrong anonymous record. It should have the fields %s."
1401+
tcAnonRecdMultipleFieldsNameSuperset,"This anonymous record has extra fields. Remove fields %s."
1402+
tcAnonRecdSingleFieldNameSingleDifferent,"This anonymous record should have field '%s' but here has field '%s'."
1403+
tcAnonRecdSingleFieldNameMultipleDifferent,"This anonymous record should have field '%s' but here has fields %s."
1404+
tcAnonRecdMultipleFieldNameSingleDifferent,"This anonymous record should have fields %s; but here has field '%s'."
1405+
tcAnonRecdMultipleFieldNameMultipleDifferent,"This anonymous record should have fields %s; but here has fields %s."
14031406
keywordDescriptionAbstract,"Indicates a method that either has no implementation in the type in which it is declared or that is virtual and has a default implementation."
14041407
keywordDescriptionAnd,"Used in mutually recursive bindings, in property declarations, and with multiple constraints on generic parameters."
14051408
keywordDescriptionAs,"Used to give the current class object an object name. Also used to give a name to a whole pattern within a pattern match."

src/Compiler/xlf/FSComp.txt.cs.xlf

+27-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compiler/xlf/FSComp.txt.de.xlf

+27-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compiler/xlf/FSComp.txt.es.xlf

+27-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)