Skip to content

Commit

Permalink
fix nim-lang#14339, nim-lang#13511, nim-lang#14420: fixes limited VM …
Browse files Browse the repository at this point in the history
…support for addr (nim-lang#16002)

* fix nim-lang#14339: fixes limited VM support for addr

* strengthen test

* reference bug nim-lang#16003

* also fixes nim-lang#13511

* also fixes nim-lang#14420
  • Loading branch information
timotheecour authored and mildred committed Jan 11, 2021
1 parent 4338f2d commit b795aba
Show file tree
Hide file tree
Showing 4 changed files with 160 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
25 changes: 25 additions & 0 deletions tests/macros/tmacros_various.nim
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,28 @@ xbenchmark:
discard inputtest
fastSHA("hey")


block: # bug #13511
type
Builder = ref object
components: seq[Component]
Component = object

proc add(builder: var Builder, component: Component) {.compileTime.} =
builder.components.add(component)

macro debugAst(arg: typed): untyped =
## just for debugging purpose.
discard arg.treeRepr
return arg

static:
var component = Component()
var builder = Builder()

template foo(): untyped =
## WAS: this doc comment causes compilation failure.
builder

debugAst:
add(foo(), component)
80 changes: 80 additions & 0 deletions tests/misc/taddr.nim
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,83 @@ block:
block: # pending bug #15959
when false:
proc byLent2[T](a: T): lent type(a[0]) = a[0]

proc test14420() = # bug #14420
# s/proc/template/ would hit bug #16005
block:
type Foo = object
x: float

proc fn(a: var Foo): var float =
## WAS: discard <- turn this into a comment (or a `discard`) and error disappears
# result = a.x # this works
a.x # WAS: Error: limited VM support for 'addr'

proc fn2(a: var Foo): var float =
result = a.x # this works
a.x # WAS: Error: limited VM support for 'addr'

var a = Foo()
discard fn(a)
discard fn2(a)

block:
proc byLent2[T](a: T): lent T =
runnableExamples: discard
a
proc byLent3[T](a: T): lent T =
runnableExamples: discard
result = a
var a = 10
let x3 = byLent3(a) # works
let x2 = byLent2(a) # WAS: Error: internal error: genAddr: nkStmtListExpr

block:
type MyOption[T] = object
case has: bool
of true:
value: T
of false:
discard
func some[T](val: T): MyOption[T] =
result = MyOption[T](has: true, value: val)
func get[T](opt: MyOption[T]): lent T =
doAssert opt.has
# result = opt.value # this was ok
opt.value # this had the bug
let x = some(10)
doAssert x.get() == 10

template test14339() = # bug #14339
block:
type
Node = ref object
val: int
proc bar(c: Node): var int =
var n = c # was: Error: limited VM support for 'addr'
c.val
var a = Node()
discard a.bar()
block:
type
Node = ref object
val: int
proc bar(c: Node): var int =
var n = c
doAssert n.val == n[].val
n.val
var a = Node(val: 3)
a.bar() = 5
when nimvm:
doAssert a.val == 5
else:
when not defined(js): # pending bug #16003
doAssert a.val == 5

template main =
# xxx wrap all other tests here like that so they're also tested in VM
test14420()
test14339()

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 b795aba

Please sign in to comment.