Skip to content

Commit 00fae43

Browse files
authored
Add warning when compiler selects among multiple record type candidates, fslang-suggestion 1091 (#15256)
1 parent cd3b581 commit 00fae43

20 files changed

+420
-0
lines changed

src/Compiler/Checking/NameResolution.fs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2726,6 +2726,36 @@ let rec ResolveLongIdentInTypePrim (ncenv: NameResolver) nenv lookupKind (resInf
27262726

27272727
let errorTextF s =
27282728
match tryTcrefOfAppTy g ty with
2729+
| ValueSome tcref when tcref.IsRecordTycon ->
2730+
let alternative = nenv.eFieldLabels |> Map.tryFind nm
2731+
match alternative with
2732+
| Some fieldLabels ->
2733+
let fieldsOfResolvedType = tcref.AllFieldsArray |> Array.map (fun f -> f.LogicalName) |> Set.ofArray
2734+
let fieldsOfAlternatives =
2735+
fieldLabels
2736+
|> Seq.collect (fun l -> l.Tycon.AllFieldsArray |> Array.map (fun f -> f.LogicalName))
2737+
|> Set.ofSeq
2738+
let intersect = Set.intersect fieldsOfAlternatives fieldsOfResolvedType
2739+
2740+
if not intersect.IsEmpty then
2741+
let resolvedTypeName = NicePrint.fqnOfEntityRef g tcref
2742+
let namesOfAlternatives =
2743+
fieldLabels
2744+
|> List.map (fun l -> $" %s{NicePrint.fqnOfEntityRef g l.TyconRef}")
2745+
|> fun names -> $" %s{resolvedTypeName}" :: names
2746+
let candidates = System.String.Join("\n", namesOfAlternatives)
2747+
let overlappingNames =
2748+
intersect
2749+
|> Set.toArray
2750+
|> Array.sort
2751+
|> Array.map (fun s -> $" %s{s}")
2752+
|> fun a -> System.String.Join("\n", a)
2753+
if g.langVersion.SupportsFeature(LanguageFeature.WarningWhenMultipleRecdTypeChoice) then
2754+
warning(Error(FSComp.SR.tcMultipleRecdTypeChoice(candidates, resolvedTypeName, overlappingNames), m))
2755+
else
2756+
informationalWarning(Error(FSComp.SR.tcMultipleRecdTypeChoice(candidates, resolvedTypeName, overlappingNames), m))
2757+
| _ -> ()
2758+
FSComp.SR.undefinedNameFieldConstructorOrMemberWhenTypeIsKnown(tcref.DisplayNameWithStaticParametersAndUnderscoreTypars, s)
27292759
| ValueSome tcref ->
27302760
FSComp.SR.undefinedNameFieldConstructorOrMemberWhenTypeIsKnown(tcref.DisplayNameWithStaticParametersAndUnderscoreTypars, s)
27312761
| _ ->

src/Compiler/Checking/NicePrint.fs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2615,6 +2615,8 @@ let stringOfFSAttrib denv x = x |> PrintTypes.layoutAttrib denv |> squareAngleL
26152615

26162616
let stringOfILAttrib denv x = x |> PrintTypes.layoutILAttrib denv |> squareAngleL |> showL
26172617

2618+
let fqnOfEntityRef g x = x |> layoutTyconRefImpl false (DisplayEnv.Empty g) |> showL
2619+
26182620
let layoutImpliedSignatureOfModuleOrNamespace showHeader denv infoReader ad m contents =
26192621
InferredSigPrinting.layoutImpliedSignatureOfModuleOrNamespace showHeader denv infoReader ad m contents
26202622

src/Compiler/Checking/NicePrint.fsi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ val stringOfFSAttrib: denv: DisplayEnv -> x: Attrib -> string
135135

136136
val stringOfILAttrib: denv: DisplayEnv -> ILType * ILAttribElem list -> string
137137

138+
val fqnOfEntityRef: g: TcGlobals -> x: EntityRef -> string
139+
138140
val layoutImpliedSignatureOfModuleOrNamespace:
139141
showHeader: bool ->
140142
denv: DisplayEnv ->

src/Compiler/FSComp.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,6 +1572,7 @@ featureStaticMembersInInterfaces,"Static members in interfaces"
15721572
featureNonInlineLiteralsAsPrintfFormat,"String values marked as literals and IL constants as printf format"
15731573
featureNestedCopyAndUpdate,"Nested record field copy-and-update"
15741574
featureExtendedStringInterpolation,"Extended string interpolation similar to C# raw string literals."
1575+
featureWarningWhenMultipleRecdTypeChoice,"Raises warnings when multiple record type matches were found during name resolution because of overlapping field names."
15751576
3353,fsiInvalidDirective,"Invalid directive '#%s %s'"
15761577
3354,tcNotAFunctionButIndexerNamedIndexingNotYetEnabled,"This value supports indexing, e.g. '%s.[index]'. The syntax '%s[index]' requires /langversion:preview. See https://aka.ms/fsharp-index-notation."
15771578
3354,tcNotAFunctionButIndexerIndexingNotYetEnabled,"This expression supports indexing, e.g. 'expr.[index]'. The syntax 'expr[index]' requires /langversion:preview. See https://aka.ms/fsharp-index-notation."
@@ -1692,3 +1693,4 @@ featureEscapeBracesInFormattableString,"Escapes curly braces before calling Form
16921693
3563,lexInvalidIdentifier,"This is not a valid identifier"
16931694
3564,parsMissingUnionCaseName,"Missing union case name"
16941695
3565,parsExpectingType,"Expecting type"
1696+
3566,tcMultipleRecdTypeChoice,"Multiple type matches were found:\n%s\nThe type '%s' was used. Due to the overlapping field names\n%s\nconsider using type annotations or change the order of open statements."

src/Compiler/Facilities/LanguageFeatures.fs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ type LanguageFeature =
6767
| NonInlineLiteralsAsPrintfFormat
6868
| NestedCopyAndUpdate
6969
| ExtendedStringInterpolation
70+
| WarningWhenMultipleRecdTypeChoice
7071

7172
/// LanguageVersion management
7273
type LanguageVersion(versionText) =
@@ -157,6 +158,7 @@ type LanguageVersion(versionText) =
157158
LanguageFeature.NonInlineLiteralsAsPrintfFormat, previewVersion
158159
LanguageFeature.NestedCopyAndUpdate, previewVersion
159160
LanguageFeature.ExtendedStringInterpolation, previewVersion
161+
LanguageFeature.WarningWhenMultipleRecdTypeChoice, previewVersion
160162

161163
]
162164

@@ -279,6 +281,7 @@ type LanguageVersion(versionText) =
279281
| LanguageFeature.NonInlineLiteralsAsPrintfFormat -> FSComp.SR.featureNonInlineLiteralsAsPrintfFormat ()
280282
| LanguageFeature.NestedCopyAndUpdate -> FSComp.SR.featureNestedCopyAndUpdate ()
281283
| LanguageFeature.ExtendedStringInterpolation -> FSComp.SR.featureExtendedStringInterpolation ()
284+
| LanguageFeature.WarningWhenMultipleRecdTypeChoice -> FSComp.SR.featureWarningWhenMultipleRecdTypeChoice ()
282285

283286
/// Get a version string associated with the given feature.
284287
static member GetFeatureVersionString feature =

src/Compiler/Facilities/LanguageFeatures.fsi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ type LanguageFeature =
5757
| NonInlineLiteralsAsPrintfFormat
5858
| NestedCopyAndUpdate
5959
| ExtendedStringInterpolation
60+
| WarningWhenMultipleRecdTypeChoice
6061

6162
/// LanguageVersion management
6263
type LanguageVersion =

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,11 @@
422422
<target state="translated">Vyvolá upozornění, když se použije „let inline ... =“ společně s atributem [&lt;MethodImpl(MethodImplOptions.NoInlining)&gt;]. Funkce není vkládána.</target>
423423
<note />
424424
</trans-unit>
425+
<trans-unit id="featureWarningWhenMultipleRecdTypeChoice">
426+
<source>Raises warnings when multiple record type matches were found during name resolution because of overlapping field names.</source>
427+
<target state="new">Raises warnings when multiple record type matches were found during name resolution because of overlapping field names.</target>
428+
<note />
429+
</trans-unit>
425430
<trans-unit id="featureWildCardInForLoop">
426431
<source>wild card in for loop</source>
427432
<target state="translated">zástupný znak ve smyčce for</target>
@@ -1077,6 +1082,11 @@
10771082
<target state="translated">Je třeba inicializovat následující požadované vlastnosti:{0}</target>
10781083
<note />
10791084
</trans-unit>
1085+
<trans-unit id="tcMultipleRecdTypeChoice">
1086+
<source>Multiple type matches were found:\n{0}\nThe type '{1}' was used. Due to the overlapping field names\n{2}\nconsider using type annotations or change the order of open statements.</source>
1087+
<target state="new">Multiple type matches were found:\n{0}\nThe type '{1}' was used. Due to the overlapping field names\n{2}\nconsider using type annotations or change the order of open statements.</target>
1088+
<note />
1089+
</trans-unit>
10801090
<trans-unit id="tcNoEagerConstraintApplicationAttribute">
10811091
<source>Using methods with 'NoEagerConstraintApplicationAttribute' requires /langversion:6.0 or later</source>
10821092
<target state="translated">Použití metod s atributem NoEagerConstraintApplicationAttribute vyžaduje /langversion:6.0 nebo novější.</target>

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,11 @@
422422
<target state="translated">Löst Warnungen aus, wenn „let inline ... =“ zusammen mit dem Attribut [&lt;MethodImpl(MethodImplOptions.NoInlining)&gt;] verwendet wird. Die Funktion wird nicht inline gesetzt.</target>
423423
<note />
424424
</trans-unit>
425+
<trans-unit id="featureWarningWhenMultipleRecdTypeChoice">
426+
<source>Raises warnings when multiple record type matches were found during name resolution because of overlapping field names.</source>
427+
<target state="new">Raises warnings when multiple record type matches were found during name resolution because of overlapping field names.</target>
428+
<note />
429+
</trans-unit>
425430
<trans-unit id="featureWildCardInForLoop">
426431
<source>wild card in for loop</source>
427432
<target state="translated">Platzhalter in for-Schleife</target>
@@ -1077,6 +1082,11 @@
10771082
<target state="translated">Die folgenden erforderlichen Eigenschaften müssen initialisiert werden:{0}</target>
10781083
<note />
10791084
</trans-unit>
1085+
<trans-unit id="tcMultipleRecdTypeChoice">
1086+
<source>Multiple type matches were found:\n{0}\nThe type '{1}' was used. Due to the overlapping field names\n{2}\nconsider using type annotations or change the order of open statements.</source>
1087+
<target state="new">Multiple type matches were found:\n{0}\nThe type '{1}' was used. Due to the overlapping field names\n{2}\nconsider using type annotations or change the order of open statements.</target>
1088+
<note />
1089+
</trans-unit>
10801090
<trans-unit id="tcNoEagerConstraintApplicationAttribute">
10811091
<source>Using methods with 'NoEagerConstraintApplicationAttribute' requires /langversion:6.0 or later</source>
10821092
<target state="translated">Die Verwendung von Methoden mit "NoEagerConstraintApplicationAttribute" erfordert /langversion:6.0 oder höher.</target>

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,11 @@
422422
<target state="translated">Genera advertencias cuando se usa "let inline ... =" junto con el atributo [&lt;MethodImpl(MethodImplOptions.NoInlining)&gt;]. La función no se está insertando.</target>
423423
<note />
424424
</trans-unit>
425+
<trans-unit id="featureWarningWhenMultipleRecdTypeChoice">
426+
<source>Raises warnings when multiple record type matches were found during name resolution because of overlapping field names.</source>
427+
<target state="new">Raises warnings when multiple record type matches were found during name resolution because of overlapping field names.</target>
428+
<note />
429+
</trans-unit>
425430
<trans-unit id="featureWildCardInForLoop">
426431
<source>wild card in for loop</source>
427432
<target state="translated">carácter comodín en bucle for</target>
@@ -1077,6 +1082,11 @@
10771082
<target state="translated">Se deben inicializar las siguientes propiedades necesarias:{0}</target>
10781083
<note />
10791084
</trans-unit>
1085+
<trans-unit id="tcMultipleRecdTypeChoice">
1086+
<source>Multiple type matches were found:\n{0}\nThe type '{1}' was used. Due to the overlapping field names\n{2}\nconsider using type annotations or change the order of open statements.</source>
1087+
<target state="new">Multiple type matches were found:\n{0}\nThe type '{1}' was used. Due to the overlapping field names\n{2}\nconsider using type annotations or change the order of open statements.</target>
1088+
<note />
1089+
</trans-unit>
10801090
<trans-unit id="tcNoEagerConstraintApplicationAttribute">
10811091
<source>Using methods with 'NoEagerConstraintApplicationAttribute' requires /langversion:6.0 or later</source>
10821092
<target state="translated">El uso de métodos con "NoEagerConstraintApplicationAttribute" requiere /langversion:6.0 o posteriores</target>

src/Compiler/xlf/FSComp.txt.fr.xlf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,11 @@
422422
<target state="translated">Génère des avertissements lorsque « let inline ... = » est utilisé avec l’attribut [&lt;MethodImpl(MethodImplOptions.NoInlining)&gt;]. La fonction n’est pas inlined.</target>
423423
<note />
424424
</trans-unit>
425+
<trans-unit id="featureWarningWhenMultipleRecdTypeChoice">
426+
<source>Raises warnings when multiple record type matches were found during name resolution because of overlapping field names.</source>
427+
<target state="new">Raises warnings when multiple record type matches were found during name resolution because of overlapping field names.</target>
428+
<note />
429+
</trans-unit>
425430
<trans-unit id="featureWildCardInForLoop">
426431
<source>wild card in for loop</source>
427432
<target state="translated">caractère générique dans une boucle for</target>
@@ -1077,6 +1082,11 @@
10771082
<target state="translated">Les propriétés requises suivantes doivent être initialisées :{0}</target>
10781083
<note />
10791084
</trans-unit>
1085+
<trans-unit id="tcMultipleRecdTypeChoice">
1086+
<source>Multiple type matches were found:\n{0}\nThe type '{1}' was used. Due to the overlapping field names\n{2}\nconsider using type annotations or change the order of open statements.</source>
1087+
<target state="new">Multiple type matches were found:\n{0}\nThe type '{1}' was used. Due to the overlapping field names\n{2}\nconsider using type annotations or change the order of open statements.</target>
1088+
<note />
1089+
</trans-unit>
10801090
<trans-unit id="tcNoEagerConstraintApplicationAttribute">
10811091
<source>Using methods with 'NoEagerConstraintApplicationAttribute' requires /langversion:6.0 or later</source>
10821092
<target state="translated">L’utilisation de méthodes avec « NoEagerConstraintApplicationAttribute » requiert/langversion:6.0 ou ultérieur</target>

0 commit comments

Comments
 (0)