tdangingref_simple.nim 635 B

123456789101112131415161718192021222324252627282930313233
  1. discard """
  2. output: '''a
  3. [FATAL] dangling references exist
  4. '''
  5. exitCode: 1
  6. cmd: "nim c --newruntime $file"
  7. """
  8. # bug #11350
  9. type
  10. Node = ref object
  11. data: int
  12. proc use(x: Node) = discard
  13. proc main =
  14. var x = Node(data: 3) # inferred to be an ``owned ref``
  15. var dangling = unown x
  16. assert dangling.data == 3
  17. #use x
  18. #dangling = nil
  19. # reassignment causes the memory of what ``x`` points to to be freed:
  20. echo "a"
  21. x = Node(data: 4)
  22. echo "b"
  23. # accessing 'dangling' here is invalid as it is nil.
  24. # at scope exit the memory of what ``x`` points to is freed
  25. if dangling != nil:
  26. echo dangling.data
  27. main()