authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2021-10-20 00:30:20+02:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2021-10-20 03:44:02+02:00
logd6f048c4565ee7b3d86c070c07dd555376a52ad2
tree4d2eef96e5bd32163c14f4c9fa1adba7363c8429
parent7b97f6792fdc4f2774f109ec016ad19bf341e768

stage2: make (typeHas)OnePossibleValue return the right value


2 files changed, 28 insertions(+), 7 deletions(-)

src/Sema.zig+10-3
...@@ -13835,7 +13835,14 @@ fn typeHasOnePossibleValue(...@@ -13835,7 +13835,14 @@ fn typeHasOnePossibleValue(
13835 return null;13835 return null;
13836 }13836 }
13837 },13837 },
13838 .enum_nonexhaustive => ty = ty.castTag(.enum_nonexhaustive).?.data.tag_ty,13838 .enum_nonexhaustive => {
13839 const tag_ty = ty.castTag(.enum_nonexhaustive).?.data.tag_ty;
13840 if (tag_ty.cast(Type.Payload.Bits).?.data == 0) {
13841 return Value.initTag(.zero);
13842 } else {
13843 return null;
13844 }
13845 },
13839 .@"union" => {13846 .@"union" => {
13840 return null; // TODO13847 return null; // TODO
13841 },13848 },
...@@ -13859,8 +13866,8 @@ fn typeHasOnePossibleValue(...@@ -13859,8 +13866,8 @@ fn typeHasOnePossibleValue(
13859 .vector, .array, .array_u8 => {13866 .vector, .array, .array_u8 => {
13860 if (ty.arrayLen() == 0)13867 if (ty.arrayLen() == 0)
13861 return Value.initTag(.empty_array);13868 return Value.initTag(.empty_array);
13862 ty = ty.elemType();13869 _ = (try sema.typeHasOnePossibleValue(block, src, ty.elemType())) orelse return null;
13863 continue;13870 return Value.initTag(.the_only_possible_value);
13864 },13871 },
1386513872
13866 .inferred_alloc_const => unreachable,13873 .inferred_alloc_const => unreachable,
src/type.zig+18-4
...@@ -3096,6 +3096,14 @@ pub const Type = extern union {...@@ -3096,6 +3096,14 @@ pub const Type = extern union {
3096 }3096 }
3097 return Value.initTag(.empty_struct_value);3097 return Value.initTag(.empty_struct_value);
3098 },3098 },
3099 .enum_numbered => {
3100 const enum_numbered = ty.castTag(.enum_numbered).?.data;
3101 if (enum_numbered.fields.count() == 1) {
3102 return enum_numbered.values.keys()[0];
3103 } else {
3104 return null;
3105 }
3106 },
3099 .enum_full => {3107 .enum_full => {
3100 const enum_full = ty.castTag(.enum_full).?.data;3108 const enum_full = ty.castTag(.enum_full).?.data;
3101 if (enum_full.fields.count() == 1) {3109 if (enum_full.fields.count() == 1) {
...@@ -3112,8 +3120,14 @@ pub const Type = extern union {...@@ -3112,8 +3120,14 @@ pub const Type = extern union {
3112 return null;3120 return null;
3113 }3121 }
3114 },3122 },
3115 .enum_nonexhaustive => ty = ty.castTag(.enum_nonexhaustive).?.data.tag_ty,3123 .enum_nonexhaustive => {
3116 .enum_numbered => ty = ty.castTag(.enum_numbered).?.data.tag_ty,3124 const tag_ty = ty.castTag(.enum_nonexhaustive).?.data.tag_ty;
3125 if (tag_ty.cast(Type.Payload.Bits).?.data == 0) {
3126 return Value.initTag(.zero);
3127 } else {
3128 return null;
3129 }
3130 },
3117 .@"union" => {3131 .@"union" => {
3118 return null; // TODO3132 return null; // TODO
3119 },3133 },
...@@ -3137,8 +3151,8 @@ pub const Type = extern union {...@@ -3137,8 +3151,8 @@ pub const Type = extern union {
3137 .vector, .array, .array_u8 => {3151 .vector, .array, .array_u8 => {
3138 if (ty.arrayLen() == 0)3152 if (ty.arrayLen() == 0)
3139 return Value.initTag(.empty_array);3153 return Value.initTag(.empty_array);
3140 ty = ty.elemType();3154 _ = ty.elemType().onePossibleValue() orelse return null;
3141 continue;3155 return Value.initTag(.the_only_possible_value);
3142 },3156 },
31433157
3144 .inferred_alloc_const => unreachable,3158 .inferred_alloc_const => unreachable,