Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit06640a5

Browse files
forkiKevinRansom
authored andcommitted
Suggest Record labels and Union cases (dotnet#2119)
* Show that we can't suggest record labels* Suggest record labels -fixesdotnet#2117* cleanup* Fix the test* New test for union cases* Suggest union cases* Suggest more unions* Fix tests
1 parent68d2f08 commit06640a5

File tree

8 files changed

+74
-19
lines changed

8 files changed

+74
-19
lines changed

‎src/fsharp/NameResolution.fs‎

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1909,6 +1909,19 @@ let DecodeFSharpEvent (pinfos:PropInfo list) ad g (ncenv:NameResolver) m =
19091909
|_->
19101910
None
19111911

1912+
/// Returns all record label names for the given type.
1913+
letGetRecordLabelsForType g nenv typ=
1914+
if isRecdTy g typthen
1915+
lettypeName= NicePrint.minimalStringOfType nenv.eDisplayEnv typ
1916+
nenv.eFieldLabels
1917+
|> Seq.filter(fun kv->
1918+
kv.Value
1919+
|> List.map(fun r-> r.TyconRef.DisplayName)
1920+
|> List.exists((=) typeName))
1921+
|> Seq.map(fun kv-> kv.Key)
1922+
|> Set.ofSeq
1923+
else
1924+
Set.empty
19121925

19131926
// REVIEW: this shows up on performance logs. Consider for example endless resolutions of "List.map" to
19141927
// the empty set of results, or "x.Length" for a list or array type. This indicates it could be worth adding a cache here.
@@ -2011,12 +2024,25 @@ let rec ResolveLongIdentInTypePrim (ncenv:NameResolver) nenv lookupKind (resInfo
20112024
|> List.filter(fun m->not m.IsClassConstructor&&not m.IsConstructor)
20122025
|> List.map(fun m-> m.DisplayName)
20132026
|> Set.ofList
2014-
2015-
2027+
letsuggestions5= GetRecordLabelsForType g nenv typ
2028+
letsuggestions6=
2029+
match lookupKindwith
2030+
| LookupKind.Expr| LookupKind.Pattern->
2031+
if isAppTy g typthen
2032+
lettcref,_= destAppTy g typ
2033+
tcref.UnionCasesArray
2034+
|> Array.map(fun uc-> uc.DisplayName)
2035+
|> Set.ofArray
2036+
else
2037+
Set.empty
2038+
|_-> Set.empty
2039+
20162040
suggestions1
20172041
|> Set.union suggestions2
20182042
|> Set.union suggestions3
20192043
|> Set.union suggestions4
2044+
|> Set.union suggestions5
2045+
|> Set.union suggestions6
20202046

20212047
raze(UndefinedName(depth,FSComp.SR.undefinedNameFieldConstructorOrMember, id, suggestMembers))
20222048

@@ -2140,8 +2166,21 @@ let rec ResolveExprLongIdentInModuleOrNamespace (ncenv:NameResolver) nenv (typeN
21402166
|> Seq.filter(fun kv-> IsEntityAccessible ncenv.amap m ad(modref.NestedTyconRef kv.Value))
21412167
|> Seq.map(fun e-> e.Value.DisplayName)
21422168
|> Set.ofSeq
2169+
2170+
letunions=
2171+
modref.ModuleOrNamespaceType.AllEntities
2172+
|> Seq.collect(fun tycon->
2173+
lethasRequireQualifiedAccessAttribute= HasFSharpAttribute ncenv.g ncenv.g.attrib_RequireQualifiedAccessAttribute tycon.Attribs
2174+
if hasRequireQualifiedAccessAttributethen
2175+
[||]
2176+
else
2177+
tycon.UnionCasesArray)
2178+
|> Seq.map(fun uc-> uc.DisplayName)
2179+
|> Set.ofSeq
21432180

2144-
Set.union types submodules// TODO: Add unions
2181+
types
2182+
|> Set.union submodules
2183+
|> Set.union unions
21452184
|_-> Set.empty
21462185

21472186
raze(UndefinedName(depth,FSComp.SR.undefinedNameValueConstructorNamespaceOrType,id,suggestPossibleTypes))
@@ -2757,15 +2796,8 @@ let rec ResolveFieldInModuleOrNamespace (ncenv:NameResolver) nenv ad (resInfo:Re
27572796
error(InternalError("ResolveFieldInModuleOrNamespace",m))
27582797

27592798
/// Suggest other labels of the same record
2760-
letSuggestOtherLabelsOfSameRecordType(nenv:NameResolutionEnv)typeName(id:Ident)(allFields:Ident list)=
2761-
letlabelsOfPossibleRecord=
2762-
nenv.eFieldLabels
2763-
|> Seq.filter(fun kv->
2764-
kv.Value
2765-
|> List.map(fun r-> r.TyconRef.DisplayName)
2766-
|> List.exists((=) typeName))
2767-
|> Seq.map(fun kv-> kv.Key)
2768-
|> Set.ofSeq
2799+
letSuggestOtherLabelsOfSameRecordType g(nenv:NameResolutionEnv)typ(id:Ident)(allFields:Ident list)=
2800+
letlabelsOfPossibleRecord= GetRecordLabelsForType g nenv typ
27692801

27702802
letgivenFields=
27712803
allFields
@@ -2830,10 +2862,10 @@ let ResolveFieldPrim (ncenv:NameResolver) nenv ad typ (mp,id:Ident) allFields =
28302862
match ncenv.InfoReader.TryFindRecdOrClassFieldInfoOfType(id.idText,m,typ)with
28312863
| Some(RecdFieldInfo(_,rfref))->[ResolutionInfo.Empty, FieldResolution(rfref,false)]
28322864
| None->
2833-
lettypeName= NicePrint.minimalStringOfType nenv.eDisplayEnv typ
28342865
if isRecdTy g typthen
28352866
// record label doesn't belong to record type -> suggest other labels of same record
2836-
letsuggestLabels()= SuggestOtherLabelsOfSameRecordType nenv typeName id allFields
2867+
letsuggestLabels()= SuggestOtherLabelsOfSameRecordType g nenv typ id allFields
2868+
lettypeName= NicePrint.minimalStringOfType nenv.eDisplayEnv typ
28372869
leterrorText= FSComp.SR.nrRecordDoesNotContainSuchLabel(typeName,id.idText)
28382870
error(ErrorWithSuggestions(errorText, m, id.idText, suggestLabels))
28392871
else

‎tests/fsharp/typecheck/sigs/neg01.bsl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ neg01a.fsi(24,8,25,7): typecheck error FS0913: Types cannot contain nested type
33

44
neg01a.fs(22,8,23,7): typecheck error FS0913: Types cannot contain nested type definitions
55

6-
neg01b.fs(2,13,2,14): typecheck error FS0039: The value, constructor, namespaceor type 'X' isnot defined. Maybe you want one of the following:fieldsInWrongOrder, missingConstructorInSignature, missingFieldInSignature, missingInterfaceInImplementation, missingInterfaceInSignature
6+
neg01b.fs(2,13,2,14): typecheck error FS0039: The value, constructor, namespaceor type 'X' isnot defined. Maybe you want one of the following:A, B, fieldsInWrongOrder, missingConstructorInSignature, missingFieldInSignature
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

22
neg14a.fs(9,6,9,33): typecheck error FS0343: The type 'missingInterfaceInSignature' implements 'System.IComparable' explicitly but provides no corresponding overridefor 'Object.Equals'. An implementation of 'Object.Equals' has been automatically provided, implemented via 'System.IComparable'. Consider implementing the override 'Object.Equals' explicitly
33

4-
neg14b.fs(2,13,2,14): typecheck error FS0039: The value, constructor, namespaceor type 'X' isnot defined. Maybe you want one of the following:fieldsInWrongOrder, missingConstructorInSignature, missingFieldInSignature, missingInterfaceInImplementation, missingInterfaceInSignature
4+
neg14b.fs(2,13,2,14): typecheck error FS0039: The value, constructor, namespaceor type 'X' isnot defined. Maybe you want one of the following:A, B, fieldsInWrongOrder, missingConstructorInSignature, missingFieldInSignature

‎tests/fsharp/typecheck/sigs/neg15.bsl‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ neg15.fs(115,19,115,48): typecheck error FS0072: Lookup on object of indetermina
2929

3030
neg15.fs(116,20,116,73): typecheck error FS0072: Lookup on object of indeterminate type based on information priorto this program point. A type annotation may be needed priorto this program pointto constrain the type of the object. This may allow the lookupto be resolved.
3131

32-
neg15.fs(122,32,122,57): typecheck error FS0039: The value, constructor, namespaceor type 'InternalTagOfInternalType' isnot defined. Maybe you want one of the following: InternalUnionType, InternalRecordType, UnionTypeWithPrivateRepresentation
32+
neg15.fs(122,32,122,57): typecheck error FS0039: The value, constructor, namespaceor type 'InternalTagOfInternalType' isnot defined. Maybe you want one of the following: InternalUnionType, InternalRecordType,DefaultTagOfInternalType, DefaultTagOfPrivateType,UnionTypeWithPrivateRepresentation
3333

3434
neg15.fs(128,31,128,61): typecheck error FS0072: Lookup on object of indeterminate type based on information priorto this program point. A type annotation may be needed priorto this program pointto constrain the type of the object. This may allow the lookupto be resolved.
3535

36-
neg15.fs(135,31,135,56): typecheck error FS0039: The value, constructor, namespaceor type 'InternalTagOfInternalType' isnot defined. Maybe you want one of the following: InternalUnionType, InternalRecordType, UnionTypeWithPrivateRepresentation
36+
neg15.fs(135,31,135,56): typecheck error FS0039: The value, constructor, namespaceor type 'InternalTagOfInternalType' isnot defined. Maybe you want one of the following: InternalUnionType, InternalRecordType,DefaultTagOfInternalType, DefaultTagOfPrivateType,UnionTypeWithPrivateRepresentation
3737

3838
neg15.fs(141,30,141,60): typecheck error FS0072: Lookup on object of indeterminate type based on information priorto this program point. A type annotation may be needed priorto this program pointto constrain the type of the object. This may allow the lookupto be resolved.
3939

‎tests/fsharp/typecheck/sigs/neg17.bsl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ neg17b.fs(16,19,16,48): typecheck error FS0072: Lookup on object of indeterminat
2121

2222
neg17b.fs(17,19,17,47): typecheck error FS0072: Lookup on object of indeterminate type based on information priorto this program point. A type annotation may be needed priorto this program pointto constrain the type of the object. This may allow the lookupto be resolved.
2323

24-
neg17b.fs(21,31,21,77): typecheck error FS0039: The value, constructor, namespaceor type 'DefaultTagOfUnionTypeWithPrivateRepresentation' isnot defined. Maybe you want one of the following: UnionTypeWithPrivateRepresentation, RecordTypeWithPrivateRepresentation
24+
neg17b.fs(21,31,21,77): typecheck error FS0039: The value, constructor, namespaceor type 'DefaultTagOfUnionTypeWithPrivateRepresentation' isnot defined. Maybe you want one of the following:DefaultTagOfInternalType, DefaultTagOfPrivateType,UnionTypeWithPrivateRepresentation, RecordTypeWithPrivateRepresentation
2525

2626
neg17b.fs(29,31,29,61): typecheck error FS0072: Lookup on object of indeterminate type based on information priorto this program point. A type annotation may be needed priorto this program pointto constrain the type of the object. This may allow the lookupto be resolved.
2727

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// #Warnings
2+
//<Expects status="Error" id="FS0039">The field, constructor or member 'ello' is not defined. Maybe you want one of the following: Hello</Expects>
3+
4+
typeMyRecord={ Hello:int; World:bool}
5+
6+
letr={ Hello=2; World=true}
7+
8+
letx= r.ello
9+
10+
exit0
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// #Warnings
2+
//<Expects status="Error" id="FS0039">The field, constructor or member 'AntherCase' is not defined. Maybe you want one of the following: AnotherCase</Expects>
3+
4+
typeMyUnion=
5+
| ASimpleCase
6+
| AnotherCaseofint
7+
8+
letu= MyUnion.AntherCase
9+
10+
11+
exit0

‎tests/fsharpqa/Source/Warnings/env.lst‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
SOURCE=SuggestModules.fs # SuggestModules.fs
2929
SOURCE=SuggestMethods.fs # SuggestMethods.fs
3030
SOURCE=SuggestAttributes.fs # SuggestAttributes.fs
31+
SOURCE=SuggestRecordLabels.fs SCFLAGS="--vserrors" # SuggestRecordLabels.fs
32+
SOURCE=SuggestUnionCases.fs SCFLAGS="--vserrors" # SuggestUnionCases.fs
3133
SOURCE=SuggestTypesInNamespace.fs # SuggestTypesInNamespace.fs
3234
SOURCE=DontSuggestCompletelyWrongStuff.fs SCFLAGS="--vserrors" # DontSuggestCompletelyWrongStuff.fs
3335
SOURCE=SuggestTypesInNamespaceVS.fs SCFLAGS="--vserrors" # SuggestTypesInNamespaceVS.fs

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp