twrong_tupleconv.nim 731 B

123456789101112131415161718192021222324252627282930313233343536
  1. discard """
  2. targets: "c cpp"
  3. matrix: "--gc:refc; --gc:arc"
  4. """
  5. # bug #1833
  6. iterator myitems*[T](a: var seq[T]): var T {.inline.} =
  7. ## iterates over each item of `a` so that you can modify the yielded value.
  8. var i = 0
  9. let L = len(a)
  10. while i < L:
  11. yield a[i]
  12. inc(i)
  13. doAssert(len(a) == L, "the length of the seq changed while iterating over it")
  14. # Works fine
  15. var xs = @[1,2,3]
  16. for x in myitems(xs):
  17. inc x
  18. # Tuples don't work
  19. var ys = @[(1,"a"),(2,"b"),(3,"c")]
  20. for y in myitems(ys):
  21. inc y[0]
  22. # bug #16331
  23. type T1 = tuple[a, b: int]
  24. proc p(b: bool): string =
  25. var x: T1 = (10, 20)
  26. x = if b: (x.b, x.a) else: (-x.b, -x.a)
  27. $x
  28. assert p(false) == "(a: -20, b: -10)"
  29. assert p(true) == "(a: 20, b: 10)"