tprevent_assign.nim 696 B

12345678910111213141516171819202122232425262728293031323334
  1. discard """
  2. errormsg: "'=' is not available for type <Foo>; requires a copy because it's not the last read of 'otherTree'"
  3. line: 29
  4. """
  5. type
  6. Foo = object
  7. x: int
  8. proc `=destroy`(f: var Foo) = f.x = 0
  9. proc `=`(a: var Foo; b: Foo) {.error.} # = a.x = b.x
  10. proc `=sink`(a: var Foo; b: Foo) = a.x = b.x
  11. proc createTree(x: int): Foo =
  12. Foo(x: x)
  13. proc take2(a, b: sink Foo) =
  14. echo a.x, " ", b.x
  15. proc allowThis() =
  16. # all these temporary lets are harmless:
  17. let otherTree = createTree(44)
  18. let b = otherTree
  19. let c = b
  20. take2(createTree(34), c)
  21. proc preventThis() =
  22. let otherTree = createTree(44)
  23. let b = otherTree
  24. take2(createTree(34), otherTree)
  25. allowThis()
  26. preventThis()