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

fixes #24641; quote do captures no variables without backticks #24642

Open
wants to merge 8 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
15 changes: 15 additions & 0 deletions compiler/semexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2447,7 +2447,22 @@ proc semQuoteAst(c: PContext, n: PNode): PNode =
dummyTemplate[paramsPos].add newTreeI(nkIdentDefs, n.info, ids[i], newNodeIT(nkType, n.info, typ), c.graph.emptyNode)
else:
dummyTemplate[paramsPos].add newTreeI(nkIdentDefs, n.info, ids[i], getSysSym(c.graph, n.info, "typed").newSymNode, c.graph.emptyNode)
# don't allow templates to capture syms without backticks
let oldScope = c.currentScope
# c.currentScope = PScope(parent: nil, symbols: initStrTable(), depthLevel: 0)
if c.p.owner.kind in routineKinds:
# skips the current routine scopes
block exitLabel:
while c.currentScope != nil:
c.currentScope = c.currentScope.parent
block continueLabel:
for s in items(c.currentScope.symbols):
if s.owner != c.p.owner:
break exitLabel
else:
break continueLabel
var tmpl = semTemplateDef(c, dummyTemplate)
c.currentScope = oldScope
quotes[0] = tmpl[namePos]
# This adds a call to newIdentNode("result") as the first argument to the template call
let identNodeSym = getCompilerProc(c.graph, "newIdentNode")
Expand Down
4 changes: 2 additions & 2 deletions tests/lexer/tcustom_numeric_literals.nim
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@ template main =
doAssert fn3() == "[[-12]]"

block: # bug 9 from https://github.com/nim-lang/Nim/pull/17020#issuecomment-803193947
func wrap1(a: string): string = "{" & a & "}"
func `'wrap3`(a: string): string = "{" & a & "}"
macro metawrap(): untyped =
func wrap1(a: string): string = "{" & a & "}"
func `'wrap3`(a: string): string = "{" & a & "}"
result = quote do:
let a1 {.inject.} = wrap1"-128"
let a2 {.inject.} = -128'wrap3
Expand Down
2 changes: 1 addition & 1 deletion tests/stdlib/tmacros.nim
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ block:
macro main =
let x = 12
result = quote do:
`hello`(12, type(x))
`hello`(12, type(`x`))

main()

Expand Down
Loading