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

Commit7c9c968

Browse files
committed
Show warning when DU is accessed without type but RequiredQualifiedAccess was set -closesdotnet#95
1 parent11ede75 commit7c9c968

File tree

5 files changed

+38
-31
lines changed

5 files changed

+38
-31
lines changed

‎src/fsharp/nameres.fs‎

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ type Item =
135135
/// Represents the resolution of a name to an F# value or function.
136136
| ValueofValRef
137137
/// Represents the resolution of a name to an F# union case.
138-
| UnionCaseofUnionCaseInfo
138+
| UnionCaseofUnionCaseInfo*bool
139139
/// Represents the resolution of a name to an F# active pattern result.
140140
| ActivePatternResultofActivePatternInfo*TType*int*range
141141
/// Represents the resolution of a name to an F# active pattern case within the body of an active pattern.
@@ -197,7 +197,7 @@ type Item =
197197
match dwith
198198
| Item.Value v-> v.DisplayName
199199
| Item.ActivePatternCase apref-> apref.Name
200-
| Item.UnionCaseuinfo-> DecompileOpName uinfo.UnionCase.DisplayName
200+
| Item.UnionCase(uinfo,_)-> DecompileOpName uinfo.UnionCase.DisplayName
201201
| Item.ExnCase tcref-> tcref.LogicalName
202202
| Item.RecdField rfinfo-> DecompileOpName rfinfo.RecdField.Name
203203
| Item.NewDef id-> id.idText
@@ -521,7 +521,7 @@ let AddRecdField (rfref:RecdFieldRef) tab = NameMultiMap.add rfref.FieldName rfr
521521
/// Add a set of union cases to the corresponding sub-table of the environment
522522
letAddUnionCases1(tab:Map<_,_>)(ucrefs:UnionCaseRef list)=
523523
(tab, ucrefs)||> List.fold(fun acc ucref->
524-
letitem= Item.UnionCase(GeneralizeUnionCaseRef ucref)
524+
letitem= Item.UnionCase(GeneralizeUnionCaseRef ucref,false)
525525
acc.Add(ucref.CaseName, item))
526526

