tbasic_lent_check.nim 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. discard """
  2. targets: "c cpp js"
  3. output: "1"
  4. """
  5. proc viewInto(a: array[4, string]): lent string =
  6. result = a[0]
  7. proc passToVar(x: var string) =
  8. discard
  9. proc main =
  10. let x = ["1", "2", "3", "4"]
  11. echo viewInto(x)
  12. doAssert(not compiles(passToVar(viewInto(x))))
  13. main()
  14. template main2 = # bug #15958
  15. proc byLent[T](a: T): lent T = a
  16. let a = [11,12]
  17. let b = @[21,23]
  18. let ss = {1, 2, 3, 5}
  19. doAssert byLent(a) == [11,12]
  20. doAssert byLent(a).unsafeAddr == a.unsafeAddr
  21. doAssert byLent(b) == @[21,23]
  22. when not defined(js): # pending bug #16073
  23. doAssert byLent(b).unsafeAddr == b.unsafeAddr
  24. doAssert byLent(ss) == {1, 2, 3, 5}
  25. doAssert byLent(ss).unsafeAddr == ss.unsafeAddr
  26. let r = new(float)
  27. r[] = 10.0
  28. when not defined(js): # pending bug #16073
  29. doAssert byLent(r)[] == 10.0
  30. when not defined(js): # pending bug https://github.com/timotheecour/Nim/issues/372
  31. let p = create(float)
  32. p[] = 20.0
  33. doAssert byLent(p)[] == 20.0
  34. proc byLent2[T](a: openarray[T]): lent T = a[0]
  35. doAssert byLent2(a) == 11
  36. doAssert byLent2(a).unsafeAddr == a[0].unsafeAddr
  37. doAssert byLent2(b) == 21
  38. doAssert byLent2(b).unsafeAddr == b[0].unsafeAddr
  39. proc byLent3[T](a: varargs[T]): lent T = a[1]
  40. let
  41. x = 10
  42. y = 20
  43. z = 30
  44. doAssert byLent3(x, y, z) == 20
  45. main2()
  46. when false:
  47. # bug: Error: unhandled exception: 'node' is not accessible using discriminant 'kind' of type 'TFullReg' [FieldDefect]
  48. static: main2()