tprevent_assign3.nim 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. discard """
  2. errormsg: "'=dup' 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: 47
  5. """
  6. type
  7. Foo = object
  8. x: int
  9. proc `=destroy`(f: var Foo) = f.x = 0
  10. proc `=copy`(a: var Foo; b: Foo) {.error.} # = a.x = b.x
  11. proc `=dup`(a: Foo): Foo {.error.}
  12. proc `=sink`(a: var Foo; b: Foo) = a.x = b.x
  13. proc createTree(x: int): Foo =
  14. Foo(x: x)
  15. proc take2(a, b: sink Foo) =
  16. echo a.x, " ", b.x
  17. when false:
  18. var otherTree: Foo
  19. try:
  20. for i in 0..3:
  21. while true:
  22. #if i == 0:
  23. otherTree = createTree(44)
  24. case i
  25. of 0:
  26. echo otherTree
  27. take2(createTree(34), otherTree)
  28. of 1:
  29. take2(createTree(34), otherTree)
  30. else:
  31. discard
  32. finally:
  33. discard
  34. proc preventThis2() =
  35. var otherTree: Foo
  36. try:
  37. try:
  38. otherTree = createTree(44)
  39. echo otherTree
  40. finally:
  41. take2(createTree(34), otherTree)
  42. finally:
  43. echo otherTree
  44. #allowThis()
  45. preventThis2()