tnewruntime_misc.nim 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. discard """
  2. cmd: '''nim cpp --newruntime --threads:on $file'''
  3. output: '''(field: "value")
  4. Indeed
  5. axc
  6. (v: 10)
  7. 0 new: 0
  8. ...
  9. destroying GenericObj[T] GenericObj[system.int]
  10. test
  11. '''
  12. """
  13. import core / allocators
  14. import system / ansi_c
  15. import tables
  16. type
  17. Node = ref object
  18. field: string
  19. # bug #11807
  20. import os
  21. putEnv("HEAPTRASHING", "Indeed")
  22. proc main =
  23. var w = newTable[string, owned Node]()
  24. w["key"] = Node(field: "value")
  25. echo w["key"][]
  26. echo getEnv("HEAPTRASHING")
  27. # bug #11891
  28. var x = "abc"
  29. x[1] = 'x'
  30. echo x
  31. main()
  32. # bug #11745
  33. type
  34. Foo = object
  35. bar: seq[int]
  36. var x = [Foo()]
  37. # bug #11563
  38. type
  39. MyTypeType = enum
  40. Zero, One
  41. MyType = object
  42. case kind: MyTypeType
  43. of Zero:
  44. s*: seq[MyType]
  45. of One:
  46. x*: int
  47. var t: MyType
  48. # bug #11254
  49. proc test(p: owned proc()) =
  50. let x = (proc())p
  51. test(proc() = discard)
  52. # bug #10689
  53. type
  54. O = object
  55. v: int
  56. proc `=sink`(d: var O, s: O) =
  57. d.v = s.v
  58. proc selfAssign =
  59. var o = O(v: 10)
  60. o = o
  61. echo o
  62. selfAssign()
  63. # bug #11833
  64. type FooAt = object
  65. proc testWrongAt() =
  66. var x = @[@[FooAt()]]
  67. testWrongAt()
  68. let (a, d) = allocCounters()
  69. discard cprintf("%ld new: %ld\n", a - unpairedEnvAllocs() - d, allocs)
  70. #-------------------------------------------------
  71. type
  72. Table[A, B] = object
  73. x: seq[(A, B)]
  74. proc toTable[A,B](p: sink openArray[(A, B)]): Table[A, B] =
  75. for zz in mitems(p):
  76. result.x.add move(zz)
  77. let table = {"a": new(int)}.toTable()
  78. # bug # #12051
  79. type
  80. GenericObj[T] = object
  81. val: T
  82. Generic[T] = owned ref GenericObj[T]
  83. proc `=destroy`[T](x: var GenericObj[T]) =
  84. echo "destroying GenericObj[T] ", x.typeof # to know when its being destroyed
  85. proc main12() =
  86. let gnrc = Generic[int](val: 42)
  87. echo "..."
  88. main12()
  89. #####################################################################
  90. ## bug #12827
  91. type
  92. MyObject = object
  93. x: string
  94. y: seq[string]
  95. needs_ref: ref int
  96. proc xx(xml: string): MyObject =
  97. let stream = xml
  98. result.x = xml
  99. defer: echo stream
  100. discard xx("test")