diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2020-05-16 01:26:18 -0400 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2020-05-16 01:26:18 -0400 |
| commit | 69a5f0d7973f2a3fefb69bc30c7dc1f0b430bba2 (patch) | |
| tree | e3e8fad5e67b66f5b51b53c421187221d1cab1e5 /test | |
| parent | a286b5de38617809db58f918a81a650b41fbdd49 (diff) | |
| parent | f8b99331a2ca98f0e938c8caaf1cd232ad1e9fa3 (diff) | |
| download | zig-69a5f0d7973f2a3fefb69bc30c7dc1f0b430bba2.tar.gz zig-69a5f0d7973f2a3fefb69bc30c7dc1f0b430bba2.zip | |
Merge remote-tracking branch 'origin/master' into self-hosted-incremental-compilation
Diffstat (limited to 'test')
| -rw-r--r-- | test/compile_errors.zig | 121 | ||||
| -rw-r--r-- | test/run_translated_c.zig | 13 | ||||
| -rw-r--r-- | test/runtime_safety.zig | 12 | ||||
| -rw-r--r-- | test/stack_traces.zig | 2 | ||||
| -rw-r--r-- | test/stage1/behavior/async_fn.zig | 70 | ||||
| -rw-r--r-- | test/stage1/behavior/await_struct.zig | 4 | ||||
| -rw-r--r-- | test/stage1/behavior/cast.zig | 13 | ||||
| -rw-r--r-- | test/standalone/main_return_error/error_u8.zig | 4 | ||||
| -rw-r--r-- | test/standalone/main_return_error/error_u8_non_zero.zig | 7 |
9 files changed, 177 insertions, 69 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", + }); } diff --git a/test/run_translated_c.zig b/test/run_translated_c.zig index ec6a11b9b4..7dc64f068f 100644 --- a/test/run_translated_c.zig +++ b/test/run_translated_c.zig @@ -243,4 +243,17 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void { \\ return 0; \\} , ""); + + cases.add("scoped typedef", + \\int main(int argc, char **argv) { + \\ typedef int Foo; + \\ typedef Foo Bar; + \\ typedef void (*func)(int); + \\ typedef int uint32_t; + \\ uint32_t a; + \\ Foo i; + \\ Bar j; + \\ return 0; + \\} + , ""); } diff --git a/test/runtime_safety.zig b/test/runtime_safety.zig index 19af470e2f..2e50c3a84c 100644 --- a/test/runtime_safety.zig +++ b/test/runtime_safety.zig @@ -234,12 +234,12 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { \\} ); - cases.addRuntimeSafety("noasync function call, callee suspends", + cases.addRuntimeSafety("nosuspend function call, callee suspends", \\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn { \\ @import("std").os.exit(126); \\} \\pub fn main() void { - \\ _ = noasync add(101, 100); + \\ _ = nosuspend add(101, 100); \\} \\fn add(a: i32, b: i32) i32 { \\ if (a > 100) { @@ -282,7 +282,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { \\ var ptr = other; \\ var frame = @asyncCall(&bytes, {}, ptr); \\} - \\async fn other() void { + \\fn other() callconv(.Async) void { \\ suspend; \\} ); @@ -874,16 +874,16 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { \\ return &failing_frame; \\} \\ - \\async fn failing() anyerror!void { + \\fn failing() anyerror!void { \\ suspend; \\ return second(); \\} \\ - \\async fn second() anyerror!void { + \\fn second() callconv(.Async) anyerror!void { \\ return error.Fail; \\} \\ - \\async fn printTrace(p: anyframe->anyerror!void) void { + \\fn printTrace(p: anyframe->anyerror!void) void { \\ (await p) catch unreachable; \\} ); diff --git a/test/stack_traces.zig b/test/stack_traces.zig index 2f7201e71d..616136b9ee 100644 --- a/test/stack_traces.zig +++ b/test/stack_traces.zig @@ -282,7 +282,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void { \\source.zig:10:8: [address] in main (test) \\ foo(); \\ ^ - \\start.zig:250:29: [address] in std.start.posixCallMainAndExit (test) + \\start.zig:249:29: [address] in std.start.posixCallMainAndExit (test) \\ return root.main(); \\ ^ \\start.zig:123:5: [address] in std.start._start (test) diff --git a/test/stage1/behavior/async_fn.zig b/test/stage1/behavior/async_fn.zig index 958e9ea55f..30df7f64aa 100644 --- a/test/stage1/behavior/async_fn.zig +++ b/test/stage1/behavior/async_fn.zig @@ -112,12 +112,12 @@ test "@frameSize" { const S = struct { fn doTheTest() void { { - var ptr = @ptrCast(async fn (i32) void, other); + var ptr = @ptrCast(fn (i32) callconv(.Async) void, other); const size = @frameSize(ptr); expect(size == @sizeOf(@Frame(other))); } { - var ptr = @ptrCast(async fn () void, first); + var ptr = @ptrCast(fn () callconv(.Async) void, first); const size = @frameSize(ptr); expect(size == @sizeOf(@Frame(first))); } @@ -184,7 +184,7 @@ test "coroutine suspend with block" { var a_promise: anyframe = undefined; var global_result = false; -async fn testSuspendBlock() void { +fn testSuspendBlock() callconv(.Async) void { suspend { comptime expect(@TypeOf(@frame()) == *@Frame(testSuspendBlock)); a_promise = @frame(); @@ -209,14 +209,14 @@ test "coroutine await" { expect(await_final_result == 1234); expect(std.mem.eql(u8, &await_points, "abcdefghi")); } -async fn await_amain() void { +fn await_amain() callconv(.Async) void { await_seq('b'); var p = async await_another(); await_seq('e'); await_final_result = await p; await_seq('h'); } -async fn await_another() i32 { +fn await_another() callconv(.Async) i32 { await_seq('c'); suspend { await_seq('d'); @@ -243,14 +243,14 @@ test "coroutine await early return" { expect(early_final_result == 1234); expect(std.mem.eql(u8, &early_points, "abcdef")); } -async fn early_amain() void { +fn early_amain() callconv(.Async) void { early_seq('b'); var p = async early_another(); early_seq('d'); early_final_result = await p; early_seq('e'); } -async fn early_another() i32 { +fn early_another() callconv(.Async) i32 { early_seq('c'); return 1234; } @@ -266,7 +266,7 @@ fn early_seq(c: u8) void { test "async function with dot syntax" { const S = struct { var y: i32 = 1; - async fn foo() void { + fn foo() callconv(.Async) void { y += 1; suspend; } @@ -278,7 +278,7 @@ test "async function with dot syntax" { test "async fn pointer in a struct field" { var data: i32 = 1; const Foo = struct { - bar: async fn (*i32) void, + bar: fn (*i32) callconv(.Async) void, }; var foo = Foo{ .bar = simpleAsyncFn2 }; var bytes: [64]u8 align(16) = undefined; @@ -294,8 +294,7 @@ test "async fn pointer in a struct field" { fn doTheAwait(f: anyframe->void) void { await f; } - -async fn simpleAsyncFn2(y: *i32) void { +fn simpleAsyncFn2(y: *i32) callconv(.Async) void { defer y.* += 2; y.* += 1; suspend; @@ -303,11 +302,10 @@ async fn simpleAsyncFn2(y: *i32) void { test "@asyncCall with return type" { const Foo = struct { - bar: async fn () i32, + bar: fn () callconv(.Async) i32, var global_frame: anyframe = undefined; - - async fn middle() i32 { + fn middle() callconv(.Async) i32 { return afunc(); } @@ -338,8 +336,7 @@ test "async fn with inferred error set" { resume global_frame; std.testing.expectError(error.Fail, result); } - - async fn middle() !void { + fn middle() callconv(.Async) !void { var f = async middle2(); return await f; } @@ -376,11 +373,11 @@ fn nonFailing() (anyframe->anyerror!void) { Static.frame = async suspendThenFail(); return &Static.frame; } -async fn suspendThenFail() anyerror!void { +fn suspendThenFail() callconv(.Async) anyerror!void { suspend; return error.Fail; } -async fn printTrace(p: anyframe->(anyerror!void)) void { +fn printTrace(p: anyframe->(anyerror!void)) callconv(.Async) void { (await p) catch |e| { std.testing.expect(e == error.Fail); if (@errorReturnTrace()) |trace| { @@ -397,7 +394,7 @@ test "break from suspend" { const p = async testBreakFromSuspend(&my_result); std.testing.expect(my_result == 2); } -async fn testBreakFromSuspend(my_result: *i32) void { +fn testBreakFromSuspend(my_result: *i32) callconv(.Async) void { suspend { resume @frame(); } @@ -826,7 +823,7 @@ test "cast fn to async fn when it is inferred to be async" { var ok = false; fn doTheTest() void { - var ptr: async fn () i32 = undefined; + var ptr: fn () callconv(.Async) i32 = undefined; ptr = func; var buf: [100]u8 align(16) = undefined; var result: i32 = undefined; @@ -854,7 +851,7 @@ test "cast fn to async fn when it is inferred to be async, awaited directly" { var ok = false; fn doTheTest() void { - var ptr: async fn () i32 = undefined; + var ptr: fn () callconv(.Async) i32 = undefined; ptr = func; var buf: [100]u8 align(16) = undefined; var result: i32 = undefined; @@ -958,8 +955,7 @@ test "@asyncCall with comptime-known function, but not awaited directly" { resume global_frame; std.testing.expectError(error.Fail, result); } - - async fn middle() !void { + fn middle() callconv(.Async) !void { var f = async middle2(); return await f; } @@ -993,7 +989,7 @@ test "@asyncCall with actual frame instead of byte buffer" { test "@asyncCall using the result location inside the frame" { const S = struct { - async fn simple2(y: *i32) i32 { + fn simple2(y: *i32) callconv(.Async) i32 { defer y.* += 2; y.* += 1; suspend; @@ -1005,7 +1001,7 @@ test "@asyncCall using the result location inside the frame" { }; var data: i32 = 1; const Foo = struct { - bar: async fn (*i32) i32, + bar: fn (*i32) callconv(.Async) i32, }; var foo = Foo{ .bar = S.simple2 }; var bytes: [64]u8 align(16) = undefined; @@ -1090,10 +1086,10 @@ test "recursive call of await @asyncCall with struct return type" { expect(res.z == 3); } -test "noasync function call" { +test "nosuspend function call" { const S = struct { fn doTheTest() void { - const result = noasync add(50, 100); + const result = nosuspend add(50, 100); expect(result == 150); } fn add(a: i32, b: i32) i32 { @@ -1115,7 +1111,7 @@ test "await used in expression and awaiting fn with no suspend but async calling const sum = (await f1) + (await f2); expect(sum == 10); } - async fn add(a: i32, b: i32) i32 { + fn add(a: i32, b: i32) callconv(.Async) i32 { return a + b; } }; @@ -1130,7 +1126,7 @@ test "await used in expression after a fn call" { sum = foo() + await f1; expect(sum == 8); } - async fn add(a: i32, b: i32) i32 { + fn add(a: i32, b: i32) callconv(.Async) i32 { return a + b; } fn foo() i32 { @@ -1147,7 +1143,7 @@ test "async fn call used in expression after a fn call" { sum = foo() + add(3, 4); expect(sum == 8); } - async fn add(a: i32, b: i32) i32 { + fn add(a: i32, b: i32) callconv(.Async) i32 { return a + b; } fn foo() i32 { @@ -1403,7 +1399,7 @@ test "async function call resolves target fn frame, runtime func" { fn foo() anyerror!void { const stack_size = 1000; var stack_frame: [stack_size]u8 align(std.Target.stack_align) = undefined; - var func: async fn () anyerror!void = bar; + var func: fn () callconv(.Async) anyerror!void = bar; return await @asyncCall(&stack_frame, {}, func); } @@ -1511,13 +1507,13 @@ test "take address of temporary async frame" { S.doTheTest(); } -test "noasync await" { +test "nosuspend await" { const S = struct { var finished = false; fn doTheTest() void { var frame = async foo(false); - expect(noasync await frame == 42); + expect(nosuspend await frame == 42); finished = true; } @@ -1532,7 +1528,7 @@ test "noasync await" { expect(S.finished); } -test "noasync on function calls" { +test "nosuspend on function calls" { const S0 = struct { b: i32 = 42, }; @@ -1544,8 +1540,8 @@ test "noasync on function calls" { return S0{}; } }; - expectEqual(@as(i32, 42), noasync S1.c().b); - expectEqual(@as(i32, 42), (try noasync S1.d()).b); + expectEqual(@as(i32, 42), nosuspend S1.c().b); + expectEqual(@as(i32, 42), (try nosuspend S1.d()).b); } test "avoid forcing frame alignment resolution implicit cast to *c_void" { @@ -1561,5 +1557,5 @@ test "avoid forcing frame alignment resolution implicit cast to *c_void" { }; var frame = async S.foo(); resume @ptrCast(anyframe->bool, @alignCast(@alignOf(@Frame(S.foo)), S.x)); - expect(noasync await frame); + expect(nosuspend await frame); } diff --git a/test/stage1/behavior/await_struct.zig b/test/stage1/behavior/await_struct.zig index 2d4faadc27..328f8f87fd 100644 --- a/test/stage1/behavior/await_struct.zig +++ b/test/stage1/behavior/await_struct.zig @@ -18,14 +18,14 @@ test "coroutine await struct" { expect(await_final_result.x == 1234); expect(std.mem.eql(u8, &await_points, "abcdefghi")); } -async fn await_amain() void { +fn await_amain() callconv(.Async) void { await_seq('b'); var p = async await_another(); await_seq('e'); await_final_result = await p; await_seq('h'); } -async fn await_another() Foo { +fn await_another() callconv(.Async) Foo { await_seq('c'); suspend { await_seq('d'); diff --git a/test/stage1/behavior/cast.zig b/test/stage1/behavior/cast.zig index d277590c7a..77cdacc307 100644 --- a/test/stage1/behavior/cast.zig +++ b/test/stage1/behavior/cast.zig @@ -762,7 +762,7 @@ test "variable initialization uses result locations properly with regards to the test "cast between [*c]T and ?[*:0]T on fn parameter" { const S = struct { - const Handler = ?extern fn ([*c]const u8) void; + const Handler = ?fn ([*c]const u8) callconv(.C) void; fn addCallback(handler: Handler) void {} fn myCallback(cstr: ?[*:0]const u8) callconv(.C) void {} @@ -823,7 +823,16 @@ test "peer type resolve array pointer and unknown pointer" { comptime expect(@TypeOf(&array, const_ptr) == [*]const u8); comptime expect(@TypeOf(const_ptr, &array) == [*]const u8); - + comptime expect(@TypeOf(&const_array, const_ptr) == [*]const u8); comptime expect(@TypeOf(const_ptr, &const_array) == [*]const u8); } + +test "comptime float casts" { + const a = @intToFloat(comptime_float, 1); + expect(a == 1); + expect(@TypeOf(a) == comptime_float); + const b = @floatToInt(comptime_int, 2); + expect(b == 2); + expect(@TypeOf(b) == comptime_int); +} diff --git a/test/standalone/main_return_error/error_u8.zig b/test/standalone/main_return_error/error_u8.zig index 4edac26afd..8fbe1e8f65 100644 --- a/test/standalone/main_return_error/error_u8.zig +++ b/test/standalone/main_return_error/error_u8.zig @@ -1,6 +1,4 @@ -const Err = error { - Foo -}; +const Err = error{Foo}; pub fn main() !u8 { return Err.Foo; diff --git a/test/standalone/main_return_error/error_u8_non_zero.zig b/test/standalone/main_return_error/error_u8_non_zero.zig index 703a19b314..9f7de780ac 100644 --- a/test/standalone/main_return_error/error_u8_non_zero.zig +++ b/test/standalone/main_return_error/error_u8_non_zero.zig @@ -1,6 +1,9 @@ -const Err = error { Foo }; +const Err = error{Foo}; -fn foo() u8 { var x = @intCast(u8, 9); return x; } +fn foo() u8 { + var x = @intCast(u8, 9); + return x; +} pub fn main() !u8 { if (foo() == 7) return Err.Foo; |
