From 23ca21a9c48a33f8e57548083ac4e99c2dc6cf5c Mon Sep 17 00:00:00 2001 From: metagn Date: Thu, 3 Apr 2025 13:53:42 +0300 Subject: [PATCH] fix infinite recursion with pushed user pragmas (#24839) fixes #24838 (cherry picked from commit 5bcd9a329ae78f5b0d1b068bd2478f56f1e2b8c1) --- compiler/pragmas.nim | 9 +++++---- tests/pragmas/tpushuserpragma.nim | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 tests/pragmas/tpushuserpragma.nim diff --git a/compiler/pragmas.nim b/compiler/pragmas.nim index a6c1917792..51e044ce0b 100644 --- a/compiler/pragmas.nim +++ b/compiler/pragmas.nim @@ -107,7 +107,7 @@ proc getPragmaVal*(procAst: PNode; name: TSpecialWord): PNode = return it[1] proc pragma*(c: PContext, sym: PSym, n: PNode, validPragmas: TSpecialWords; - isStatement: bool = false) + isStatement: bool = false; comesFromPush = false) proc recordPragma(c: PContext; n: PNode; args: varargs[string]) = var recorded = newNodeI(nkReplayAction, n.info) @@ -893,7 +893,7 @@ proc singlePragma(c: PContext, sym: PSym, n: PNode, i: var int, if keyDeep: localError(c.config, it.info, "user pragma cannot have arguments") - pragma(c, sym, userPragma.ast, validPragmas, isStatement) + pragma(c, sym, userPragma.ast, validPragmas, isStatement, comesFromPush) n.sons[i..i] = userPragma.ast.sons # expand user pragma with its content i.inc(userPragma.ast.len - 1) # inc by -1 is ok, user pragmas was empty else: @@ -1405,11 +1405,12 @@ proc pragmaRec(c: PContext, sym: PSym, n: PNode, validPragmas: TSpecialWords; inc i proc pragma(c: PContext, sym: PSym, n: PNode, validPragmas: TSpecialWords; - isStatement: bool) = + isStatement: bool; comesFromPush = false) = if n == nil: return pragmaRec(c, sym, n, validPragmas, isStatement) # XXX: in the case of a callable def, this should use its info - implicitPragmas(c, sym, n.info, validPragmas) + if not comesFromPush: + implicitPragmas(c, sym, n.info, validPragmas) proc pragmaCallable*(c: PContext, sym: PSym, n: PNode, validPragmas: TSpecialWords, isStatement: bool = false) = diff --git a/tests/pragmas/tpushuserpragma.nim b/tests/pragmas/tpushuserpragma.nim new file mode 100644 index 0000000000..8a7ca33e86 --- /dev/null +++ b/tests/pragmas/tpushuserpragma.nim @@ -0,0 +1,15 @@ +# issue #24838 + +{.pragma: testit, raises: [], deprecated: "abc".} + +{.push testit.} +proc xxx() {.testit.} = + discard "hello" +proc yyy() = + discard "hello" +{.pop.} + +xxx() #[tt.Warning +^ abc; xxx is deprecated [Deprecated]]# +yyy() #[tt.Warning +^ abc; yyy is deprecated [Deprecated]]#