Skip to content

try fix nested dirty template #23890

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

Draft
wants to merge 4 commits into
base: devel
Choose a base branch
from
Draft
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
10 changes: 6 additions & 4 deletions compiler/semgnrc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ proc semGenericStmtSymbol(c: PContext, n: PNode, s: PSym,
onUse(n.info, s)

proc lookup(c: PContext, n: PNode, flags: TSemGenericFlags,
ctx: var GenericCtx): PNode =
ctx: var GenericCtx, mustExist=true): PNode =
result = n
let ident = considerQuotedIdent(c, n)
var amb = false
Expand All @@ -137,8 +137,9 @@ proc lookup(c: PContext, n: PNode, flags: TSemGenericFlags,
#if s != nil and contains(c.ambiguousSymbols, s.id):
# s = nil
if s == nil:
if ident.id notin ctx.toMixin and withinMixin notin flags:
errorUndeclaredIdentifier(c, n.info, ident.s)
if mustExist:
if ident.id notin ctx.toMixin and withinMixin notin flags:
errorUndeclaredIdentifier(c, n.info, ident.s)
else:
if withinBind in flags or s.id in ctx.toBind:
result = symChoice(c, n, s, scClosed)
Expand Down Expand Up @@ -228,7 +229,8 @@ proc semGenericStmt(c: PContext, n: PNode,

case n.kind
of nkIdent, nkAccQuoted:
result = lookup(c, n, flags, ctx)
# If this generic is in a template it's too early to bind for sure
result = lookup(c, n, flags, ctx, mustExist=c.config.evalTemplateCounter <= 0)
if result != nil and result.kind == nkSym:
assert result.sym != nil
markUsed(c, n.info, result.sym)
Expand Down
10 changes: 10 additions & 0 deletions tests/template/t23889/t23889.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
template inner(i: int) {.dirty.} =
let thing = 1

template outer() =
proc p[T](x: T): int =
inner(5)
return thing

outer()
assert p(0) == 1
15 changes: 15 additions & 0 deletions tests/template/t23889/tfail1.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
discard """
errormsg: "redefinition of 'thing'"
"""

template inner(i: int) {.dirty.} =
let thing = 1

template outer() =
proc p[T](x: T) =
inner(5)
var thing = 5
echo thing

outer()
p(0)
17 changes: 17 additions & 0 deletions tests/template/t23889/tfail2.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
discard """
errormsg: "undeclared identifier: 'thing'"
"""

template inner(i: int) {.dirty.} =
let thing = 1

template outer() =
proc p[T](x: T) =
echo thing

template outerouter() =
outer()
inner(5)

outerouter()
p(0)
10 changes: 10 additions & 0 deletions tests/template/t23889/tsanity.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
discard """
errormsg: "undeclared identifier: 'something'"
"""

proc p[T](x: T) =
echo something

var something = 5

p(5)