diff options
author | ringabout <43030857+ringabout@users.noreply.github.com> | 2023-07-29 16:57:03 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-29 10:57:03 +0200 |
commit | f0f3904ff04a46bae6f876b0326162354466f415 (patch) | |
tree | c83cd3d31c646221488c82e90fd48461d5028c5c /tests/system/tensuremove.nim | |
parent | f1ac979184ad7fa0d8c44415e781181a00a0095f (diff) | |
download | Nim-f0f3904ff04a46bae6f876b0326162354466f415.tar.gz |
implement `ensureMove` (#22339)
* implement `ensureMove` * use an additional flag * improve some logics * progress: fixes discard ensureMove * forbids nested expressions * improve error messages * checkpoint * fixes cursor * ADD MORE TESTS * fixes cursorinference again * tiny cleanup * improve error messages * fixes docs * implement comments add more tests * fixes js
Diffstat (limited to 'tests/system/tensuremove.nim')
-rw-r--r-- | tests/system/tensuremove.nim | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/tests/system/tensuremove.nim b/tests/system/tensuremove.nim new file mode 100644 index 000000000..980f2ea58 --- /dev/null +++ b/tests/system/tensuremove.nim @@ -0,0 +1,130 @@ +discard """ + target: "c js" + matrix: "--cursorinference:on; --cursorinference:off" +""" + +block: + type + X = object + s: string + + proc `=copy`(x: var X, y: X) = + x.s = "copied " & y.s + + proc `=sink`(x: var X, y: X) = + `=destroy`(x) + wasMoved(x) + x.s = "moved " & y.s + + proc consume(x: sink X) = + discard x.s + + proc main = + var x = X(s: "abcdefg") + consume(ensureMove x) + + static: main() + main() + +block: + type + String = object + id: string + + proc hello = + var s = String(id: "1") + var m = ensureMove s + doAssert m.id == "1" + + hello() + +block: + type + String = object + id: string + + proc hello = + var n = "1" + var s = String(id: ensureMove n) + var m = ensureMove s + doAssert m.id == "1" + + hello() + +block: + type + String = object + id: string + + proc hello = + var n = "1" + var s = [ensureMove n] + var m = ensureMove s + doAssert m[0] == "1" + + hello() + +block: + type + String = object + id: string + + proc hello = + var n = "1" + var s = @[ensureMove n] + var m = ensureMove s + doAssert m[0] == "1" + + hello() + +block: + type + String = object + id: string + + proc hello = + var s = String(id: "1") + var m = ensureMove s.id + doAssert m == "1" + + hello() + +block: + proc foo = + var x = 1 + let y = ensureMove x # move + when not defined(js): + doAssert (y, x) == (1, 0) # (1, 0) + foo() + +block: + proc foo = + var x = 1 + let y = ensureMove x # move + doAssert y == 1 + foo() + +block: + proc foo = + var x = @[1, 2, 3] + let y = ensureMove x[0] # move + doAssert y == 1 + when not defined(js): + doAssert x == @[0, 2, 3] + foo() + +block: + proc foo = + var x = [1, 2, 3] + let y = ensureMove x[0] # move + doAssert y == 1 + when not defined(js): + doAssert x == @[0, 2, 3] + foo() + +block: + proc foo = + var x = @["1", "2", "3"] + let y = ensureMove x[0] # move + doAssert y == "1" + foo() |