From 14dfabb230b63541cba6d16af9a5c8876d6ab7b9 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Tue, 26 Nov 2024 19:35:48 +0800 Subject: [PATCH] fixes #24472; let symbol created by template is reused in nimvm branch (#24473) fixes #24472 Excluding variables which are initialized in the nimvm branch so that they won't interfere the other branch (cherry picked from commit e7f48cdd5c17cbd0c1c74c2c1f360ae2ea122b64) --- compiler/sempass2.nim | 8 +++++++- lib/pure/volatile.nim | 5 ++--- tests/init/tlet.nim | 18 ++++++++++++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/compiler/sempass2.nim b/compiler/sempass2.nim index e8a3dc8475..94aa22ae40 100644 --- a/compiler/sempass2.nim +++ b/compiler/sempass2.nim @@ -177,6 +177,7 @@ proc varDecl(a: PEffects; n: PNode) {.inline.} = proc skipHiddenDeref(n: PNode): PNode {.inline.} = result = if n.kind == nkHiddenDeref: n[0] else: n + proc initVar(a: PEffects, n: PNode; volatileCheck: bool) = let n = skipHiddenDeref(n) if n.kind != nkSym: return @@ -1144,7 +1145,12 @@ proc track(tracked: PEffects, n: PNode) = let last = lastSon(child) track(tracked, last) of nkCaseStmt: trackCase(tracked, n) - of nkWhen, nkIfStmt, nkIfExpr: trackIf(tracked, n) + of nkWhen: # This should be a "when nimvm" node. + let oldState = tracked.init.len + track(tracked, n[0][1]) + tracked.init.setLen(oldState) + track(tracked, n[1][0]) + of nkIfStmt, nkIfExpr: trackIf(tracked, n) of nkBlockStmt, nkBlockExpr: trackBlock(tracked, n[1]) of nkWhileStmt: # 'while true' loop? diff --git a/lib/pure/volatile.nim b/lib/pure/volatile.nim index f30d899df0..8c55f17d1a 100644 --- a/lib/pure/volatile.nim +++ b/lib/pure/volatile.nim @@ -19,9 +19,8 @@ template volatileLoad*[T](src: ptr T): T = when defined(js): src[] else: - var res: T - {.emit: [res, " = (*(", typeof(src[]), " volatile*)", src, ");"].} - res + result = default(T) + {.emit: [result, " = (*(", typeof(src[]), " volatile*)", src, ");"].} template volatileStore*[T](dest: ptr T, val: T) = ## Generates a volatile store into the container `dest` of the value diff --git a/tests/init/tlet.nim b/tests/init/tlet.nim index e32bedb184..d4afd5c0c3 100644 --- a/tests/init/tlet.nim +++ b/tests/init/tlet.nim @@ -53,3 +53,21 @@ proc foo() = static: foo() foo() + +# bug #24472 +template bar1314(): bool = + let hello = true + hello + +template foo1314*(val: bool): bool = + when nimvm: + val + else: + val + +proc test() = # Doesn't fail when top level + # Original code is calling `unlikely` which has a `nimvm` branch + let s = foo1314(bar1314()) + doAssert s + +test()