Skip to content

Commit

Permalink
fix nim-lang#14339: fixes limited VM support for addr
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Nov 25, 2020
1 parent e220f75 commit d960390
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 44 deletions.
7 changes: 5 additions & 2 deletions compiler/vm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -746,10 +746,13 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
regs[ra].regAddr = addr(regs[rb])
of opcAddrNode:
decodeB(rkNodeAddr)
if regs[rb].kind == rkNode:
case regs[rb].kind
of rkNode:
regs[ra].nodeAddr = addr(regs[rb].node)
of rkNodeAddr: # bug #14339
regs[ra].nodeAddr = regs[rb].nodeAddr
else:
stackTrace(c, tos, pc, "limited VM support for 'addr'")
stackTrace(c, tos, pc, "limited VM support for 'addr', got kind: " & $regs[rb].kind)
of opcLdDeref:
# a = b[]
let ra = instr.regA
Expand Down
13 changes: 13 additions & 0 deletions tests/misc/taddr.nim
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,16 @@ block:
block: # pending bug #15959
when false:
proc byLent2[T](a: T): lent type(a[0]) = a[0]

block: # bug #14339
type
Node = ref object
val: int
proc bar(c: Node): var int =
var n = c # was: Error: limited VM support for 'addr'
c.val
proc main =
var a = Node()
discard a.bar()
static: main()
main()
92 changes: 50 additions & 42 deletions tests/stdlib/tcritbits.nim
Original file line number Diff line number Diff line change
@@ -1,61 +1,69 @@
import sequtils, critbits
import std/[sequtils,critbits]

template main =
var r: CritBitTree[void]
r.incl "abc"
r.incl "xyz"
r.incl "def"
r.incl "definition"
r.incl "prefix"
r.incl "foo"

var r: CritBitTree[void]
r.incl "abc"
r.incl "xyz"
r.incl "def"
r.incl "definition"
r.incl "prefix"
r.incl "foo"
doAssert r.contains"def"

doAssert r.contains"def"
r.excl "def"
assert r.missingOrExcl("foo") == false
assert "foo" notin toSeq(r.items)

r.excl "def"
assert r.missingOrExcl("foo") == false
assert "foo" notin toSeq(r.items)
assert r.missingOrExcl("foo") == true

assert r.missingOrExcl("foo") == true
assert toSeq(r.items) == @["abc", "definition", "prefix", "xyz"]

assert toSeq(r.items) == @["abc", "definition", "prefix", "xyz"]
assert toSeq(r.itemsWithPrefix("de")) == @["definition"]
var c = CritBitTree[int]()

assert toSeq(r.itemsWithPrefix("de")) == @["definition"]
var c = CritBitTree[int]()
c.inc("a")
assert c["a"] == 1

c.inc("a")
assert c["a"] == 1
c.inc("a", 4)
assert c["a"] == 5

c.inc("a", 4)
assert c["a"] == 5
c.inc("a", -5)
assert c["a"] == 0

c.inc("a", -5)
assert c["a"] == 0
c.inc("b", 2)
assert c["b"] == 2

c.inc("b", 2)
assert c["b"] == 2
c.inc("c", 3)
assert c["c"] == 3

c.inc("c", 3)
assert c["c"] == 3
c.inc("a", 1)
assert c["a"] == 1

c.inc("a", 1)
assert c["a"] == 1
var cf = CritBitTree[float]()

var cf = CritBitTree[float]()
cf.incl("a", 1.0)
assert cf["a"] == 1.0

cf.incl("a", 1.0)
assert cf["a"] == 1.0
cf.incl("b", 2.0)
assert cf["b"] == 2.0

cf.incl("b", 2.0)
assert cf["b"] == 2.0
cf.incl("c", 3.0)
assert cf["c"] == 3.0

cf.incl("c", 3.0)
assert cf["c"] == 3.0
assert cf.len == 3
cf.excl("c")
assert cf.len == 2

assert cf.len == 3
cf.excl("c")
assert cf.len == 2
var cb: CritBitTree[string]
cb.incl("help", "help")
for k in cb.keysWithPrefix("helpp"):
doAssert false, "there is no prefix helpp"

var cb: CritBitTree[string]
cb.incl("help", "help")
for k in cb.keysWithPrefix("helpp"):
doAssert false, "there is no prefix helpp"
block: # bug #14339
var strings: CritBitTree[int]
discard strings.containsOrIncl("foo", 3)
doAssert strings["foo"] == 3

main()
static: main()

0 comments on commit d960390

Please sign in to comment.