* fixes #20715; range[a..b] inside object variant fails * step one fix * better fix * fixes private fields * mistake
This commit is contained in:
parent
a0653ae71a
commit
39f925b95d
2 changed files with 36 additions and 5 deletions
|
|
@ -599,15 +599,16 @@ proc defaultFieldsForTheUninitialized(c: PContext, recNode: PNode): seq[PNode] =
|
||||||
of nkRecCase:
|
of nkRecCase:
|
||||||
let discriminator = recNode[0]
|
let discriminator = recNode[0]
|
||||||
var selectedBranch: int
|
var selectedBranch: int
|
||||||
let defaultValue = discriminator.sym.ast
|
var defaultValue = discriminator.sym.ast
|
||||||
if defaultValue == nil:
|
if defaultValue == nil:
|
||||||
# None of the branches were explicitly selected by the user and no value
|
# None of the branches were explicitly selected by the user and no value
|
||||||
# was given to the discrimator. We can assume that it will be initialized
|
# was given to the discrimator. We can assume that it will be initialized
|
||||||
# to zero and this will select a particular branch as a result:
|
# to zero and this will select a particular branch as a result:
|
||||||
selectedBranch = recNode.pickCaseBranchIndex newIntNode(nkIntLit#[c.graph]#, 0)
|
defaultValue = newIntNode(nkIntLit#[c.graph]#, 0)
|
||||||
else: # Try to use default value
|
defaultValue.typ = discriminator.typ
|
||||||
selectedBranch = recNode.pickCaseBranchIndex defaultValue
|
selectedBranch = recNode.pickCaseBranchIndex defaultValue
|
||||||
result.add newTree(nkExprColonExpr, discriminator, defaultValue)
|
defaultValue.flags.incl nfUseDefaultField
|
||||||
|
result.add newTree(nkExprColonExpr, discriminator, defaultValue)
|
||||||
result.add defaultFieldsForTheUninitialized(c, recNode[selectedBranch][^1])
|
result.add defaultFieldsForTheUninitialized(c, recNode[selectedBranch][^1])
|
||||||
of nkSym:
|
of nkSym:
|
||||||
let field = recNode.sym
|
let field = recNode.sym
|
||||||
|
|
|
||||||
|
|
@ -426,6 +426,36 @@ template main {.dirty.} =
|
||||||
let x = default(A)
|
let x = default(A)
|
||||||
doAssert $x == "(d: Uninitialized DateTime)"
|
doAssert $x == "(d: Uninitialized DateTime)"
|
||||||
|
|
||||||
|
block: # bug #20715
|
||||||
|
block:
|
||||||
|
type
|
||||||
|
Foo = enum
|
||||||
|
A
|
||||||
|
B
|
||||||
|
|
||||||
|
Bar = object
|
||||||
|
case foo: Foo
|
||||||
|
of A:
|
||||||
|
t: range[-1..2]
|
||||||
|
else: discard
|
||||||
|
|
||||||
|
var d = default(Bar)
|
||||||
|
doAssert d.t == -1
|
||||||
|
|
||||||
|
block:
|
||||||
|
type
|
||||||
|
Foo = enum
|
||||||
|
A
|
||||||
|
B
|
||||||
|
|
||||||
|
Bar = object
|
||||||
|
case foo: Foo
|
||||||
|
of A:
|
||||||
|
t: range[0..2]
|
||||||
|
else: discard
|
||||||
|
|
||||||
|
var d = default(Bar)
|
||||||
|
doAssert d.t == 0
|
||||||
|
|
||||||
static: main()
|
static: main()
|
||||||
main()
|
main()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue