only generate first field for default value of union (#24303)

fixes #20653

(cherry picked from commit 6df050d6d2)
This commit is contained in:
metagn
2024-10-14 18:07:57 +03:00
committed by narimiran
parent 5cbc7a6d1b
commit f45ca4fdf4
2 changed files with 36 additions and 0 deletions

View File

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

View 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)