From e7f48cdd5c17cbd0c1c74c2c1f360ae2ea122b64 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 --- compiler/sempass2.nim | 3 +++ lib/pure/volatile.nim | 1 + testament/important_packages.nim | 2 +- tests/cpp/tasync_cpp.nim | 2 +- tests/init/tlet.nim | 18 ++++++++++++++++++ 5 files changed, 24 insertions(+), 2 deletions(-) diff --git a/compiler/sempass2.nim b/compiler/sempass2.nim index 29ee67c3cb..f1e31b7f7e 100644 --- a/compiler/sempass2.nim +++ b/compiler/sempass2.nim @@ -212,6 +212,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 @@ -1302,7 +1303,9 @@ proc track(tracked: PEffects, n: PNode) = track(tracked, last) of nkCaseStmt: trackCase(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]) diff --git a/lib/pure/volatile.nim b/lib/pure/volatile.nim index a38247c7db..0b79b61015 100644 --- a/lib/pure/volatile.nim +++ b/lib/pure/volatile.nim @@ -19,6 +19,7 @@ proc volatileLoad*[T](src: ptr T): T {.inline, noinit.} = when defined(js): result = src[] else: + result = default(T) {.emit: [result, " = (*(", typeof(src[]), " volatile*)", src, ");"].} proc volatileStore*[T](dest: ptr T, val: T) {.inline.} = diff --git a/testament/important_packages.nim b/testament/important_packages.nim index 833d285bdf..3f2d4a7e3c 100644 --- a/testament/important_packages.nim +++ b/testament/important_packages.nim @@ -138,7 +138,7 @@ pkg "plotly", "nim c examples/all.nim" pkg "pnm" pkg "polypbren" pkg "presto" -# pkg "prologue", "nimble tcompile" +pkg "prologue", "nimble tcompile" pkg "protobuf", "nim c -o:protobuff -r src/protobuf.nim" pkg "rbtree" pkg "react", "nimble example" diff --git a/tests/cpp/tasync_cpp.nim b/tests/cpp/tasync_cpp.nim index 953f6a2b48..3f4ec62084 100644 --- a/tests/cpp/tasync_cpp.nim +++ b/tests/cpp/tasync_cpp.nim @@ -1,7 +1,7 @@ discard """ targets: "cpp" output: "hello" - cmd: "nim cpp --legacy:nostrictdefs --clearNimblePath --nimblePath:build/deps/pkgs2 $file" + cmd: "nim cpp --clearNimblePath --nimblePath:build/deps/pkgs2 $file" """ # bug #3299 diff --git a/tests/init/tlet.nim b/tests/init/tlet.nim index a3041baf87..b566de9f62 100644 --- a/tests/init/tlet.nim +++ b/tests/init/tlet.nim @@ -83,3 +83,21 @@ proc foo2 = doAssert z == 3 foo2() + +# 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()