Macros sometimes create auxiliary types and instances about them, and they rely on the instance name generate to create unique names in that case. This modifies the automatic name generator to add a fresh macro scope to the generated name if any of the constants in the type of the instance themselves have macro scopes. Closes #2044
65 lines
1.2 KiB
Text
65 lines
1.2 KiB
Text
import Lean
|
|
/-!
|
|
# Make sure generated instance names are unique if there are macro scopes
|
|
-/
|
|
|
|
open Lean Elab Command
|
|
|
|
/-!
|
|
Example adapted from #2044
|
|
-/
|
|
|
|
#eval (do
|
|
let stx ← `(
|
|
structure Foo where
|
|
field : String
|
|
instance : Nonempty Foo := ⟨⟨"hi"⟩⟩)
|
|
elabCommand stx
|
|
: CommandElabM Unit
|
|
)
|
|
|
|
/-- error: unknown identifier 'instNonemptyFoo' -/
|
|
#guard_msgs in #check instNonemptyFoo
|
|
|
|
-- Verify that `instNonemptyFoo` is the name it would generate if it weren't hygienic
|
|
|
|
structure Foo where
|
|
field : String
|
|
instance : Nonempty Foo := ⟨⟨"hi"⟩⟩
|
|
|
|
/-- info: instNonemptyFoo : Nonempty Foo -/
|
|
#guard_msgs in #check instNonemptyFoo
|
|
|
|
/-!
|
|
Example directly from #2044, deriving ToJson
|
|
-/
|
|
|
|
open Lean Elab Command
|
|
#eval (do
|
|
let stx ← `(
|
|
structure Foo where
|
|
field : String
|
|
deriving ToJson)
|
|
elabCommand stx
|
|
: CommandElabM Unit
|
|
)
|
|
/-- error: unknown identifier 'instToJsonFoo' -/
|
|
#guard_msgs in #check instToJsonFoo
|
|
|
|
deriving instance ToJson for Foo
|
|
|
|
/-- info: instToJsonFoo : ToJson Foo -/
|
|
#guard_msgs in #check instToJsonFoo
|
|
|
|
|
|
/-!
|
|
Can derive RpcEncodable multiple times in the same namespace.
|
|
-/
|
|
|
|
structure A1 where
|
|
n : Nat
|
|
deriving Lean.Server.RpcEncodable
|
|
|
|
structure A2 where
|
|
n : Nat
|
|
deriving Lean.Server.RpcEncodable
|