aboutsummaryrefslogtreecommitdiff
path: root/test/compile_errors.zig
diff options
context:
space:
mode:
authorMichael Dusan <michael.dusan@gmail.com>2020-04-03 11:41:55 -0400
committerAndrew Kelley <andrew@ziglang.org>2020-04-03 19:05:30 -0400
commitdb4c06ce606dcfe989b5c1f76d5ed00e90d5dd9f (patch)
tree9feedf90f8670da85296ca253c7f79053a87c839 /test/compile_errors.zig
parentf1425fd9da1b2e1297a36108b12b0bd89398b057 (diff)
downloadzig-db4c06ce606dcfe989b5c1f76d5ed00e90d5dd9f.tar.gz
zig-db4c06ce606dcfe989b5c1f76d5ed00e90d5dd9f.zip
stage1: add compile errors for sentinel slicing
closes #3963
Diffstat (limited to 'test/compile_errors.zig')
-rw-r--r--test/compile_errors.zig295
1 files changed, 295 insertions, 0 deletions
diff --git a/test/compile_errors.zig b/test/compile_errors.zig
index e54f6591b5..a3c29e0dd6 100644
--- a/test/compile_errors.zig
+++ b/test/compile_errors.zig
@@ -6857,4 +6857,299 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
"tmp.zig:7:32: error: destination type 'u16' has size 2 but source type '[]u8' has size 16",
"tmp.zig:7:37: note: referenced here",
});
+
+ cases.add("comptime slice-sentinel is out of bounds (unterminated)",
+ \\export fn foo_array() void {
+ \\ comptime {
+ \\ var target = [_]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
+ \\ const slice = target[0..14 :0];
+ \\ }
+ \\}
+ \\export fn foo_ptr_array() void {
+ \\ comptime {
+ \\ var buf = [_]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
+ \\ var target = &buf;
+ \\ const slice = target[0..14 :0];
+ \\ }
+ \\}
+ \\export fn foo_vector_ConstPtrSpecialBaseArray() void {
+ \\ comptime {
+ \\ var buf = [_]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
+ \\ var target: [*]u8 = &buf;
+ \\ const slice = target[0..14 :0];
+ \\ }
+ \\}
+ \\export fn foo_vector_ConstPtrSpecialRef() void {
+ \\ comptime {
+ \\ var buf = [_]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
+ \\ var target: [*]u8 = @ptrCast([*]u8, &buf);
+ \\ const slice = target[0..14 :0];
+ \\ }
+ \\}
+ \\export fn foo_cvector_ConstPtrSpecialBaseArray() void {
+ \\ comptime {
+ \\ var buf = [_]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
+ \\ var target: [*c]u8 = &buf;
+ \\ const slice = target[0..14 :0];
+ \\ }
+ \\}
+ \\export fn foo_cvector_ConstPtrSpecialRef() void {
+ \\ comptime {
+ \\ var buf = [_]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
+ \\ var target: [*c]u8 = @ptrCast([*c]u8, &buf);
+ \\ const slice = target[0..14 :0];
+ \\ }
+ \\}
+ \\export fn foo_slice() void {
+ \\ comptime {
+ \\ var buf = [_]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
+ \\ var target: []u8 = &buf;
+ \\ const slice = target[0..14 :0];
+ \\ }
+ \\}
+ , &[_][]const u8{
+ ":4:29: error: slice-sentinel is out of bounds",
+ ":11:29: error: slice-sentinel is out of bounds",
+ ":18:29: error: slice-sentinel is out of bounds",
+ ":25:29: error: slice-sentinel is out of bounds",
+ ":32:29: error: slice-sentinel is out of bounds",
+ ":39:29: error: slice-sentinel is out of bounds",
+ ":46:29: error: slice-sentinel is out of bounds",
+ });
+
+ cases.add("comptime slice-sentinel is out of bounds (terminated)",
+ \\export fn foo_array() void {
+ \\ comptime {
+ \\ var target = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
+ \\ const slice = target[0..15 :1];
+ \\ }
+ \\}
+ \\export fn foo_ptr_array() void {
+ \\ comptime {
+ \\ var buf = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
+ \\ var target = &buf;
+ \\ const slice = target[0..15 :0];
+ \\ }
+ \\}
+ \\export fn foo_vector_ConstPtrSpecialBaseArray() void {
+ \\ comptime {
+ \\ var buf = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
+ \\ var target: [*]u8 = &buf;
+ \\ const slice = target[0..15 :0];
+ \\ }
+ \\}
+ \\export fn foo_vector_ConstPtrSpecialRef() void {
+ \\ comptime {
+ \\ var buf = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
+ \\ var target: [*]u8 = @ptrCast([*]u8, &buf);
+ \\ const slice = target[0..15 :0];
+ \\ }
+ \\}
+ \\export fn foo_cvector_ConstPtrSpecialBaseArray() void {
+ \\ comptime {
+ \\ var buf = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
+ \\ var target: [*c]u8 = &buf;
+ \\ const slice = target[0..15 :0];
+ \\ }
+ \\}
+ \\export fn foo_cvector_ConstPtrSpecialRef() void {
+ \\ comptime {
+ \\ var buf = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
+ \\ var target: [*c]u8 = @ptrCast([*c]u8, &buf);
+ \\ const slice = target[0..15 :0];
+ \\ }
+ \\}
+ \\export fn foo_slice() void {
+ \\ comptime {
+ \\ var buf = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
+ \\ var target: []u8 = &buf;
+ \\ const slice = target[0..15 :0];
+ \\ }
+ \\}
+ , &[_][]const u8{
+ ":4:29: error: out of bounds slice",
+ ":11:29: error: out of bounds slice",
+ ":18:29: error: out of bounds slice",
+ ":25:29: error: out of bounds slice",
+ ":32:29: error: out of bounds slice",
+ ":39:29: error: out of bounds slice",
+ ":46:29: error: out of bounds slice",
+ });
+
+ cases.add("comptime slice-sentinel does not match memory at target index (unterminated)",
+ \\export fn foo_array() void {
+ \\ comptime {
+ \\ var target = [_]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
+ \\ const slice = target[0..3 :0];
+ \\ }
+ \\}
+ \\export fn foo_ptr_array() void {
+ \\ comptime {
+ \\ var buf = [_]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
+ \\ var target = &buf;
+ \\ const slice = target[0..3 :0];
+ \\ }
+ \\}
+ \\export fn foo_vector_ConstPtrSpecialBaseArray() void {
+ \\ comptime {
+ \\ var buf = [_]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
+ \\ var target: [*]u8 = &buf;
+ \\ const slice = target[0..3 :0];
+ \\ }
+ \\}
+ \\export fn foo_vector_ConstPtrSpecialRef() void {
+ \\ comptime {
+ \\ var buf = [_]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
+ \\ var target: [*]u8 = @ptrCast([*]u8, &buf);
+ \\ const slice = target[0..3 :0];
+ \\ }
+ \\}
+ \\export fn foo_cvector_ConstPtrSpecialBaseArray() void {
+ \\ comptime {
+ \\ var buf = [_]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
+ \\ var target: [*c]u8 = &buf;
+ \\ const slice = target[0..3 :0];
+ \\ }
+ \\}
+ \\export fn foo_cvector_ConstPtrSpecialRef() void {
+ \\ comptime {
+ \\ var buf = [_]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
+ \\ var target: [*c]u8 = @ptrCast([*c]u8, &buf);
+ \\ const slice = target[0..3 :0];
+ \\ }
+ \\}
+ \\export fn foo_slice() void {
+ \\ comptime {
+ \\ var buf = [_]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
+ \\ var target: []u8 = &buf;
+ \\ const slice = target[0..3 :0];
+ \\ }
+ \\}
+ , &[_][]const u8{
+ ":4:29: error: slice-sentinel does not match memory at target index",
+ ":11:29: error: slice-sentinel does not match memory at target index",
+ ":18:29: error: slice-sentinel does not match memory at target index",
+ ":25:29: error: slice-sentinel does not match memory at target index",
+ ":32:29: error: slice-sentinel does not match memory at target index",
+ ":39:29: error: slice-sentinel does not match memory at target index",
+ ":46:29: error: slice-sentinel does not match memory at target index",
+ });
+
+ cases.add("comptime slice-sentinel does not match memory at target index (terminated)",
+ \\export fn foo_array() void {
+ \\ comptime {
+ \\ var target = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
+ \\ const slice = target[0..3 :0];
+ \\ }
+ \\}
+ \\export fn foo_ptr_array() void {
+ \\ comptime {
+ \\ var buf = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
+ \\ var target = &buf;
+ \\ const slice = target[0..3 :0];
+ \\ }
+ \\}
+ \\export fn foo_vector_ConstPtrSpecialBaseArray() void {
+ \\ comptime {
+ \\ var buf = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
+ \\ var target: [*]u8 = &buf;
+ \\ const slice = target[0..3 :0];
+ \\ }
+ \\}
+ \\export fn foo_vector_ConstPtrSpecialRef() void {
+ \\ comptime {
+ \\ var buf = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
+ \\ var target: [*]u8 = @ptrCast([*]u8, &buf);
+ \\ const slice = target[0..3 :0];
+ \\ }
+ \\}
+ \\export fn foo_cvector_ConstPtrSpecialBaseArray() void {
+ \\ comptime {
+ \\ var buf = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
+ \\ var target: [*c]u8 = &buf;
+ \\ const slice = target[0..3 :0];
+ \\ }
+ \\}
+ \\export fn foo_cvector_ConstPtrSpecialRef() void {
+ \\ comptime {
+ \\ var buf = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
+ \\ var target: [*c]u8 = @ptrCast([*c]u8, &buf);
+ \\ const slice = target[0..3 :0];
+ \\ }
+ \\}
+ \\export fn foo_slice() void {
+ \\ comptime {
+ \\ var buf = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
+ \\ var target: []u8 = &buf;
+ \\ const slice = target[0..3 :0];
+ \\ }
+ \\}
+ , &[_][]const u8{
+ ":4:29: error: slice-sentinel does not match memory at target index",
+ ":11:29: error: slice-sentinel does not match memory at target index",
+ ":18:29: error: slice-sentinel does not match memory at target index",
+ ":25:29: error: slice-sentinel does not match memory at target index",
+ ":32:29: error: slice-sentinel does not match memory at target index",
+ ":39:29: error: slice-sentinel does not match memory at target index",
+ ":46:29: error: slice-sentinel does not match memory at target index",
+ });
+
+ cases.add("comptime slice-sentinel does not match target-sentinel",
+ \\export fn foo_array() void {
+ \\ comptime {
+ \\ var target = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
+ \\ const slice = target[0..14 :255];
+ \\ }
+ \\}
+ \\export fn foo_ptr_array() void {
+ \\ comptime {
+ \\ var buf = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
+ \\ var target = &buf;
+ \\ const slice = target[0..14 :255];
+ \\ }
+ \\}
+ \\export fn foo_vector_ConstPtrSpecialBaseArray() void {
+ \\ comptime {
+ \\ var buf = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
+ \\ var target: [*]u8 = &buf;
+ \\ const slice = target[0..14 :255];
+ \\ }
+ \\}
+ \\export fn foo_vector_ConstPtrSpecialRef() void {
+ \\ comptime {
+ \\ var buf = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
+ \\ var target: [*]u8 = @ptrCast([*]u8, &buf);
+ \\ const slice = target[0..14 :255];
+ \\ }
+ \\}
+ \\export fn foo_cvector_ConstPtrSpecialBaseArray() void {
+ \\ comptime {
+ \\ var buf = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
+ \\ var target: [*c]u8 = &buf;
+ \\ const slice = target[0..14 :255];
+ \\ }
+ \\}
+ \\export fn foo_cvector_ConstPtrSpecialRef() void {
+ \\ comptime {
+ \\ var buf = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
+ \\ var target: [*c]u8 = @ptrCast([*c]u8, &buf);
+ \\ const slice = target[0..14 :255];
+ \\ }
+ \\}
+ \\export fn foo_slice() void {
+ \\ comptime {
+ \\ var buf = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
+ \\ var target: []u8 = &buf;
+ \\ const slice = target[0..14 :255];
+ \\ }
+ \\}
+ , &[_][]const u8{
+ ":4:29: error: slice-sentinel does not match target-sentinel",
+ ":11:29: error: slice-sentinel does not match target-sentinel",
+ ":18:29: error: slice-sentinel does not match target-sentinel",
+ ":25:29: error: slice-sentinel does not match target-sentinel",
+ ":32:29: error: slice-sentinel does not match target-sentinel",
+ ":39:29: error: slice-sentinel does not match target-sentinel",
+ ":46:29: error: slice-sentinel does not match target-sentinel",
+ });
}