Skip to content

Commit

Permalink
fixes #10938; fixes #13312; fixes #13918; fixes #20985; always initia…
Browse files Browse the repository at this point in the history
…lizes global variables with null values in VM (#21351)

* fixes #10938; always initialize global variable in VM

* fixes importc vars

* there is a pre-existing issue regarding closure types in the VM

* add tests
  • Loading branch information
ringabout authored Mar 1, 2023
1 parent c4dd0c4 commit 1b1412f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
12 changes: 12 additions & 0 deletions compiler/vmgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1909,6 +1909,18 @@ 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
# 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.
# the problem is that closure types are tuples in VM, but the types of its children
# shouldn't have the same type as closure types.
let tmp = c.genx(a[0], {gfNodeAddr})
let sa = getNullValue(s.typ, a.info, c.config)
let val = c.genx(sa)
c.genAdditionalCopy(sa, opcWrDeref, tmp, 0, val)
c.freeTemp(val)
c.freeTemp(tmp)
else:
setSlot(c, s)
if a[2].kind == nkEmpty:
Expand Down
46 changes: 44 additions & 2 deletions tests/vm/tvmmisc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -564,14 +564,14 @@ block:
block:
static:
let x = int 1
echo x.type # Foo
doAssert $(x.type) == "Foo" # Foo

block:
static:
let x = int 1
let y = x + 1
# Error: unhandled exception: value out of range: -8 notin 0 .. 65535 [RangeDefect]
echo y
doAssert y == 2


type Atom* = object
Expand Down Expand Up @@ -601,3 +601,45 @@ proc foo(s: sink string) = doAssert s.len == 3

static:
foo("abc")


static:
for i in '1' .. '2': # bug #10938
var s: set[char]
doAssert s == {}
incl(s, i)

for _ in 0 ..< 3: # bug #13312
var s: string
s.add("foo")
doAssert s == "foo"

for i in 1 .. 5: # bug #13918
var arr: array[3, int]
var val: int
doAssert arr == [0, 0, 0] and val == 0
for j in 0 ..< len(arr):
arr[j] = i
val = i

# bug #20985
let a = block:
var groups: seq[seq[int]]
for i in 0 ..< 3:
var group: seq[int]
for j in 0 ..< 3:
group.add j
groups.add group
groups

const b = block:
var groups: seq[seq[int]]
for i in 0 ..< 3:
var group: seq[int]
for j in 0 ..< 3:
group.add j
groups.add group
groups

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

0 comments on commit 1b1412f

Please sign in to comment.