authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-21 10:30:36-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-21 10:31:11-04:00
log44f2ee101f244342ec7e4cd81bb21d9a0c23267b
treee3e1ea2c38df41bfb37fbeafe0cdc5fc02652437
parent073f7ebb0e2168d04212fd855235c3543f86a2be
signaturelock-open Commit is signed but in an unrecognized format.

fix comptime slice of pointer to array

closes #1565

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

src/ir.cpp+10-8
......@@ -19073,11 +19073,7 @@ static ZigType *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructionSlice
1907319073 return ira->codegen->builtin_types.entry_invalid;
1907419074 }
1907519075 } else {
19076 ZigType *slice_ptr_type = get_pointer_to_type_extra(ira->codegen, array_type->data.pointer.child_type,
19077 array_type->data.pointer.is_const, array_type->data.pointer.is_volatile,
19078 PtrLenUnknown,
19079 array_type->data.pointer.explicit_alignment, 0, 0);
19080 return_type = get_slice_type(ira->codegen, slice_ptr_type);
19076 return_type = get_slice_type(ira->codegen, array_type);
1908119077 if (!end) {
1908219078 ir_add_error(ira, &instruction->base, buf_sprintf("slice of pointer must include end value"));
1908319079 return ira->codegen->builtin_types.entry_invalid;
......@@ -19141,9 +19137,15 @@ static ZigType *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructionSlice
1914119137 case ConstPtrSpecialDiscard:
1914219138 zig_unreachable();
1914319139 case ConstPtrSpecialRef:
19144 array_val = nullptr;
19145 abs_offset = SIZE_MAX;
19146 rel_end = 1;
19140 if (parent_ptr->data.x_ptr.data.ref.pointee->type->id == ZigTypeIdArray) {
19141 array_val = parent_ptr->data.x_ptr.data.ref.pointee;
19142 abs_offset = 0;
19143 rel_end = array_val->type->data.array.len;
19144 } else {
19145 array_val = nullptr;
19146 abs_offset = SIZE_MAX;
19147 rel_end = 1;
19148 }
1914719149 break;
1914819150 case ConstPtrSpecialBaseArray:
1914919151 array_val = parent_ptr->data.x_ptr.data.base_array.array_val;
test/cases/eval.zig+13
......@@ -710,3 +710,16 @@ test "@bytesToslice on a packed struct" {
710710 var f = @bytesToSlice(F, b);
711711 assert(f[0].a == 9);
712712}
713
714test "comptime pointer cast array and then slice" {
715 const array = []u8{ 1, 2, 3, 4, 5, 6, 7, 8 };
716
717 const ptrA: [*]const u8 = @ptrCast([*]const u8, &array);
718 const sliceA: []const u8 = ptrA[0..2];
719
720 const ptrB: [*]const u8 = &array;
721 const sliceB: []const u8 = ptrB[0..2];
722
723 assert(sliceA[1] == 2);
724 assert(sliceB[1] == 2);
725}