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
signature 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 {
25552555 IrInstruction *array_ptr;
25562556 IrInstruction *elem_index;
25572557 PtrLen ptr_len;
2558 bool safety_check_on;
25592558 bool initializing;
2559 bool safety_check_on;
25602560};
25612561
25622562struct IrInstructionVarPtr {
src/ir.cpp+11-10
......@@ -16833,7 +16833,7 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
1683316833 if (ptr_field->data.x_ptr.special == ConstPtrSpecialHardCodedAddr) {
1683416834 IrInstruction *result = ir_build_elem_ptr(&ira->new_irb, elem_ptr_instruction->base.scope,
1683516835 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);
1683716837 result->value.type = return_type;
1683816838 return result;
1683916839 }
......@@ -16898,15 +16898,6 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
1689816898 }
1689916899 }
1690016900 }
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 }
1691016901 } else {
1691116902 // runtime known element index
1691216903 switch (type_requires_comptime(ira->codegen, return_type)) {
......@@ -19053,6 +19044,16 @@ static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira,
1905319044 IrInstruction *result_loc = instruction->result_loc->child;
1905419045 if (type_is_invalid(result_loc->value.type))
1905519046 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 }
1905619057 return ir_get_deref(ira, &instruction->base, result_loc, nullptr);
1905719058 } else if (container_type->id == ZigTypeIdVoid) {
1905819059 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" {
222222 const S = struct {
223223 fn entry(two: i32) void {
224224 const cases = [_][]const []const i32{
225 &[_][]const i32{&[_]i32{1}},
226 &[_][]const i32{&[_]i32{ 2, 3 }},
227 &[_][]const i32{
228 &[_]i32{4},
229 &[_]i32{ 5, 6, 7 },
225 [_][]const i32{[_]i32{1}},
226 [_][]const i32{[_]i32{ 2, 3 }},
227 [_][]const i32{
228 [_]i32{4},
229 [_]i32{ 5, 6, 7 },
230230 },
231231 };
232232 check(cases);
233233
234234 const cases2 = [_][]const i32{
235 &[_]i32{1},
235 [_]i32{1},
236236 &[_]i32{ two, 3 },
237237 };
238238 expect(cases2.len == 2);
......@@ -243,11 +243,11 @@ test "double nested array to const slice cast in array literal" {
243243 expect(cases2[1][1] == 3);
244244
245245 const cases3 = [_][]const []const i32{
246 &[_][]const i32{&[_]i32{1}},
246 [_][]const i32{[_]i32{1}},
247247 &[_][]const i32{&[_]i32{ two, 3 }},
248 &[_][]const i32{
249 &[_]i32{4},
250 &[_]i32{ 5, 6, 7 },
248 [_][]const i32{
249 [_]i32{4},
250 [_]i32{ 5, 6, 7 },
251251 },
252252 };
253253 check(cases3);