From d6f048c4565ee7b3d86c070c07dd555376a52ad2 Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Wed, 20 Oct 2021 00:30:20 +0200 Subject: [PATCH] stage2: make (typeHas)OnePossibleValue return the right value --- src/Sema.zig | 13 ++++++++++--- src/type.zig | 22 ++++++++++++++++++---- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/Sema.zig b/src/Sema.zig index 7a5ac1baa1..d30b46ffba 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -13835,7 +13835,14 @@ fn typeHasOnePossibleValue( return null; } }, - .enum_nonexhaustive => ty = ty.castTag(.enum_nonexhaustive).?.data.tag_ty, + .enum_nonexhaustive => { + const tag_ty = ty.castTag(.enum_nonexhaustive).?.data.tag_ty; + if (tag_ty.cast(Type.Payload.Bits).?.data == 0) { + return Value.initTag(.zero); + } else { + return null; + } + }, .@"union" => { return null; // TODO }, @@ -13859,8 +13866,8 @@ fn typeHasOnePossibleValue( .vector, .array, .array_u8 => { if (ty.arrayLen() == 0) return Value.initTag(.empty_array); - ty = ty.elemType(); - continue; + _ = (try sema.typeHasOnePossibleValue(block, src, ty.elemType())) orelse return null; + return Value.initTag(.the_only_possible_value); }, .inferred_alloc_const => unreachable, diff --git a/src/type.zig b/src/type.zig index 2bf268bc5f..459a9b387f 100644 --- a/src/type.zig +++ b/src/type.zig @@ -3096,6 +3096,14 @@ pub const Type = extern union { } return Value.initTag(.empty_struct_value); }, + .enum_numbered => { + const enum_numbered = ty.castTag(.enum_numbered).?.data; + if (enum_numbered.fields.count() == 1) { + return enum_numbered.values.keys()[0]; + } else { + return null; + } + }, .enum_full => { const enum_full = ty.castTag(.enum_full).?.data; if (enum_full.fields.count() == 1) { @@ -3112,8 +3120,14 @@ pub const Type = extern union { return null; } }, - .enum_nonexhaustive => ty = ty.castTag(.enum_nonexhaustive).?.data.tag_ty, - .enum_numbered => ty = ty.castTag(.enum_numbered).?.data.tag_ty, + .enum_nonexhaustive => { + const tag_ty = ty.castTag(.enum_nonexhaustive).?.data.tag_ty; + if (tag_ty.cast(Type.Payload.Bits).?.data == 0) { + return Value.initTag(.zero); + } else { + return null; + } + }, .@"union" => { return null; // TODO }, @@ -3137,8 +3151,8 @@ pub const Type = extern union { .vector, .array, .array_u8 => { if (ty.arrayLen() == 0) return Value.initTag(.empty_array); - ty = ty.elemType(); - continue; + _ = ty.elemType().onePossibleValue() orelse return null; + return Value.initTag(.the_only_possible_value); }, .inferred_alloc_const => unreachable,