From e4eff04945bb8f7234d541f8200a4aaf5e26b4aa Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Fri, 17 Apr 2026 15:59:22 +0800 Subject: [PATCH] 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. (cherry picked from commit 2b2872928b5f8ac1779c980481ecee4a575b4590) --- compiler/transf.nim | 5 +++++ tests/arc/tdistinct_for_nodup.nim | 33 +++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 tests/arc/tdistinct_for_nodup.nim diff --git a/compiler/transf.nim b/compiler/transf.nim index 043f194f79..a8c073f79e 100644 --- a/compiler/transf.nim +++ b/compiler/transf.nim @@ -703,6 +703,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..