tcannot_borrow.nim 535 B

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