authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-22 10:04:47-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-22 10:04:47-04:00
log5c15c5fc48fa61569eca7bcd49e1c819db2a90c9
treec0095f46e83632ba5ac75fa1960efcd2ecfdcfe0
parent2e2740716103784334348c491a0e4d84052c5734
signaturelock-open Commit is signed but in an unrecognized format.

add compile error for slice of undefined slice

closes #1293

2 files changed, 15 insertions(+), 0 deletions(-)

src/ir.cpp+5
...@@ -19229,6 +19229,11 @@ static ZigType *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructionSlice...@@ -19229,6 +19229,11 @@ static ZigType *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructionSlice
19229 return ira->codegen->builtin_types.entry_invalid;19229 return ira->codegen->builtin_types.entry_invalid;
1923019230
19231 parent_ptr = &slice_ptr->data.x_struct.fields[slice_ptr_index];19231 parent_ptr = &slice_ptr->data.x_struct.fields[slice_ptr_index];
19232 if (parent_ptr->special == ConstValSpecialUndef) {
19233 ir_add_error(ira, &instruction->base, buf_sprintf("slice of undefined"));
19234 return ira->codegen->builtin_types.entry_invalid;
19235 }
19236
19232 ConstExprValue *len_val = &slice_ptr->data.x_struct.fields[slice_len_index];19237 ConstExprValue *len_val = &slice_ptr->data.x_struct.fields[slice_len_index];
1923319238
19234 switch (parent_ptr->data.x_ptr.special) {19239 switch (parent_ptr->data.x_ptr.special) {
test/compile_errors.zig+10
...@@ -1,6 +1,16 @@...@@ -1,6 +1,16 @@
1const tests = @import("tests.zig");1const tests = @import("tests.zig");
22
3pub fn addCases(cases: *tests.CompileErrorContext) void {3pub fn addCases(cases: *tests.CompileErrorContext) void {
4 cases.add(
5 "comptime slice of an undefined slice",
6 \\comptime {
7 \\ var a: []u8 = undefined;
8 \\ var b = a[0..10];
9 \\}
10 ,
11 ".tmp_source.zig:3:14: error: slice of undefined",
12 );
13
4 cases.add(14 cases.add(
5 "implicit cast const array to mutable slice",15 "implicit cast const array to mutable slice",
6 \\export fn entry() void {16 \\export fn entry() void {