aboutsummaryrefslogtreecommitdiff
path: root/test/compile_errors.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2020-05-16 01:26:18 -0400
committerAndrew Kelley <andrew@ziglang.org>2020-05-16 01:26:18 -0400
commit69a5f0d7973f2a3fefb69bc30c7dc1f0b430bba2 (patch)
treee3e8fad5e67b66f5b51b53c421187221d1cab1e5 /test/compile_errors.zig
parenta286b5de38617809db58f918a81a650b41fbdd49 (diff)
parentf8b99331a2ca98f0e938c8caaf1cd232ad1e9fa3 (diff)
downloadzig-69a5f0d7973f2a3fefb69bc30c7dc1f0b430bba2.tar.gz
zig-69a5f0d7973f2a3fefb69bc30c7dc1f0b430bba2.zip
Merge remote-tracking branch 'origin/master' into self-hosted-incremental-compilation
Diffstat (limited to 'test/compile_errors.zig')
-rw-r--r--test/compile_errors.zig121
1 files changed, 105 insertions, 16 deletions
diff --git a/test/compile_errors.zig b/test/compile_errors.zig
index fd27b8e666..a6e2d0b98f 100644
--- a/test/compile_errors.zig
+++ b/test/compile_errors.zig
@@ -2,6 +2,29 @@ const tests = @import("tests.zig");
const std = @import("std");
pub fn addCases(cases: *tests.CompileErrorContext) void {
+ cases.add("call assigned to constant",
+ \\const Foo = struct {
+ \\ x: i32,
+ \\};
+ \\fn foo() Foo {
+ \\ return .{ .x = 42 };
+ \\}
+ \\fn bar(val: var) Foo {
+ \\ return .{ .x = val };
+ \\}
+ \\export fn entry() void {
+ \\ const baz: Foo = undefined;
+ \\ baz = foo();
+ \\}
+ \\export fn entry1() void {
+ \\ const baz: Foo = undefined;
+ \\ baz = bar(42);
+ \\}
+ , &[_][]const u8{
+ "tmp.zig:12:14: error: cannot assign to constant",
+ "tmp.zig:16:14: error: cannot assign to constant",
+ });
+
cases.add("invalid pointer syntax",
\\export fn foo() void {
\\ var guid: *:0 const u8 = undefined;
@@ -243,9 +266,9 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
"tmp.zig:17:17: error: RHS of shift is too large for LHS type",
});
- cases.addTest("combination of noasync and async",
+ cases.addTest("combination of nosuspend and async",
\\export fn entry() void {
- \\ noasync {
+ \\ nosuspend {
\\ const bar = async foo();
\\ suspend;
\\ resume bar;
@@ -253,9 +276,9 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
\\}
\\fn foo() void {}
, &[_][]const u8{
- "tmp.zig:3:21: error: async call in noasync scope",
- "tmp.zig:4:9: error: suspend in noasync scope",
- "tmp.zig:5:9: error: resume in noasync scope",
+ "tmp.zig:3:21: error: async call in nosuspend scope",
+ "tmp.zig:4:9: error: suspend in nosuspend scope",
+ "tmp.zig:5:9: error: resume in nosuspend scope",
});
cases.add("atomicrmw with bool op not .Xchg",
@@ -779,7 +802,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
});
cases.add("exported async function",
- \\export async fn foo() void {}
+ \\export fn foo() callconv(.Async) void {}
, &[_][]const u8{
"tmp.zig:1:1: error: exported function cannot be async",
});
@@ -1258,11 +1281,11 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
cases.add("bad alignment in @asyncCall",
\\export fn entry() void {
- \\ var ptr: async fn () void = func;
+ \\ var ptr: fn () callconv(.Async) void = func;
\\ var bytes: [64]u8 = undefined;
\\ _ = @asyncCall(&bytes, {}, ptr);
\\}
- \\async fn func() void {}
+ \\fn func() callconv(.Async) void {}
, &[_][]const u8{
"tmp.zig:4:21: error: expected type '[]align(16) u8', found '*[64]u8'",
});
@@ -1408,7 +1431,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
\\export fn entry() void {
\\ _ = async amain();
\\}
- \\async fn amain() void {
+ \\fn amain() callconv(.Async) void {
\\ other();
\\}
\\fn other() void {
@@ -1424,7 +1447,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
\\export fn entry() void {
\\ _ = async amain();
\\}
- \\async fn amain() void {
+ \\fn amain() callconv(.Async) void {
\\ var x: [@sizeOf(@Frame(amain))]u8 = undefined;
\\}
, &[_][]const u8{
@@ -1451,7 +1474,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
\\ var ptr = afunc;
\\ _ = ptr();
\\}
- \\async fn afunc() void {}
+ \\fn afunc() callconv(.Async) void {}
, &[_][]const u8{
"tmp.zig:6:12: error: function is not comptime-known; @asyncCall required",
});
@@ -1462,7 +1485,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
\\ _ = async ptr();
\\}
\\
- \\async fn afunc() void { }
+ \\fn afunc() callconv(.Async) void { }
, &[_][]const u8{
"tmp.zig:3:15: error: function is not comptime-known; @asyncCall required",
});
@@ -3051,7 +3074,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
\\export fn entry() void {
\\ _ = async foo();
\\}
- \\async fn foo() void {
+ \\fn foo() void {
\\ suspend {
\\ suspend {
\\ }
@@ -3099,7 +3122,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
\\export fn entry() void {
\\ _ = async amain();
\\}
- \\async fn amain() void {
+ \\fn amain() callconv(.Async) void {
\\ return error.ShouldBeCompileError;
\\}
, &[_][]const u8{
@@ -3569,7 +3592,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
});
cases.add("attempt to use 0 bit type in extern fn",
- \\extern fn foo(ptr: extern fn(*void) void) void;
+ \\extern fn foo(ptr: fn(*void) callconv(.C) void) void;
\\
\\export fn entry() void {
\\ foo(bar);
@@ -3580,7 +3603,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
\\ bar(&{});
\\}
, &[_][]const u8{
- "tmp.zig:1:30: error: parameter of type '*void' has 0 bits; not allowed in function with calling convention 'C'",
+ "tmp.zig:1:23: error: parameter of type '*void' has 0 bits; not allowed in function with calling convention 'C'",
"tmp.zig:7:11: error: parameter of type '*void' has 0 bits; not allowed in function with calling convention 'C'",
});
@@ -5352,6 +5375,50 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
break :x tc;
});
+ cases.addCase(x: {
+ const tc = cases.create("multiple files with private member instance function (canonical invocation) error",
+ \\const Foo = @import("foo.zig",).Foo;
+ \\
+ \\export fn callPrivFunction() void {
+ \\ var foo = Foo{};
+ \\ Foo.privateFunction(foo);
+ \\}
+ , &[_][]const u8{
+ "tmp.zig:5:8: error: 'privateFunction' is private",
+ "foo.zig:2:5: note: declared here",
+ });
+
+ tc.addSourceFile("foo.zig",
+ \\pub const Foo = struct {
+ \\ fn privateFunction(self: *Foo) void { }
+ \\};
+ );
+
+ break :x tc;
+ });
+
+ cases.addCase(x: {
+ const tc = cases.create("multiple files with private member instance function error",
+ \\const Foo = @import("foo.zig",).Foo;
+ \\
+ \\export fn callPrivFunction() void {
+ \\ var foo = Foo{};
+ \\ foo.privateFunction();
+ \\}
+ , &[_][]const u8{
+ "tmp.zig:5:8: error: 'privateFunction' is private",
+ "foo.zig:2:5: note: declared here",
+ });
+
+ tc.addSourceFile("foo.zig",
+ \\pub const Foo = struct {
+ \\ fn privateFunction(self: *Foo) void { }
+ \\};
+ );
+
+ break :x tc;
+ });
+
cases.add("container init with non-type",
\\const zero: i32 = 0;
\\const a = zero{1};
@@ -7330,4 +7397,26 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
":3:18: error: expected type '[*:0]const u8', found '*[64]u8'",
":3:18: note: destination pointer requires a terminating '0' sentinel",
});
+
+ cases.add("issue #5221: invalid struct init type referenced by @typeInfo and passed into function",
+ \\fn ignore(comptime param: var) void {}
+ \\
+ \\export fn foo() void {
+ \\ const MyStruct = struct {
+ \\ wrong_type: []u8 = "foo",
+ \\ };
+ \\
+ \\ comptime ignore(@typeInfo(MyStruct).Struct.fields[0]);
+ \\}
+ , &[_][]const u8{
+ ":5:28: error: expected type '[]u8', found '*const [3:0]u8'",
+ });
+
+ cases.add("integer underflow error",
+ \\export fn entry() void {
+ \\ _ = @intToPtr(*c_void, ~@as(usize, @import("std").math.maxInt(usize)) - 1);
+ \\}
+ , &[_][]const u8{
+ ":2:75: error: operation caused overflow",
+ });
}