authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-01-13 13:12:53-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-01-16 21:58:52-05:00
loge48157c3cb817ff1551f74fc1da9f420e38ccbd5
tree24ef674f95c5cf1866e642f8467ca884dfef72cf
parentfb8da16a60e13b09c5c6bb4989c204da536597b7
signaturelock-open Commit is signed but in an unrecognized format.

fix regression with inferred struct fields


3 files changed, 59 insertions(+), 52 deletions(-)

src/all_types.hpp+1
......@@ -1230,6 +1230,7 @@ static const uint32_t VECTOR_INDEX_RUNTIME = UINT32_MAX - 1;
12301230struct InferredStructField {
12311231 ZigType *inferred_struct_type;
12321232 Buf *field_name;
1233 bool already_resolved;
12331234};
12341235
12351236struct ZigTypePointer {
src/ir.cpp+55-49
......@@ -17519,7 +17519,6 @@ static IrInstruction *ir_resolve_result(IrAnalyze *ira, IrInstruction *suspend_s
1751917519 {
1752017520 result_loc_pass1 = no_result_loc();
1752117521 }
17522 bool was_already_resolved = result_loc_pass1->resolved_loc != nullptr;
1752317522 IrInstruction *result_loc = ir_resolve_result_raw(ira, suspend_source_instr, result_loc_pass1, value_type,
1752417523 value, force_runtime, allow_discard);
1752517524 if (result_loc == nullptr || (instr_is_unreachable(result_loc) || type_is_invalid(result_loc->value->type)))
......@@ -17532,56 +17531,63 @@ static IrInstruction *ir_resolve_result(IrAnalyze *ira, IrInstruction *suspend_s
1753217531 }
1753317532
1753417533 InferredStructField *isf = result_loc->value->type->data.pointer.inferred_struct_field;
17535 if (!was_already_resolved && isf != nullptr) {
17536 // Now it's time to add the field to the struct type.
17537 uint32_t old_field_count = isf->inferred_struct_type->data.structure.src_field_count;
17538 uint32_t new_field_count = old_field_count + 1;
17539 isf->inferred_struct_type->data.structure.src_field_count = new_field_count;
17540 isf->inferred_struct_type->data.structure.fields = realloc_type_struct_fields(
17541 isf->inferred_struct_type->data.structure.fields, old_field_count, new_field_count);
17542
17543 TypeStructField *field = isf->inferred_struct_type->data.structure.fields[old_field_count];
17544 field->name = isf->field_name;
17545 field->type_entry = value_type;
17546 field->type_val = create_const_type(ira->codegen, field->type_entry);
17547 field->src_index = old_field_count;
17548 field->decl_node = value ? value->source_node : suspend_source_instr->source_node;
17549 if (value && instr_is_comptime(value)) {
17550 ZigValue *val = ir_resolve_const(ira, value, UndefOk);
17551 if (!val)
17552 return ira->codegen->invalid_instruction;
17553 field->is_comptime = true;
17554 field->init_val = create_const_vals(1);
17555 copy_const_val(field->init_val, val);
17556 return result_loc;
17557 }
17558
17559 ZigType *struct_ptr_type = get_pointer_to_type(ira->codegen, isf->inferred_struct_type, false);
17534 if (isf != nullptr) {
17535 TypeStructField *field;
1756017536 IrInstruction *casted_ptr;
17561 if (instr_is_comptime(result_loc)) {
17562 casted_ptr = ir_const(ira, suspend_source_instr, struct_ptr_type);
17563 copy_const_val(casted_ptr->value, result_loc->value);
17564 casted_ptr->value->type = struct_ptr_type;
17565 } else {
17537 if (isf->already_resolved) {
17538 field = find_struct_type_field(isf->inferred_struct_type, isf->field_name);
1756617539 casted_ptr = result_loc;
17567 }
17568 if (instr_is_comptime(casted_ptr)) {
17569 ZigValue *ptr_val = ir_resolve_const(ira, casted_ptr, UndefBad);
17570 if (!ptr_val)
17571 return ira->codegen->invalid_instruction;
17572 if (ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr) {
17573 ZigValue *struct_val = const_ptr_pointee(ira, ira->codegen, ptr_val,
17574 suspend_source_instr->source_node);
17575 struct_val->special = ConstValSpecialStatic;
17576 struct_val->data.x_struct.fields = realloc_const_vals_ptrs(struct_val->data.x_struct.fields,
17577 old_field_count, new_field_count);
17578
17579 ZigValue *field_val = struct_val->data.x_struct.fields[old_field_count];
17580 field_val->special = ConstValSpecialUndef;
17581 field_val->type = field->type_entry;
17582 field_val->parent.id = ConstParentIdStruct;
17583 field_val->parent.data.p_struct.struct_val = struct_val;
17584 field_val->parent.data.p_struct.field_index = old_field_count;
17540 } else {
17541 isf->already_resolved = true;
17542 // Now it's time to add the field to the struct type.
17543 uint32_t old_field_count = isf->inferred_struct_type->data.structure.src_field_count;
17544 uint32_t new_field_count = old_field_count + 1;
17545 isf->inferred_struct_type->data.structure.src_field_count = new_field_count;
17546 isf->inferred_struct_type->data.structure.fields = realloc_type_struct_fields(
17547 isf->inferred_struct_type->data.structure.fields, old_field_count, new_field_count);
17548
17549 field = isf->inferred_struct_type->data.structure.fields[old_field_count];
17550 field->name = isf->field_name;
17551 field->type_entry = value_type;
17552 field->type_val = create_const_type(ira->codegen, field->type_entry);
17553 field->src_index = old_field_count;
17554 field->decl_node = value ? value->source_node : suspend_source_instr->source_node;
17555 if (value && instr_is_comptime(value)) {
17556 ZigValue *val = ir_resolve_const(ira, value, UndefOk);
17557 if (!val)
17558 return ira->codegen->invalid_instruction;
17559 field->is_comptime = true;
17560 field->init_val = create_const_vals(1);
17561 copy_const_val(field->init_val, val);
17562 return result_loc;
17563 }
17564
17565 ZigType *struct_ptr_type = get_pointer_to_type(ira->codegen, isf->inferred_struct_type, false);
17566 if (instr_is_comptime(result_loc)) {
17567 casted_ptr = ir_const(ira, suspend_source_instr, struct_ptr_type);
17568 copy_const_val(casted_ptr->value, result_loc->value);
17569 casted_ptr->value->type = struct_ptr_type;
17570 } else {
17571 casted_ptr = result_loc;
17572 }
17573 if (instr_is_comptime(casted_ptr)) {
17574 ZigValue *ptr_val = ir_resolve_const(ira, casted_ptr, UndefBad);
17575 if (!ptr_val)
17576 return ira->codegen->invalid_instruction;
17577 if (ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr) {
17578 ZigValue *struct_val = const_ptr_pointee(ira, ira->codegen, ptr_val,
17579 suspend_source_instr->source_node);
17580 struct_val->special = ConstValSpecialStatic;
17581 struct_val->data.x_struct.fields = realloc_const_vals_ptrs(struct_val->data.x_struct.fields,
17582 old_field_count, new_field_count);
17583
17584 ZigValue *field_val = struct_val->data.x_struct.fields[old_field_count];
17585 field_val->special = ConstValSpecialUndef;
17586 field_val->type = field->type_entry;
17587 field_val->parent.id = ConstParentIdStruct;
17588 field_val->parent.data.p_struct.struct_val = struct_val;
17589 field_val->parent.data.p_struct.field_index = old_field_count;
17590 }
1758517591 }
1758617592 }
1758717593
test/stage1/behavior.zig+3-3
......@@ -60,7 +60,7 @@ comptime {
6060 _ = @import("behavior/enum.zig");
6161 _ = @import("behavior/enum_with_members.zig");
6262 //_ = @import("behavior/error.zig");
63 //_ = @import("behavior/eval.zig");
63 _ = @import("behavior/eval.zig");
6464 _ = @import("behavior/field_parent_ptr.zig");
6565 _ = @import("behavior/floatop.zig");
6666 _ = @import("behavior/fn.zig");
......@@ -93,7 +93,7 @@ comptime {
9393 _ = @import("behavior/sizeof_and_typeof.zig");
9494 _ = @import("behavior/slice.zig");
9595 _ = @import("behavior/slicetobytes.zig");
96 //_ = @import("behavior/struct.zig");
96 _ = @import("behavior/struct.zig");
9797 _ = @import("behavior/struct_contains_null_ptr_itself.zig");
9898 _ = @import("behavior/struct_contains_slice_of_itself.zig");
9999 _ = @import("behavior/switch.zig");
......@@ -111,7 +111,7 @@ comptime {
111111 _ = @import("behavior/underscore.zig");
112112 _ = @import("behavior/union.zig");
113113 _ = @import("behavior/usingnamespace.zig");
114 //_ = @import("behavior/var_args.zig");
114 _ = @import("behavior/var_args.zig");
115115 _ = @import("behavior/vector.zig");
116116 _ = @import("behavior/void.zig");
117117 _ = @import("behavior/while.zig");