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
signaturelock-open 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
1847718477 IrInstGen *casted_ptr;
1847818478 if (isf->already_resolved) {
1847918479 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 }
1848918480 casted_ptr = result_loc;
1849018481 } else {
1849118482 isf->already_resolved = true;
......@@ -18502,6 +18493,15 @@ static IrInstGen *ir_resolve_result(IrAnalyze *ira, IrInst *suspend_source_instr
1850218493 field->type_val = create_const_type(ira->codegen, field->type_entry);
1850318494 field->src_index = old_field_count;
1850418495 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
1850618506 ZigType *struct_ptr_type = get_pointer_to_type(ira->codegen, isf->inferred_struct_type, false);
1850718507 if (instr_is_comptime(result_loc)) {
......@@ -18532,16 +18532,6 @@ static IrInstGen *ir_resolve_result(IrAnalyze *ira, IrInst *suspend_source_instr
1853218532 }
1853318533 }
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
1854518535 result_loc = ir_analyze_struct_field_ptr(ira, suspend_source_instr, field, casted_ptr,
1854618536 isf->inferred_struct_type, true);
1854718537 result_loc_pass1->resolved_loc = result_loc;
......@@ -22967,9 +22957,6 @@ static IrInstGen *ir_analyze_container_init_fields(IrAnalyze *ira, IrInst *sourc
2296722957 first_non_const_instruction = result_loc;
2296822958 }
2296922959 }
22970
22971 heap::c_allocator.deallocate(field_assign_nodes, actual_field_count);
22972
2297322960 if (any_missing)
2297422961 return ira->codegen->invalid_inst_gen;
2297522962
......@@ -22985,8 +22972,6 @@ static IrInstGen *ir_analyze_container_init_fields(IrAnalyze *ira, IrInst *sourc
2298522972 }
2298622973 }
2298722974
22988 const_ptrs.deinit();
22989
2299022975 IrInstGen *result = ir_get_deref(ira, source_instr, result_loc, nullptr);
2299122976
2299222977 if (is_comptime && !instr_is_comptime(result)) {
test/stage1/behavior/tuple.zig-39
......@@ -54,42 +54,3 @@ test "tuple concatenation" {
5454 T.doTheTest();
5555 comptime T.doTheTest();
5656}
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}