summary refs log tree commit diff stats
path: root/tests/destructor/tsink.nim
blob: e8750ad7ccbb4a7d32283eb36f20c3ba6b400e98 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
discard """
  matrix: "--mm:arc"
"""

type AnObject = object of RootObj
  value*: int

proc mutate(shit: sink AnObject) =
  shit.value = 1

proc foo = # bug #23359
  var bar = AnObject(value: 42)
  mutate(bar)
  doAssert bar.value == 42

foo()

block: # bug #23902
  proc foo(a: sink string): auto = (a, a)

  proc bar(a: sink int): auto = return a

  proc foo(a: sink string) =
    var x = (a, a)

block: # bug #24175
  block:
    func mutate(o: sink string): string =
      o[1] = '1'
      result = o

    static:
      let s = "999"
      let m = mutate(s)
      doAssert s == "999"
      doAssert m == "919"

    func foo() =
      let s = "999"
      let m = mutate(s)
      doAssert s == "999"
      doAssert m == "919"

    static:
      foo()
    foo()

  block:
    type O = object
      a: int

    func mutate(o: sink O): O =
      o.a += 1
      o

    static:
      let x = O(a: 1)
      let y = mutate(x)
      doAssert x.a == 1
      doAssert y.a == 2

    proc foo() =
      let x = O(a: 1)
      let y = mutate(x)
      doAssert x.a == 1
      doAssert y.a == 2

    static:
      foo()
    foo()