tvarres0.nim 2.5 KB

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