taddr.nim 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. discard """
  2. targets: "c cpp js"
  3. matrix: "; -d:release"
  4. """
  5. type T = object
  6. x: int
  7. s: string
  8. var obj: T
  9. var fieldAddr = addr(obj.x)
  10. var objAddr = addr(obj)
  11. # Integer tests
  12. var field = fieldAddr[]
  13. doAssert field == 0
  14. var objDeref = objAddr[]
  15. doAssert objDeref.x == 0
  16. # Change value
  17. obj.x = 42
  18. doAssert field == 0
  19. doAssert objDeref.x == 0
  20. field = fieldAddr[]
  21. objDeref = objAddr[]
  22. doAssert field == 42
  23. doAssert objDeref.x == 42
  24. # String tests
  25. obj.s = "lorem ipsum dolor sit amet"
  26. var indexAddr = addr(obj.s[2])
  27. doAssert indexAddr[] == 'r'
  28. indexAddr[] = 'd'
  29. doAssert indexAddr[] == 'd'
  30. doAssert obj.s == "lodem ipsum dolor sit amet"
  31. # Bug #2148
  32. var x: array[2, int]
  33. var y = addr x[1]
  34. y[] = 12
  35. doAssert(x[1] == 12)
  36. type
  37. Foo = object
  38. bar: int
  39. var foo: array[2, Foo]
  40. var z = addr foo[1]
  41. z[].bar = 12345
  42. doAssert(foo[1].bar == 12345)
  43. var t : tuple[a, b: int]
  44. var pt = addr t[1]
  45. pt[] = 123
  46. doAssert(t.b == 123)
  47. #block: # Test "untyped" pointer.
  48. proc testPtr(p: pointer, a: int) =
  49. doAssert(a == 5)
  50. (cast[ptr int](p))[] = 124
  51. var i = 123
  52. testPtr(addr i, 5)
  53. doAssert(i == 124)
  54. var someGlobal = 5
  55. proc getSomeGlobalPtr(): ptr int = addr someGlobal
  56. let someGlobalPtr = getSomeGlobalPtr()
  57. doAssert(someGlobalPtr[] == 5)
  58. someGlobalPtr[] = 10
  59. doAssert(someGlobal == 10)
  60. block:
  61. # bug #14576
  62. # lots of these used to give: Error: internal error: genAddr: 2
  63. proc byLent[T](a: T): lent T = a
  64. proc byPtr[T](a: T): ptr T = a.unsafeAddr
  65. block:
  66. let a = (10,11)
  67. let (x,y) = byLent(a)
  68. doAssert (x,y) == a
  69. block: # (with -d:release) bug #14578
  70. let a = 10
  71. doAssert byLent(a) == 10
  72. let a2 = byLent(a)
  73. doAssert a2 == 10
  74. block:
  75. let a = [11,12]
  76. doAssert byLent(a) == [11,12] # bug #15958
  77. let a2 = (11,)
  78. doAssert byLent(a2) == (11,)
  79. block:
  80. when (defined(c) or defined(cpp)) and defined(release):
  81. discard # probably not a bug since optimizer is free to pass by value, and `unsafeAddr` is used
  82. else:
  83. var a = @[12]
  84. doAssert byPtr(a)[] == @[12]
  85. let a2 = [13]
  86. doAssert byPtr(a2)[] == [13]
  87. let a3 = 14
  88. doAssert byPtr(a3)[] == 14
  89. block:
  90. proc byLent2[T](a: seq[T]): lent T = a[1]
  91. var a = @[20,21,22]
  92. doAssert byLent2(a) == 21
  93. block: # sanity checks
  94. proc bar[T](a: var T): var T = a
  95. var a = (10, 11)
  96. let (k,v) = bar(a)
  97. doAssert (k, v) == a
  98. doAssert k == 10
  99. bar(a)[0]+=100
  100. doAssert a == (110, 11)
  101. var a2 = 12
  102. doAssert bar(a2) == a2
  103. bar(a2).inc
  104. doAssert a2 == 13
  105. block: # pending bug #15959
  106. when false:
  107. proc byLent2[T](a: T): lent type(a[0]) = a[0]