Skip to content

Commit

Permalink
fixes explicit globals in macros (#21502)
Browse files Browse the repository at this point in the history
(cherry picked from commit b2c1dcb)
  • Loading branch information
ringabout authored and narimiran committed Apr 26, 2023
1 parent db9442b commit 844717f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion compiler/vmgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1932,7 +1932,8 @@ proc genVarSection(c: PCtx; n: PNode) =
c.genAdditionalCopy(a[2], opcWrDeref, tmp, 0, val)
c.freeTemp(val)
c.freeTemp(tmp)
elif not importcCondVar(s) and not (s.typ.kind == tyProc and s.typ.callConv == ccClosure): # fixes #10938
elif not importcCondVar(s) and not (s.typ.kind == tyProc and s.typ.callConv == ccClosure) and
sfPure notin s.flags: # fixes #10938
# there is a pre-existing issue with closure types in VM
# if `(var s: proc () = default(proc ()); doAssert s == nil)` works for you;
# you might remove the second condition.
Expand Down
13 changes: 13 additions & 0 deletions tests/vm/tvmmisc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -643,3 +643,16 @@ const b = block:

doAssert a == @[@[0, 1, 2], @[0, 1, 2], @[0, 1, 2]]
doAssert b == @[@[0, 1, 2], @[0, 1, 2], @[0, 1, 2]]

macro m1(s: string): int =
var ProcID {.global, compileTime.}: int
inc(ProcID)
result = newLit(ProcID)

proc macroGlobal =
doAssert m1("Macro argument") == 1
doAssert m1("Macro argument") == 2
doAssert m1("Macro argument") == 3

static: macroGlobal()
macroGlobal()

0 comments on commit 844717f

Please sign in to comment.