authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-11-24 16:25:26-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-11-24 16:25:26-05:00
log09ec720dab6331c8be0300f2943f7d757ad0d7c3
treec9b2fc59b1efb6148dbf39f1c4b9c476b2a9a512
parentf7574f44c120dc834ebaf2773dc1ab098a20c232
signaturelock-open Commit is signed but in an unrecognized format.

fix comptime `@ptrCast` of pointers to arrays


2 files changed, 22 insertions(+), 8 deletions(-)

src/ir.cpp+20-5
......@@ -18585,12 +18585,27 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
1858518585 case ConstPtrSpecialDiscard:
1858618586 zig_unreachable();
1858718587 case ConstPtrSpecialRef:
18588 mem_size = 1;
18589 old_size = 1;
18590 new_index = index;
18588 if (array_ptr_val->data.x_ptr.data.ref.pointee->type->id == ZigTypeIdArray) {
18589 ConstExprValue *array_val = array_ptr_val->data.x_ptr.data.ref.pointee;
18590 new_index = index;
18591 ZigType *array_type = array_val->type;
18592 mem_size = array_type->data.array.len;
18593 if (array_type->data.array.sentinel != nullptr) {
18594 mem_size += 1;
18595 }
18596 old_size = mem_size;
1859118597
18592 out_val->data.x_ptr.special = ConstPtrSpecialRef;
18593 out_val->data.x_ptr.data.ref.pointee = array_ptr_val->data.x_ptr.data.ref.pointee;
18598 out_val->data.x_ptr.special = ConstPtrSpecialBaseArray;
18599 out_val->data.x_ptr.data.base_array.array_val = array_val;
18600 out_val->data.x_ptr.data.base_array.elem_index = new_index;
18601 } else {
18602 mem_size = 1;
18603 old_size = 1;
18604 new_index = index;
18605
18606 out_val->data.x_ptr.special = ConstPtrSpecialRef;
18607 out_val->data.x_ptr.data.ref.pointee = array_ptr_val->data.x_ptr.data.ref.pointee;
18608 }
1859418609 break;
1859518610 case ConstPtrSpecialBaseArray:
1859618611 {
test/stage1/behavior/pointers.zig+2-3
......@@ -204,13 +204,12 @@ test "assign null directly to C pointer and test null equality" {
204204test "null terminated pointer" {
205205 const S = struct {
206206 fn doTheTest() void {
207 var array_with_zero = [_]u8{'h', 'e', 'l', 'l', 'o', 0};
207 var array_with_zero = [_:0]u8{'h', 'e', 'l', 'l', 'o'};
208208 var zero_ptr: [*:0]const u8 = @ptrCast([*:0]const u8, &array_with_zero);
209209 var no_zero_ptr: [*]const u8 = zero_ptr;
210210 expect(std.mem.eql(u8, std.mem.toSliceConst(u8, no_zero_ptr), "hello"));
211211 }
212212 };
213213 S.doTheTest();
214 // TODO test fails at comptime
215 //comptime S.doTheTest();
214 comptime S.doTheTest();
216215}