From f591b447a7a988ed929fb0da50223395ba0e0ada Mon Sep 17 00:00:00 2001 From: Araq Date: Tue, 24 Nov 2020 11:32:44 +0100 Subject: [PATCH] fixes #16069; refs https://github.com/nim-lang/RFCs/issues/257 [backport:1.2] [backport:1.4] --- compiler/ast.nim | 2 +- compiler/ccgexprs.nim | 1 + compiler/semfold.nim | 1 + compiler/transf.nim | 5 +++++ compiler/trees.nim | 2 +- tests/vm/tconstobj.nim | 20 +++++++++++++++++++- 6 files changed, 28 insertions(+), 3 deletions(-) diff --git a/compiler/ast.nim b/compiler/ast.nim index c75a149c02e4f..af86d51ddfa88 100644 --- a/compiler/ast.nim +++ b/compiler/ast.nim @@ -1122,7 +1122,7 @@ proc safeLen*(n: PNode): int {.inline.} = proc safeArrLen*(n: PNode): int {.inline.} = ## works for array-like objects (strings passed as openArray in VM). - if n.kind in {nkStrLit..nkTripleStrLit}:result = n.strVal.len + if n.kind in {nkStrLit..nkTripleStrLit}: result = n.strVal.len elif n.kind in {nkNone..nkFloat128Lit}: result = 0 else: result = n.len diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim index ec28eb1ec3a1d..ef3ceae62f9a1 100644 --- a/compiler/ccgexprs.nim +++ b/compiler/ccgexprs.nim @@ -881,6 +881,7 @@ proc genFieldCheck(p: BProc, e: PNode, obj: Rope, field: PSym) = [rdLoc(test), strLit, raiseInstr(p)]) proc genCheckedRecordField(p: BProc, e: PNode, d: var TLoc) = + assert e[0].kind == nkDotExpr if optFieldCheck in p.options: var a: TLoc genRecordFieldAux(p, e[0], d, a) diff --git a/compiler/semfold.nim b/compiler/semfold.nim index b92895b1928a7..958d8c14e7f52 100644 --- a/compiler/semfold.nim +++ b/compiler/semfold.nim @@ -685,6 +685,7 @@ proc getConstExpr(m: PSym, n: PNode; idgen: IdGenerator; g: ModuleGraph): PNode of nkBracketExpr: result = foldArrayAccess(m, n, idgen, g) of nkDotExpr: result = foldFieldAccess(m, n, idgen, g) of nkCheckedFieldExpr: + assert n[0].kind == nkDotExpr result = foldFieldAccess(m, n[0], idgen, g) of nkStmtListExpr: var i = 0 diff --git a/compiler/transf.nim b/compiler/transf.nim index 42a78c8e31aa0..ae233d3224ff2 100644 --- a/compiler/transf.nim +++ b/compiler/transf.nim @@ -1002,6 +1002,11 @@ proc transform(c: PTransf, n: PNode): PNode = return n of nkExceptBranch: result = transformExceptBranch(c, n) + of nkCheckedFieldExpr: + result = transformSons(c, n) + if result[0].kind != nkDotExpr: + # simplfied beyond a dot expression --> simplify further. + result = result[0] else: result = transformSons(c, n) when false: diff --git a/compiler/trees.nim b/compiler/trees.nim index 63459babe0dc4..05c0605953736 100644 --- a/compiler/trees.nim +++ b/compiler/trees.nim @@ -211,6 +211,6 @@ proc stupidStmtListExpr*(n: PNode): bool = proc dontInlineConstant*(orig, cnst: PNode): bool {.inline.} = # symbols that expand to a complex constant (array, etc.) should not be # inlined, unless it's the empty array: - result = orig.kind == nkSym and + result = orig.kind != cnst.kind and cnst.kind in {nkCurly, nkPar, nkTupleConstr, nkBracket, nkObjConstr} and cnst.len > ord(cnst.kind == nkObjConstr) diff --git a/tests/vm/tconstobj.nim b/tests/vm/tconstobj.nim index 93d0e1d7d07fc..7dc20a0ba1fb8 100644 --- a/tests/vm/tconstobj.nim +++ b/tests/vm/tconstobj.nim @@ -3,6 +3,7 @@ discard """ (name: "hello") (-1, 0) (FirstName: "James", LastName: "Franco") +[1, 2, 3] ''' """ @@ -74,4 +75,21 @@ static: # issue #11861 static: # issue #15662 proc a(T: typedesc) = echo T.type - a((int, int)) \ No newline at end of file + a((int, int)) + +# bug #16069 +type + E = enum + val1, val2 + Obj = object + case k: E + of val1: + x: array[3, int] + of val2: + y: uint32 + +const + foo = [1, 2, 3] + arr = Obj(k: val1, x: foo) + +echo arr.x