From cac09a43aea0e8bcc6a7f2634cfcd4274a260b49 Mon Sep 17 00:00:00 2001 From: Bung Date: Tue, 28 Jul 2020 03:13:49 +0800 Subject: [PATCH] =?UTF-8?q?fix=20#11354=20jsgen=20not=20carefully=20handle?= =?UTF-8?q?=20genAddr=20with=20nkHiddenAddr,nkStm=E2=80=A6=20(#15078)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix #11354 jsgen not carefully handle genAddr with nkHiddenAddr,nkStmtListExpr; genAsgn with lvalue tyVar and rvalue tyPtr * correct logic * add test for #11354 * handle nkHiddenAddr when n.len == 1 * Update compiler/jsgen.nim * Update compiler/jsgen.nim * Apply suggestions from code review Co-authored-by: Andreas Rumpf --- compiler/jsgen.nim | 11 ++++++++++- tests/js/t11354.nim | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 tests/js/t11354.nim diff --git a/compiler/jsgen.nim b/compiler/jsgen.nim index 3351d64aee..ede4d14e02 100644 --- a/compiler/jsgen.nim +++ b/compiler/jsgen.nim @@ -1069,6 +1069,10 @@ proc genAsgnAux(p: PProc, x, y: PNode, noCopyNeeded: bool) = elif b.typ == etyNone: internalAssert p.config, b.address == nil lineF(p, "$# = [$#, 0];$n", [a.address, b.res]) + elif x.typ.kind == tyVar and y.typ.kind == tyPtr: + lineF(p, "$# = [$#, $#];$n", [a.res, b.address, b.res]) + lineF(p, "$1 = $2;$n", [a.address, b.res]) + lineF(p, "$1 = $2;$n", [a.rdLoc, b.rdLoc]) else: internalError(p.config, x.info, $("genAsgn", b.typ, a.typ)) else: @@ -1318,7 +1322,12 @@ proc genAddr(p: PProc, n: PNode, r: var TCompRes) = of nkObjDownConv: gen(p, n[0], r) of nkHiddenDeref: - gen(p, n[0][0], r) + gen(p, n[0], r) + of nkHiddenAddr: + gen(p, n[0], r) + of nkStmtListExpr: + if n.len == 1: gen(p, n[0], r) + else: internalError(p.config, n[0].info, "genAddr for complex nkStmtListExpr") else: internalError(p.config, n[0].info, "genAddr: " & $n[0].kind) proc attachProc(p: PProc; content: Rope; s: PSym) = diff --git a/tests/js/t11354.nim b/tests/js/t11354.nim new file mode 100644 index 0000000000..8dee90de0f --- /dev/null +++ b/tests/js/t11354.nim @@ -0,0 +1,20 @@ +discard """ + output: ''' +0 +@[@[0, 1]] +''' +""" + +type + TrackySeq[T] = object + s: seq[T] + pos: int + +proc foobar(ls: var TrackySeq[seq[int]], i: int): var seq[int] = + echo ls.pos # removing this, or making the return explicit works + ls.s[i] + +var foo: TrackySeq[seq[int]] +foo.s.add(@[0]) +foo.foobar(0).add(1) +echo foo.s \ No newline at end of file