authorgravatar for alex_naskos@hotmail.comAlexandros Naskos <alex_naskos@hotmail.com> 2020-03-05 19:23:06+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-05 15:48:03-05:00
logdebcc79d56a40f77b92e243b4e344fc9385bd405
tree3ad6df0c9b40bd9a1142d8774595467ba2dfe045
parentc92957da0ed6f74e328fd0414c56f885cdcff30b

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


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

src/ir.cpp+24-9
...@@ -18477,6 +18477,15 @@ static IrInstGen *ir_resolve_result(IrAnalyze *ira, IrInst *suspend_source_instr...@@ -18477,6 +18477,15 @@ 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 }
18480 casted_ptr = result_loc;18489 casted_ptr = result_loc;
18481 } else {18490 } else {
18482 isf->already_resolved = true;18491 isf->already_resolved = true;
...@@ -18493,15 +18502,6 @@ static IrInstGen *ir_resolve_result(IrAnalyze *ira, IrInst *suspend_source_instr...@@ -18493,15 +18502,6 @@ static IrInstGen *ir_resolve_result(IrAnalyze *ira, IrInst *suspend_source_instr
18493 field->type_val = create_const_type(ira->codegen, field->type_entry);18502 field->type_val = create_const_type(ira->codegen, field->type_entry);
18494 field->src_index = old_field_count;18503 field->src_index = old_field_count;
18495 field->decl_node = value ? value->base.source_node : suspend_source_instr->source_node;18504 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,6 +18532,16 @@ static IrInstGen *ir_resolve_result(IrAnalyze *ira, IrInst *suspend_source_instr...@@ -18532,6 +18532,16 @@ 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
18535 result_loc = ir_analyze_struct_field_ptr(ira, suspend_source_instr, field, casted_ptr,18545 result_loc = ir_analyze_struct_field_ptr(ira, suspend_source_instr, field, casted_ptr,
18536 isf->inferred_struct_type, true);18546 isf->inferred_struct_type, true);
18537 result_loc_pass1->resolved_loc = result_loc;18547 result_loc_pass1->resolved_loc = result_loc;
...@@ -22957,6 +22967,9 @@ static IrInstGen *ir_analyze_container_init_fields(IrAnalyze *ira, IrInst *sourc...@@ -22957,6 +22967,9 @@ static IrInstGen *ir_analyze_container_init_fields(IrAnalyze *ira, IrInst *sourc
22957 first_non_const_instruction = result_loc;22967 first_non_const_instruction = result_loc;
22958 }22968 }
22959 }22969 }
22970
22971 heap::c_allocator.deallocate(field_assign_nodes, actual_field_count);
22972
22960 if (any_missing)22973 if (any_missing)
22961 return ira->codegen->invalid_inst_gen;22974 return ira->codegen->invalid_inst_gen;
2296222975
...@@ -22972,6 +22985,8 @@ static IrInstGen *ir_analyze_container_init_fields(IrAnalyze *ira, IrInst *sourc...@@ -22972,6 +22985,8 @@ static IrInstGen *ir_analyze_container_init_fields(IrAnalyze *ira, IrInst *sourc
22972 }22985 }
22973 }22986 }
2297422987
22988 const_ptrs.deinit();
22989
22975 IrInstGen *result = ir_get_deref(ira, source_instr, result_loc, nullptr);22990 IrInstGen *result = ir_get_deref(ira, source_instr, result_loc, nullptr);
2297622991
22977 if (is_comptime && !instr_is_comptime(result)) {22992 if (is_comptime && !instr_is_comptime(result)) {
test/stage1/behavior/tuple.zig+39
...@@ -54,3 +54,42 @@ test "tuple concatenation" {...@@ -54,3 +54,42 @@ 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}