summary refs log tree commit diff stats
path: root/tests/destructor/tselect.nim
blob: c22bf7203b50abf06e8e351fd6f7eb77c3d31ba1 (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
discard """
   output: '''abcsuffix
xyzsuffix
destroy foo 2
destroy foo 1
'''
  cmd: '''nim c --gc:arc $file'''
"""

proc select(cond: bool; a, b: sink string): string =
  if cond:
    result = a # moves a into result
  else:
    result = b # moves b into result

proc test(param: string; cond: bool) =
  var x = "abc" & param
  var y = "xyz" & param

  # possible self-assignment:
  x = select(cond, x, y)

  echo x
  # 'select' must communicate what parameter has been
  # consumed. We cannot simply generate:
  # (select(...); wasMoved(x); wasMoved(y))

test("suffix", true)
test("suffix", false)



#--------------------------------------------------------------------
# issue #13659

type
  Foo = ref object
    data: int
    parent: Foo

proc `=destroy`(self: var type(Foo()[])) =
  echo "destroy foo ", self.data
  for i in self.fields: i.reset

proc getParent(self: Foo): Foo = self.parent

var foo1 = Foo(data: 1)
var foo2 = Foo(data: 2, parent: foo1)

foo2.getParent.data = 1