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

process non-language pragma nodes in templates #24183

Open
wants to merge 6 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
15 changes: 11 additions & 4 deletions compiler/semtempl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -535,13 +535,20 @@ proc semTemplBody(c: var TemplCtx, n: PNode): PNode =
of nkConverterDef:
result = semRoutineInTemplBody(c, n, skConverter)
of nkPragmaExpr:
result[0] = semTemplBody(c, n[0])
result = semTemplBodySons(c, n)
of nkPostfix:
result[1] = semTemplBody(c, n[1])
of nkPragma:
for x in n:
if x.kind == nkExprColonExpr:
x[1] = semTemplBody(c, x[1])
for i in 0 ..< n.len:
let x = n[i]
let prag = whichPragma(x)
if prag == wInvalid:
# only sem if not a language-level pragma
result[i] = semTemplBody(c, x)
elif x.kind in nkPragmaCallKinds:
# is pragma, but value still needs to be checked
for j in 1 ..< x.len:
x[j] = semTemplBody(c, x[j])
of nkBracketExpr:
if n.typ == nil:
# if a[b] is nested inside a typed expression, don't convert it
Expand Down
7 changes: 7 additions & 0 deletions compiler/trees.nim
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,13 @@ proc whichPragma*(n: PNode): TSpecialWord =
of nkCast: return wCast
of nkClosedSymChoice, nkOpenSymChoice:
return whichPragma(key[0])
of nkBracketExpr:
if n.kind notin nkPragmaCallKinds: return wInvalid
result = whichPragma(key[0])
if result notin {wHint, wHintAsError, wWarning, wWarningAsError}:
# note bracket pragmas, see processNote
result = wInvalid
return
else: return wInvalid
if result in nonPragmaWordsLow..nonPragmaWordsHigh:
result = wInvalid
Expand Down
28 changes: 28 additions & 0 deletions tests/template/tpragma.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# issue #24186

macro mymacro(typ: typedesc; def) =
def

macro mymacro2(typ: typedesc; typ2: typedesc; def) =
def

template mytemplate(typ: typedesc) = # works
proc myproc() {.mymacro: typ .} =
discard

template mytemplate2(typ: typedesc) = # Error: undeclared identifier: 'typ'
proc myproc2() {.mymacro(typ) .} =
discard

template mytemplate3(typ: typedesc, typ2: typedesc) = # Error: undeclared identifier: 'typ'
proc myproc3() {.mymacro2(typ, typ2) .} =
discard

template mytemplate4() = # works
proc myproc4() {.mymacro2(string, int) .} =
discard

mytemplate(string)
mytemplate2(string)
mytemplate3(string, int)
mytemplate4()
Loading