add compile error for slice of undefined slice

closes #1293
This commit is contained in:
Andrew Kelley 2018-09-22 10:04:47 -04:00
parent 2e27407161
commit 5c15c5fc48
No known key found for this signature in database
GPG Key ID: 4E7CD66038A4D47C
2 changed files with 15 additions and 0 deletions

View File

@ -19229,6 +19229,11 @@ static ZigType *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructionSlice
return ira->codegen->builtin_types.entry_invalid;
parent_ptr = &slice_ptr->data.x_struct.fields[slice_ptr_index];
if (parent_ptr->special == ConstValSpecialUndef) {
ir_add_error(ira, &instruction->base, buf_sprintf("slice of undefined"));
return ira->codegen->builtin_types.entry_invalid;
}
ConstExprValue *len_val = &slice_ptr->data.x_struct.fields[slice_len_index];
switch (parent_ptr->data.x_ptr.special) {

View File

@ -1,6 +1,16 @@
const tests = @import("tests.zig");
pub fn addCases(cases: *tests.CompileErrorContext) void {
cases.add(
"comptime slice of an undefined slice",
\\comptime {
\\ var a: []u8 = undefined;
\\ var b = a[0..10];
\\}
,
".tmp_source.zig:3:14: error: slice of undefined",
);
cases.add(
"implicit cast const array to mutable slice",
\\export fn entry() void {