Skip to content

Commit 0499207

Browse files
authored
Refactor ResolveLongIdentAsModuleOrNamespace (dotnet#14661)
1 parent d0dfdb9 commit 0499207

File tree

4 files changed

+30
-44
lines changed

4 files changed

+30
-44
lines changed

src/Compiler/Checking/CheckDeclarations.fs

+4-4
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ let ImplicitlyOpenOwnNamespace tcSink g amap scopem enclosingNamespacePath (env:
362362
match enclosingNamespacePathToOpen with
363363
| id :: rest ->
364364
let ad = env.AccessRights
365-
match ResolveLongIdentAsModuleOrNamespace tcSink ResultCollectionSettings.AllResults amap scopem true OpenQualified env.eNameResEnv ad id rest true with
365+
match ResolveLongIdentAsModuleOrNamespace tcSink amap scopem true OpenQualified env.eNameResEnv ad id rest true with
366366
| Result modrefs ->
367367
let modrefs = List.map p23 modrefs
368368
let lid = SynLongIdent(enclosingNamespacePathToOpen, [] , [])
@@ -637,7 +637,7 @@ let TcOpenLidAndPermitAutoResolve tcSink (env: TcEnv) amap (longId : Ident list)
637637
| [] -> []
638638
| id :: rest ->
639639
let m = longId |> List.map (fun id -> id.idRange) |> List.reduce unionRanges
640-
match ResolveLongIdentAsModuleOrNamespace tcSink ResultCollectionSettings.AllResults amap m true OpenQualified env.NameEnv ad id rest true with
640+
match ResolveLongIdentAsModuleOrNamespace tcSink amap m true OpenQualified env.NameEnv ad id rest true with
641641
| Result res -> res
642642
| Exception err ->
643643
errorR(err); []
@@ -1440,7 +1440,7 @@ module MutRecBindingChecking =
14401440
let resolved =
14411441
match p with
14421442
| [] -> Result []
1443-
| id :: rest -> ResolveLongIdentAsModuleOrNamespace cenv.tcSink ResultCollectionSettings.AllResults cenv.amap m true OpenQualified env.NameEnv ad id rest false
1443+
| id :: rest -> ResolveLongIdentAsModuleOrNamespace cenv.tcSink cenv.amap m true OpenQualified env.NameEnv ad id rest false
14441444

14451445
let mvvs = ForceRaise resolved
14461446

@@ -4575,7 +4575,7 @@ let rec TcSignatureElementNonMutRec (cenv: cenv) parent typeNames endm (env: TcE
45754575
let resolved =
45764576
match p with
45774577
| [] -> Result []
4578-
| id :: rest -> ResolveLongIdentAsModuleOrNamespace cenv.tcSink ResultCollectionSettings.AllResults cenv.amap m true OpenQualified env.NameEnv ad id rest false
4578+
| id :: rest -> ResolveLongIdentAsModuleOrNamespace cenv.tcSink cenv.amap m true OpenQualified env.NameEnv ad id rest false
45794579
let mvvs = ForceRaise resolved
45804580
let scopem = unionRanges m endm
45814581
let unfilteredModrefs = mvvs |> List.map p23

src/Compiler/Checking/CheckExpressions.fs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7225,7 +7225,7 @@ and TcConstExpr cenv (overallTy: OverallTy) env m tpenv c =
72257225
let expr =
72267226
let modName = "NumericLiteral" + suffix
72277227
let ad = env.eAccessRights
7228-
match ResolveLongIdentAsModuleOrNamespace cenv.tcSink ResultCollectionSettings.AtMostOneResult cenv.amap m true OpenQualified env.eNameResEnv ad (ident (modName, m)) [] false with
7228+
match ResolveLongIdentAsModuleOrNamespace cenv.tcSink cenv.amap m true OpenQualified env.eNameResEnv ad (ident (modName, m)) [] false with
72297229
| Result []
72307230
| Exception _ -> error(Error(FSComp.SR.tcNumericLiteralRequiresModule modName, m))
72317231
| Result ((_, mref, _) :: _) ->
@@ -7965,7 +7965,7 @@ and TcNameOfExpr (cenv: cenv) env tpenv (synArg: SynExpr) =
79657965
let resolvedToModuleOrNamespaceName =
79667966
if delayed.IsEmpty then
79677967
let id,rest = List.headAndTail longId
7968-
match ResolveLongIdentAsModuleOrNamespace cenv.tcSink ResultCollectionSettings.AllResults cenv.amap m true OpenQualified env.eNameResEnv ad id rest true with
7968+
match ResolveLongIdentAsModuleOrNamespace cenv.tcSink cenv.amap m true OpenQualified env.eNameResEnv ad id rest true with
79697969
| Result modref when delayed.IsEmpty && modref |> List.exists (p23 >> IsEntityAccessible cenv.amap m ad) ->
79707970
true // resolved to a module or namespace, done with checks
79717971
| _ ->

src/Compiler/Checking/NameResolution.fs

+24-37
Original file line numberDiff line numberDiff line change
@@ -2373,13 +2373,13 @@ let CheckForTypeLegitimacyAndMultipleGenericTypeAmbiguities
23732373
//-------------------------------------------------------------------------
23742374

23752375
/// Perform name resolution for an identifier which must resolve to be a module or namespace.
2376-
let rec ResolveLongIdentAsModuleOrNamespace sink (atMostOne: ResultCollectionSettings) (amap: Import.ImportMap) m first fullyQualified (nenv: NameResolutionEnv) ad (id:Ident) (rest: Ident list) isOpenDecl =
2376+
let rec ResolveLongIdentAsModuleOrNamespace sink (amap: Import.ImportMap) m first fullyQualified (nenv: NameResolutionEnv) ad (id:Ident) (rest: Ident list) isOpenDecl =
23772377
if first && id.idText = MangledGlobalName then
23782378
match rest with
23792379
| [] ->
23802380
error (Error(FSComp.SR.nrGlobalUsedOnlyAsFirstName(), id.idRange))
23812381
| id2 :: rest2 ->
2382-
ResolveLongIdentAsModuleOrNamespace sink atMostOne amap m false FullyQualified nenv ad id2 rest2 isOpenDecl
2382+
ResolveLongIdentAsModuleOrNamespace sink amap m false FullyQualified nenv ad id2 rest2 isOpenDecl
23832383
else
23842384
let notFoundAux (id: Ident) depth error (tcrefs: TyconRef seq) =
23852385
let suggestNames (addToBuffer: string -> unit) =
@@ -2390,7 +2390,7 @@ let rec ResolveLongIdentAsModuleOrNamespace sink (atMostOne: ResultCollectionSet
23902390
UndefinedName(depth, error, id, suggestNames)
23912391

23922392
let moduleOrNamespaces = nenv.ModulesAndNamespaces fullyQualified
2393-
let namespaceNotFound =
2393+
let namespaceOrModuleNotFound =
23942394
lazy
23952395
seq { for kv in moduleOrNamespaces do
23962396
for modref in kv.Value do
@@ -2399,70 +2399,57 @@ let rec ResolveLongIdentAsModuleOrNamespace sink (atMostOne: ResultCollectionSet
23992399

24002400
// Avoid generating the same error and name suggestion thunk twice It's not clear this is necessary
24012401
// since it's just saving an allocation.
2402-
let mutable moduleNotFoundErrorCache = None
2403-
let moduleNotFound (modref: ModuleOrNamespaceRef) (mty: ModuleOrNamespaceType) (id: Ident) depth =
2404-
match moduleNotFoundErrorCache with
2402+
let mutable namespaceNotFoundErrorCache = None
2403+
let namespaceNotFound (modref: ModuleOrNamespaceRef) (mty: ModuleOrNamespaceType) (id: Ident) depth =
2404+
match namespaceNotFoundErrorCache with
24052405
| Some (oldId, error) when equals oldId id.idRange -> error
24062406
| _ ->
24072407
let error =
24082408
seq { for kv in mty.ModulesAndNamespacesByDemangledName do
24092409
modref.NestedTyconRef kv.Value }
24102410
|> notFoundAux id depth FSComp.SR.undefinedNameNamespace
24112411
let error = raze error
2412-
moduleNotFoundErrorCache <- Some(id.idRange, error)
2412+
namespaceNotFoundErrorCache <- Some(id.idRange, error)
24132413
error
24142414

24152415
let notifyNameResolution (modref: ModuleOrNamespaceRef) m =
24162416
let item = Item.ModuleOrNamespaces [modref]
24172417
let occurence = if isOpenDecl then ItemOccurence.Open else ItemOccurence.Use
24182418
CallNameResolutionSink sink (m, nenv, item, emptyTyparInst, occurence, ad)
24192419

2420-
let modrefs =
2421-
match moduleOrNamespaces.TryGetValue id.idText with
2422-
| true, modrefs -> modrefs
2423-
| _ -> []
2424-
2425-
if not modrefs.IsEmpty then
2420+
match moduleOrNamespaces.TryGetValue id.idText with
2421+
| true, modrefs when not modrefs.IsEmpty ->
24262422
/// Look through the sub-namespaces and/or modules
24272423
let rec look depth (modref: ModuleOrNamespaceRef) (lid: Ident list) =
24282424
let mty = modref.ModuleOrNamespaceType
2429-
match lid with
2430-
| [] ->
2431-
success [ (depth, modref, mty) ]
24322425

2426+
match lid with
2427+
| [] -> success [ (depth, modref, mty) ]
24332428
| id :: rest ->
2434-
let modrefs =
2435-
match mty.ModulesAndNamespacesByDemangledName.TryGetValue id.idText with
2436-
| true, res -> [res]
2437-
| _ -> []
2438-
2439-
if not modrefs.IsEmpty then
2440-
modrefs
2441-
|> List.map (fun espec ->
2442-
let subref = modref.NestedTyconRef espec
2443-
if IsEntityAccessible amap m ad subref then
2444-
notifyNameResolution subref id.idRange
2445-
look (depth+1) subref rest
2446-
else
2447-
moduleNotFound modref mty id depth)
2448-
|> List.reduce AddResults
2449-
else
2450-
moduleNotFound modref mty id depth
2429+
match mty.ModulesAndNamespacesByDemangledName.TryGetValue id.idText with
2430+
| true, res ->
2431+
let subref = modref.NestedTyconRef res
24512432

2433+
if IsEntityAccessible amap m ad subref then
2434+
notifyNameResolution subref id.idRange
2435+
look (depth + 1) subref rest
2436+
else
2437+
namespaceNotFound modref mty id depth
2438+
| _ -> namespaceNotFound modref mty id depth
2439+
24522440
modrefs
24532441
|> List.map (fun modref ->
24542442
if IsEntityAccessible amap m ad modref then
24552443
notifyNameResolution modref id.idRange
24562444
look 1 modref rest
24572445
else
2458-
raze (namespaceNotFound.Force()))
2446+
raze (namespaceOrModuleNotFound.Force()))
24592447
|> List.reduce AddResults
2460-
else
2461-
raze (namespaceNotFound.Force())
2448+
| _ -> raze (namespaceOrModuleNotFound.Force())
24622449

24632450
// Note - 'rest' is annotated due to a bug currently in Unity (see: https://github.com/dotnet/fsharp/pull/7427)
24642451
let ResolveLongIdentAsModuleOrNamespaceThen sink atMostOne amap m fullyQualified (nenv: NameResolutionEnv) ad id (rest: Ident list) isOpenDecl f =
2465-
match ResolveLongIdentAsModuleOrNamespace sink ResultCollectionSettings.AllResults amap m true fullyQualified nenv ad id [] isOpenDecl with
2452+
match ResolveLongIdentAsModuleOrNamespace sink amap m true fullyQualified nenv ad id [] isOpenDecl with
24662453
| Result modrefs ->
24672454
match rest with
24682455
| [] -> error(Error(FSComp.SR.nrUnexpectedEmptyLongId(), id.idRange))

src/Compiler/Checking/NameResolution.fsi

-1
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,6 @@ val FreshenRecdFieldRef: NameResolver -> range -> RecdFieldRef -> RecdFieldInfo
663663
/// Resolve a long identifier to a namespace, module.
664664
val internal ResolveLongIdentAsModuleOrNamespace:
665665
sink: TcResultsSink ->
666-
atMostOne: ResultCollectionSettings ->
667666
amap: ImportMap ->
668667
m: range ->
669668
first: bool ->

0 commit comments

Comments
 (0)