tdestructor.nim 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. discard """
  2. output: '''----1
  3. myobj constructed
  4. myobj destroyed
  5. ----2
  6. mygeneric1 constructed
  7. mygeneric1 destroyed
  8. ----3
  9. mygeneric2 constructed
  10. mygeneric2 destroyed
  11. myobj destroyed
  12. ----4
  13. mygeneric3 constructed
  14. mygeneric1 destroyed
  15. ----5
  16. mydistinctObj constructed
  17. myobj destroyed
  18. mygeneric2 destroyed
  19. ------------------8
  20. mygeneric1 destroyed
  21. ----6
  22. myobj destroyed
  23. ----7
  24. ---9
  25. myobj destroyed
  26. myobj destroyed
  27. '''
  28. """
  29. type
  30. TMyObj = object
  31. x, y: int
  32. p: pointer
  33. proc `=destroy`(o: var TMyObj) =
  34. if o.p != nil:
  35. dealloc o.p
  36. o.p = nil
  37. echo "myobj destroyed"
  38. type
  39. TMyGeneric1[T] = object
  40. x: T
  41. TMyGeneric2[A, B] = object
  42. x: A
  43. y: B
  44. proc `=destroy`(o: var TMyGeneric1[int]) =
  45. echo "mygeneric1 destroyed"
  46. proc `=destroy`[A, B](o: var TMyGeneric2[A, B]) =
  47. echo "mygeneric2 destroyed"
  48. type
  49. TMyGeneric3[A, B, C] = object
  50. x: A
  51. y: B
  52. z: C
  53. TDistinctObjX = distinct TMyGeneric3[TMyObj, TMyGeneric2[int, int], int]
  54. TDistinctObj = TDistinctObjX
  55. TObjKind = enum Z, A, B, C, D
  56. TCaseObj = object
  57. z: TMyGeneric3[TMyObj, float, int]
  58. case kind: TObjKind
  59. of Z: discard
  60. of A:
  61. x: TMyGeneric1[int]
  62. of B, C:
  63. y: TMyObj
  64. else:
  65. case innerKind: TObjKind
  66. of Z: discard
  67. of A, B, C:
  68. p: TMyGeneric3[int, float, string]
  69. of D:
  70. q: TMyGeneric3[TMyObj, int, int]
  71. r: string
  72. proc open: TMyObj =
  73. # allow for superfluous ()
  74. result = (TMyObj(x: 1, y: 2, p: alloc(3)))
  75. proc `$`(x: TMyObj): string = $x.y
  76. proc myobj() =
  77. var x = open()
  78. echo "myobj constructed"
  79. proc mygeneric1() =
  80. var x = TMyGeneric1[int](x: 10)
  81. echo "mygeneric1 constructed"
  82. proc mygeneric2[T](val: T) =
  83. var a = open()
  84. var b = TMyGeneric2[int, T](x: 10, y: val)
  85. echo "mygeneric2 constructed"
  86. var c = TMyGeneric3[int, int, string](x: 10, y: 20, z: "test")
  87. proc mygeneric3 =
  88. var x = TMyGeneric3[int, string, TMyGeneric1[int]](
  89. x: 10, y: "test", z: TMyGeneric1[int](x: 10))
  90. echo "mygeneric3 constructed"
  91. proc mydistinctObj =
  92. var x = TMyGeneric3[TMyObj, TMyGeneric2[int, int], int](
  93. x: open(), y: TMyGeneric2[int, int](x: 5, y: 15), z: 20)
  94. echo "mydistinctObj constructed"
  95. echo "----1"
  96. myobj()
  97. echo "----2"
  98. mygeneric1()
  99. echo "----3"
  100. mygeneric2[int](10)
  101. echo "----4"
  102. mygeneric3()
  103. echo "----5"
  104. mydistinctObj()
  105. proc caseobj =
  106. block:
  107. var o1 = TCaseObj(kind: A, x: TMyGeneric1[int](x: 10))
  108. block:
  109. echo "----6"
  110. var o2 = TCaseObj(kind: B, y: open())
  111. block:
  112. echo "----7"
  113. var o3 = TCaseObj(kind: D, innerKind: B, r: "test",
  114. p: TMyGeneric3[int, float, string](x: 10, y: 1.0, z: "test"))
  115. echo "------------------8"
  116. caseobj()
  117. proc caseobj_test_sink: TCaseObj =
  118. # check that lifted sink can destroy case val correctly
  119. result = TCaseObj(kind: D, innerKind: D, r: "test",
  120. q: TMyGeneric3[TMyObj, int, int](x: open(), y: 1, z: 0))
  121. result = TCaseObj(kind: B, y: open())
  122. echo "---9"
  123. discard caseobj_test_sink()
  124. # issue #14315
  125. type Vector*[T] = object
  126. x1: int
  127. # x2: T # uncomment will remove error
  128. # proc `=destroy`*(x: var Vector[int]) = discard # this will remove error
  129. proc `=destroy`*[T](x: var Vector[T]) = discard
  130. var a: Vector[int] # Error: unresolved generic parameter