authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2019-04-28 12:45:50+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-28 13:13:42-04:00
log9902b604cb01072d4599035b59724731cf03fdbc
tree67d5deb790c0abca47b60b95c729dc0e10b93a38
parent205e501e42b9a2e1d35afa0873311165100863ae

Fix generation of container initializers

The code creates temporary ConstExprValue with global_refs set to nullptr and that's carried over to the final value. Doing so prevents the deduplication mechanism to work correctly, causing all sorts of runtime crashes. Fixes #1636 Fixes #1608 (Even though it was already fixed by #1991)

2 files changed, 26 insertions(+), 7 deletions(-)

src/ir.cpp+6-5
......@@ -9931,6 +9931,7 @@ static void ir_add_alloca(IrAnalyze *ira, IrInstruction *instruction, ZigType *t
99319931
99329932static void copy_const_val(ConstExprValue *dest, ConstExprValue *src, bool same_global_refs) {
99339933 ConstGlobalRefs *global_refs = dest->global_refs;
9934 assert(!same_global_refs || src->global_refs != nullptr);
99349935 *dest = *src;
99359936 if (!same_global_refs) {
99369937 dest->global_refs = global_refs;
......@@ -17468,6 +17469,7 @@ static IrInstruction *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstruc
1746817469 ConstExprValue const_val = {};
1746917470 const_val.special = ConstValSpecialStatic;
1747017471 const_val.type = container_type;
17472 // const_val.global_refs = allocate<ConstGlobalRefs>(1);
1747117473 const_val.data.x_struct.fields = create_const_vals(actual_field_count);
1747217474 for (size_t i = 0; i < instr_field_count; i += 1) {
1747317475 IrInstructionContainerInitFieldsField *field = &fields[i];
......@@ -17531,7 +17533,7 @@ static IrInstruction *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstruc
1753117533 if (const_val.special == ConstValSpecialStatic) {
1753217534 IrInstruction *result = ir_const(ira, instruction, nullptr);
1753317535 ConstExprValue *out_val = &result->value;
17534 copy_const_val(out_val, &const_val, true);
17536 copy_const_val(out_val, &const_val, false);
1753517537 out_val->type = container_type;
1753617538
1753717539 for (size_t i = 0; i < instr_field_count; i += 1) {
......@@ -17603,6 +17605,7 @@ static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira,
1760317605 ConstExprValue const_val = {};
1760417606 const_val.special = ConstValSpecialStatic;
1760517607 const_val.type = fixed_size_array_type;
17608 // const_val.global_refs = allocate<ConstGlobalRefs>(1);
1760617609 const_val.data.x_array.data.s_none.elements = create_const_vals(elem_count);
1760717610
1760817611 bool is_comptime = ir_should_inline(ira->new_irb.exec, instruction->base.scope);
......@@ -17639,8 +17642,6 @@ static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira,
1763917642 if (const_val.special == ConstValSpecialStatic) {
1764017643 IrInstruction *result = ir_const(ira, &instruction->base, nullptr);
1764117644 ConstExprValue *out_val = &result->value;
17642 // Make sure to pass same_global_refs=false here in order not to
17643 // zero the global_refs field for `result` (#1608)
1764417645 copy_const_val(out_val, &const_val, false);
1764517646 result->value.type = fixed_size_array_type;
1764617647 for (size_t i = 0; i < elem_count; i += 1) {
......@@ -21319,7 +21320,7 @@ static IrInstruction *ir_align_cast(IrAnalyze *ira, IrInstruction *target, uint3
2131921320 }
2132021321
2132121322 IrInstruction *result = ir_const(ira, target, result_type);
21322 copy_const_val(&result->value, val, false);
21323 copy_const_val(&result->value, val, true);
2132321324 result->value.type = result_type;
2132421325 return result;
2132521326 }
......@@ -21389,7 +21390,7 @@ static IrInstruction *ir_analyze_ptr_cast(IrAnalyze *ira, IrInstruction *source_
2138921390 }
2139021391
2139121392 IrInstruction *result = ir_const(ira, source_instr, dest_type);
21392 copy_const_val(&result->value, val, false);
21393 copy_const_val(&result->value, val, true);
2139321394 result->value.type = dest_type;
2139421395
2139521396 // Keep the bigger alignment, it can only help-
test/stage1/behavior/struct.zig+20-2
......@@ -2,8 +2,7 @@ const std = @import("std");
22const expect = std.testing.expect;
33const expectEqualSlices = std.testing.expectEqualSlices;
44const builtin = @import("builtin");
5const maxInt = std.math.maxInt;
6
5const maxInt = std.math.maxInt;
76const StructWithNoFields = struct {
87 fn add(a: i32, b: i32) i32 {
98 return a + b;
......@@ -505,3 +504,22 @@ test "packed struct with u0 field access" {
505504 var s = S{ .f0 = 0 };
506505 comptime expect(s.f0 == 0);
507506}
507
508const S0 = struct{
509 bar: S1,
510
511 pub const S1 = struct{
512 value: u8,
513 };
514
515 fn init() @This() {
516 return S0{ .bar = S1{ .value = 123 } };
517 }
518};
519
520var g_foo: S0 = S0.init();
521
522test "access to global struct fields" {
523 g_foo.bar.value = 42;
524 expect(g_foo.bar.value == 42);
525}