fixes #25469; Conversion from distinct in for forces a copy of underlying instance (#25746)

fixes #25469

This pull request introduces an important fix to argument handling in
the compiler's transformation logic and adds a new test to verify
correct behavior with distinct types and ARC memory management.

### Compiler transformation improvements

* Updated `putArgInto` in `compiler/transf.nim` to handle
`nkHiddenStdConv`, `nkHiddenSubConv`, and `nkConv` nodes more
accurately. Now, if the types match (ignoring distinctness and shallow
range differences), the argument is recursively processed; otherwise, it
falls back to a fast assignment. This prevents incorrect assignments
when dealing with type conversions and distinct types.

### Testing for distinct types and ARC

* Added a new test `tdistinct_for_nodup.nim` to ensure correct iteration
and memory management for distinct sequences of large arrays under ARC.
The test checks that the sequence length remains unchanged during
iteration, helping catch regressions related to ARC and distinct types.
This commit is contained in:
ringabout
2026-04-17 15:59:22 +08:00
committed by GitHub
parent b4d4028afa
commit 2b2872928b
2 changed files with 38 additions and 0 deletions

View File

@@ -702,6 +702,11 @@ proc putArgInto(arg: PNode, formal: PType): TPutArgInto =
of nkAddr, nkHiddenAddr:
result = putArgInto(arg[0], formal)
if result == paViaIndirection: result = paFastAsgn
of nkHiddenStdConv, nkHiddenSubConv, nkConv:
if compareTypes(arg.typ, arg[1].typ, dcEqIgnoreDistinct, {IgnoreRangeShallow}):
result = putArgInto(arg[1], formal)
else:
result = paFastAsgn
of nkCurly, nkBracket:
for i in 0..<arg.len:
if putArgInto(arg[i], formal) != paDirectMapping:

View File

@@ -0,0 +1,33 @@
discard """
cmd: '''nim c --mm:arc --expandArc:foo $file'''
nimout: '''
--expandArc: foo
var broken_cursor
block :tmp:
var i
var i_1 = 0
let L = len(seq[Large](broken_cursor))
block :tmp_1:
while i_1 < L:
i = seq[Large](broken_cursor)[i_1]
discard i
{.push, overflowChecks: false.}
inc(i_1, 1)
{.pop.}
-- end of expandArc ------------------------
'''
"""
type
Large = array[1024, byte]
List = distinct seq[Large]
proc foo =
var
broken: List
for i in seq[Large](broken):
discard i
foo()