Skip to content
This repository was archived by the owner on Dec 23, 2024. It is now read-only.

Commit efdef78

Browse files
auduchinoknosami
authored andcommitted
Don't add "opens" for parent namespaces to tc environments (dotnet#10386)
* Don't add "opens" for parens namespaces to tc environments * Revert sorted opens change * Better parameter name * Add test * Update parameter name in signature * Add another test case
1 parent d84b3d6 commit efdef78

File tree

5 files changed

+66
-12
lines changed

5 files changed

+66
-12
lines changed

src/fsharp/CheckDeclarations.fs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2175,7 +2175,7 @@ module MutRecBindingChecking =
21752175
/// Compute the active environments within each nested module.
21762176
let TcMutRecDefns_ComputeEnvs getTyconOpt getVals (cenv: cenv) report scopem m envInitial mutRecShape =
21772177
(envInitial, mutRecShape) ||> MutRecShapes.computeEnvs
2178-
(fun envAbove (MutRecDefnsPhase2DataForModule (mtypeAcc, mspec)) -> MakeInnerEnvWithAcc envAbove mspec.Id mtypeAcc mspec.ModuleOrNamespaceType.ModuleOrNamespaceKind)
2178+
(fun envAbove (MutRecDefnsPhase2DataForModule (mtypeAcc, mspec)) -> MakeInnerEnvWithAcc true envAbove mspec.Id mtypeAcc mspec.ModuleOrNamespaceType.ModuleOrNamespaceKind)
21792179
(fun envAbove decls ->
21802180

21812181
// Collect the type definitions, exception definitions, modules and "open" declarations
@@ -3202,7 +3202,7 @@ module EstablishTypeDefinitionCores =
32023202
CheckForDuplicateConcreteType envInitial id.idText im
32033203
CheckNamespaceModuleOrTypeName cenv.g id
32043204

3205-
let envForDecls, mtypeAcc = MakeInnerEnv envInitial id modKind
3205+
let envForDecls, mtypeAcc = MakeInnerEnv true envInitial id modKind
32063206
let mty = Construct.NewEmptyModuleOrNamespaceType modKind
32073207
let doc = xml.ToXmlDoc(true, Some [])
32083208
let mspec = Construct.NewModuleOrNamespace (Some envInitial.eCompPath) vis id doc modAttrs (MaybeLazy.Strict mty)
@@ -4341,7 +4341,7 @@ module EstablishTypeDefinitionCores =
43414341
(envInitial, withEntities) ||> MutRecShapes.computeEnvs
43424342
(fun envAbove (MutRecDefnsPhase2DataForModule (mtypeAcc, mspec)) ->
43434343
PublishModuleDefn cenv envAbove mspec
4344-
MakeInnerEnvWithAcc envAbove mspec.Id mtypeAcc mspec.ModuleOrNamespaceType.ModuleOrNamespaceKind)
4344+
MakeInnerEnvWithAcc true envAbove mspec.Id mtypeAcc mspec.ModuleOrNamespaceType.ModuleOrNamespaceKind)
43454345
(fun envAbove _ -> envAbove)
43464346

43474347
// Updates the types of the modules to contain the contents so far, which now includes the nested modules and types
@@ -5212,7 +5212,7 @@ and TcModuleOrNamespaceSignatureElementsNonMutRec cenv parent env (id, modKind,
52125212
let endm = m.EndRange // use end of range for errors
52135213

52145214
// Create the module type that will hold the results of type checking....
5215-
let envForModule, mtypeAcc = MakeInnerEnv env id modKind
5215+
let envForModule, mtypeAcc = MakeInnerEnv true env id modKind
52165216

52175217
// Now typecheck the signature, using mutation to fill in the submodule description.
52185218
let! envAtEnd = TcSignatureElements cenv parent endm envForModule xml None defs
@@ -5350,7 +5350,7 @@ let rec TcModuleOrNamespaceElementNonMutRec (cenv: cenv) parent typeNames scopem
53505350

53515351
CheckNamespaceModuleOrTypeName cenv.g id
53525352

5353-
let envForModule, mtypeAcc = MakeInnerEnv env id modKind
5353+
let envForModule, mtypeAcc = MakeInnerEnv true env id modKind
53545354

53555355
// Create the new module specification to hold the accumulated results of the type of the module
53565356
// Also record this in the environment as the accumulator

src/fsharp/CheckExpressions.fs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -449,22 +449,26 @@ let UnifyTypes cenv (env: TcEnv) m actualTy expectedTy =
449449
ConstraintSolver.AddCxTypeEqualsType env.eContextInfo env.DisplayEnv cenv.css m (tryNormalizeMeasureInType cenv.g actualTy) (tryNormalizeMeasureInType cenv.g expectedTy)
450450

451451
/// Make an environment suitable for a module or namespace. Does not create a new accumulator but uses one we already have/
452-
let MakeInnerEnvWithAcc env nm mtypeAcc modKind =
452+
let MakeInnerEnvWithAcc addOpenToNameEnv env nm mtypeAcc modKind =
453453
let path = env.ePath @ [nm]
454454
let cpath = env.eCompPath.NestedCompPath nm.idText modKind
455455
{ env with
456456
ePath = path
457457
eCompPath = cpath
458458
eAccessPath = cpath
459459
eAccessRights = ComputeAccessRights cpath env.eInternalsVisibleCompPaths env.eFamilyType // update this computed field
460-
eNameResEnv = { env.NameEnv with eDisplayEnv = env.DisplayEnv.AddOpenPath (pathOfLid path) }
460+
eNameResEnv =
461+
if addOpenToNameEnv then
462+
{ env.NameEnv with eDisplayEnv = env.DisplayEnv.AddOpenPath (pathOfLid path) }
463+
else
464+
env.NameEnv
461465
eModuleOrNamespaceTypeAccumulator = mtypeAcc }
462466

463467
/// Make an environment suitable for a module or namespace, creating a new accumulator.
464-
let MakeInnerEnv env nm modKind =
468+
let MakeInnerEnv addOpenToNameEnv env nm modKind =
465469
// Note: here we allocate a new module type accumulator
466470
let mtypeAcc = ref (Construct.NewEmptyModuleOrNamespaceType modKind)
467-
MakeInnerEnvWithAcc env nm mtypeAcc modKind, mtypeAcc
471+
MakeInnerEnvWithAcc addOpenToNameEnv env nm mtypeAcc modKind, mtypeAcc
468472

469473
/// Make an environment suitable for processing inside a type definition
470474
let MakeInnerEnvForTyconRef env tcref isExtrinsicExtension =
@@ -502,7 +506,8 @@ let LocateEnv ccu env enclosingNamespacePath =
502506
eAccessPath = cpath
503507
// update this computed field
504508
eAccessRights = ComputeAccessRights cpath env.eInternalsVisibleCompPaths env.eFamilyType }
505-
let env = List.fold (fun env id -> MakeInnerEnv env id Namespace |> fst) env enclosingNamespacePath
509+
let env = List.fold (fun env id -> MakeInnerEnv false env id Namespace |> fst) env enclosingNamespacePath
510+
let env = { env with eNameResEnv = { env.NameEnv with eDisplayEnv = env.DisplayEnv.AddOpenPath (pathOfLid env.ePath) } }
506511
env
507512

