fixes #16671; openarray conversion for object construction (#23618)

fixes #16671

related to https://github.com/nim-lang/Nim/pull/18911
This commit is contained in:
ringabout
2024-05-17 05:27:08 +08:00
committed by GitHub
parent 0ba932132e
commit b87732b5f1
2 changed files with 29 additions and 3 deletions

View File

@@ -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")
@@ -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

View File

@@ -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()