diff --git a/src/ir.cpp b/src/ir.cpp index a35c6380a7e55a07498ffb3b9bb3201525e0fef4..7e98e99af10629aa134fb3657e0b60130f1e121e 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -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) { diff --git a/test/compile_errors.zig b/test/compile_errors.zig index c1e25c1df9a96a52a2d7368cc74cdfc88859d5b8..62df9f077d42da91425c3329ca744af3b1dba3ab 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -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 {