timportutils.nim 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. discard """
  2. matrix: "--mm:refc; --mm:orc"
  3. """
  4. import std/[importutils, assertions]
  5. import stdtest/testutils
  6. import mimportutils
  7. template main =
  8. block: # privateAccess
  9. assertAll:
  10. var a: A
  11. var b = initB() # B is private
  12. compiles(a.a0)
  13. compiles(b.b0)
  14. not compiles(a.ha1)
  15. not compiles(b.hb1)
  16. block:
  17. assertAll:
  18. privateAccess A
  19. compiles(a.ha1)
  20. a.ha1 == 0.0
  21. not compiles(a.hb1)
  22. privateAccess b.typeof
  23. b.hb1 = 3
  24. type B2 = b.typeof
  25. let b2 = B2(b0: 4, hb1: 5)
  26. b.hb1 == 3
  27. b2 == B2(b0: 4, hb1: 5)
  28. assertAll:
  29. not compiles(a.ha1)
  30. not compiles(b.hb1)
  31. block:
  32. assertAll:
  33. not compiles(C(c0: 1, hc1: 2))
  34. privateAccess C
  35. let c = C(c0: 1, hc1: 2)
  36. c.hc1 == 2
  37. block:
  38. assertAll:
  39. not compiles(E[int](he1: 1))
  40. privateAccess E[int]
  41. var e = E[int](he1: 1)
  42. e.he1 == 1
  43. e.he1 = 2
  44. e.he1 == 2
  45. e.he1 += 3
  46. e.he1 == 5
  47. # xxx caveat: this currently compiles but in future, we may want
  48. # to make `privateAccess E[int]` only affect a specific instantiation;
  49. # note that `privateAccess E` does work to cover all instantiations.
  50. var e2 = E[float](he1: 1)
  51. block:
  52. assertAll:
  53. not compiles(E[int](he1: 1))
  54. privateAccess E
  55. var e = E[int](he1: 1)
  56. e.he1 == 1
  57. block:
  58. assertAll:
  59. not compiles(F[int, int](h3: 1))
  60. privateAccess F[int, int]
  61. var e = F[int, int](h3: 1)
  62. e.h3 == 1
  63. block:
  64. assertAll:
  65. not compiles(F[int, int](h3: 1))
  66. privateAccess F[int, int].default[].typeof
  67. var e = F[int, int](h3: 1)
  68. e.h3 == 1
  69. block:
  70. assertAll:
  71. var a = G[int]()
  72. var b = a.addr
  73. privateAccess b.type
  74. discard b.he1
  75. discard b[][].he1
  76. block:
  77. assertAll:
  78. privateAccess H[int]
  79. var a = H[int](h5: 2)
  80. block:
  81. assertAll:
  82. privateAccess PA
  83. var pa = PA(a0: 1, ha1: 2)
  84. pa.ha1 == 2
  85. pa.ha1 = 3
  86. pa.ha1 == 3
  87. block:
  88. assertAll:
  89. var b = BAalias()
  90. not compiles(b.hb1)
  91. privateAccess BAalias
  92. discard b.hb1
  93. block:
  94. assertAll:
  95. var a = A(a0: 1)
  96. var a2 = a.addr
  97. not compiles(a2.ha1)
  98. privateAccess PtA
  99. a2.type is PtA
  100. a2.ha1 = 2
  101. a2.ha1 == 2
  102. a.ha1 = 3
  103. a2.ha1 == 3
  104. block:
  105. disableVm:
  106. assertAll:
  107. var a = A.create()
  108. defer: dealloc(a)
  109. a is PtA
  110. a.typeof is PtA
  111. not compiles(a.ha1)
  112. privateAccess a.typeof
  113. a.ha1 = 2
  114. a.ha1 == 2
  115. a[].ha1 = 3
  116. a.ha1 == 3
  117. block:
  118. disableVm:
  119. assertAll:
  120. var a = A.create()
  121. defer: dealloc(a)
  122. privateAccess PtA
  123. a.ha1 == 0
  124. block:
  125. privateAccess PityRef
  126. let x = PityRef[int](a: 1) # works
  127. doAssert x.a == 1
  128. privateAccess Hope
  129. let y = Hope[int](a: 1)
  130. doAssert y.a == 1
  131. static: main()
  132. main()