diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2015-06-13 20:31:47 +0200 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2015-06-13 20:31:47 +0200 |
commit | 2782cddb56b2f3b56893915bc227839dc3d246c0 (patch) | |
tree | 3521e2d09faded17206933e899473d84da8490a9 | |
parent | 35e922d18e0d24eb7c537debb85b8017eae2f186 (diff) | |
parent | f14ca63417136a4d9fedb4ce776bdea958c9faef (diff) | |
download | Nim-2782cddb56b2f3b56893915bc227839dc3d246c0.tar.gz |
Merge pull request #2903 from yglukhov/js-bracket-addr
Fixed addr of bracket expression. Fixes #2148.
-rw-r--r-- | compiler/jsgen.nim | 19 | ||||
-rw-r--r-- | tests/js/taddr.nim | 22 |
2 files changed, 33 insertions, 8 deletions
diff --git a/compiler/jsgen.nim b/compiler/jsgen.nim index 1fd9aa937..f6ec256d2 100644 --- a/compiler/jsgen.nim +++ b/compiler/jsgen.nim @@ -946,14 +946,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 = skipTypes(n.sons[0].sons[0].typ, abstractVarRange).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 f9c89fbc3..e5c8d0881 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) |