Skip to content

Commit

Permalink
fixes #20715; range[a..b] inside object variant fails (#20716)
Browse files Browse the repository at this point in the history
* fixes #20715; range[a..b] inside object variant fails

* step one fix

* better fix

* fixes private fields

* mistake
  • Loading branch information
ringabout authored Oct 31, 2022
1 parent a0653ae commit 39f925b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
11 changes: 6 additions & 5 deletions compiler/sem.nim
Original file line number Diff line number Diff line change
Expand Up @@ -599,15 +599,16 @@ proc defaultFieldsForTheUninitialized(c: PContext, recNode: PNode): seq[PNode] =
of nkRecCase:
let discriminator = recNode[0]
var selectedBranch: int
let defaultValue = discriminator.sym.ast
var defaultValue = discriminator.sym.ast
if defaultValue == nil:
# 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
# to zero and this will select a particular branch as a result:
selectedBranch = recNode.pickCaseBranchIndex newIntNode(nkIntLit#[c.graph]#, 0)
else: # Try to use default value
selectedBranch = recNode.pickCaseBranchIndex defaultValue
result.add newTree(nkExprColonExpr, discriminator, defaultValue)
defaultValue = newIntNode(nkIntLit#[c.graph]#, 0)
defaultValue.typ = discriminator.typ
selectedBranch = recNode.pickCaseBranchIndex defaultValue
defaultValue.flags.incl nfUseDefaultField
result.add newTree(nkExprColonExpr, discriminator, defaultValue)
result.add defaultFieldsForTheUninitialized(c, recNode[selectedBranch][^1])
of nkSym:
let field = recNode.sym
Expand Down
30 changes: 30 additions & 0 deletions tests/objects/tobject_default_value.nim
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,36 @@ template main {.dirty.} =
let x = default(A)
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()
main()

0 comments on commit 39f925b

Please sign in to comment.