tprevent_assign3.nim 1014 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. discard """
  2. errormsg: "'=copy' is not available for type <Foo>; requires a copy because it's not the last read of 'otherTree'"
  3. file: "tprevent_assign3.nim"
  4. line: 46
  5. """
  6. type
  7. Foo = object
  8. x: int
  9. proc `=destroy`(f: var Foo) = f.x = 0
  10. proc `=`(a: var Foo; b: Foo) {.error.} # = a.x = b.x
  11. proc `=sink`(a: var Foo; b: Foo) = a.x = b.x
  12. proc createTree(x: int): Foo =
  13. Foo(x: x)
  14. proc take2(a, b: sink Foo) =
  15. echo a.x, " ", b.x
  16. proc allowThis() =
  17. var otherTree: Foo
  18. try:
  19. for i in 0..3:
  20. while true:
  21. #if i == 0:
  22. otherTree = createTree(44)
  23. case i
  24. of 0:
  25. echo otherTree
  26. take2(createTree(34), otherTree)
  27. of 1:
  28. take2(createTree(34), otherTree)
  29. else:
  30. discard
  31. finally:
  32. discard
  33. proc preventThis2() =
  34. var otherTree: Foo
  35. try:
  36. try:
  37. otherTree = createTree(44)
  38. echo otherTree
  39. finally:
  40. take2(createTree(34), otherTree)
  41. finally:
  42. echo otherTree
  43. allowThis()
  44. preventThis2()