tbasic_lent_check.nim 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. when defined(js):
  16. proc sameAddress[T](a, b: T): bool {.importjs: "(# === #)".}
  17. else:
  18. template sameAddress(a, b): bool = a.unsafeAddr == b.unsafeAddr
  19. proc byLent[T](a: T): lent T = a
  20. let a = [11,12]
  21. let b = @[21,23]
  22. let ss = {1, 2, 3, 5}
  23. doAssert byLent(a) == [11,12]
  24. doAssert sameAddress(byLent(a), a)
  25. doAssert byLent(b) == @[21,23]
  26. # bug #16073
  27. doAssert sameAddress(byLent(b), b)
  28. doAssert byLent(ss) == {1, 2, 3, 5}
  29. doAssert sameAddress(byLent(ss), ss)
  30. let r = new(float)
  31. r[] = 10.0
  32. # bug #16073
  33. doAssert byLent(r)[] == 10.0
  34. when not defined(js): # pending bug https://github.com/timotheecour/Nim/issues/372
  35. let p = create(float)
  36. p[] = 20.0
  37. doAssert byLent(p)[] == 20.0
  38. proc byLent2[T](a: openArray[T]): lent T = a[0]
  39. doAssert byLent2(a) == 11
  40. doAssert sameAddress(byLent2(a), a[0])
  41. doAssert byLent2(b) == 21
  42. doAssert sameAddress(byLent2(b), b[0])
  43. proc byLent3[T](a: varargs[T]): lent T = a[1]
  44. let
  45. x = 10
  46. y = 20
  47. z = 30
  48. doAssert byLent3(x, y, z) == 20
  49. main2()
  50. when false:
  51. # bug: Error: unhandled exception: 'node' is not accessible using discriminant 'kind' of type 'TFullReg' [FieldDefect]
  52. static: main2()