tcannot_borrow.nim 546 B

123456789101112131415161718192021222324
  1. discard """
  2. errormsg: "cannot borrow"
  3. nimout: '''tcannot_borrow.nim(21, 7) Error: cannot borrow meh; what it borrows from is potentially mutated
  4. tcannot_borrow.nim(22, 3) the mutation is here'''
  5. line: 21
  6. """
  7. {.experimental: "views".}
  8. type
  9. Foo = object
  10. field: string
  11. proc valid(s: var seq[Foo]) =
  12. let v: lent Foo = s[0] # begin of borrow
  13. echo v.field # end of borrow
  14. s.setLen 0 # valid because 'v' isn't used afterwards
  15. proc dangerous(s: var seq[Foo]) =
  16. let meh: lent Foo = s[0]
  17. s.setLen 0
  18. echo meh.field