tensuremove.nim 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. discard """
  2. target: "c js"
  3. matrix: "--cursorinference:on; --cursorinference:off"
  4. """
  5. block:
  6. type
  7. X = object
  8. s: string
  9. proc `=copy`(x: var X, y: X) =
  10. x.s = "copied " & y.s
  11. proc `=sink`(x: var X, y: X) =
  12. `=destroy`(x)
  13. wasMoved(x)
  14. x.s = "moved " & y.s
  15. proc consume(x: sink X) =
  16. discard x.s
  17. proc main =
  18. let m = "abcdefg"
  19. var x = X(s: ensureMove m)
  20. consume(ensureMove x)
  21. static: main()
  22. main()
  23. block:
  24. type
  25. String = object
  26. id: string
  27. proc hello =
  28. var s = String(id: "1")
  29. var m = ensureMove s
  30. doAssert m.id == "1"
  31. hello()
  32. block:
  33. type
  34. String = object
  35. id: string
  36. proc hello =
  37. var n = "1"
  38. var s = String(id: ensureMove n)
  39. var m = ensureMove s
  40. doAssert m.id == "1"
  41. hello()
  42. block:
  43. type
  44. String = object
  45. id: string
  46. proc hello =
  47. var n = "1"
  48. var s = [ensureMove n]
  49. var m = ensureMove s
  50. doAssert m[0] == "1"
  51. hello()
  52. block:
  53. type
  54. String = object
  55. id: string
  56. proc hello =
  57. var n = "1"
  58. var s = @[ensureMove n]
  59. var m = ensureMove s
  60. doAssert m[0] == "1"
  61. hello()
  62. block:
  63. type
  64. String = object
  65. id: string
  66. proc hello =
  67. var s = String(id: "1")
  68. var m = ensureMove s.id
  69. doAssert m == "1"
  70. hello()
  71. block:
  72. proc foo =
  73. var x = 1
  74. let y = ensureMove x # move
  75. when not defined(js):
  76. doAssert (y, x) == (1, 0) # (1, 0)
  77. foo()
  78. block:
  79. proc foo =
  80. var x = 1
  81. let y = ensureMove x # move
  82. doAssert y == 1
  83. foo()
  84. block:
  85. proc foo =
  86. var x = @[1, 2, 3]
  87. let y = ensureMove x[0] # move
  88. doAssert y == 1
  89. when not defined(js):
  90. doAssert x == @[0, 2, 3]
  91. foo()
  92. block:
  93. proc foo =
  94. var x = [1, 2, 3]
  95. let y = ensureMove x[0] # move
  96. doAssert y == 1
  97. when not defined(js):
  98. doAssert x == @[0, 2, 3]
  99. foo()
  100. block:
  101. proc foo =
  102. var x = @["1", "2", "3"]
  103. let y = ensureMove x[0] # move
  104. doAssert y == "1"
  105. foo()