twrapnils.nim 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. discard """
  2. matrix: "--mm:refc; --mm:orc"
  3. """
  4. import std/wrapnils
  5. from std/options import get, isSome
  6. import std/assertions
  7. proc checkNotZero(x: float): float =
  8. doAssert x != 0
  9. x
  10. proc main() =
  11. var witness = 0
  12. block:
  13. type Bar = object
  14. b1: int
  15. b2: ptr string
  16. type Foo = ref object
  17. x1: float
  18. x2: Foo
  19. x3: string
  20. x4: Bar
  21. x5: seq[int]
  22. x6: ptr Bar
  23. x7: array[2, string]
  24. x8: seq[int]
  25. x9: ref Bar
  26. type Goo = ref object
  27. foo: Foo
  28. proc fun(a: Bar): auto = a.b2
  29. var a: Foo = nil
  30. var x6: ptr Bar = nil
  31. when nimvm: discard # pending https://github.com/timotheecour/Nim/issues/568
  32. else:
  33. x6 = create(Bar)
  34. x6.b1 = 42
  35. var a2 = Foo(x1: 1.0, x5: @[10, 11], x6: x6)
  36. var a3 = Foo(x1: 1.2, x3: "abc")
  37. a3.x2 = a3
  38. var goo = Goo(foo: a)
  39. proc initFoo(x1: float): auto =
  40. witness.inc
  41. result = Foo(x1: x1)
  42. doAssert ?.a.x2.x2.x1 == 0.0
  43. doAssert ?.a3.x2.x2.x1 == 1.2
  44. doAssert ?.a3.x2.x2.x3[1] == 'b'
  45. doAssert ?.a3.x2.x2.x5.len == 0
  46. doAssert a3.x2.x2.x3.len == 3
  47. doAssert ?.a.x2.x2.x3[1] == default(char)
  48. # here we only apply wrapnil around goo.foo, not goo (and assume goo is not nil)
  49. doAssert ?.(goo.foo).x2.x2.x1 == 0.0
  50. when nimvm: discard
  51. else:
  52. doAssert ?.a2.x6[] == Bar(b1: 42) # deref for ptr Bar
  53. doAssert ?.a2.x1.checkNotZero == 1.0
  54. doAssert a == nil
  55. # shows that checkNotZero won't be called if a nil is found earlier in chain
  56. doAssert ?.a.x1.checkNotZero == 0.0
  57. when nimvm: discard
  58. else:
  59. # checks that a chain without nil but with an empty seq still raises
  60. doAssertRaises(IndexDefect): discard ?.a2.x8[3]
  61. # make sure no double evaluation bug
  62. doAssert witness == 0
  63. doAssert ?.initFoo(1.3).x1 == 1.3
  64. doAssert witness == 1
  65. # here, it's used twice, to deref `ref Bar` and then `ptr string`
  66. doAssert ?.a.x9[].fun[] == ""
  67. block: # `??.`
  68. doAssert (??.a3.x2.x2.x3.len).get == 3
  69. doAssert (??.a2.x4).isSome
  70. doAssert not (??.a.x4).isSome
  71. block:
  72. type
  73. A = object
  74. b: B
  75. B = object
  76. c: C
  77. C = object
  78. d: D
  79. D = ref object
  80. e: E
  81. e2: array[2, E]
  82. e3: seq[E]
  83. d3: D
  84. i4: int
  85. E = object
  86. f: int
  87. d2: D
  88. proc identity[T](a: T): T = a
  89. proc identity2[T](a: T, ignore: int): T = a
  90. var a: A = default(A)
  91. doAssert ?.a.b.c.d.e.f == 0
  92. doAssert ?.a.b.c.d.e.d2.d3[].d3.e.d2.e.f == 0
  93. doAssert ?.a.b.c.d.d3[].e.f == 0
  94. doAssert ?.a.b.c.d.e2[0].d2.e3[0].f == 0
  95. doAssert ?.a == A.default
  96. doAssert ?.a.b.c.d.e == E.default
  97. doAssert ?.a.b.c.d.e.d2 == nil
  98. doAssert ?.a.identity.b.c.identity2(12).d.d3.e.f == 0
  99. doAssert ?.a.b.c.d.d3.e2[0].f == 0
  100. a.b.c.d = D()
  101. a.b.c.d.d3 = a.b.c.d
  102. a.b.c.d.e2[0].f = 5
  103. doAssert ?.a.b.c.d.d3.e2[0].f == 5
  104. var d: D = nil
  105. doAssert ?.d.identity.i4 == 0
  106. doAssert ?.d.i4.identity == 0
  107. block: # case objects
  108. type
  109. Kind = enum k0, k1, k2
  110. V = object
  111. case kind: Kind
  112. of k0:
  113. x0: int
  114. of k1:
  115. x1: int
  116. of k2:
  117. x2: int
  118. A = object
  119. v0: V
  120. block:
  121. var a = V(kind: k0, x0: 3)
  122. doAssert ?.a.x0 == 3
  123. doAssert ?.a.x1 == 0
  124. a = V(kind: k1, x1: 5)
  125. doAssert ?.a.x0 == 0
  126. doAssert ?.a.x1 == 5
  127. block:
  128. var a = A(v0: V(kind: k0, x0: 10))
  129. doAssert ?.a.v0.x0 == 10
  130. doAssert ?.a.v0.x1 == 0
  131. a.v0 = V(kind: k2, x2: 8)
  132. doAssert ?.a.v0.x0 == 0
  133. doAssert ?.a.v0.x1 == 0
  134. doAssert ?.a.v0.x2 == 8
  135. block: # `nnkCall`
  136. type
  137. A = object
  138. a0: int
  139. d: D
  140. D = ref object
  141. i4: int
  142. proc identity[T](a: T): T = a
  143. var d: D = nil
  144. doAssert ?.d.i4.identity == 0
  145. doAssert ?.identity(?.d.i4) == 0
  146. doAssert ?.identity(d.i4) == 0
  147. doAssert ?.identity(d) == nil
  148. doAssert ?.identity(d[]) == default(typeof(d[]))
  149. doAssert ?.identity(d[]).i4 == 0
  150. var a: A = default(A)
  151. doAssert ?.identity(a) == default(A)
  152. doAssert ?.identity(a.a0) == 0
  153. doAssert ?.identity(a.d) == nil
  154. doAssert ?.identity(a.d.i4) == 0
  155. block: # lvalue semantic propagation
  156. type
  157. A = ref object
  158. a0: A
  159. a1: seq[A]
  160. a2: int
  161. B = object
  162. b0: int
  163. case cond: bool
  164. of false: discard
  165. of true:
  166. b1: float
  167. block:
  168. var a: A = default(A)
  169. doAssert ?.a.a0.a1[0].a2.addr == nil
  170. a = A(a2: 3)
  171. doAssert ?.a.a0.a1[0].a2.addr == nil
  172. a.a0 = a
  173. a.a1 = @[a]
  174. let p = ?.a.a0.a1[0].a2.addr
  175. doAssert p != nil
  176. p[] = 5
  177. doAssert a.a2 == 5
  178. block:
  179. var b = B(cond: false, b0: 3)
  180. let p = ?.b.b1.addr
  181. doAssert p == nil
  182. b = B(cond: true, b1: 4.5)
  183. let p2 = ?.b.b1.addr
  184. doAssert p2 != nil
  185. p2[] = 4.6
  186. doAssert b.b1 == 4.6
  187. # useful pattern, impossible with Options
  188. if (let p3 = ?.b.b1.addr; p3 != nil):
  189. p3[] = 4.7
  190. doAssert b.b1 == 4.7
  191. main()
  192. static: main()