Skip to content

Commit

Permalink
add exhaustive tests for var result
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Apr 13, 2020
1 parent 5faadf4 commit fb00b93
Showing 1 changed file with 86 additions and 13 deletions.
99 changes: 86 additions & 13 deletions tests/varres/tvarres0.nim
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,100 @@ echo getF().a

block: # #13848
template fun() =
var m = 1
var m2 = 10
block:
var m = 1

proc identity(o: var int): var int =
result = o
result += 5
proc identity(o: var int): var int =
result = o
result += 5

proc identity2(o: var int): var int =
result = m2
result += 100
identity(m) += 3
doAssert m == 5+4

identity(m) += 3
doAssert m == 5+4
block:
var m = 10
proc identity2(o: var int): var int =
result = m
result += 100

var ignored = 27
identity2(ignored) += 7
doAssert m2 == 10 + 100 + 7
var ignored = 27
identity2(ignored) += 7
doAssert m == 10 + 100 + 7

block:
iterator test3(o: var int): var int = yield o
var m = 1
for m2 in test3(m): m2+=3
doAssert m == 4

static: fun()
fun()

template fun2() =
block:
var m = 1
var m2 = 1
iterator test3(o: var int): (var int, var int) =
yield (o, m2)

for ti in test3(m):
ti[0]+=3
ti[1]+=4

doAssert (m, m2) == (4, 5)
fun2()
# static: fun2() # BUG: Error: attempt to access a nil address kind: rkInt

template fun3() =
block:
proc test4[T1](o: var T1): var int = o[1]
block:
var m = @[1,2]
test4(m) += 10
doAssert m[1] == 2+10
block:
var m = [1,2]
test4(m) += 10
doAssert m[1] == 2+10
block:
var m = (1, 2)
test4(m) += 10
doAssert m[1] == 2+10

proc test5[T1](o: var T1): var int = o.x
block:
type Foo = object
x: int
var m = Foo(x: 2)
test5(m) += 10
doAssert m.x == 2+10
block:
type Foo = ref object
x: int
var m = Foo(x: 2)
test5(m) += 10
doAssert m.x == 2+10

proc test6[T1](o: T1): var int = o.x
block:
type Foo = ref object
x: int
var m = Foo(x: 2)
test6(m) += 10
doAssert m.x == 2+10

fun3()
static: fun3()

when false:
# BUG:
# c: SIGSEGV
# cpp: error: call to implicitly-deleted default constructor of 'tyTuple__ILZebuYefUeQLAzY85QkHA'
proc test7[T](o: var T): (var int,) =
(o[1], )
var m = @[1,2]
test7(m)[0] += 10

block:
# example from #13848
type
Expand Down

0 comments on commit fb00b93

Please sign in to comment.