tlent_from_var.nim 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. discard """
  2. output: '''x
  3. [10, 11, 12, 13]'''
  4. """
  5. # bug #14805
  6. type Foo = object
  7. a: string
  8. proc bar(f: var Foo): lent string =
  9. result = f.a
  10. var foo = Foo(a: "x")
  11. echo bar(foo)
  12. # bug #14878
  13. # proc byLentImpl[T](a: T): lent T = a
  14. proc byLentVar[T](a: var T): lent T =
  15. result = a
  16. # result = a.byLentImpl # this doesn't help
  17. var x = 3 # error: cannot take the address of an rvalue of type 'NI *'
  18. var xs = [10,11,12,13] # SIGSEGV
  19. let x2 = x.byLentVar
  20. let xs2 = xs.byLentVar
  21. echo xs2
  22. # bug #22138
  23. type Xxx = object
  24. type
  25. Opt[T] = object
  26. case oResultPrivate*: bool
  27. of false:
  28. discard
  29. of true:
  30. vResultPrivate*: T
  31. func value*[T: not void](self: Opt[T]): lent T {.inline.} =
  32. self.vResultPrivate
  33. template get*[T: not void](self: Opt[T]): T = self.value()
  34. method connect*(
  35. self: Opt[(int, int)]) =
  36. discard self.get()[0]
  37. block: # bug #23454
  38. type
  39. Letter = enum
  40. A
  41. LetterPairs = object
  42. values: seq[(Letter, string)]
  43. iterator items(list: var LetterPairs): lent (Letter, string) =
  44. for item in list.values:
  45. yield item
  46. var instance = LetterPairs(values: @[(A, "foo")])
  47. for (a, _) in instance:
  48. case a
  49. of A: discard
  50. block: # bug #23454
  51. type
  52. Letter = enum
  53. A
  54. LetterPairs = object
  55. values: seq[(Letter, string)]
  56. iterator items(list: var LetterPairs): var (Letter, string) =
  57. for item in list.values.mItems:
  58. yield item
  59. var instance = LetterPairs(values: @[(A, "foo")])
  60. for (a, _) in instance:
  61. case a
  62. of A: discard
  63. block: # bug #24034
  64. type T = object
  65. v: array[100, byte]
  66. iterator pairs(t: T): (int, lent array[100, byte]) =
  67. yield (0, t.v)
  68. block:
  69. for a, b in default(T):
  70. doAssert a == 0
  71. doAssert b.len == 100
  72. block:
  73. for (a, b) in pairs(default(T)):
  74. doAssert a == 0
  75. doAssert b.len == 100