authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-12-05 18:59:55+01:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-12-05 20:14:04+01:00
log97f36d93f4766a8d225d19665309e825cdef72bf
treea2bf281da218754ddd558cbe0fc03d2e741ef145
parent0f7954831a881c899002eaf8c203aec35dedeeee

stage1: Don't fold single-element enums too aggressively

When the tag type is not a zero-sized type (eg. `enum(i32)`) we absolutely need to avoid constant-folding this values. Doing so masked any invalid input and, since the folding was not even applied consistently, introduced some hard to catch errors.

1 files changed, 29 insertions(+), 19 deletions(-)

src/stage1/ir.cpp+29-19
......@@ -14186,6 +14186,18 @@ static ZigType *ir_resolve_union_tag_type(IrAnalyze *ira, AstNode *source_node,
1418614186 }
1418714187}
1418814188
14189static bool can_fold_enum_type(ZigType *ty) {
14190 assert(ty->id == ZigTypeIdEnum);
14191 // We can fold the enum type (and avoid any check, be it at runtime or at
14192 // compile time) iff it has only a single element and its tag type is
14193 // zero-sized.
14194 ZigType *tag_int_type = ty->data.enumeration.tag_int_type;
14195 return ty->data.enumeration.layout == ContainerLayoutAuto &&
14196 ty->data.enumeration.src_field_count == 1 &&
14197 !ty->data.enumeration.non_exhaustive &&
14198 (tag_int_type->id == ZigTypeIdInt && tag_int_type->data.integral.bit_count == 0);
14199}
14200
1418914201static IrInstGen *ir_analyze_enum_to_int(IrAnalyze *ira, IrInst *source_instr, IrInstGen *target) {
1419014202 Error err;
1419114203
......@@ -14214,10 +14226,7 @@ static IrInstGen *ir_analyze_enum_to_int(IrAnalyze *ira, IrInst *source_instr, I
1421414226 assert(tag_type->id == ZigTypeIdInt || tag_type->id == ZigTypeIdComptimeInt);
1421514227
1421614228 // If there is only one possible tag, then we know at comptime what it is.
14217 if (enum_type->data.enumeration.layout == ContainerLayoutAuto &&
14218 enum_type->data.enumeration.src_field_count == 1 &&
14219 !enum_type->data.enumeration.non_exhaustive)
14220 {
14229 if (can_fold_enum_type(enum_type)) {
1422114230 IrInstGen *result = ir_const(ira, source_instr, tag_type);
1422214231 init_const_bigint(result->value, tag_type,
1422314232 &enum_type->data.enumeration.fields[0].value);
......@@ -14255,10 +14264,7 @@ static IrInstGen *ir_analyze_union_to_tag(IrAnalyze *ira, IrInst* source_instr,
1425514264 }
1425614265
1425714266 // If there is only 1 possible tag, then we know at comptime what it is.
14258 if (wanted_type->data.enumeration.layout == ContainerLayoutAuto &&
14259 wanted_type->data.enumeration.src_field_count == 1 &&
14260 !wanted_type->data.enumeration.non_exhaustive)
14261 {
14267 if (can_fold_enum_type(wanted_type)) {
1426214268 IrInstGen *result = ir_const(ira, source_instr, wanted_type);
1426314269 result->value->special = ConstValSpecialStatic;
1426414270 result->value->type = wanted_type;
......@@ -24039,7 +24045,8 @@ static IrInstGen *ir_analyze_instruction_switch_target(IrAnalyze *ira,
2403924045 bigint_init_bigint(&result->value->data.x_enum_tag, &pointee_val->data.x_union.tag);
2404024046 return result;
2404124047 }
24042 if (tag_type->data.enumeration.src_field_count == 1 && !tag_type->data.enumeration.non_exhaustive) {
24048
24049 if (can_fold_enum_type(tag_type)) {
2404324050 IrInstGen *result = ir_const(ira, &switch_target_instruction->base.base, tag_type);
2404424051 TypeEnumField *only_field = &tag_type->data.enumeration.fields[0];
2404524052 bigint_init_bigint(&result->value->data.x_enum_tag, &only_field->value);
......@@ -24054,7 +24061,8 @@ static IrInstGen *ir_analyze_instruction_switch_target(IrAnalyze *ira,
2405424061 case ZigTypeIdEnum: {
2405524062 if ((err = type_resolve(ira->codegen, target_type, ResolveStatusZeroBitsKnown)))
2405624063 return ira->codegen->invalid_inst_gen;
24057 if (target_type->data.enumeration.src_field_count == 1 && !target_type->data.enumeration.non_exhaustive) {
24064
24065 if (can_fold_enum_type(target_type)) {
2405824066 TypeEnumField *only_field = &target_type->data.enumeration.fields[0];
2405924067 IrInstGen *result = ir_const(ira, &switch_target_instruction->base.base, target_type);
2406024068 bigint_init_bigint(&result->value->data.x_enum_tag, &only_field->value);
......@@ -24789,7 +24797,9 @@ static IrInstGen *ir_analyze_instruction_enum_tag_name(IrAnalyze *ira, IrInstSrc
2478924797 if (type_is_invalid(target->value->type))
2479024798 return ira->codegen->invalid_inst_gen;
2479124799
24792 if (target->value->type->id == ZigTypeIdEnumLiteral) {
24800 ZigType *target_type = target->value->type;
24801
24802 if (target_type->id == ZigTypeIdEnumLiteral) {
2479324803 IrInstGen *result = ir_const(ira, &instruction->base.base, nullptr);
2479424804 Buf *field_name = target->value->data.x_enum_literal;
2479524805 ZigValue *array_val = create_const_str_lit(ira->codegen, field_name)->data.x_ptr.data.ref.pointee;
......@@ -24797,21 +24807,21 @@ static IrInstGen *ir_analyze_instruction_enum_tag_name(IrAnalyze *ira, IrInstSrc
2479724807 return result;
2479824808 }
2479924809
24800 if (target->value->type->id == ZigTypeIdUnion) {
24810 if (target_type->id == ZigTypeIdUnion) {
2480124811 target = ir_analyze_union_tag(ira, &instruction->base.base, target, instruction->base.is_gen);
2480224812 if (type_is_invalid(target->value->type))
2480324813 return ira->codegen->invalid_inst_gen;
24814 target_type = target->value->type;
2480424815 }
2480524816
24806 if (target->value->type->id != ZigTypeIdEnum) {
24817 if (target_type->id != ZigTypeIdEnum) {
2480724818 ir_add_error(ira, &target->base,
24808 buf_sprintf("expected enum tag, found '%s'", buf_ptr(&target->value->type->name)));
24819 buf_sprintf("expected enum tag, found '%s'", buf_ptr(&target_type->name)));
2480924820 return ira->codegen->invalid_inst_gen;
2481024821 }
2481124822
24812 if (target->value->type->data.enumeration.src_field_count == 1 &&
24813 !target->value->type->data.enumeration.non_exhaustive) {
24814 TypeEnumField *only_field = &target->value->type->data.enumeration.fields[0];
24823 if (can_fold_enum_type(target_type)) {
24824 TypeEnumField *only_field = &target_type->data.enumeration.fields[0];
2481524825 ZigValue *array_val = create_const_str_lit(ira->codegen, only_field->name)->data.x_ptr.data.ref.pointee;
2481624826 IrInstGen *result = ir_const(ira, &instruction->base.base, nullptr);
2481724827 init_const_slice(ira->codegen, result->value, array_val, 0, buf_len(only_field->name), true);
......@@ -24819,9 +24829,9 @@ static IrInstGen *ir_analyze_instruction_enum_tag_name(IrAnalyze *ira, IrInstSrc
2481924829 }
2482024830
2482124831 if (instr_is_comptime(target)) {
24822 if ((err = type_resolve(ira->codegen, target->value->type, ResolveStatusZeroBitsKnown)))
24832 if ((err = type_resolve(ira->codegen, target_type, ResolveStatusZeroBitsKnown)))
2482324833 return ira->codegen->invalid_inst_gen;
24824 TypeEnumField *field = find_enum_field_by_tag(target->value->type, &target->value->data.x_bigint);
24834 TypeEnumField *field = find_enum_field_by_tag(target_type, &target->value->data.x_bigint);
2482524835 if (field == nullptr) {
2482624836 Buf *int_buf = buf_alloc();
2482724837 bigint_append_buf(int_buf, &target->value->data.x_bigint, 10);