From 027cc5013eedce70d5f925e18037e9d1786ebd15 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Mon, 17 Sep 2018 18:52:40 +0200 Subject: [PATCH] Fix error during field access in VM Tuple constructors can't have nkExprColonExpr but may contain NimNodes of that kind. Fixes #4952 --- compiler/vm.nim | 10 +++++++--- tests/vm/t4952.nim | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 tests/vm/t4952.nim diff --git a/compiler/vm.nim b/compiler/vm.nim index e38642de82..faff81697f 100644 --- a/compiler/vm.nim +++ b/compiler/vm.nim @@ -557,11 +557,15 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg = # a = b.c decodeBC(rkNode) let src = regs[rb].node - if src.kind notin {nkEmpty..nkNilLit}: - let n = src.sons[rc + ord(src.kind == nkObjConstr)].skipColon + case src.kind + of nkEmpty..nkNilLit: + stackTrace(c, tos, pc, errNilAccess) + of nkObjConstr: + let n = src.sons[rc + 1].skipColon regs[ra].node = n else: - stackTrace(c, tos, pc, errNilAccess) + let n = src.sons[rc] + regs[ra].node = n of opcWrObj: # a.b = c decodeBC(rkNode) diff --git a/tests/vm/t4952.nim b/tests/vm/t4952.nim new file mode 100644 index 0000000000..fc76fa4dfe --- /dev/null +++ b/tests/vm/t4952.nim @@ -0,0 +1,17 @@ +import macros + +proc doCheck(tree: NimNode) = + let res: tuple[n: NimNode] = (n: tree) + assert: tree.kind == res.n.kind + for sub in tree: + doCheck(sub) + +macro id(body: untyped): untyped = + doCheck(body) + +id(foo((i: int))) + +static: + let tree = newTree(nnkExprColonExpr) + let t = (n: tree) + assert: t.n.kind == tree.kind