aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2018-09-22 10:04:47 -0400
committerAndrew Kelley <superjoe30@gmail.com>2018-09-22 10:04:47 -0400
commit5c15c5fc48fa61569eca7bcd49e1c819db2a90c9 (patch)
treec0095f46e83632ba5ac75fa1960efcd2ecfdcfe0
parent2e2740716103784334348c491a0e4d84052c5734 (diff)
downloadzig-5c15c5fc48fa61569eca7bcd49e1c819db2a90c9.tar.gz
zig-5c15c5fc48fa61569eca7bcd49e1c819db2a90c9.zip
add compile error for slice of undefined slice
closes #1293
-rw-r--r--src/ir.cpp5
-rw-r--r--test/compile_errors.zig10
2 files changed, 15 insertions, 0 deletions
diff --git a/src/ir.cpp b/src/ir.cpp
index a35c6380a7..7e98e99af1 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 c1e25c1df9..62df9f077d 100644
--- a/test/compile_errors.zig
+++ b/test/compile_errors.zig
@@ -2,6 +2,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 {
\\ const buffer: [1]u8 = []u8{8};