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;...@@ -1230,6 +1230,7 @@ static const uint32_t VECTOR_INDEX_RUNTIME = UINT32_MAX - 1;
1230struct InferredStructField {1230struct InferredStructField {
1231 ZigType *inferred_struct_type;1231 ZigType *inferred_struct_type;
1232 Buf *field_name;1232 Buf *field_name;
1233 bool already_resolved;
1233};1234};
12341235
1235struct ZigTypePointer {1236struct ZigTypePointer {
src/ir.cpp+55-49
...@@ -17519,7 +17519,6 @@ static IrInstruction *ir_resolve_result(IrAnalyze *ira, IrInstruction *suspend_s...@@ -17519,7 +17519,6 @@ static IrInstruction *ir_resolve_result(IrAnalyze *ira, IrInstruction *suspend_s
17519 {17519 {
17520 result_loc_pass1 = no_result_loc();17520 result_loc_pass1 = no_result_loc();
17521 }17521 }
17522 bool was_already_resolved = result_loc_pass1->resolved_loc != nullptr;
17523 IrInstruction *result_loc = ir_resolve_result_raw(ira, suspend_source_instr, result_loc_pass1, value_type,17522 IrInstruction *result_loc = ir_resolve_result_raw(ira, suspend_source_instr, result_loc_pass1, value_type,
17524 value, force_runtime, allow_discard);17523 value, force_runtime, allow_discard);
17525 if (result_loc == nullptr || (instr_is_unreachable(result_loc) || type_is_invalid(result_loc->value->type)))17524 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...@@ -17532,56 +17531,63 @@ static IrInstruction *ir_resolve_result(IrAnalyze *ira, IrInstruction *suspend_s
17532 }17531 }
1753317532
17534 InferredStructField *isf = result_loc->value->type->data.pointer.inferred_struct_field;17533 InferredStructField *isf = result_loc->value->type->data.pointer.inferred_struct_field;
17535 if (!was_already_resolved && isf != nullptr) {17534 if (isf != nullptr) {
17536 // Now it's time to add the field to the struct type.17535 TypeStructField *field;
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);
17560 IrInstruction *casted_ptr;17536 IrInstruction *casted_ptr;
17561 if (instr_is_comptime(result_loc)) {17537 if (isf->already_resolved) {
17562 casted_ptr = ir_const(ira, suspend_source_instr, struct_ptr_type);17538 field = find_struct_type_field(isf->inferred_struct_type, isf->field_name);
17563 copy_const_val(casted_ptr->value, result_loc->value);
17564 casted_ptr->value->type = struct_ptr_type;
17565 } else {
17566 casted_ptr = result_loc;17539 casted_ptr = result_loc;
17567 }17540 } else {
17568 if (instr_is_comptime(casted_ptr)) {17541 isf->already_resolved = true;
17569 ZigValue *ptr_val = ir_resolve_const(ira, casted_ptr, UndefBad);17542 // Now it's time to add the field to the struct type.
17570 if (!ptr_val)17543 uint32_t old_field_count = isf->inferred_struct_type->data.structure.src_field_count;
17571 return ira->codegen->invalid_instruction;17544 uint32_t new_field_count = old_field_count + 1;
17572 if (ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr) {17545 isf->inferred_struct_type->data.structure.src_field_count = new_field_count;
17573 ZigValue *struct_val = const_ptr_pointee(ira, ira->codegen, ptr_val,17546 isf->inferred_struct_type->data.structure.fields = realloc_type_struct_fields(
17574 suspend_source_instr->source_node);17547 isf->inferred_struct_type->data.structure.fields, old_field_count, new_field_count);
17575 struct_val->special = ConstValSpecialStatic;17548
17576 struct_val->data.x_struct.fields = realloc_const_vals_ptrs(struct_val->data.x_struct.fields,17549 field = isf->inferred_struct_type->data.structure.fields[old_field_count];
17577 old_field_count, new_field_count);17550 field->name = isf->field_name;
1757817551 field->type_entry = value_type;
17579 ZigValue *field_val = struct_val->data.x_struct.fields[old_field_count];17552 field->type_val = create_const_type(ira->codegen, field->type_entry);
17580 field_val->special = ConstValSpecialUndef;17553 field->src_index = old_field_count;
17581 field_val->type = field->type_entry;17554 field->decl_node = value ? value->source_node : suspend_source_instr->source_node;
17582 field_val->parent.id = ConstParentIdStruct;17555 if (value && instr_is_comptime(value)) {
17583 field_val->parent.data.p_struct.struct_val = struct_val;17556 ZigValue *val = ir_resolve_const(ira, value, UndefOk);
17584 field_val->parent.data.p_struct.field_index = old_field_count;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 }
17585 }17591 }
17586 }17592 }
1758717593
test/stage1/behavior.zig+3-3
...@@ -60,7 +60,7 @@ comptime {...@@ -60,7 +60,7 @@ comptime {
60 _ = @import("behavior/enum.zig");60 _ = @import("behavior/enum.zig");
61 _ = @import("behavior/enum_with_members.zig");61 _ = @import("behavior/enum_with_members.zig");
62 //_ = @import("behavior/error.zig");62 //_ = @import("behavior/error.zig");
63 //_ = @import("behavior/eval.zig");63 _ = @import("behavior/eval.zig");
64 _ = @import("behavior/field_parent_ptr.zig");64 _ = @import("behavior/field_parent_ptr.zig");
65 _ = @import("behavior/floatop.zig");65 _ = @import("behavior/floatop.zig");
66 _ = @import("behavior/fn.zig");66 _ = @import("behavior/fn.zig");
...@@ -93,7 +93,7 @@ comptime {...@@ -93,7 +93,7 @@ comptime {
93 _ = @import("behavior/sizeof_and_typeof.zig");93 _ = @import("behavior/sizeof_and_typeof.zig");
94 _ = @import("behavior/slice.zig");94 _ = @import("behavior/slice.zig");
95 _ = @import("behavior/slicetobytes.zig");95 _ = @import("behavior/slicetobytes.zig");
96 //_ = @import("behavior/struct.zig");96 _ = @import("behavior/struct.zig");
97 _ = @import("behavior/struct_contains_null_ptr_itself.zig");97 _ = @import("behavior/struct_contains_null_ptr_itself.zig");
98 _ = @import("behavior/struct_contains_slice_of_itself.zig");98 _ = @import("behavior/struct_contains_slice_of_itself.zig");
99 _ = @import("behavior/switch.zig");99 _ = @import("behavior/switch.zig");
...@@ -111,7 +111,7 @@ comptime {...@@ -111,7 +111,7 @@ comptime {
111 _ = @import("behavior/underscore.zig");111 _ = @import("behavior/underscore.zig");
112 _ = @import("behavior/union.zig");112 _ = @import("behavior/union.zig");
113 _ = @import("behavior/usingnamespace.zig");113 _ = @import("behavior/usingnamespace.zig");
114 //_ = @import("behavior/var_args.zig");114 _ = @import("behavior/var_args.zig");
115 _ = @import("behavior/vector.zig");115 _ = @import("behavior/vector.zig");
116 _ = @import("behavior/void.zig");116 _ = @import("behavior/void.zig");
117 _ = @import("behavior/while.zig");117 _ = @import("behavior/while.zig");