Skip to content

Commit 8639afa

Browse files
authored
fix: when constructing instance names, avoid private names (#11385)
This PR lets implicit instance names avoid name clashes with private declarations. This fixes #10329.
1 parent fea5553 commit 8639afa

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

src/Lean/Elab/Util.lean

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,11 @@ def liftMacroM [Monad m] [MonadMacroAdapter m] [MonadEnv m] [MonadRecDepth m] [M
186186
match (← expandMacroImpl? env stx) with
187187
| some (_, stx?) => liftExcept stx?
188188
| none => return none
189-
hasDecl := fun declName => return env.contains declName
189+
hasDecl := fun declName => do
190+
-- this is used (by mkUnusedBaseName) to find available names, so check
191+
-- for both private and public names
192+
let env := env.setExporting false
193+
return env.contains (mkPrivateName env declName) || env.contains (privateToUserName declName)
190194
getCurrNamespace := return currNamespace
191195
resolveNamespace := fun n => return ResolveName.resolveNamespace env currNamespace openDecls n
192196
resolveGlobalName := fun n => return ResolveName.resolveGlobalName env opts currNamespace openDecls n

tests/lean/run/issue10329.lean

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
module
2+
3+
set_option warn.sorry false
4+
5+
public class X
6+
instance : X := sorry
7+
public instance : X := sorry
8+
instance : X := sorry
9+
10+
namespace InNamespace
11+
public class Y
12+
instance : Y := sorry
13+
public instance : Y := sorry
14+
instance : Y := sorry
15+
end InNamespace
16+
17+
18+
inductive Day where
19+
| mo | tu | we
20+
deriving Repr
21+
22+
def Day.succ? : Day → Option Day
23+
| mo => some tu
24+
| tu => some we
25+
| we => none
26+
27+
instance : Std.PRange.UpwardEnumerable Day where
28+
succ? := Day.succ?
29+
30+
def Day.toNat : Day → Nat
31+
| mo => 0
32+
| tu => 1
33+
| we => 2
34+
35+
instance : LT Day where
36+
lt _ _ := True
37+
38+
instance : LE Day where
39+
le _ _ := True
40+
41+
instance : Std.Rxo.IsAlwaysFinite Day where
42+
finite init hi := ⟨3, by sorry
43+
44+
instance : Std.Rxi.IsAlwaysFinite Day where
45+
finite init := ⟨3, by sorry
46+
47+
instance : Std.Rxc.IsAlwaysFinite Day where
48+
finite init hi := ⟨3, by sorry

0 commit comments

Comments
 (0)