authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-13 16:51:26-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-13 16:51:26-04:00
log24cfa3534f31f503b3c0310b935e4fc654e02cda
tree2a8dc50e07b17f016413d9f4c719baad991cb492
parent3cbe82746489e467706c9002c0068368bb73e6d5
signaturelock-open Commit is signed but in an unrecognized format.

allow comptime array literals casted to slices


3 files changed, 22 insertions(+), 21 deletions(-)

src/all_types.hpp+1-1
...@@ -2555,8 +2555,8 @@ struct IrInstructionElemPtr {...@@ -2555,8 +2555,8 @@ struct IrInstructionElemPtr {
2555 IrInstruction *array_ptr;2555 IrInstruction *array_ptr;
2556 IrInstruction *elem_index;2556 IrInstruction *elem_index;
2557 PtrLen ptr_len;2557 PtrLen ptr_len;
2558 bool safety_check_on;
2559 bool initializing;2558 bool initializing;
2559 bool safety_check_on;
2560};2560};
25612561
2562struct IrInstructionVarPtr {2562struct IrInstructionVarPtr {
src/ir.cpp+11-10
...@@ -16833,7 +16833,7 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct...@@ -16833,7 +16833,7 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
16833 if (ptr_field->data.x_ptr.special == ConstPtrSpecialHardCodedAddr) {16833 if (ptr_field->data.x_ptr.special == ConstPtrSpecialHardCodedAddr) {
16834 IrInstruction *result = ir_build_elem_ptr(&ira->new_irb, elem_ptr_instruction->base.scope,16834 IrInstruction *result = ir_build_elem_ptr(&ira->new_irb, elem_ptr_instruction->base.scope,
16835 elem_ptr_instruction->base.source_node, array_ptr, casted_elem_index, false,16835 elem_ptr_instruction->base.source_node, array_ptr, casted_elem_index, false,
16836 elem_ptr_instruction->ptr_len, true);16836 elem_ptr_instruction->ptr_len, false);
16837 result->value.type = return_type;16837 result->value.type = return_type;
16838 return result;16838 return result;
16839 }16839 }
...@@ -16898,15 +16898,6 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct...@@ -16898,15 +16898,6 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
16898 }16898 }
16899 }16899 }
16900 }16900 }
16901
16902 if (is_slice(array_type) && elem_ptr_instruction->initializing) {
16903 // we need a pointer to an element inside a slice. but we're initializing an array.
16904 // this means that the slice isn't actually pointing at anything.
16905 ir_add_error(ira, &elem_ptr_instruction->base,
16906 buf_sprintf("runtime-initialized array cannot be casted to slice type '%s'",
16907 buf_ptr(&array_type->name)));
16908 return ira->codegen->invalid_instruction;
16909 }
16910 } else {16901 } else {
16911 // runtime known element index16902 // runtime known element index
16912 switch (type_requires_comptime(ira->codegen, return_type)) {16903 switch (type_requires_comptime(ira->codegen, return_type)) {
...@@ -19053,6 +19044,16 @@ static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira,...@@ -19053,6 +19044,16 @@ static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira,
19053 IrInstruction *result_loc = instruction->result_loc->child;19044 IrInstruction *result_loc = instruction->result_loc->child;
19054 if (type_is_invalid(result_loc->value.type))19045 if (type_is_invalid(result_loc->value.type))
19055 return result_loc;19046 return result_loc;
19047 ir_assert(result_loc->value.type->id == ZigTypeIdPointer, &instruction->base);
19048 ZigType *result_elem_type = result_loc->value.type->data.pointer.child_type;
19049 if (is_slice(result_elem_type)) {
19050 ErrorMsg *msg = ir_add_error(ira, &instruction->base,
19051 buf_sprintf("runtime-initialized array cannot be casted to slice type '%s'",
19052 buf_ptr(&result_elem_type->name)));
19053 add_error_note(ira->codegen, msg, first_non_const_instruction->source_node,
19054 buf_sprintf("this value is not comptime-known"));
19055 return ira->codegen->invalid_instruction;
19056 }
19056 return ir_get_deref(ira, &instruction->base, result_loc, nullptr);19057 return ir_get_deref(ira, &instruction->base, result_loc, nullptr);
19057 } else if (container_type->id == ZigTypeIdVoid) {19058 } else if (container_type->id == ZigTypeIdVoid) {
19058 if (elem_count != 0) {19059 if (elem_count != 0) {
test/stage1/behavior/array.zig+10-10
...@@ -222,17 +222,17 @@ test "double nested array to const slice cast in array literal" {...@@ -222,17 +222,17 @@ test "double nested array to const slice cast in array literal" {
222 const S = struct {222 const S = struct {
223 fn entry(two: i32) void {223 fn entry(two: i32) void {
224 const cases = [_][]const []const i32{224 const cases = [_][]const []const i32{
225 &[_][]const i32{&[_]i32{1}},225 [_][]const i32{[_]i32{1}},
226 &[_][]const i32{&[_]i32{ 2, 3 }},226 [_][]const i32{[_]i32{ 2, 3 }},
227 &[_][]const i32{227 [_][]const i32{
228 &[_]i32{4},228 [_]i32{4},
229 &[_]i32{ 5, 6, 7 },229 [_]i32{ 5, 6, 7 },
230 },230 },
231 };231 };
232 check(cases);232 check(cases);
233233
234 const cases2 = [_][]const i32{234 const cases2 = [_][]const i32{
235 &[_]i32{1},235 [_]i32{1},
236 &[_]i32{ two, 3 },236 &[_]i32{ two, 3 },
237 };237 };
238 expect(cases2.len == 2);238 expect(cases2.len == 2);
...@@ -243,11 +243,11 @@ test "double nested array to const slice cast in array literal" {...@@ -243,11 +243,11 @@ test "double nested array to const slice cast in array literal" {
243 expect(cases2[1][1] == 3);243 expect(cases2[1][1] == 3);
244244
245 const cases3 = [_][]const []const i32{245 const cases3 = [_][]const []const i32{
246 &[_][]const i32{&[_]i32{1}},246 [_][]const i32{[_]i32{1}},
247 &[_][]const i32{&[_]i32{ two, 3 }},247 &[_][]const i32{&[_]i32{ two, 3 }},
248 &[_][]const i32{248 [_][]const i32{
249 &[_]i32{4},249 [_]i32{4},
250 &[_]i32{ 5, 6, 7 },250 [_]i32{ 5, 6, 7 },
251 },251 },
252 };252 };
253 check(cases3);253 check(cases3);