tvarres0.nim 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. discard """
  2. output: '''123
  3. 1234
  4. 123
  5. 1234
  6. 12345
  7. 123456
  8. '''
  9. """
  10. # Test simple type
  11. var a = 123
  12. proc getA(): var int = a
  13. echo getA()
  14. getA() = 1234
  15. echo getA()
  16. # Test object type
  17. type Foo = object
  18. a: int
  19. var f: Foo
  20. f.a = 123
  21. proc getF(): var Foo = f
  22. echo getF().a
  23. getF().a = 1234
  24. echo getF().a
  25. getF() = Foo(a: 12345)
  26. echo getF().a
  27. (addr getF())[] = Foo(a: 123456)
  28. echo getF().a
  29. block: # #13848
  30. template fun() =
  31. block:
  32. var m = 1
  33. proc identity(o: var int): var int =
  34. result = o
  35. result += 5
  36. identity(m) += 3
  37. doAssert m == 5+4
  38. block:
  39. var m = 10
  40. proc identity2(o: var int): var int =
  41. result = m
  42. result += 100
  43. var ignored = 27
  44. identity2(ignored) += 7
  45. doAssert m == 10 + 100 + 7
  46. block:
  47. iterator test3(o: var int): var int = yield o
  48. var m = 1
  49. for m2 in test3(m): m2+=3
  50. doAssert m == 4
  51. static: fun()
  52. fun()
  53. template fun2() =
  54. block:
  55. var m = 1
  56. var m2 = 1
  57. iterator test3(o: var int): (var int, var int) =
  58. yield (o, m2)
  59. for ti in test3(m):
  60. ti[0]+=3
  61. ti[1]+=4
  62. doAssert (m, m2) == (4, 5)
  63. fun2()
  64. # static: fun2() # BUG: Error: attempt to access a nil address kind: rkInt
  65. template fun3() =
  66. block:
  67. proc test4[T1](o: var T1): var int = o[1]
  68. block:
  69. var m = @[1,2]
  70. test4(m) += 10
  71. doAssert m[1] == 2+10
  72. block:
  73. var m = [1,2]
  74. test4(m) += 10
  75. doAssert m[1] == 2+10
  76. block:
  77. var m = (1, 2)
  78. test4(m) += 10
  79. doAssert m[1] == 2+10
  80. proc test5[T1](o: var T1): var int = o.x
  81. block:
  82. type Foo = object
  83. x: int
  84. var m = Foo(x: 2)
  85. test5(m) += 10
  86. doAssert m.x == 2+10
  87. block:
  88. type Foo = ref object
  89. x: int
  90. var m = Foo(x: 2)
  91. test5(m) += 10
  92. doAssert m.x == 2+10
  93. proc test6[T1](o: T1): var int = o.x
  94. block:
  95. type Foo = ref object
  96. x: int
  97. var m = Foo(x: 2)
  98. test6(m) += 10
  99. doAssert m.x == 2+10
  100. fun3()
  101. static: fun3()
  102. when false:
  103. # BUG:
  104. # c: SIGSEGV
  105. # cpp: error: call to implicitly-deleted default constructor of 'tyTuple__ILZebuYefUeQLAzY85QkHA'
  106. proc test7[T](o: var T): (var int,) =
  107. (o[1], )
  108. var m = @[1,2]
  109. test7(m)[0] += 10
  110. block:
  111. # example from #13848
  112. type
  113. MyType[T] = object
  114. a,b: T
  115. MyTypeAlias = MyType[float32]
  116. var m: MyTypeAlias
  117. proc identity(o: var MyTypeAlias): var MyTypeAlias = o
  118. discard identity(m)