508513

src/fsharp/CheckExpressions.fsi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -610,11 +610,11 @@ val MakeInnerEnvForTyconRef: env: TcEnv -> tcref: TyconRef -> isExtrinsicExtensi
610610

611611
/// Return a new environment suitable for processing declarations in the interior of a module definition
612612
/// including creating an accumulator for the module type.
613-
val MakeInnerEnv: env: TcEnv -> nm: Ident -> modKind: ModuleOrNamespaceKind -> TcEnv * ModuleOrNamespaceType ref
613+
val MakeInnerEnv: addOpenToNameEnv: bool -> env: TcEnv -> nm: Ident -> modKind: ModuleOrNamespaceKind -> TcEnv * ModuleOrNamespaceType ref
614614

615615
/// Return a new environment suitable for processing declarations in the interior of a module definition
616616
/// given that the accumulator for the module type already exisits.
617-
val MakeInnerEnvWithAcc: env: TcEnv -> nm: Ident -> mtypeAcc: ModuleOrNamespaceType ref -> modKind: ModuleOrNamespaceKind -> TcEnv
617+
val MakeInnerEnvWithAcc: addOpenToNameEnv: bool -> env: TcEnv -> nm: Ident -> mtypeAcc: ModuleOrNamespaceType ref -> modKind: ModuleOrNamespaceKind -> TcEnv
618618

619619
/// Produce a post-generalization type scheme for a simple type where no type inference generalization
620620
/// is appplied.

tests/service/Common.fs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,19 @@ let assertHasSymbolUsages (names: string list) (results: FSharpCheckFileResults)
395395
for name in names do
396396
Assert.That(Set.contains name symbolNames, name)
397397

398+
399+
let findSymbolUseByName (name: string) (results: FSharpCheckFileResults) =
400+
getSymbolUses results
401+
|> Array.find (fun symbolUse ->
402+
match getSymbolName symbolUse.Symbol with
403+
| Some symbolName -> symbolName = name
404+
| _ -> false)
405+
406+
let findSymbolByName (name: string) (results: FSharpCheckFileResults) =
407+
let symbolUse = findSymbolUseByName name results
408+
symbolUse.Symbol
409+
410+
398411
let getRangeCoords (r: range) =
399412
(r.StartLine, r.StartColumn), (r.EndLine, r.EndColumn)
400413

tests/service/Symbols.fs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,39 @@ let x = 123
122122
|> Option.orElseWith (fun _ -> failwith "Could not get symbol")
123123
|> Option.map (fun su -> su.Symbol :?> FSharpMemberOrFunctionOrValue)
124124
|> Option.iter (fun symbol -> symbol.Attributes.Count |> shouldEqual 1)
125+
126+
module Types =
127+
[<Test>]
128+
let ``FSharpType.Print parent namespace qualifiers`` () =
129+
let _, checkResults = getParseAndCheckResults """
130+
namespace Ns1.Ns2
131+
type T() = class end
132+
type A = T
133+
134+
namespace Ns1.Ns3
135+
type B = Ns1.Ns2.T
136+
137+
namespace Ns1.Ns4
138+
open Ns1.Ns2
139+
type C = Ns1.Ns2.T
140+
141+
namespace Ns1.Ns5
142+
open Ns1
143+
type D = Ns1.Ns2.T
144+
145+
namespace Ns1.Ns2.Ns6
146+
type E = Ns1.Ns2.T
147+
"""
148+
[| "A", "T"
149+
"B", "Ns1.Ns2.T"
150+
"C", "T"
151+
"D", "Ns2.T"
152+
"E", "Ns1.Ns2.T" |]
153+
|> Array.iter (fun (symbolName, expectedPrintedType) ->
154+
let symbolUse = findSymbolUseByName symbolName checkResults
155+
match symbolUse.Symbol with
156+
| :? FSharpEntity as entity ->
157+
entity.AbbreviatedType.Format(symbolUse.DisplayContext)
158+
|> should equal expectedPrintedType
159+
160+
| _ -> failwithf "Couldn't get entity: %s" symbolName)

0 commit comments

Comments
 (0)