Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #16003 js: ref object assignment silently gives wrong results #20805

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions compiler/jsgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1434,8 +1434,19 @@ proc genAddr(p: PProc, n: PNode, r: var TCompRes) =
of nkConv:
genAddr(p, n[0], r)
of nkStmtListExpr:
if n.len == 1: gen(p, n[0], r)
bung87 marked this conversation as resolved.
Show resolved Hide resolved
else: internalError(p.config, n[0].info, "genAddr for complex nkStmtListExpr")
let x = n[0]
if n.len == 1:
let isExpr = not isEmptyType(x.typ)
bung87 marked this conversation as resolved.
Show resolved Hide resolved
if x.kind == nkStmtListExpr and isExpr and n.kind in {nkHiddenAddr, nkAddr}:
bung87 marked this conversation as resolved.
Show resolved Hide resolved
for i in 0 ..< x.len - isExpr.ord:
genStmt(p, x[i])
let l = lastSon(x)
var y = newNodeIT(n.kind, l.info, n.typ)
y.add l
genAddr(p, y, r)
else:
gen(p, x, r)
else: internalError(p.config, x.info, "genAddr for complex nkStmtListExpr")
of nkCallKinds:
if n[0].typ.kind == tyOpenArray:
# 'var openArray' for instance produces an 'addr' but this is harmless:
Expand Down
10 changes: 10 additions & 0 deletions tests/js/t16003.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

type
Node = ref object
val: int
proc bar(c: Node): var int =
var n = c
n.val
var a = Node(val: 3)
a.bar() = 5
doAssert a.val == 5 # fails, got 3