From 3646469d00785c545b5efad8c211fa57475d8dba Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Thu, 9 Oct 2025 21:09:32 +0800 Subject: [PATCH 1/3] test deref --- compiler/transf.nim | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/compiler/transf.nim b/compiler/transf.nim index 7ad4634ea347..066be57f8763 100644 --- a/compiler/transf.nim +++ b/compiler/transf.nim @@ -519,8 +519,7 @@ proc transformAddrDeref(c: PTransf, n: PNode, kinds: TNodeKinds, isAddr = false) n.typ.kind == tyVar and n.typ.skipTypes(abstractVar).kind == tyOpenArray and n[0][0].typ.skipTypes(abstractVar).kind == tyString) and - not (isAddr and n.typ.kind == tyVar and n[0][0].typ.kind == tyRef and - n[0][0].kind == nkObjConstr) + not (isAddr and n.typ.kind == tyVar and n[0][0].typ.kind == tyRef) : # elimination is harmful to `for tuple unpack` because of newTupleAccess # it is also harmful to openArrayLoc (var openArray) for strings # addr ( deref ( x )) --> x From 11f08ac0ce22161f9fd4fd571b431cb16ad04a5c Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Thu, 9 Oct 2025 22:28:29 +0800 Subject: [PATCH 2/3] adds a test case --- tests/vm/tmisc_vm.nim | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/vm/tmisc_vm.nim b/tests/vm/tmisc_vm.nim index 1ad830b5fff5..6a0c0c33fb40 100644 --- a/tests/vm/tmisc_vm.nim +++ b/tests/vm/tmisc_vm.nim @@ -457,3 +457,25 @@ proc publish*(): void {.transform.} = map["k"].incl "d" publish() + + +iterator it(x: var int): var int = + yield x + +proc it(x: var int): var int = + x + +proc xxx() = + type Obj = object + field: ref int + var obj = Obj(field: new(int)) + obj.field[] = 123 + assert it(obj.field[]) == 123 # fails + for x in it(obj.field[]): # fails + assert x == 123 + +static: + xxx() + +xxx() + From 319fc7e473a9f1b09318c1b220ee2b19b0505aa1 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Fri, 10 Oct 2025 20:41:02 +0800 Subject: [PATCH 3/3] fixes JS --- compiler/jsgen.nim | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/compiler/jsgen.nim b/compiler/jsgen.nim index 7ae649374044..9240c36c63b0 100644 --- a/compiler/jsgen.nim +++ b/compiler/jsgen.nim @@ -1586,7 +1586,11 @@ proc genAddr(p: PProc, n: PNode, r: var TCompRes) = of nkObjDownConv: gen(p, n[0], r) of nkHiddenDeref, nkDerefExpr: - gen(p, n[0], r) + if n.kind in {nkAddr, nkHiddenAddr}: + # addr ( deref ( x )) --> x + gen(p, n[0][0], r) + else: + gen(p, n[0], r) of nkHiddenAddr: gen(p, n[0], r) of nkConv: