Commit d6f048c456

Robin Voetter <robin@voetter.nl>
2021-10-20 00:30:20
stage2: make (typeHas)OnePossibleValue return the right value
1 parent 7b97f67
Changed files (2)
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,
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,