Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resolve _root_ in macro, syntax, elab #2239

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Lean/Elab/ElabRules.lean
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ def elabElab : CommandElab
| some name => pure name.getId
| none => addMacroScopeIfLocal (← liftMacroM <| mkNameFromParserSyntax cat.getId (mkNullNode stxParts)) attrKind
let nameId := name?.getD (mkIdentFrom tk name (canonical := true))
let pat := ⟨mkNode ((← getCurrNamespace) ++ name) patArgs⟩
let stxNodeKind := if (`_root_).isPrefixOf name then
name.replacePrefix `_root_ Name.anonymous
else
(← getCurrNamespace) ++ name
let pat := ⟨mkNode stxNodeKind patArgs⟩
elabCommand <|← `(
$[$doc?:docComment]? $[@[$attrs?,*]]? $attrKind:attrKind
syntax%$tk$[:$prec?]? (name := $nameId) (priority := $(quote prio):num) $[$stxParts]* : $cat
Expand Down
6 changes: 5 additions & 1 deletion src/Lean/Elab/Macro.lean
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ open Lean.Parser.Command
| none => addMacroScopeIfLocal (← liftMacroM <| mkNameFromParserSyntax cat.getId (mkNullNode stxParts)) attrKind
/- The command `syntax [<kind>] ...` adds the current namespace to the syntax node kind.
So, we must include current namespace when we create a pattern for the following `macro_rules` commands. -/
let pat := ⟨mkNode ((← getCurrNamespace) ++ name) patArgs⟩
let stxNodeKind := if (`_root_).isPrefixOf name then
name.replacePrefix `_root_ Name.anonymous
else
(← getCurrNamespace) ++ name
let pat := ⟨mkNode stxNodeKind patArgs⟩
let stxCmd ← `($[$doc?:docComment]? $[@[$attrs?,*]]? $attrKind:attrKind
syntax$[:$prec?]?
(name := $(name?.getD (mkIdentFrom tk name (canonical := true))))
Expand Down
15 changes: 11 additions & 4 deletions src/Lean/Elab/Syntax.lean
Original file line number Diff line number Diff line change
Expand Up @@ -381,10 +381,12 @@ def addMacroScopeIfLocal [MonadQuotation m] [Monad m] (name : Name) (attrKind :
let name ← match name? with
| some name => pure name.getId
| none => addMacroScopeIfLocal (← liftMacroM <| mkNameFromParserSyntax cat syntaxParser) attrKind
trace[Meta.debug] "name: {name}"
let prio ← liftMacroM <| evalOptPrio prio?
let idRef := (name?.map (·.raw)).getD tk
let stxNodeKind := (← getCurrNamespace) ++ name
let stxNodeKind := if (`_root_).isPrefixOf name then
name.replacePrefix `_root_ Name.anonymous
else
(← getCurrNamespace) ++ name
let catParserId := mkIdentFrom idRef (cat.appendAfter "_parser")
let (val, lhsPrec?) ← runTermElabM fun _ => Term.toParserDescr syntaxParser cat
let declName := name?.getD (mkIdentFrom idRef name (canonical := true))
Expand All @@ -404,8 +406,13 @@ def addMacroScopeIfLocal [MonadQuotation m] [Monad m] (name : Name) (attrKind :
let `($[$doc?:docComment]? syntax $declName:ident := $[$ps:stx]*) ← pure stx | throwUnsupportedSyntax
-- TODO: nonatomic names
let (val, _) ← runTermElabM fun _ => Term.toParserDescr (mkNullNode ps) Name.anonymous
let stxNodeKind := (← getCurrNamespace) ++ declName.getId
let stx' ← `($[$doc?:docComment]? def $declName:ident : Lean.ParserDescr := ParserDescr.nodeWithAntiquot $(quote (toString declName.getId)) $(quote stxNodeKind) $val)
let name := declName.getId
let stxNodeKind := if (`_root_).isPrefixOf name then
name.replacePrefix `_root_ Name.anonymous
else
(← getCurrNamespace) ++ name
let stx' ← `($[$doc?:docComment]? def $declName:ident : Lean.ParserDescr :=
ParserDescr.nodeWithAntiquot $(quote (toString declName.getId)) $(quote stxNodeKind) $val)
withMacroExpansion stx stx' <| elabCommand stx'

def checkRuleKind (given expected : SyntaxNodeKind) : Bool :=
Expand Down
16 changes: 8 additions & 8 deletions stage0/src/include/lean/lean.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions stage0/src/lean.mk.in

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 38 additions & 8 deletions stage0/src/library/compiler/llvm.cpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion stage0/src/runtime/io.cpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion stage0/src/runtime/object.cpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion stage0/src/runtime/platform.cpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions stage0/src/shell/CMakeLists.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion stage0/src/stdlib.make.in

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion stage0/src/stdlib_flags.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions stage0/stdlib/Init/Meta.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading