File tree Expand file tree Collapse file tree 9 files changed +60
-22
lines changed
Expand file tree Collapse file tree 9 files changed +60
-22
lines changed Original file line number Diff line number Diff line change @@ -182,6 +182,12 @@ structure EnvironmentHeader where
182182 `ModuleIdx` for the same module.
183183 -/
184184 modules : Array EffectiveImport := #[]
185+ /--
186+ Subset of `modules` for which `importAll` is `true`. This is assumed to be a much smaller set so
187+ we precompute it instead of iterating over all of `modules` multiple times. However, note that
188+ in a non-`module` file, this is identical to `modules`.
189+ -/
190+ importAllModules : Array EffectiveImport := modules.filter (·.importAll)
185191 /-- Module data for all imported modules. -/
186192 moduleData : Array ModuleData := #[]
187193 deriving Nonempty
Original file line number Diff line number Diff line change @@ -26,12 +26,19 @@ def mkPrivateName (env : Environment) (n : Name) : Name :=
2626 -- is private to *this* module.
2727 mkPrivateNameCore env.mainModule <| privateToUserName n
2828
29- def isInaccessiblePrivateName (env : Environment) (n : Name) : Bool :=
30- if env.header.isModule then
31- -- Allow access through `import all`.
32- env.isExporting && isPrivateName n
33- else match privateToUserName? n with
34- | some userName => mkPrivateName env userName != n
35- | _ => false
29+ def isInaccessiblePrivateName (env : Environment) (n : Name) : Bool := Id.run do
30+ if !isPrivateName n then
31+ return false
32+ -- All private names are inaccessible from the public scope
33+ if env.isExporting then
34+ return true
35+ -- In the private scope, ...
36+ match env.getModuleIdxFor? n with
37+ | some modIdx =>
38+ -- ... allow access through `import all`
39+ !env.header.isModule || !env.header.modules[modIdx]?.any (·.importAll)
40+ | none =>
41+ -- ... allow all accesses in the current module
42+ false
3643
3744end Lean
Original file line number Diff line number Diff line change @@ -111,11 +111,9 @@ private partial def resolvePrivateName (env : Environment) (declName : Name) : O
111111 if containsDeclOrReserved env (mkPrivateName env declName) then
112112 return mkPrivateName env declName
113113 -- Under the module system, we assume there are at most a few `import all`s and we can just test
114- -- them on by one.
114+ -- them one by one.
115115 guard <| env.header.isModule
116- -- As `all` is not transitive, we only have to check the direct imports.
117- env.header.imports.findSome? fun i => do
118- guard i.importAll
116+ env.header.importAllModules.findSome? fun i => do
119117 let n := mkPrivateNameCore i.module declName
120118 guard <| containsDeclOrReserved env n
121119 return n
Original file line number Diff line number Diff line change @@ -7,9 +7,6 @@ def test : CoreM Unit := do
77 let ctorLayout ← getCtorLayout ``Lean.IR.Expr.reuse;
88 ctorLayout.fieldInfo.forM $ fun finfo => IO.println (format finfo);
99 IO.println "---" ;
10- let ctorLayout ← getCtorLayout ``Lean.EnvironmentHeader.mk;
11- ctorLayout.fieldInfo.forM $ fun finfo => IO.println (format finfo);
12- IO.println "---" ;
1310 let ctorLayout ← getCtorLayout ``Subtype.mk;
1411 ctorLayout.fieldInfo.forM $ fun finfo => IO.println (format finfo);
1512 pure ()
Original file line number Diff line number Diff line change @@ -3,13 +3,5 @@ obj@1:obj
33scalar#1@0:u8
44obj@2:obj
55---
6- scalar#4@0:u32
7- obj@0:tobj
8- scalar#1@4:u8
9- obj@1:obj
10- obj@2:obj
11- obj@3:obj
12- obj@4:obj
13- ---
146obj@0:tobj
157◾
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ import Module.ImportedAll
55import Module.ImportedPrivateImported
66import Module.PrivateImported
77import Module.ImportedAllPrivateImported
8+ import Module.ImportedAllImportedAll
89import Module.NonModule
910import Module.MetaImported
1011
Original file line number Diff line number Diff line change @@ -312,6 +312,15 @@ constructor:
312312#with_exporting
313313#check { x := 1 : StructWithPrivateField }
314314
315+ #check (⟨1 ⟩ : StructWithPrivateField)
316+
317+ /--
318+ error: Invalid `⟨...⟩` notation: Constructor for `StructWithPrivateField` is marked as private
319+ -/
320+ #guard_msgs in
321+ #with_exporting
322+ #check (⟨1 ⟩ : StructWithPrivateField)
323+
315324#check StructWithPrivateField.x
316325
317326/-- error: Unknown constant `StructWithPrivateField.x` -/
Original file line number Diff line number Diff line change @@ -144,3 +144,19 @@ Note: A private declaration `priv✝` (from `Module.Basic`) exists but would nee
144144-/
145145#guard_msgs in
146146@[expose] public def pub' := priv
147+
148+ #check { x := 1 : StructWithPrivateField }
149+
150+ /-- error: invalid {...} notation, constructor for `StructWithPrivateField` is marked as private -/
151+ #guard_msgs in
152+ #with_exporting
153+ #check { x := 1 : StructWithPrivateField }
154+
155+ #check (⟨1 ⟩ : StructWithPrivateField)
156+
157+ /--
158+ error: Invalid `⟨...⟩` notation: Constructor for `StructWithPrivateField` is marked as private
159+ -/
160+ #guard_msgs in
161+ #with_exporting
162+ #check (⟨1 ⟩ : StructWithPrivateField)
Original file line number Diff line number Diff line change 1+ module
2+
3+ prelude
4+ import all Module.ImportedAll
5+
6+ /-! `import all` should chain with nested `import all`s. -/
7+
8+ #check priv
9+
10+ #check { x := 1 : StructWithPrivateField }
11+
12+ #check (⟨1 ⟩ : StructWithPrivateField)
You can’t perform that action at this time.
0 commit comments