diff --git a/compiler/vmgen.nim b/compiler/vmgen.nim index 898b5b5def..dd8b8365ca 100644 --- a/compiler/vmgen.nim +++ b/compiler/vmgen.nim @@ -1842,6 +1842,8 @@ proc genArrAccessOpcode(c: PCtx; n: PNode; dest: var TDest; opc: TOpcode; if dest < 0: dest = c.getTemp(n.typ) if opc in {opcLdArrAddr, opcLdStrIdxAddr} and gfNodeAddr in flags: c.gABC(n, opc, dest, a, b) + if c.prc.regInfo[a].kind >= slotTempUnknown: + c.prc.regInfo[a].kind = slotTempPerm elif needsRegLoad(): var cc = c.getTemp(n.typ) c.gABC(n, opc, cc, a, b) @@ -1858,6 +1860,8 @@ proc genObjAccessAux(c: PCtx; n: PNode; a, b: int, dest: var TDest; flags: TGenF if dest < 0: dest = c.getTemp(n.typ) if {gfNodeAddr} * flags != {}: c.gABC(n, opcLdObjAddr, dest, a, b) + if a < c.prc.regInfo.len and c.prc.regInfo[a].kind >= slotTempUnknown: + c.prc.regInfo[a].kind = slotTempPerm elif needsRegLoad(): var cc = c.getTemp(n.typ) c.gABC(n, opcLdObj, cc, a, b) diff --git a/lib/system/iterators.nim b/lib/system/iterators.nim index 125bee98ff..21ac355250 100644 --- a/lib/system/iterators.nim +++ b/lib/system/iterators.nim @@ -37,7 +37,7 @@ iterator mitems*[T](a: var openArray[T]): var T {.inline.} = yield a[i] unCheckedInc(i) -iterator items*[IX, T](a: array[IX, T]): T {.inline.} = +iterator items*[IX, T](a: array[IX, T]): lent2 T {.inline.} = ## Iterates over each item of `a`. when a.len > 0: var i = low(IX) diff --git a/tests/lent/titems_array_lent.nim b/tests/lent/titems_array_lent.nim new file mode 100644 index 0000000000..f423c17963 --- /dev/null +++ b/tests/lent/titems_array_lent.nim @@ -0,0 +1,38 @@ +discard """ + targets: "c cpp js" +""" + +template sameAddress(a, b): bool = + when defined(js): + a == b + else: + a.unsafeAddr == b.unsafeAddr + +proc main() = + block: + let a = [10, 11, 12] + for ai in items(a): + doAssert sameAddress(ai, a[0]) + break + + block: + let a = [[1, 2], [1, 2], [1, 2]] + for ai in items(a): + doAssert sameAddress(ai[0], a[0][0]) + break + + block: + let s = @[(1, 2), (3, 4), (5, 6)] + doAssert (3, 4) in s + +main() + +static: + main() + +block: # issue #25849 + static: + const key = "NIM_TESTS_TOSENV_KEY" + for val in ["val", "", "\xc3\x86"]: + let s = @[(key, "val"), (key, ""), (key, "\xc3\x86")] + doAssert (key, val) in s \ No newline at end of file