Skip to content

Commit

Permalink
fixes #16069; [backport:1.2] [backport:1.4] (#16115)
Browse files Browse the repository at this point in the history
* fixes #16069; refs nim-lang/RFCs#257 [backport:1.2] [backport:1.4]

* make tests green again

(cherry picked from commit d306a04)
  • Loading branch information
Araq authored and narimiran committed Nov 26, 2020
1 parent c2982d5 commit 0888ac4
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion compiler/ast.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,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

Expand Down
1 change: 1 addition & 0 deletions compiler/ccgexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,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)
Expand Down
3 changes: 3 additions & 0 deletions compiler/semfold.nim
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,9 @@ proc getConstExpr(m: PSym, n: PNode; g: ModuleGraph): PNode =
result.typ = n.typ
of nkBracketExpr: result = foldArrayAccess(m, n, g)
of nkDotExpr: result = foldFieldAccess(m, n, g)
of nkCheckedFieldExpr:
assert n[0].kind == nkDotExpr
result = foldFieldAccess(m, n[0], g)
of nkStmtListExpr:
var i = 0
while i <= n.len - 2:
Expand Down
5 changes: 5 additions & 0 deletions compiler/transf.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,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:
Expand Down
2 changes: 1 addition & 1 deletion compiler/trees.nim
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,6 @@ proc extractRange*(k: TNodeKind, n: PNode, a, b: int): PNode =
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)
18 changes: 18 additions & 0 deletions tests/vm/tconstobj.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ discard """
(name: "hello")
(-1, 0)
(FirstName: "James", LastName: "Franco")
[1, 2, 3]
'''
"""

Expand Down Expand Up @@ -70,3 +71,20 @@ static: # issue #11861
var ifb2: InheritedFromBase
initBase(ifb2)
doAssert(ifb2.txt == "Initialized string from base")

# 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

0 comments on commit 0888ac4

Please sign in to comment.