fixes #6294
This commit is contained in:
parent
2db96d4f7b
commit
21e22624a2
2 changed files with 67 additions and 12 deletions
|
|
@ -2237,20 +2237,22 @@ proc getDefaultValue(p: BProc; typ: PType; info: TLineInfo): Rope =
|
||||||
else:
|
else:
|
||||||
globalError(info, "cannot create null element for: " & $t.kind)
|
globalError(info, "cannot create null element for: " & $t.kind)
|
||||||
|
|
||||||
proc getNullValueAux(p: BProc; obj, cons: PNode, result: var Rope) =
|
proc getNullValueAux(p: BProc; t: PType; obj, cons: PNode, result: var Rope; count: var int) =
|
||||||
case obj.kind
|
case obj.kind
|
||||||
of nkRecList:
|
of nkRecList:
|
||||||
for i in countup(0, sonsLen(obj) - 1): getNullValueAux(p, obj.sons[i], cons, result)
|
for i in countup(0, sonsLen(obj) - 1):
|
||||||
|
getNullValueAux(p, t, obj.sons[i], cons, result, count)
|
||||||
of nkRecCase:
|
of nkRecCase:
|
||||||
getNullValueAux(p, obj.sons[0], cons, result)
|
getNullValueAux(p, t, obj.sons[0], cons, result, count)
|
||||||
for i in countup(1, sonsLen(obj) - 1):
|
for i in countup(1, sonsLen(obj) - 1):
|
||||||
getNullValueAux(p, lastSon(obj.sons[i]), cons, result)
|
getNullValueAux(p, t, lastSon(obj.sons[i]), cons, result, count)
|
||||||
of nkSym:
|
of nkSym:
|
||||||
if not result.isNil: result.add ", "
|
if count > 0: result.add ", "
|
||||||
|
inc count
|
||||||
let field = obj.sym
|
let field = obj.sym
|
||||||
for i in 1..<cons.len:
|
for i in 1..<cons.len:
|
||||||
if cons[i].kind == nkExprColonExpr:
|
if cons[i].kind == nkExprColonExpr:
|
||||||
if cons[i][0].sym.name == field.name:
|
if cons[i][0].sym.name.id == field.name.id:
|
||||||
result.add genConstExpr(p, cons[i][1])
|
result.add genConstExpr(p, cons[i][1])
|
||||||
return
|
return
|
||||||
elif i == field.position:
|
elif i == field.position:
|
||||||
|
|
@ -2261,14 +2263,31 @@ proc getNullValueAux(p: BProc; obj, cons: PNode, result: var Rope) =
|
||||||
else:
|
else:
|
||||||
localError(cons.info, "cannot create null element for: " & $obj)
|
localError(cons.info, "cannot create null element for: " & $obj)
|
||||||
|
|
||||||
|
proc getNullValueAuxT(p: BProc; orig, t: PType; obj, cons: PNode, result: var Rope; count: var int) =
|
||||||
|
var base = t.sons[0]
|
||||||
|
let oldRes = result
|
||||||
|
result.add "{"
|
||||||
|
let oldcount = count
|
||||||
|
if base != nil:
|
||||||
|
base = skipTypes(base, skipPtrs)
|
||||||
|
getNullValueAuxT(p, orig, base, base.n, cons, result, count)
|
||||||
|
elif not isObjLackingTypeField(t) and not p.module.compileToCpp:
|
||||||
|
addf(result, "$1", [genTypeInfo(p.module, orig)])
|
||||||
|
inc count
|
||||||
|
getNullValueAux(p, t, obj, cons, result, count)
|
||||||
|
# do not emit '{}' as that is not valid C:
|
||||||
|
if oldcount == count: result = oldres
|
||||||
|
else: result.add "}"
|
||||||
|
|
||||||
proc genConstObjConstr(p: BProc; n: PNode): Rope =
|
proc genConstObjConstr(p: BProc; n: PNode): Rope =
|
||||||
var length = sonsLen(n)
|
|
||||||
result = nil
|
result = nil
|
||||||
let t = n.typ.skipTypes(abstractInst)
|
let t = n.typ.skipTypes(abstractInst)
|
||||||
if not isObjLackingTypeField(t) and not p.module.compileToCpp:
|
var count = 0
|
||||||
addf(result, "{$1}", [genTypeInfo(p.module, t)])
|
#if not isObjLackingTypeField(t) and not p.module.compileToCpp:
|
||||||
getNullValueAux(p, t.n, n, result)
|
# addf(result, "{$1}", [genTypeInfo(p.module, t)])
|
||||||
result = "{$1}$n" % [result]
|
# inc count
|
||||||
|
getNullValueAuxT(p, t, t, t.n, n, result, count)
|
||||||
|
#result = "{$1}$n" % [result]
|
||||||
|
|
||||||
proc genConstSimpleList(p: BProc, n: PNode): Rope =
|
proc genConstSimpleList(p: BProc, n: PNode): Rope =
|
||||||
var length = sonsLen(n)
|
var length = sonsLen(n)
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,14 @@ discard """
|
||||||
(k: kindA, a: (x: abc, z: [1, 7, 3]), method: ())
|
(k: kindA, a: (x: abc, z: [1, 7, 3]), method: ())
|
||||||
(k: kindA, a: (x: abc, z: [1, 8, 3]), method: ())
|
(k: kindA, a: (x: abc, z: [1, 8, 3]), method: ())
|
||||||
(k: kindA, a: (x: abc, z: [1, 9, 3]), method: ())
|
(k: kindA, a: (x: abc, z: [1, 9, 3]), method: ())
|
||||||
(k: kindA, a: (x: abc, z: [1, 10, 3]), method: ())'''
|
(k: kindA, a: (x: abc, z: [1, 10, 3]), method: ())
|
||||||
|
(x: 123)
|
||||||
|
(x: 123)
|
||||||
|
(z: 89, y: 0, x: 128)
|
||||||
|
(y: 678, x: 123)
|
||||||
|
(y: 678, x: 123)
|
||||||
|
(y: 0, x: 123)
|
||||||
|
(y: 678, x: 123)'''
|
||||||
"""
|
"""
|
||||||
|
|
||||||
type
|
type
|
||||||
|
|
@ -39,3 +46,32 @@ proc main() =
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|
||||||
|
# bug #6294
|
||||||
|
type
|
||||||
|
A = object of RootObj
|
||||||
|
x*: int
|
||||||
|
B = object of A
|
||||||
|
y*: int
|
||||||
|
BS = object of B
|
||||||
|
C = object of BS
|
||||||
|
z*: int
|
||||||
|
# inherited fields are ignored, so no fields are set
|
||||||
|
when true:
|
||||||
|
var
|
||||||
|
o: A
|
||||||
|
o = B(x: 123)
|
||||||
|
echo o
|
||||||
|
o = B(y: 678, x: 123)
|
||||||
|
echo o
|
||||||
|
|
||||||
|
# inherited fields are ignored
|
||||||
|
echo C(x: 128, z: 89) # (y: 0, x: 0)
|
||||||
|
echo B(y: 678, x: 123) # (y: 678, x: 0)
|
||||||
|
echo B(x: 123, y: 678) # (y: 678, x: 0)
|
||||||
|
|
||||||
|
when true:
|
||||||
|
# correct, both with `var` and `let`;
|
||||||
|
var b=B(x: 123)
|
||||||
|
echo b # (y: 0, x: 123)
|
||||||
|
b=B(y: 678, x: 123)
|
||||||
|
echo b # (y: 678, x: 123)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue