@@ -208,51 +208,29 @@ optionalt<exprt> expr_initializert<nondet>::expr_initializer_rec(
208208 }
209209 else if (type_id==ID_union)
210210 {
211- const union_typet::componentst &components=
212- to_union_type (type).components ();
211+ const union_typet &union_type = to_union_type (type);
213212
214- union_typet::componentt component;
215- bool found=false ;
216- mp_integer component_size=0 ;
217-
218- // we need to find the largest member
219-
220- for (const auto &c : components)
221- {
222- // skip methods
223- if (c.type ().id () == ID_code)
224- continue ;
225-
226- auto bits = pointer_offset_bits (c.type (), ns);
227-
228- if (bits.has_value () && *bits > component_size)
229- {
230- component = c;
231- found=true ;
232- component_size = *bits;
233- }
234- }
235-
236- if (!found)
213+ if (union_type.components ().empty ())
237214 {
238- // stupid empty union
239- union_exprt value (irep_idt (), nil_exprt (), type);
215+ empty_union_exprt value{type};
240216 value.add_source_location () = source_location;
241217 return std::move (value);
242218 }
243- else
244- {
245- auto component_value =
246- expr_initializer_rec (component.type (), source_location);
247219
248- if (!component_value.has_value ())
249- return {};
220+ const auto &widest_member = union_type.find_widest_union_component (ns);
221+ if (!widest_member.has_value ())
222+ return {};
250223
251- union_exprt value (component. get_name (), * component_value, type);
252- value. add_source_location () = source_location;
224+ auto component_value =
225+ expr_initializer_rec (widest_member-> first . type (), source_location) ;
253226
254- return std::move (value);
255- }
227+ if (!component_value.has_value ())
228+ return {};
229+
230+ union_exprt value{widest_member->first .get_name (), *component_value, type};
231+ value.add_source_location () = source_location;
232+
233+ return std::move (value);
256234 }
257235 else if (type_id==ID_c_enum_tag)
258236 {
0 commit comments