From 98ec87d65e678ccf3aee9f59c729607089e7cece Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Sun, 4 May 2025 09:29:59 +0800 Subject: [PATCH] fixes #23355; pop optionStack when exiting scopes (#24926) fixes #23355 --- compiler/ast.nim | 1 + compiler/lookups.nim | 5 ++++- tests/errmsgs/t23355.nim | 11 +++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 tests/errmsgs/t23355.nim diff --git a/compiler/ast.nim b/compiler/ast.nim index e35a0b2031..3d7dcbdfc7 100644 --- a/compiler/ast.nim +++ b/compiler/ast.nim @@ -684,6 +684,7 @@ type symbols*: TStrTable parent*: PScope allowPrivateAccess*: seq[PSym] # # enable access to private fields + optionStackLen*: int PScope* = ref TScope diff --git a/compiler/lookups.nim b/compiler/lookups.nim index ec5fdd69b0..34f65973cf 100644 --- a/compiler/lookups.nim +++ b/compiler/lookups.nim @@ -75,10 +75,13 @@ proc addUniqueSym*(scope: PScope, s: PSym): PSym = proc openScope*(c: PContext): PScope {.discardable.} = result = PScope(parent: c.currentScope, symbols: initStrTable(), - depthLevel: c.scopeDepth + 1) + depthLevel: c.scopeDepth + 1, + optionStackLen: c.optionStack.len) c.currentScope = result proc rawCloseScope*(c: PContext) = + if c.currentScope.optionStackLen >= 1: + c.optionStack.setLen(c.currentScope.optionStackLen) c.currentScope = c.currentScope.parent proc closeScope*(c: PContext) = diff --git a/tests/errmsgs/t23355.nim b/tests/errmsgs/t23355.nim new file mode 100644 index 0000000000..281d098eb8 --- /dev/null +++ b/tests/errmsgs/t23355.nim @@ -0,0 +1,11 @@ +discard """ + errormsg: "{.pop.} without a corresponding {.push.}" +""" + +block: + {.push raises: [].} + +proc f() = + {.pop.} + +proc g() = raise newException(ValueError, "") \ No newline at end of file