fixes #23790; roll back instCounter properly in case of exceptions (#23802)

fixes #23790

(cherry picked from commit 841d30a213)
This commit is contained in:
Alexander Kernozhitsky
2024-07-06 22:50:46 +02:00
committed by narimiran
parent 22896b3a95
commit ecc7e3d41d
3 changed files with 17 additions and 3 deletions

View File

@@ -862,6 +862,7 @@ proc singlePragma(c: PContext, sym: PSym, n: PNode, i: var int,
# number of pragmas increase/decrease with user pragma expansion
inc c.instCounter
defer: dec c.instCounter
if c.instCounter > 100:
globalError(c.config, it.info, "recursive dependency: " & userPragma.name.s)
@@ -871,7 +872,6 @@ proc singlePragma(c: PContext, sym: PSym, n: PNode, i: var int,
pragma(c, sym, userPragma.ast, validPragmas, isStatement)
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
dec c.instCounter
else:
let k = whichKeyword(ident)
if k in validPragmas:

View File

@@ -339,7 +339,8 @@ proc generateInstance(c: PContext, fn: PSym, pt: TIdTable,
# generates an instantiated proc
if c.instCounter > 50:
globalError(c.config, info, "generic instantiation too nested")
inc(c.instCounter)
inc c.instCounter
defer: dec c.instCounter
# careful! we copy the whole AST including the possibly nil body!
var n = copyTree(fn.ast)
# NOTE: for access of private fields within generics from a different module
@@ -416,7 +417,6 @@ proc generateInstance(c: PContext, fn: PSym, pt: TIdTable,
popOwner(c)
c.currentScope = oldScope
discard c.friendModules.pop()
dec(c.instCounter)
c.matchedConcept = oldMatchedConcept
if result.kind == skMethod: finishMethod(c, result)

14
tests/generics/t23790.nim Normal file
View File

@@ -0,0 +1,14 @@
# bug #23790
discard compiles($default(seq[seq[ref int]]))
discard compiles($default(seq[seq[ref uint]]))
discard compiles($default(seq[seq[ref int8]]))
discard compiles($default(seq[seq[ref uint8]]))
discard compiles($default(seq[seq[ref int16]]))
discard compiles($default(seq[seq[ref uint16]]))
discard compiles($default(seq[seq[ref int32]]))
discard compiles($default(seq[seq[ref uint32]]))
discard compiles($default(seq[seq[ref int64]]))
discard compiles($default(seq[seq[ref uint64]]))
proc s(_: int | string) = discard
s(0)