From f47081b3004eb1bed538a74c4902b51256925fea Mon Sep 17 00:00:00 2001 From: yglukhov Date: Thu, 11 Jun 2015 18:32:22 +0300 Subject: [PATCH 1/2] Fixed addr of bracket expression. Fixes #2148. --- compiler/jsgen.nim | 19 +++++++++++-------- tests/js/taddr.nim | 22 ++++++++++++++++++++++ 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/compiler/jsgen.nim b/compiler/jsgen.nim index 1f82306d26..bb5f6812b8 100644 --- a/compiler/jsgen.nim +++ b/compiler/jsgen.nim @@ -976,14 +976,17 @@ proc genAddr(p: PProc, n: PNode, r: var TCompRes) = genFieldAccess(p, n.sons[0], r) of nkBracketExpr: var ty = skipTypes(n.sons[0].typ, abstractVarRange) - if ty.kind in {tyRef, tyPtr}: ty = skipTypes(ty.lastSon, abstractVarRange) - case ty.kind - of tyArray, tyArrayConstr, tyOpenArray, tySequence, tyString, tyCString, - tyVarargs, tyChar: - genArrayAddr(p, n.sons[0], r) - of tyTuple: - genFieldAddr(p, n.sons[0], r) - else: internalError(n.sons[0].info, "expr(nkBracketExpr, " & $ty.kind & ')') + if ty.kind in MappedToObject: + gen(p, n.sons[0], r) + else: + let kindOfIndexedExpr = n.sons[0].sons[0].typ.kind + case kindOfIndexedExpr + of tyArray, tyArrayConstr, tyOpenArray, tySequence, tyString, tyCString, + tyVarargs: + genArrayAddr(p, n.sons[0], r) + of tyTuple: + genFieldAddr(p, n.sons[0], r) + else: internalError(n.sons[0].info, "expr(nkBracketExpr, " & $kindOfIndexedExpr & ')') else: internalError(n.sons[0].info, "genAddr") proc genProcForSymIfNeeded(p: PProc, s: PSym) = diff --git a/tests/js/taddr.nim b/tests/js/taddr.nim index f9c89fbc3c..e5c8d08810 100644 --- a/tests/js/taddr.nim +++ b/tests/js/taddr.nim @@ -40,3 +40,25 @@ indexAddr[] = 'd' doAssert indexAddr[] == 'd' doAssert obj.s == "lodem ipsum dolor sit amet" + +# Bug #2148 +var x: array[2, int] +var y = addr x[1] + +y[] = 12 +doAssert(x[1] == 12) + +type + Foo = object + bar: int + +var foo: array[2, Foo] +var z = addr foo[1] + +z[].bar = 12345 +doAssert(foo[1].bar == 12345) + +var t : tuple[a, b: int] +var pt = addr t[1] +pt[] = 123 +doAssert(t.b == 123) From f14ca63417136a4d9fedb4ce776bdea958c9faef Mon Sep 17 00:00:00 2001 From: yglukhov Date: Fri, 12 Jun 2015 00:06:01 +0300 Subject: [PATCH 2/2] Singing and dancing with skipTypes. --- compiler/jsgen.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/jsgen.nim b/compiler/jsgen.nim index bb5f6812b8..6724a81001 100644 --- a/compiler/jsgen.nim +++ b/compiler/jsgen.nim @@ -979,7 +979,7 @@ proc genAddr(p: PProc, n: PNode, r: var TCompRes) = if ty.kind in MappedToObject: gen(p, n.sons[0], r) else: - let kindOfIndexedExpr = n.sons[0].sons[0].typ.kind + let kindOfIndexedExpr = skipTypes(n.sons[0].sons[0].typ, abstractVarRange).kind case kindOfIndexedExpr of tyArray, tyArrayConstr, tyOpenArray, tySequence, tyString, tyCString, tyVarargs: