mirror of
https://github.com/nim-lang/Nim.git
synced 2026-01-01 10:52:14 +00:00
only generate first field for default value of union (#24303)
fixes #20653
(cherry picked from commit 6df050d6d2)
This commit is contained in:
@@ -3312,8 +3312,12 @@ proc getNullValueAux(p: BProc; t: PType; obj, constOrNil: PNode,
|
||||
isConst: bool, info: TLineInfo) =
|
||||
case obj.kind
|
||||
of nkRecList:
|
||||
let isUnion = tfUnion in t.flags
|
||||
for it in obj.sons:
|
||||
getNullValueAux(p, t, it, constOrNil, result, count, isConst, info)
|
||||
if isUnion:
|
||||
# generate only 1 field for default value of union
|
||||
return
|
||||
of nkRecCase:
|
||||
getNullValueAux(p, t, obj[0], constOrNil, result, count, isConst, info)
|
||||
var res = ""
|
||||
|
||||
32
tests/ccgbugs/tunioninit.nim
Normal file
32
tests/ccgbugs/tunioninit.nim
Normal file
@@ -0,0 +1,32 @@
|
||||
# issue #20653
|
||||
|
||||
type
|
||||
EmptySeq* {.bycopy.} = object
|
||||
|
||||
ChoiceWithEmptySeq_d* {.bycopy.} = object
|
||||
a*: bool
|
||||
|
||||
INNER_C_UNION* {.bycopy, union.} = object
|
||||
a*: char
|
||||
b*: EmptySeq
|
||||
c*: byte
|
||||
d*: ChoiceWithEmptySeq_d
|
||||
|
||||
ChoiceWithEmptySeq_selection* = enum
|
||||
ChoiceWithEmptySeq_NONE,
|
||||
ChoiceWithEmptySeq_a_PRESENT,
|
||||
ChoiceWithEmptySeq_b_PRESENT,
|
||||
ChoiceWithEmptySeq_c_PRESENT,
|
||||
ChoiceWithEmptySeq_d_PRESENT
|
||||
|
||||
ChoiceWithEmptySeq* {.bycopy.} = object
|
||||
kind*: ChoiceWithEmptySeq_selection
|
||||
u*: INNER_C_UNION
|
||||
|
||||
Og_Context* {.bycopy.} = object
|
||||
state*: int
|
||||
init_done*: bool
|
||||
ch*: ChoiceWithEmptySeq
|
||||
em*: EmptySeq
|
||||
|
||||
var context* : Og_Context = Og_Context(init_done: false)
|
||||
Reference in New Issue
Block a user