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 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
32 changes: 19 additions & 13 deletions compiler/semexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2397,9 +2397,6 @@ proc processQuotations(c: PContext; n: var PNode, op: string,
tempNode[0] = n[0]
tempNode[1] = n[1]
handlePrefixOp(tempNode)
elif n.kind == nkIdent:
if n.ident.s == "result":
n = ids[0]

for i in 0..<n.safeLen:
processQuotations(c, n[i], op, quotes, ids)
Expand All @@ -2413,18 +2410,15 @@ proc semQuoteAst(c: PContext, n: PNode): PNode =
var
quotedBlock = n[^1]
op = if n.len == 3: expectString(c, n[1]) else: "``"
quotes = newSeq[PNode](2)
quotes = newSeq[PNode](1)
# the quotes will be added to a nkCall statement
# leave some room for the callee symbol and the result symbol
ids = newSeq[PNode](1)
# leave some room for the callee symbol
ids = newSeq[PNode]()
# this will store the generated param names
# leave some room for the result symbol

if quotedBlock.kind != nkStmtList:
localError(c.config, n.info, errXExpected, "block")

# This adds a default first field to pass the result symbol
ids[0] = newAnonSym(c, skParam, n.info).newSymNode
processQuotations(c, quotedBlock, op, quotes, ids)

let dummyTemplateSym = newAnonSym(c, skTemplate, n.info)
Expand All @@ -2439,25 +2433,37 @@ proc semQuoteAst(c: PContext, n: PNode): PNode =
if ids.len > 0:
dummyTemplate[paramsPos] = newNodeI(nkFormalParams, n.info)
dummyTemplate[paramsPos].add getSysSym(c.graph, n.info, "untyped").newSymNode # return type
dummyTemplate[paramsPos].add newTreeI(nkIdentDefs, n.info, ids[0], getSysSym(c.graph, n.info, "typed").newSymNode, c.graph.emptyNode)
for i in 1..<ids.len:
for i in 0..<ids.len:
let exp = semExprWithType(c, quotes[i+1], {})
let typ = exp.typ
if tfTriggersCompileTime notin typ.flags and typ.kind != tyStatic and exp.kind == nkSym and exp.sym.kind notin routineKinds + {skType}:
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 != nil and c.p.owner.kind in routineKinds:
# skips the current routine scopes
block exitLabel:
while c.currentScope != c.topLevelScope:
block continueLabel:
for s in items(c.currentScope.symbols):
if s.owner != c.p.owner:
break exitLabel
else:
break continueLabel
c.currentScope = c.currentScope.parent
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")
# so that new Nim compilers can compile old macros.nim versions, we check for 'nil'
# here and provide the old fallback solution:
let identNode = if identNodeSym == nil:
newIdentNode(getIdent(c.cache, "newIdentNode"), n.info)
else:
identNodeSym.newSymNode
quotes[1] = newTreeI(nkCall, n.info, identNode, newStrNode(nkStrLit, "result"))
result = newTreeI(nkCall, n.info,
createMagic(c.graph, c.idgen, "getAst", mExpandToAst).newSymNode,
newTreeI(nkCall, n.info, quotes))
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