527527
/// Add a set of union cases to the corresponding sub-table of the environment
@@ -530,13 +530,13 @@ let AddUnionCases2 bulkAddMode (eUnqualifiedItems: LayeredMap<_,_>) (ucrefs :Uni
530530
| BulkAdd.Yes->
531531
letitems=
532532
ucrefs|> Array.ofList|> Array.map(fun ucref->
533-
letitem= Item.UnionCase(GeneralizeUnionCaseRef ucref)
533+
letitem= Item.UnionCase(GeneralizeUnionCaseRef ucref,false)
534534
KeyValuePair(ucref.CaseName,item))
535535
eUnqualifiedItems.AddAndMarkAsCollapsible items
536536

537537
| BulkAdd.No->
538538
(eUnqualifiedItems,ucrefs)||> List.fold(fun acc ucref->
539-
letitem= Item.UnionCase(GeneralizeUnionCaseRef ucref)
539+
letitem= Item.UnionCase(GeneralizeUnionCaseRef ucref,false)
540540
acc.Add(ucref.CaseName, item))
541541

542542
/// Add any implied contents of a type definition to the environment.
@@ -754,7 +754,7 @@ let FreshenUnionCaseRef (ncenv: NameResolver) m (ucref:UnionCaseRef) =
754754
/// This must be called after fetching unqualified items that may need to be freshened
755755
letFreshenUnqualifiedItem(ncenv:NameResolver)m res=
756756
match reswith
757-
| Item.UnionCase(UnionCaseInfo(_,ucref))-> Item.UnionCase(FreshenUnionCaseRef ncenv m ucref)
757+
| Item.UnionCase(UnionCaseInfo(_,ucref),_)-> Item.UnionCase(FreshenUnionCaseRef ncenv m ucref,false)
758758
|_-> res
759759

760760

@@ -1549,7 +1549,7 @@ let rec ResolveLongIdentInTypePrim (ncenv:NameResolver) nenv lookupKind (resInfo
15491549
// Lookup: datatype constructors take precedence
15501550
match unionCaseSearchwith
15511551
| Some ucase->
1552-
success(resInfo,Item.UnionCase(ucase),rest)
1552+
success(resInfo,Item.UnionCase(ucase,false),rest)
15531553
| None->
15541554
match TryFindIntrinsicNamedItemOfType ncenv.InfoReader(nm,ad) findFlag m typwith
15551555
| Some(PropertyItem psets)when(match lookupKindwith LookupKind.Expr->true|_->false)->
@@ -1652,8 +1652,9 @@ let rec ResolveExprLongIdentInModuleOrNamespace (ncenv:NameResolver) nenv (typeN
16521652
match TryFindTypeWithUnionCase modref idwith
16531653
| Some tyconwhen IsTyconReprAccessible ncenv.amap m ad(modref.MkNestedTyconRef tycon)->
16541654
letucref= mkUnionCaseRef(modref.MkNestedTyconRef tycon) id.idText
1655+
letshowDeprecated= HasFSharpAttribute ncenv.g ncenv.g.attrib_RequireQualifiedAccessAttribute tycon.Attribs
16551656
letucinfo= FreshenUnionCaseRef ncenv m ucref
1656-
success(resInfo,Item.UnionCaseucinfo,rest)
1657+
success(resInfo,Item.UnionCase(ucinfo,showDeprecated),rest)
16571658
|_->
16581659
match mty.ExceptionDefinitionsByDemangledName.TryFind(id.idText)with
16591660
| Some exconwhen IsTyconReprAccessible ncenv.amap m ad(modref.MkNestedTyconRef excon)->
@@ -1854,8 +1855,9 @@ let rec ResolvePatternLongIdentInModuleOrNamespace (ncenv:NameResolver) nenv num
18541855
| Some tyconwhen IsTyconReprAccessible ncenv.amap m ad(modref.MkNestedTyconRef tycon)->
18551856
lettcref= modref.MkNestedTyconRef tycon
18561857
letucref= mkUnionCaseRef tcref id.idText
1858+
letshowDeprecated= HasFSharpAttribute ncenv.g ncenv.g.attrib_RequireQualifiedAccessAttribute tycon.Attribs
18571859
letucinfo= FreshenUnionCaseRef ncenv m ucref
1858-
success(resInfo,Item.UnionCaseucinfo,rest)
1860+
success(resInfo,Item.UnionCase(ucinfo,showDeprecated),rest)
18591861
|_->
18601862
match mty.ExceptionDefinitionsByDemangledName.TryFind(id.idText)with
18611863
| Some exncwhen IsEntityAccessible ncenv.amap m ad(modref.MkNestedTyconRef exnc)->
@@ -2411,7 +2413,7 @@ let IsUnionCaseUnseen ad g amap m (ucref:UnionCaseRef) =
24112413
letItemIsUnseen ad g amap m item=
24122414
match itemwith
24132415
| Item.Value x-> IsValUnseen ad g m x
2414-
| Item.UnionCase x-> IsUnionCaseUnseen ad g amap m x.UnionCaseRef
2416+
| Item.UnionCase(x,_)-> IsUnionCaseUnseen ad g amap m x.UnionCaseRef
24152417
| Item.ExnCase x-> IsTyconUnseen ad g amap m x
24162418
|_->false
24172419

@@ -2466,7 +2468,7 @@ let ResolveCompletionsInType (ncenv: NameResolver) nenv isApplicableMeth m ad st
24662468
lettc,tinst= destAppTy g typ
24672469
tc.UnionCasesAsRefList
24682470
|> List.filter(IsUnionCaseUnseen ad g ncenv.amap m>>not)
2469-
|> List.map(fun ucref-> Item.UnionCase(UnionCaseInfo(tinst,ucref)))
2471+
|> List.map(fun ucref-> Item.UnionCase(UnionCaseInfo(tinst,ucref),false))
24702472
else[]
24712473

24722474
leteinfos=
@@ -2738,7 +2740,7 @@ let rec ResolvePartialLongIdentInModuleOrNamespace (ncenv: NameResolver) nenv is
27382740
@(UnionCaseRefsInModuleOrNamespace modref
27392741
|> List.filter(IsUnionCaseUnseen ad g ncenv.amap m>>not)
27402742
|> List.map GeneralizeUnionCaseRef
2741-
|> List.map Item.UnionCase)
2743+
|> List.map(fun x->Item.UnionCase(x,false)))
27422744

27432745
// Collect up the accessible active patterns in the module
27442746
@(ActivePatternElemsOfModuleOrNamespace modref

‎src/fsharp/nameres.fsi‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ type ArgumentContainer =
3838
typeItem=
3939
// These exist in the "eUnqualifiedItems" List.map in the type environment.
4040
| ValueofValRef
41-
| UnionCaseofUnionCaseInfo
41+
// UnionCaseInfo and temporary flag which is used to show a "use case is deprecated" message
42+
| UnionCaseofUnionCaseInfo*bool
4243
| ActivePatternResultofActivePatternInfo*TType*int*range
4344
| ActivePatternCaseofActivePatternElemRef
4445
| ExnCaseofTyconRef

‎src/fsharp/tc.fs‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1876,7 +1876,11 @@ let rec ApplyUnionCaseOrExn (makerForUnionCase,makerForExnTag) m cenv env overal
18761876
let mkf = makerForExnTag(ecref)
18771877
mkf,recdFieldTysOfExnDefRef ecref, [ for f in (recdFieldsOfExnDefRef ecref) -> f.Name ]
18781878

1879-
| Item.UnionCase ucinfo ->
1879+
| Item.UnionCase(ucinfo,showDeprecated) ->
1880+
if showDeprecated then
1881+
let message = sprintf "The union type for union case '%s' was defined with the RequireQualifiedAccessAttribute. Include the name of the union type ('%s') in the name you are using.'" ucinfo.Name ucinfo.Tycon.DisplayName
1882+
warning(Deprecated(message,m))
1883+
18801884
let ucref = ucinfo.UnionCaseRef
18811885
CheckUnionCaseAttributes cenv.g ucref m |> CommitOperationResult
18821886
CheckUnionCaseAccessible cenv.amap m ad ucref |> ignore
@@ -4872,7 +4876,7 @@ and TcPat warnOnUpper cenv env topValInfo vFlags (tpenv,names,takenNames) ty pat
48724876
| None ->
48734877
let caseName =
48744878
match item with
4875-
| Item.UnionCaseuci -> uci.Name
4879+
| Item.UnionCase(uci,_) -> uci.Name
48764880
| Item.ExnCase tcref -> tcref.DisplayName
48774881
| _ -> failwith "impossible"
48784882
error(Error(FSComp.SR.tcUnionCaseConstructorDoesNotHaveFieldWithGivenName(caseName, id.idText), id.idRange))
@@ -4881,7 +4885,7 @@ and TcPat warnOnUpper cenv env topValInfo vFlags (tpenv,names,takenNames) ty pat
48814885
| null ->
48824886
result.[idx] <- pat
48834887
let argContainerOpt = match item with
4884-
| Item.UnionCaseuci -> Some(ArgumentContainer.UnionCase(uci))
4888+
| Item.UnionCase(uci,_) -> Some(ArgumentContainer.UnionCase(uci))
48854889
| Item.ExnCase tref -> Some(ArgumentContainer.Type(tref))
48864890
| _ -> None
48874891
let argItem = Item.ArgName (id, (List.nth argtys idx), argContainerOpt)
@@ -7871,7 +7875,7 @@ and TcItemThen cenv overallTy env tpenv (item,mItem,rest,afterOverloadResolution
78717875
let ucref = mkChoiceCaseRef cenv.g mItem aparity n
78727876
let _,_,tinst,_ = infoOfTyconRef mItem ucref.TyconRef
78737877
let ucinfo = UnionCaseInfo(tinst,ucref)
7874-
ApplyUnionCaseOrExnTypes mItem cenv env ucaseAppTy (Item.UnionCaseucinfo)
7878+
ApplyUnionCaseOrExnTypes mItem cenv env ucaseAppTy (Item.UnionCase(ucinfo,false))
78757879
| _ ->
78767880
ApplyUnionCaseOrExnTypes mItem cenv env ucaseAppTy item
78777881
let nargtys = List.length argtys
@@ -7927,7 +7931,7 @@ and TcItemThen cenv overallTy env tpenv (item,mItem,rest,afterOverloadResolution
79277931
if box fittedArgs.[i] = null then
79287932
fittedArgs.[i] <- arg
79297933
let argContainerOpt = match item with
7930-
| Item.UnionCaseuci -> Some(ArgumentContainer.UnionCase(uci))
7934+
| Item.UnionCase(uci,_) -> Some(ArgumentContainer.UnionCase(uci))
79317935
| Item.ExnCase tref -> Some(ArgumentContainer.Type(tref))
79327936
| _ -> None
79337937
let argItem = Item.ArgName (id, (List.nth argtys i), argContainerOpt)
@@ -7956,7 +7960,7 @@ and TcItemThen cenv overallTy env tpenv (item,mItem,rest,afterOverloadResolution
79567960
else
79577961
let caseName =
79587962
match item with
7959-
| Item.UnionCaseuci -> uci.Name
7963+
| Item.UnionCase(uci,_) -> uci.Name
79607964
| Item.ExnCase tcref -> tcref.DisplayName
79617965
| _ -> failwith "impossible"
79627966
error(Error(FSComp.SR.tcUnionCaseConstructorDoesNotHaveFieldWithGivenName(caseName, id.idText), id.idRange))
@@ -14021,9 +14025,9 @@ module EstablishTypeDefinitionCores = begin
1402114025
// Constructors should be visible from IntelliSense, so add fake names for them
1402214026
for unionCase in unionCases do
1402314027
let info = UnionCaseInfo(thisTyInst,mkUnionCaseRef thisTyconRef unionCase.Id.idText)
14024-
let nenv' = AddFakeNameToNameEnv unionCase.Id.idText nenv (Item.UnionCaseinfo)
14028+
let nenv' = AddFakeNameToNameEnv unionCase.Id.idText nenv (Item.UnionCase(info,false))
1402514029
// Report to both - as in previous function
14026-
let item = Item.UnionCaseinfo
14030+
let item = Item.UnionCase(info,false)
1402714031
CallNameResolutionSink cenv.tcSink (unionCase.Range,nenv,item,item,ItemOccurence.Binding,envinner.DisplayEnv,ad)
1402814032
CallEnvSink cenv.tcSink (unionCase.Id.idRange, nenv', ad)
1402914033

‎src/fsharp/vs/ServiceDeclarations.fs‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ module internal ItemDescriptionsImpl =
162162
let recrangeOfItem(g:TcGlobals)isDeclInfo d=
163163
match dwith
164164
| Item.Value vref| Item.CustomBuilder(_,vref)-> Some(if isDeclInfothen vref.Rangeelse vref.DefinitionRange)
165-
| Item.UnionCaseucinfo-> Some ucinfo.UnionCase.Range
165+
| Item.UnionCase(ucinfo,_)-> Some ucinfo.UnionCase.Range
166166
| Item.ActivePatternCase apref-> Some apref.ActivePatternVal.Range
167167
| Item.ExnCase tcref-> Some tcref.Range
168168
| Item.RecdField rfinfo-> Some rfinfo.RecdFieldRef.Range
@@ -192,7 +192,7 @@ module internal ItemDescriptionsImpl =
192192
let recccuOfItem g d=
193193
match dwith
194194
| Item.Value vref| Item.CustomBuilder(_,vref)-> ccuOfValRef vref
195-
| Item.UnionCaseucinfo-> computeCcuOfTyconRef ucinfo.TyconRef
195+
| Item.UnionCase(ucinfo,_)-> computeCcuOfTyconRef ucinfo.TyconRef
196196
| Item.ActivePatternCase apref-> ccuOfValRef apref.ActivePatternVal
197197
| Item.ExnCase tcref-> computeCcuOfTyconRef tcref
198198
| Item.RecdField rfinfo-> computeCcuOfTyconRef rfinfo.RecdFieldRef.TyconRef
@@ -347,7 +347,7 @@ module internal ItemDescriptionsImpl =
347347
| None-> XmlCommentNone
348348
else
349349
XmlCommentNone
350-
| Item.UnionCaseucinfo-> GetXmlDocSigOfUnionCaseInfo ucinfo
350+
| Item.UnionCase(ucinfo,_)-> GetXmlDocSigOfUnionCaseInfo ucinfo
351351
| Item.ExnCase tcref-> GetXmlDocSigOfEntityRef infoReader m tcref
352352
| Item.RecdField rfinfo-> GetXmlDocSigOfRecdFieldInfo rfinfo
353353
| Item.NewDef_-> XmlCommentNone
@@ -511,7 +511,7 @@ module internal ItemDescriptionsImpl =
511511
| Wrap(Item.Value vref1| Item.CustomBuilder(_,vref1)), Wrap(Item.Value vref2| Item.CustomBuilder(_,vref2))-> valRefEq g vref1 vref2
512512
| Wrap(Item.ActivePatternCase(APElemRef(_apinfo1, vref1, idx1))), Wrap(Item.ActivePatternCase(APElemRef(_apinfo2, vref2, idx2)))->
513513
idx1= idx2&& valRefEq g vref1 vref2
514-
| Wrap(Item.UnionCase(UnionCaseInfo(_, ur1))), Wrap(Item.UnionCase(UnionCaseInfo(_, ur2)))-> g.unionCaseRefEq ur1 ur2
514+
| Wrap(Item.UnionCase(UnionCaseInfo(_, ur1),_)), Wrap(Item.UnionCase(UnionCaseInfo(_, ur2),_))-> g.unionCaseRefEq ur1 ur2
515515
| Wrap(Item.RecdField(RecdFieldInfo(_, RFRef(tcref1, n1)))), Wrap(Item.RecdField(RecdFieldInfo(_, RFRef(tcref2, n2))))->
516516
(tyconRefEq g tcref1 tcref2)&&(n1= n2)// there is no direct function as in the previous case
517517
| Wrap(Item.Property(_, pi1s)), Wrap(Item.Property(_, pi2s))->
@@ -542,7 +542,7 @@ module internal ItemDescriptionsImpl =
542542
| Wrap(Item.Value vref| Item.CustomBuilder(_,vref))-> hash vref.LogicalName
543543
| Wrap(Item.ActivePatternCase(APElemRef(_apinfo, vref, idx)))-> hash(vref.LogicalName, idx)
544544
| Wrap(Item.ExnCase(tcref))-> hash tcref.Stamp
545-
| Wrap(Item.UnionCase(UnionCaseInfo(_, UCRef(tcref, n))))-> hash(tcref.Stamp, n)
545+
| Wrap(Item.UnionCase(UnionCaseInfo(_, UCRef(tcref, n)),_))-> hash(tcref.Stamp, n)
546546
| Wrap(Item.RecdField(RecdFieldInfo(_, RFRef(tcref, n))))-> hash(tcref.Stamp, n)
547547
| Wrap(Item.Event evt)-> evt.ComputeHashCode()
548548
| Wrap(Item.Property(_name, pis))-> hash(pis|> List.map(fun pi-> pi.ComputeHashCode()))
@@ -611,7 +611,7 @@ module internal ItemDescriptionsImpl =
611611
DataTipElement(text, xml)
612612

613613
// Union tags (constructors)
614-
| Item.UnionCaseucinfo->
614+
| Item.UnionCase(ucinfo,_)->
615615
letuc= ucinfo.UnionCase
616616
letrty= generalizedTyconRef ucinfo.TyconRef
617617
letrecd= uc.RecdFields
@@ -886,7 +886,7 @@ module internal ItemDescriptionsImpl =
886886
bufferL os tpcsL
887887
else
888888
bufferL os(NicePrint.layoutPrettifiedTypeAndConstraints denv[] tau)
889-
| Item.UnionCaseucinfo->
889+
| Item.UnionCase(ucinfo,_)->
890890
letrty= generalizedTyconRef ucinfo.TyconRef
891891
NicePrint.outputTy denv os rty
892892
| Item.ActivePatternCase(apref)->
@@ -970,7 +970,7 @@ module internal ItemDescriptionsImpl =
970970
| Item.Value vref| Item.CustomBuilder(_,vref)-> getKeywordForValRef vref
971971
| Item.ActivePatternCase apref-> apref.ActivePatternVal|> getKeywordForValRef
972972

973-
| Item.UnionCaseucinfo->
973+
| Item.UnionCase(ucinfo,_)->
974974
(ucinfo.TyconRef|> ticksAndArgCountTextOfTyconRef)+"."+ucinfo.Name|> Some
975975

976976
| Item.RecdField rfi->

‎src/fsharp/vs/service.fs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ module internal Params =
180180
|> List.map ParamNameAndType.FromArgInfo
181181
|> List.map(fun(ParamNameAndType(nmOpt,pty))-> ParamData(false,false, NotOptional, nmOpt, ReflectedArgInfo.None, pty))
182182
ParamsOfParamDatas g denv paramDatas returnTy
183-
| Item.UnionCase(ucr)->
183+
| Item.UnionCase(ucr,_)->
184184
match ucr.UnionCase.RecdFieldswith
185185
|[f]->[ParamOfUnionCaseField g denv NicePrint.isGeneratedUnionCaseField-1 f]
186186
| fs-> fs|> List.mapi(ParamOfUnionCaseField g denv NicePrint.isGeneratedUnionCaseField)
@@ -294,7 +294,7 @@ type MethodOverloads( name: string, unsortedMethods: Method[] ) =
294294
if isFunction g rfinfo.FieldTypethen[item]else[]
295295
| Item.Value v->
296296
if isFunction g v.Typethen[item]else[]
297-
| Item.UnionCase(ucr)->
297+
| Item.UnionCase(ucr,_)->
298298
ifnot ucr.UnionCase.IsNullarythen[item]else[]
299299
| Item.ExnCase(ecr)->
300300
if recdFieldsOfExnDefRef ecr|> nonNilthen[item]else[]

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp