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...@@ -19073,11 +19073,7 @@ static ZigType *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructionSlice
19073 return ira->codegen->builtin_types.entry_invalid;19073 return ira->codegen->builtin_types.entry_invalid;
19074 }19074 }
19075 } else {19075 } else {
19076 ZigType *slice_ptr_type = get_pointer_to_type_extra(ira->codegen, array_type->data.pointer.child_type,19076 return_type = get_slice_type(ira->codegen, array_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);
19081 if (!end) {19077 if (!end) {
19082 ir_add_error(ira, &instruction->base, buf_sprintf("slice of pointer must include end value"));19078 ir_add_error(ira, &instruction->base, buf_sprintf("slice of pointer must include end value"));
19083 return ira->codegen->builtin_types.entry_invalid;19079 return ira->codegen->builtin_types.entry_invalid;
...@@ -19141,9 +19137,15 @@ static ZigType *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructionSlice...@@ -19141,9 +19137,15 @@ static ZigType *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructionSlice
19141 case ConstPtrSpecialDiscard:19137 case ConstPtrSpecialDiscard:
19142 zig_unreachable();19138 zig_unreachable();
19143 case ConstPtrSpecialRef:19139 case ConstPtrSpecialRef:
19144 array_val = nullptr;19140 if (parent_ptr->data.x_ptr.data.ref.pointee->type->id == ZigTypeIdArray) {
19145 abs_offset = SIZE_MAX;19141 array_val = parent_ptr->data.x_ptr.data.ref.pointee;
19146 rel_end = 1;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 }
19147 break;19149 break;
19148 case ConstPtrSpecialBaseArray:19150 case ConstPtrSpecialBaseArray:
19149 array_val = parent_ptr->data.x_ptr.data.base_array.array_val;19151 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" {...@@ -710,3 +710,16 @@ test "@bytesToslice on a packed struct" {
710 var f = @bytesToSlice(F, b);710 var f = @bytesToSlice(F, b);
711 assert(f[0].a == 9);711 assert(f[0].a == 9);
712}712}
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}