From f45ca4fdf4b5f3ed8b87b7473372d91f79760ecf Mon Sep 17 00:00:00 2001 From: metagn Date: Mon, 14 Oct 2024 18:07:57 +0300 Subject: [PATCH] only generate first field for default value of union (#24303) fixes #20653 (cherry picked from commit 6df050d6d2e6e0b62aff3bba093149e15c6e1060) --- compiler/ccgexprs.nim | 4 ++++ tests/ccgbugs/tunioninit.nim | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 tests/ccgbugs/tunioninit.nim diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim index 9e647a6e30..24ef03201a 100644 --- a/compiler/ccgexprs.nim +++ b/compiler/ccgexprs.nim @@ -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 = "" diff --git a/tests/ccgbugs/tunioninit.nim b/tests/ccgbugs/tunioninit.nim new file mode 100644 index 0000000000..a58c8d27f6 --- /dev/null +++ b/tests/ccgbugs/tunioninit.nim @@ -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)