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(
1383513835 return null;
1383613836 }
1383713837 },
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 },
1383913846 .@"union" => {
1384013847 return null; // TODO
1384113848 },
......@@ -13859,8 +13866,8 @@ fn typeHasOnePossibleValue(
1385913866 .vector, .array, .array_u8 => {
1386013867 if (ty.arrayLen() == 0)
1386113868 return Value.initTag(.empty_array);
13862 ty = ty.elemType();
13863 continue;
13869 _ = (try sema.typeHasOnePossibleValue(block, src, ty.elemType())) orelse return null;
13870 return Value.initTag(.the_only_possible_value);
1386413871 },
1386513872
1386613873 .inferred_alloc_const => unreachable,
src/type.zig+18-4
......@@ -3096,6 +3096,14 @@ pub const Type = extern union {
30963096 }
30973097 return Value.initTag(.empty_struct_value);
30983098 },
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 },
30993107 .enum_full => {
31003108 const enum_full = ty.castTag(.enum_full).?.data;
31013109 if (enum_full.fields.count() == 1) {
......@@ -3112,8 +3120,14 @@ pub const Type = extern union {
31123120 return null;
31133121 }
31143122 },
3115 .enum_nonexhaustive => ty = ty.castTag(.enum_nonexhaustive).?.data.tag_ty,
3116 .enum_numbered => ty = ty.castTag(.enum_numbered).?.data.tag_ty,
3123 .enum_nonexhaustive => {
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 },
31173131 .@"union" => {
31183132 return null; // TODO
31193133 },
......@@ -3137,8 +3151,8 @@ pub const Type = extern union {
31373151 .vector, .array, .array_u8 => {
31383152 if (ty.arrayLen() == 0)
31393153 return Value.initTag(.empty_array);
3140 ty = ty.elemType();
3141 continue;
3154 _ = ty.elemType().onePossibleValue() orelse return null;
3155 return Value.initTag(.the_only_possible_value);
31423156 },
31433157
31443158 .inferred_alloc_const => unreachable,