authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-05 17:19:01-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-05 17:19:01-05:00
log01c722c21cd4f0216a722fa698d704e23707a5ba
tree32e0b49d824a1c99ef5d1911709e40ad669e42e0
parentf5954dad8356c05c294630f19609c343d65ea544
signature Commit is signed but in an unrecognized format.

Revert "Allow constant struct val to reallocate its fields when resolving an inferred struct field with a comptime value."

This reverts commit debcc79d56a40f77b92e243b4e344fc9385bd405. This caused a regression when building self-hosted

2 files changed, 9 insertions(+), 63 deletions(-)

src/ir.cpp+9-24
...@@ -18477,15 +18477,6 @@ static IrInstGen *ir_resolve_result(IrAnalyze *ira, IrInst *suspend_source_instr...@@ -18477,15 +18477,6 @@ static IrInstGen *ir_resolve_result(IrAnalyze *ira, IrInst *suspend_source_instr
18477 IrInstGen *casted_ptr;18477 IrInstGen *casted_ptr;
18478 if (isf->already_resolved) {18478 if (isf->already_resolved) {
18479 field = find_struct_type_field(isf->inferred_struct_type, isf->field_name);18479 field = find_struct_type_field(isf->inferred_struct_type, isf->field_name);
18480
18481 // If the value originates from another node than the original value's we overwrite the inferred struct's
18482 // type so that the new result can be written successfully.
18483 // The duplicate field will be detected and reported in 'ir_analyze_container_init_fields'
18484 AstNode *decl_node = value ? value->base.source_node : suspend_source_instr->source_node;
18485 if (decl_node != field->decl_node) {
18486 field->type_entry = value_type;
18487 field->type_val = create_const_type(ira->codegen, field->type_entry);
18488 }
18489 casted_ptr = result_loc;18480 casted_ptr = result_loc;
18490 } else {18481 } else {
18491 isf->already_resolved = true;18482 isf->already_resolved = true;
...@@ -18502,6 +18493,15 @@ static IrInstGen *ir_resolve_result(IrAnalyze *ira, IrInst *suspend_source_instr...@@ -18502,6 +18493,15 @@ static IrInstGen *ir_resolve_result(IrAnalyze *ira, IrInst *suspend_source_instr
18502 field->type_val = create_const_type(ira->codegen, field->type_entry);18493 field->type_val = create_const_type(ira->codegen, field->type_entry);
18503 field->src_index = old_field_count;18494 field->src_index = old_field_count;
18504 field->decl_node = value ? value->base.source_node : suspend_source_instr->source_node;18495 field->decl_node = value ? value->base.source_node : suspend_source_instr->source_node;
18496 if (value && instr_is_comptime(value)) {
18497 ZigValue *val = ir_resolve_const(ira, value, UndefOk);
18498 if (!val)
18499 return ira->codegen->invalid_inst_gen;
18500 field->is_comptime = true;
18501 field->init_val = ira->codegen->pass1_arena->create<ZigValue>();
18502 copy_const_val(ira->codegen, field->init_val, val);
18503 return result_loc;
18504 }
1850518505
18506 ZigType *struct_ptr_type = get_pointer_to_type(ira->codegen, isf->inferred_struct_type, false);18506 ZigType *struct_ptr_type = get_pointer_to_type(ira->codegen, isf->inferred_struct_type, false);
18507 if (instr_is_comptime(result_loc)) {18507 if (instr_is_comptime(result_loc)) {
...@@ -18532,16 +18532,6 @@ static IrInstGen *ir_resolve_result(IrAnalyze *ira, IrInst *suspend_source_instr...@@ -18532,16 +18532,6 @@ static IrInstGen *ir_resolve_result(IrAnalyze *ira, IrInst *suspend_source_instr
18532 }18532 }
18533 }18533 }
1853418534
18535 if (value && instr_is_comptime(value)) {
18536 ZigValue *val = ir_resolve_const(ira, value, UndefOk);
18537 if (!val)
18538 return ira->codegen->invalid_inst_gen;
18539 field->is_comptime = true;
18540 field->init_val = ira->codegen->pass1_arena->create<ZigValue>();
18541 copy_const_val(ira->codegen, field->init_val, val);
18542 return result_loc;
18543 }
18544
18545 result_loc = ir_analyze_struct_field_ptr(ira, suspend_source_instr, field, casted_ptr,18535 result_loc = ir_analyze_struct_field_ptr(ira, suspend_source_instr, field, casted_ptr,
18546 isf->inferred_struct_type, true);18536 isf->inferred_struct_type, true);
18547 result_loc_pass1->resolved_loc = result_loc;18537 result_loc_pass1->resolved_loc = result_loc;
...@@ -22967,9 +22957,6 @@ static IrInstGen *ir_analyze_container_init_fields(IrAnalyze *ira, IrInst *sourc...@@ -22967,9 +22957,6 @@ static IrInstGen *ir_analyze_container_init_fields(IrAnalyze *ira, IrInst *sourc
22967 first_non_const_instruction = result_loc;22957 first_non_const_instruction = result_loc;
22968 }22958 }
22969 }22959 }
22970
22971 heap::c_allocator.deallocate(field_assign_nodes, actual_field_count);
22972
22973 if (any_missing)22960 if (any_missing)
22974 return ira->codegen->invalid_inst_gen;22961 return ira->codegen->invalid_inst_gen;
2297522962
...@@ -22985,8 +22972,6 @@ static IrInstGen *ir_analyze_container_init_fields(IrAnalyze *ira, IrInst *sourc...@@ -22985,8 +22972,6 @@ static IrInstGen *ir_analyze_container_init_fields(IrAnalyze *ira, IrInst *sourc
22985 }22972 }
22986 }22973 }
2298722974
22988 const_ptrs.deinit();
22989
22990 IrInstGen *result = ir_get_deref(ira, source_instr, result_loc, nullptr);22975 IrInstGen *result = ir_get_deref(ira, source_instr, result_loc, nullptr);
2299122976
22992 if (is_comptime && !instr_is_comptime(result)) {22977 if (is_comptime && !instr_is_comptime(result)) {
test/stage1/behavior/tuple.zig-39
...@@ -54,42 +54,3 @@ test "tuple concatenation" {...@@ -54,42 +54,3 @@ test "tuple concatenation" {
54 T.doTheTest();54 T.doTheTest();
55 comptime T.doTheTest();55 comptime T.doTheTest();
56}56}
57
58test "tuple initialization with structure initializer and constant expression" {
59 const TestStruct = struct {
60 state: u8,
61 };
62
63 const S = struct {
64 fn doTheTest() void {
65 const tuple_with_struct = .{ TestStruct{ .state = 42 }, 0 };
66 expect(tuple_with_struct.len == 2);
67 expect(tuple_with_struct[0].state == 42);
68 expect(tuple_with_struct[1] == 0);
69 }
70 };
71 S.doTheTest();
72 comptime S.doTheTest();
73}
74
75test "passing tuples as comptime generic parameters" {
76 const S = struct {
77 fn expect_len(comptime pack: var, comptime len: usize) void {
78 expect(pack.len == len);
79 }
80
81 fn expect_first_element(comptime pack: var, comptime elem: var) void {
82 expect(pack[0] == elem);
83 }
84
85 fn doTheTest() void {
86 expect_len(.{}, 0);
87 expect_len(.{ 0 }, 1);
88 expect_first_element(.{ 0 }, 0);
89 expect_len(.{ u8, 1, "literal" }, 3);
90 expect_first_element(.{ u8, 1, "literal" }, u8);
91 }
92 };
93 S.doTheTest();
94 comptime S.doTheTest();
95}