Skip to content

Commit

Permalink
fixes #16671; openarray conversion for object construction (#23618)
Browse files Browse the repository at this point in the history
fixes #16671

related to #18911
  • Loading branch information
ringabout committed May 16, 2024
1 parent 0ba9321 commit b87732b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
10 changes: 7 additions & 3 deletions compiler/ccgexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1497,8 +1497,7 @@ proc handleConstExpr(p: BProc, n: PNode, d: var TLoc): bool =


proc genFieldObjConstr(p: BProc; ty: PType; useTemp, isRef: bool; nField, val, check: PNode; d: var TLoc; r: Rope; info: TLineInfo) =
var tmp2: TLoc = default(TLoc)
tmp2.r = r
var tmp2 = TLoc(r: r)
let field = lookupFieldAgain(p, ty, nField.sym, tmp2.r)
if field.loc.r == "": fillObjectFields(p.module, ty)
if field.loc.r == "": internalError(p.config, info, "genFieldObjConstr")
Expand All @@ -1513,7 +1512,12 @@ proc genFieldObjConstr(p: BProc; ty: PType; useTemp, isRef: bool; nField, val, c
tmp2.k = d.k
tmp2.storage = if isRef: OnHeap else: d.storage
tmp2.lode = val
expr(p, val, tmp2)
if nField.typ.skipTypes(abstractVar).kind in {tyOpenArray, tyVarargs}:
var tmp3 = getTemp(p, val.typ)
expr(p, val, tmp3)
genOpenArrayConv(p, tmp2, tmp3, {})
else:
expr(p, val, tmp2)

proc genObjConstr(p: BProc, e: PNode, d: var TLoc) =
# inheritance in C++ does not allow struct initialization so
Expand Down
22 changes: 22 additions & 0 deletions tests/views/tviews2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,25 @@ block: # bug #15778
doAssert @(reader.read(3)) == @['l', 'l', 'o']
doAssert count == 2

block: # bug #16671
block:
type X = ref object of RootObj
type Y = ref object of X
field: openArray[int]

var s: seq[X]
proc f() =
s.add(Y(field: [1]))

f()

block:
type X = ref object of RootObj
type Y = ref object of X
field: openArray[int]

var s: seq[X]
proc f() =
s.add(Y(field: toOpenArray([1, 2, 3], 0, 1)))

f()

0 comments on commit b87732b

Please sign in to comment.