progress: skips the current routine scopes

This commit is contained in:
ringabout
2025-01-24 17:10:44 +08:00
parent c70f67b0d6
commit 1b004f1632
3 changed files with 14 additions and 6 deletions

View File

@@ -2450,9 +2450,17 @@ proc semQuoteAst(c: PContext, n: PNode): PNode =
# don't allow templates to capture syms without backticks
let oldScope = c.currentScope
# c.currentScope = PScope(parent: nil, symbols: initStrTable(), depthLevel: 0)
# TODO: allows toplevel syms to be captured for backwards compatibility
# perhaps `{.dirty.}` can be used for `dummyTemplate`
c.currentScope = c.topLevelScope
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]

View File

@@ -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

View File

@@ -332,7 +332,7 @@ block:
macro main =
let x = 12
result = quote do:
`hello`(12, type(x))
`hello`(12, type(`x`))
main()