authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2019-02-20 00:20:04+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-13 11:35:05-04:00
log49838a9f3eee0df33d2994b92875314363b32dfc
tree9d88deed4d89a5b528d8f6500910ca98d322566b
parent1ed38a682ec6cabcd72472b6988b27b10714216a
signaturelock-open Commit is signed but in an unrecognized format.

Fix generation of comptime slices

By erasing the global_refs field we'd trip any codepath using const_values_equal_ptr to figure if two slices were different.

2 files changed, 16 insertions(+), 1 deletions(-)

src/ir.cpp+3-1
...@@ -17329,7 +17329,9 @@ static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira,...@@ -17329,7 +17329,9 @@ static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira,
17329 if (const_val.special == ConstValSpecialStatic) {17329 if (const_val.special == ConstValSpecialStatic) {
17330 IrInstruction *result = ir_const(ira, &instruction->base, nullptr);17330 IrInstruction *result = ir_const(ira, &instruction->base, nullptr);
17331 ConstExprValue *out_val = &result->value;17331 ConstExprValue *out_val = &result->value;
17332 copy_const_val(out_val, &const_val, true);17332 // Make sure to pass same_global_refs=false here in order not to
17333 // zero the global_refs field for `result` (#1608)
17334 copy_const_val(out_val, &const_val, false);
17333 result->value.type = fixed_size_array_type;17335 result->value.type = fixed_size_array_type;
17334 for (size_t i = 0; i < elem_count; i += 1) {17336 for (size_t i = 0; i < elem_count; i += 1) {
17335 ConstExprValue *elem_val = &out_val->data.x_array.data.s_none.elements[i];17337 ConstExprValue *elem_val = &out_val->data.x_array.data.s_none.elements[i];
test/stage1/behavior/slice.zig+13
...@@ -47,3 +47,16 @@ test "C pointer" {...@@ -47,3 +47,16 @@ test "C pointer" {
47 var slice = buf[0..len];47 var slice = buf[0..len];
48 expectEqualSlices(u8, "kjdhfkjdhf", slice);48 expectEqualSlices(u8, "kjdhfkjdhf", slice);
49}49}
50
51fn sliceSum(comptime q: []const u8) i32 {
52 comptime var result = 0;
53 inline for (q) |item| {
54 result += item;
55 }
56 return result;
57}
58
59test "comptime slices are disambiguated" {
60 expect(sliceSum([]u8{ 1, 2 }) == 3);
61 expect(sliceSum([]u8{ 3, 4 }) == 7);
62}