Skip to content

Commit fb4a82f

Browse files
authored
fixes #25210; VM error when passing object field ref to proc(var T): var T (#25213)
fixes #25210 no longer transform `addr(obj.field[])` into `obj.field` to keep the addressing needed for VM
1 parent c0fa868 commit fb4a82f

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

compiler/jsgen.nim

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1586,7 +1586,11 @@ proc genAddr(p: PProc, n: PNode, r: var TCompRes) =
15861586
of nkObjDownConv:
15871587
gen(p, n[0], r)
15881588
of nkHiddenDeref, nkDerefExpr:
1589-
gen(p, n[0], r)
1589+
if n.kind in {nkAddr, nkHiddenAddr}:
1590+
# addr ( deref ( x )) --> x
1591+
gen(p, n[0][0], r)
1592+
else:
1593+
gen(p, n[0], r)
15901594
of nkHiddenAddr:
15911595
gen(p, n[0], r)
15921596
of nkConv:

compiler/transf.nim

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,8 +519,7 @@ proc transformAddrDeref(c: PTransf, n: PNode, kinds: TNodeKinds, isAddr = false)
519519
n.typ.kind == tyVar and
520520
n.typ.skipTypes(abstractVar).kind == tyOpenArray and
521521
n[0][0].typ.skipTypes(abstractVar).kind == tyString) and
522-
not (isAddr and n.typ.kind == tyVar and n[0][0].typ.kind == tyRef and
523-
n[0][0].kind == nkObjConstr)
522+
not (isAddr and n.typ.kind == tyVar and n[0][0].typ.kind == tyRef)
524523
: # elimination is harmful to `for tuple unpack` because of newTupleAccess
525524
# it is also harmful to openArrayLoc (var openArray) for strings
526525
# addr ( deref ( x )) --> x

tests/vm/tmisc_vm.nim

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,3 +457,25 @@ proc publish*(): void {.transform.} =
457457
map["k"].incl "d"
458458

459459
publish()
460+
461+
462+
iterator it(x: var int): var int =
463+
yield x
464+
465+
proc it(x: var int): var int =
466+
x
467+
468+
proc xxx() =
469+
type Obj = object
470+
field: ref int
471+
var obj = Obj(field: new(int))
472+
obj.field[] = 123
473+
assert it(obj.field[]) == 123 # fails
474+
for x in it(obj.field[]): # fails
475+
assert x == 123
476+
477+
static:
478+
xxx()
479+
480+
xxx()
481+

0 commit comments

Comments
 (0)