From 51b1083d66b29d110c8cf60b59052170dd34a95f Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Fri, 4 Nov 2022 18:38:42 +0200 Subject: stage2: fix onePossibleValue of empty unions and enums Closes #13402 --- src/type.zig | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'src/type.zig') diff --git a/src/type.zig b/src/type.zig index a12733cbcb..605e4396c0 100644 --- a/src/type.zig +++ b/src/type.zig @@ -5015,22 +5015,22 @@ pub const Type = extern union { if (enum_full.tag_ty.hasRuntimeBits()) { return null; } - if (enum_full.fields.count() == 1) { - if (enum_full.values.count() == 0) { - return Value.zero; + switch (enum_full.fields.count()) { + 0 => return Value.initTag(.unreachable_value), + 1 => if (enum_full.values.count() == 0) { + return Value.zero; // auto-numbered } else { return enum_full.values.keys()[0]; - } - } else { - return null; + }, + else => return null, } }, .enum_simple => { const enum_simple = ty.castTag(.enum_simple).?.data; - if (enum_simple.fields.count() == 1) { - return Value.zero; - } else { - return null; + switch (enum_simple.fields.count()) { + 0 => return Value.initTag(.unreachable_value), + 1 => return Value.zero, + else => return null, } }, .enum_nonexhaustive => { @@ -5044,6 +5044,7 @@ pub const Type = extern union { .@"union", .union_safety_tagged, .union_tagged => { const union_obj = ty.cast(Payload.Union).?.data; const tag_val = union_obj.tag_ty.onePossibleValue() orelse return null; + if (union_obj.fields.count() == 0) return Value.initTag(.unreachable_value); const only_field = union_obj.fields.values()[0]; const val_val = only_field.ty.onePossibleValue() orelse return null; _ = tag_val; -- cgit v1.2.3