authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-05-04 21:23:22+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-05-08 15:15:30+03:00
log0a38f61d58018ce856ce058efb655ff0199228d2
tree8e56b966eb21616e4f5a3adef36f94b0233243bf
parentfd77f2cfed81f3414c079909e079a812e23071c3

update usage of std.testing in behavior and standalone tests


125 files changed, 3807 insertions(+), 3817 deletions(-)

test/cli.zig+14-14
...@@ -29,7 +29,7 @@ pub fn main() !void {...@@ -29,7 +29,7 @@ pub fn main() !void {
2929
30 const dir_path = try fs.path.join(a, &[_][]const u8{ cache_root, "clitest" });30 const dir_path = try fs.path.join(a, &[_][]const u8{ cache_root, "clitest" });
31 defer fs.cwd().deleteTree(dir_path) catch {};31 defer fs.cwd().deleteTree(dir_path) catch {};
32 32
33 const TestFn = fn ([]const u8, []const u8) anyerror!void;33 const TestFn = fn ([]const u8, []const u8) anyerror!void;
34 const test_fns = [_]TestFn{34 const test_fns = [_]TestFn{
35 testZigInitLib,35 testZigInitLib,
...@@ -94,13 +94,13 @@ fn exec(cwd: []const u8, expect_0: bool, argv: []const []const u8) !ChildProcess...@@ -94,13 +94,13 @@ fn exec(cwd: []const u8, expect_0: bool, argv: []const []const u8) !ChildProcess
94fn testZigInitLib(zig_exe: []const u8, dir_path: []const u8) !void {94fn testZigInitLib(zig_exe: []const u8, dir_path: []const u8) !void {
95 _ = try exec(dir_path, true, &[_][]const u8{ zig_exe, "init-lib" });95 _ = try exec(dir_path, true, &[_][]const u8{ zig_exe, "init-lib" });
96 const test_result = try exec(dir_path, true, &[_][]const u8{ zig_exe, "build", "test" });96 const test_result = try exec(dir_path, true, &[_][]const u8{ zig_exe, "build", "test" });
97 testing.expectStringEndsWith(test_result.stderr, "All 1 tests passed.\n");97 try testing.expectStringEndsWith(test_result.stderr, "All 1 tests passed.\n");
98}98}
9999
100fn testZigInitExe(zig_exe: []const u8, dir_path: []const u8) !void {100fn testZigInitExe(zig_exe: []const u8, dir_path: []const u8) !void {
101 _ = try exec(dir_path, true, &[_][]const u8{ zig_exe, "init-exe" });101 _ = try exec(dir_path, true, &[_][]const u8{ zig_exe, "init-exe" });
102 const run_result = try exec(dir_path, true, &[_][]const u8{ zig_exe, "build", "run" });102 const run_result = try exec(dir_path, true, &[_][]const u8{ zig_exe, "build", "run" });
103 testing.expectEqualStrings("info: All your codebase are belong to us.\n", run_result.stderr);103 try testing.expectEqualStrings("info: All your codebase are belong to us.\n", run_result.stderr);
104}104}
105105
106fn testGodboltApi(zig_exe: []const u8, dir_path: []const u8) anyerror!void {106fn testGodboltApi(zig_exe: []const u8, dir_path: []const u8) anyerror!void {
...@@ -136,9 +136,9 @@ fn testGodboltApi(zig_exe: []const u8, dir_path: []const u8) anyerror!void {...@@ -136,9 +136,9 @@ fn testGodboltApi(zig_exe: []const u8, dir_path: []const u8) anyerror!void {
136 _ = try exec(dir_path, true, args.items);136 _ = try exec(dir_path, true, args.items);
137137
138 const out_asm = try std.fs.cwd().readFileAlloc(a, example_s_path, std.math.maxInt(usize));138 const out_asm = try std.fs.cwd().readFileAlloc(a, example_s_path, std.math.maxInt(usize));
139 testing.expect(std.mem.indexOf(u8, out_asm, "square:") != null);139 try testing.expect(std.mem.indexOf(u8, out_asm, "square:") != null);
140 testing.expect(std.mem.indexOf(u8, out_asm, "mov\teax, edi") != null);140 try testing.expect(std.mem.indexOf(u8, out_asm, "mov\teax, edi") != null);
141 testing.expect(std.mem.indexOf(u8, out_asm, "imul\teax, edi") != null);141 try testing.expect(std.mem.indexOf(u8, out_asm, "imul\teax, edi") != null);
142}142}
143143
144fn testMissingOutputPath(zig_exe: []const u8, dir_path: []const u8) !void {144fn testMissingOutputPath(zig_exe: []const u8, dir_path: []const u8) !void {
...@@ -149,7 +149,7 @@ fn testMissingOutputPath(zig_exe: []const u8, dir_path: []const u8) !void {...@@ -149,7 +149,7 @@ fn testMissingOutputPath(zig_exe: []const u8, dir_path: []const u8) !void {
149 const result = try exec(dir_path, false, &[_][]const u8{ zig_exe, "build-exe", source_path, output_arg });149 const result = try exec(dir_path, false, &[_][]const u8{ zig_exe, "build-exe", source_path, output_arg });
150 const s = std.fs.path.sep_str;150 const s = std.fs.path.sep_str;
151 const expected: []const u8 = "error: unable to open output directory 'does" ++ s ++ "not" ++ s ++ "exist': FileNotFound\n";151 const expected: []const u8 = "error: unable to open output directory 'does" ++ s ++ "not" ++ s ++ "exist': FileNotFound\n";
152 testing.expectEqualStrings(expected, result.stderr);152 try testing.expectEqualStrings(expected, result.stderr);
153}153}
154154
155fn testZigFmt(zig_exe: []const u8, dir_path: []const u8) !void {155fn testZigFmt(zig_exe: []const u8, dir_path: []const u8) !void {
...@@ -162,20 +162,20 @@ fn testZigFmt(zig_exe: []const u8, dir_path: []const u8) !void {...@@ -162,20 +162,20 @@ fn testZigFmt(zig_exe: []const u8, dir_path: []const u8) !void {
162162
163 const run_result1 = try exec(dir_path, true, &[_][]const u8{ zig_exe, "fmt", fmt1_zig_path });163 const run_result1 = try exec(dir_path, true, &[_][]const u8{ zig_exe, "fmt", fmt1_zig_path });
164 // stderr should be file path + \n164 // stderr should be file path + \n
165 testing.expect(std.mem.startsWith(u8, run_result1.stdout, fmt1_zig_path));165 try testing.expect(std.mem.startsWith(u8, run_result1.stdout, fmt1_zig_path));
166 testing.expect(run_result1.stdout.len == fmt1_zig_path.len + 1 and run_result1.stdout[run_result1.stdout.len - 1] == '\n');166 try testing.expect(run_result1.stdout.len == fmt1_zig_path.len + 1 and run_result1.stdout[run_result1.stdout.len - 1] == '\n');
167167
168 const fmt2_zig_path = try fs.path.join(a, &[_][]const u8{ dir_path, "fmt2.zig" });168 const fmt2_zig_path = try fs.path.join(a, &[_][]const u8{ dir_path, "fmt2.zig" });
169 try fs.cwd().writeFile(fmt2_zig_path, unformatted_code);169 try fs.cwd().writeFile(fmt2_zig_path, unformatted_code);
170170
171 const run_result2 = try exec(dir_path, true, &[_][]const u8{ zig_exe, "fmt", dir_path });171 const run_result2 = try exec(dir_path, true, &[_][]const u8{ zig_exe, "fmt", dir_path });
172 // running it on the dir, only the new file should be changed172 // running it on the dir, only the new file should be changed
173 testing.expect(std.mem.startsWith(u8, run_result2.stdout, fmt2_zig_path));173 try testing.expect(std.mem.startsWith(u8, run_result2.stdout, fmt2_zig_path));
174 testing.expect(run_result2.stdout.len == fmt2_zig_path.len + 1 and run_result2.stdout[run_result2.stdout.len - 1] == '\n');174 try testing.expect(run_result2.stdout.len == fmt2_zig_path.len + 1 and run_result2.stdout[run_result2.stdout.len - 1] == '\n');
175175
176 const run_result3 = try exec(dir_path, true, &[_][]const u8{ zig_exe, "fmt", dir_path });176 const run_result3 = try exec(dir_path, true, &[_][]const u8{ zig_exe, "fmt", dir_path });
177 // both files have been formatted, nothing should change now177 // both files have been formatted, nothing should change now
178 testing.expect(run_result3.stdout.len == 0);178 try testing.expect(run_result3.stdout.len == 0);
179179
180 // Check UTF-16 decoding180 // Check UTF-16 decoding
181 const fmt4_zig_path = try fs.path.join(a, &[_][]const u8{ dir_path, "fmt4.zig" });181 const fmt4_zig_path = try fs.path.join(a, &[_][]const u8{ dir_path, "fmt4.zig" });
...@@ -183,6 +183,6 @@ fn testZigFmt(zig_exe: []const u8, dir_path: []const u8) !void {...@@ -183,6 +183,6 @@ fn testZigFmt(zig_exe: []const u8, dir_path: []const u8) !void {
183 try fs.cwd().writeFile(fmt4_zig_path, unformatted_code_utf16);183 try fs.cwd().writeFile(fmt4_zig_path, unformatted_code_utf16);
184184
185 const run_result4 = try exec(dir_path, true, &[_][]const u8{ zig_exe, "fmt", dir_path });185 const run_result4 = try exec(dir_path, true, &[_][]const u8{ zig_exe, "fmt", dir_path });
186 testing.expect(std.mem.startsWith(u8, run_result4.stdout, fmt4_zig_path));186 try testing.expect(std.mem.startsWith(u8, run_result4.stdout, fmt4_zig_path));
187 testing.expect(run_result4.stdout.len == fmt4_zig_path.len + 1 and run_result4.stdout[run_result4.stdout.len - 1] == '\n');187 try testing.expect(run_result4.stdout.len == fmt4_zig_path.len + 1 and run_result4.stdout[run_result4.stdout.len - 1] == '\n');
188}188}
test/compile_errors.zig+1-1
...@@ -230,7 +230,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -230,7 +230,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
230230
231 cases.add("array in c exported function",231 cases.add("array in c exported function",
232 \\export fn zig_array(x: [10]u8) void {232 \\export fn zig_array(x: [10]u8) void {
233 \\ expect(std.mem.eql(u8, &x, "1234567890"));233 \\try expect(std.mem.eql(u8, &x, "1234567890"));
234 \\}234 \\}
235 \\235 \\
236 \\export fn zig_return_array() [10]u8 {236 \\export fn zig_return_array() [10]u8 {
test/stack_traces.zig+17-17
...@@ -5,13 +5,13 @@ const tests = @import("tests.zig");...@@ -5,13 +5,13 @@ const tests = @import("tests.zig");
5pub fn addCases(cases: *tests.StackTracesContext) void {5pub fn addCases(cases: *tests.StackTracesContext) void {
6 cases.addCase(.{6 cases.addCase(.{
7 .name = "return",7 .name = "return",
8 .source =8 .source =
9 \\pub fn main() !void {9 \\pub fn main() !void {
10 \\ return error.TheSkyIsFalling;10 \\ return error.TheSkyIsFalling;
11 \\}11 \\}
12 ,12 ,
13 .Debug = .{13 .Debug = .{
14 .expect =14 .expect =
15 \\error: TheSkyIsFalling15 \\error: TheSkyIsFalling
16 \\source.zig:2:5: [address] in main (test)16 \\source.zig:2:5: [address] in main (test)
17 \\ return error.TheSkyIsFalling;17 \\ return error.TheSkyIsFalling;
...@@ -23,7 +23,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {...@@ -23,7 +23,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
23 .exclude_os = .{23 .exclude_os = .{
24 .windows, // segfault24 .windows, // segfault
25 },25 },
26 .expect =26 .expect =
27 \\error: TheSkyIsFalling27 \\error: TheSkyIsFalling
28 \\source.zig:2:5: [address] in [function]28 \\source.zig:2:5: [address] in [function]
29 \\ return error.TheSkyIsFalling;29 \\ return error.TheSkyIsFalling;
...@@ -32,13 +32,13 @@ pub fn addCases(cases: *tests.StackTracesContext) void {...@@ -32,13 +32,13 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
32 ,32 ,
33 },33 },
34 .ReleaseFast = .{34 .ReleaseFast = .{
35 .expect =35 .expect =
36 \\error: TheSkyIsFalling36 \\error: TheSkyIsFalling
37 \\37 \\
38 ,38 ,
39 },39 },
40 .ReleaseSmall = .{40 .ReleaseSmall = .{
41 .expect =41 .expect =
42 \\error: TheSkyIsFalling42 \\error: TheSkyIsFalling
43 \\43 \\
44 ,44 ,
...@@ -47,7 +47,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {...@@ -47,7 +47,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
4747
48 cases.addCase(.{48 cases.addCase(.{
49 .name = "try return",49 .name = "try return",
50 .source =50 .source =
51 \\fn foo() !void {51 \\fn foo() !void {
52 \\ return error.TheSkyIsFalling;52 \\ return error.TheSkyIsFalling;
53 \\}53 \\}
...@@ -57,7 +57,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {...@@ -57,7 +57,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
57 \\}57 \\}
58 ,58 ,
59 .Debug = .{59 .Debug = .{
60 .expect =60 .expect =
61 \\error: TheSkyIsFalling61 \\error: TheSkyIsFalling
62 \\source.zig:2:5: [address] in foo (test)62 \\source.zig:2:5: [address] in foo (test)
63 \\ return error.TheSkyIsFalling;63 \\ return error.TheSkyIsFalling;
...@@ -72,7 +72,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {...@@ -72,7 +72,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
72 .exclude_os = .{72 .exclude_os = .{
73 .windows, // segfault73 .windows, // segfault
74 },74 },
75 .expect =75 .expect =
76 \\error: TheSkyIsFalling76 \\error: TheSkyIsFalling
77 \\source.zig:2:5: [address] in [function]77 \\source.zig:2:5: [address] in [function]
78 \\ return error.TheSkyIsFalling;78 \\ return error.TheSkyIsFalling;
...@@ -84,13 +84,13 @@ pub fn addCases(cases: *tests.StackTracesContext) void {...@@ -84,13 +84,13 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
84 ,84 ,
85 },85 },
86 .ReleaseFast = .{86 .ReleaseFast = .{
87 .expect =87 .expect =
88 \\error: TheSkyIsFalling88 \\error: TheSkyIsFalling
89 \\89 \\
90 ,90 ,
91 },91 },
92 .ReleaseSmall = .{92 .ReleaseSmall = .{
93 .expect =93 .expect =
94 \\error: TheSkyIsFalling94 \\error: TheSkyIsFalling
95 \\95 \\
96 ,96 ,
...@@ -99,7 +99,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {...@@ -99,7 +99,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
9999
100 cases.addCase(.{100 cases.addCase(.{
101 .name = "try try return return",101 .name = "try try return return",
102 .source =102 .source =
103 \\fn foo() !void {103 \\fn foo() !void {
104 \\ try bar();104 \\ try bar();
105 \\}105 \\}
...@@ -117,7 +117,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {...@@ -117,7 +117,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
117 \\}117 \\}
118 ,118 ,
119 .Debug = .{119 .Debug = .{
120 .expect =120 .expect =
121 \\error: TheSkyIsFalling121 \\error: TheSkyIsFalling
122 \\source.zig:10:5: [address] in make_error (test)122 \\source.zig:10:5: [address] in make_error (test)
123 \\ return error.TheSkyIsFalling;123 \\ return error.TheSkyIsFalling;
...@@ -138,7 +138,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {...@@ -138,7 +138,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
138 .exclude_os = .{138 .exclude_os = .{
139 .windows, // segfault139 .windows, // segfault
140 },140 },
141 .expect =141 .expect =
142 \\error: TheSkyIsFalling142 \\error: TheSkyIsFalling
143 \\source.zig:10:5: [address] in [function]143 \\source.zig:10:5: [address] in [function]
144 \\ return error.TheSkyIsFalling;144 \\ return error.TheSkyIsFalling;
...@@ -156,13 +156,13 @@ pub fn addCases(cases: *tests.StackTracesContext) void {...@@ -156,13 +156,13 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
156 ,156 ,
157 },157 },
158 .ReleaseFast = .{158 .ReleaseFast = .{
159 .expect =159 .expect =
160 \\error: TheSkyIsFalling160 \\error: TheSkyIsFalling
161 \\161 \\
162 ,162 ,
163 },163 },
164 .ReleaseSmall = .{164 .ReleaseSmall = .{
165 .expect =165 .expect =
166 \\error: TheSkyIsFalling166 \\error: TheSkyIsFalling
167 \\167 \\
168 ,168 ,
...@@ -174,7 +174,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {...@@ -174,7 +174,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
174 .windows,174 .windows,
175 },175 },
176 .name = "dumpCurrentStackTrace",176 .name = "dumpCurrentStackTrace",
177 .source =177 .source =
178 \\const std = @import("std");178 \\const std = @import("std");
179 \\179 \\
180 \\fn bar() void {180 \\fn bar() void {
...@@ -189,7 +189,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {...@@ -189,7 +189,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
189 \\}189 \\}
190 ,190 ,
191 .Debug = .{191 .Debug = .{
192 .expect =192 .expect =
193 \\source.zig:7:8: [address] in foo (test)193 \\source.zig:7:8: [address] in foo (test)
194 \\ bar();194 \\ bar();
195 \\ ^195 \\ ^
test/stage1/behavior/align.zig+66-66
...@@ -5,16 +5,16 @@ const builtin = @import("builtin");...@@ -5,16 +5,16 @@ const builtin = @import("builtin");
5var foo: u8 align(4) = 100;5var foo: u8 align(4) = 100;
66
7test "global variable alignment" {7test "global variable alignment" {
8 comptime expect(@typeInfo(@TypeOf(&foo)).Pointer.alignment == 4);8 comptime try expect(@typeInfo(@TypeOf(&foo)).Pointer.alignment == 4);
9 comptime expect(@TypeOf(&foo) == *align(4) u8);9 comptime try expect(@TypeOf(&foo) == *align(4) u8);
10 {10 {
11 const slice = @as(*[1]u8, &foo)[0..];11 const slice = @as(*[1]u8, &foo)[0..];
12 comptime expect(@TypeOf(slice) == *align(4) [1]u8);12 comptime try expect(@TypeOf(slice) == *align(4) [1]u8);
13 }13 }
14 {14 {
15 var runtime_zero: usize = 0;15 var runtime_zero: usize = 0;
16 const slice = @as(*[1]u8, &foo)[runtime_zero..];16 const slice = @as(*[1]u8, &foo)[runtime_zero..];
17 comptime expect(@TypeOf(slice) == []align(4) u8);17 comptime try expect(@TypeOf(slice) == []align(4) u8);
18 }18 }
19}19}
2020
...@@ -28,9 +28,9 @@ test "function alignment" {...@@ -28,9 +28,9 @@ test "function alignment" {
28 // function alignment is a compile error on wasm32/wasm6428 // function alignment is a compile error on wasm32/wasm64
29 if (builtin.arch == .wasm32 or builtin.arch == .wasm64) return error.SkipZigTest;29 if (builtin.arch == .wasm32 or builtin.arch == .wasm64) return error.SkipZigTest;
3030
31 expect(derp() == 1234);31 try expect(derp() == 1234);
32 expect(@TypeOf(noop1) == fn () align(1) void);32 try expect(@TypeOf(noop1) == fn () align(1) void);
33 expect(@TypeOf(noop4) == fn () align(4) void);33 try expect(@TypeOf(noop4) == fn () align(4) void);
34 noop1();34 noop1();
35 noop4();35 noop4();
36}36}
...@@ -41,7 +41,7 @@ var baz: packed struct {...@@ -41,7 +41,7 @@ var baz: packed struct {
41} = undefined;41} = undefined;
4242
43test "packed struct alignment" {43test "packed struct alignment" {
44 expect(@TypeOf(&baz.b) == *align(1) u32);44 try expect(@TypeOf(&baz.b) == *align(1) u32);
45}45}
4646
47const blah: packed struct {47const blah: packed struct {
...@@ -51,17 +51,17 @@ const blah: packed struct {...@@ -51,17 +51,17 @@ const blah: packed struct {
51} = undefined;51} = undefined;
5252
53test "bit field alignment" {53test "bit field alignment" {
54 expect(@TypeOf(&blah.b) == *align(1:3:1) const u3);54 try expect(@TypeOf(&blah.b) == *align(1:3:1) const u3);
55}55}
5656
57test "default alignment allows unspecified in type syntax" {57test "default alignment allows unspecified in type syntax" {
58 expect(*u32 == *align(@alignOf(u32)) u32);58 try expect(*u32 == *align(@alignOf(u32)) u32);
59}59}
6060
61test "implicitly decreasing pointer alignment" {61test "implicitly decreasing pointer alignment" {
62 const a: u32 align(4) = 3;62 const a: u32 align(4) = 3;
63 const b: u32 align(8) = 4;63 const b: u32 align(8) = 4;
64 expect(addUnaligned(&a, &b) == 7);64 try expect(addUnaligned(&a, &b) == 7);
65}65}
6666
67fn addUnaligned(a: *align(1) const u32, b: *align(1) const u32) u32 {67fn addUnaligned(a: *align(1) const u32, b: *align(1) const u32) u32 {
...@@ -71,16 +71,16 @@ fn addUnaligned(a: *align(1) const u32, b: *align(1) const u32) u32 {...@@ -71,16 +71,16 @@ fn addUnaligned(a: *align(1) const u32, b: *align(1) const u32) u32 {
71test "implicitly decreasing slice alignment" {71test "implicitly decreasing slice alignment" {
72 const a: u32 align(4) = 3;72 const a: u32 align(4) = 3;
73 const b: u32 align(8) = 4;73 const b: u32 align(8) = 4;
74 expect(addUnalignedSlice(@as(*const [1]u32, &a)[0..], @as(*const [1]u32, &b)[0..]) == 7);74 try expect(addUnalignedSlice(@as(*const [1]u32, &a)[0..], @as(*const [1]u32, &b)[0..]) == 7);
75}75}
76fn addUnalignedSlice(a: []align(1) const u32, b: []align(1) const u32) u32 {76fn addUnalignedSlice(a: []align(1) const u32, b: []align(1) const u32) u32 {
77 return a[0] + b[0];77 return a[0] + b[0];
78}78}
7979
80test "specifying alignment allows pointer cast" {80test "specifying alignment allows pointer cast" {
81 testBytesAlign(0x33);81 try testBytesAlign(0x33);
82}82}
83fn testBytesAlign(b: u8) void {83fn testBytesAlign(b: u8) !void {
84 var bytes align(4) = [_]u8{84 var bytes align(4) = [_]u8{
85 b,85 b,
86 b,86 b,
...@@ -88,13 +88,13 @@ fn testBytesAlign(b: u8) void {...@@ -88,13 +88,13 @@ fn testBytesAlign(b: u8) void {
88 b,88 b,
89 };89 };
90 const ptr = @ptrCast(*u32, &bytes[0]);90 const ptr = @ptrCast(*u32, &bytes[0]);
91 expect(ptr.* == 0x33333333);91 try expect(ptr.* == 0x33333333);
92}92}
9393
94test "@alignCast pointers" {94test "@alignCast pointers" {
95 var x: u32 align(4) = 1;95 var x: u32 align(4) = 1;
96 expectsOnly1(&x);96 expectsOnly1(&x);
97 expect(x == 2);97 try expect(x == 2);
98}98}
99fn expectsOnly1(x: *align(1) u32) void {99fn expectsOnly1(x: *align(1) u32) void {
100 expects4(@alignCast(4, x));100 expects4(@alignCast(4, x));
...@@ -110,7 +110,7 @@ test "@alignCast slices" {...@@ -110,7 +110,7 @@ test "@alignCast slices" {
110 };110 };
111 const slice = array[0..];111 const slice = array[0..];
112 sliceExpectsOnly1(slice);112 sliceExpectsOnly1(slice);
113 expect(slice[0] == 2);113 try expect(slice[0] == 2);
114}114}
115fn sliceExpectsOnly1(slice: []align(1) u32) void {115fn sliceExpectsOnly1(slice: []align(1) u32) void {
116 sliceExpects4(@alignCast(4, slice));116 sliceExpects4(@alignCast(4, slice));
...@@ -123,12 +123,12 @@ test "implicitly decreasing fn alignment" {...@@ -123,12 +123,12 @@ test "implicitly decreasing fn alignment" {
123 // function alignment is a compile error on wasm32/wasm64123 // function alignment is a compile error on wasm32/wasm64
124 if (builtin.arch == .wasm32 or builtin.arch == .wasm64) return error.SkipZigTest;124 if (builtin.arch == .wasm32 or builtin.arch == .wasm64) return error.SkipZigTest;
125125
126 testImplicitlyDecreaseFnAlign(alignedSmall, 1234);126 try testImplicitlyDecreaseFnAlign(alignedSmall, 1234);
127 testImplicitlyDecreaseFnAlign(alignedBig, 5678);127 try testImplicitlyDecreaseFnAlign(alignedBig, 5678);
128}128}
129129
130fn testImplicitlyDecreaseFnAlign(ptr: fn () align(1) i32, answer: i32) void {130fn testImplicitlyDecreaseFnAlign(ptr: fn () align(1) i32, answer: i32) !void {
131 expect(ptr() == answer);131 try expect(ptr() == answer);
132}132}
133133
134fn alignedSmall() align(8) i32 {134fn alignedSmall() align(8) i32 {
...@@ -143,7 +143,7 @@ test "@alignCast functions" {...@@ -143,7 +143,7 @@ test "@alignCast functions" {
143 if (builtin.arch == .wasm32 or builtin.arch == .wasm64) return error.SkipZigTest;143 if (builtin.arch == .wasm32 or builtin.arch == .wasm64) return error.SkipZigTest;
144 if (builtin.arch == .thumb) return error.SkipZigTest;144 if (builtin.arch == .thumb) return error.SkipZigTest;
145145
146 expect(fnExpectsOnly1(simple4) == 0x19);146 try expect(fnExpectsOnly1(simple4) == 0x19);
147}147}
148fn fnExpectsOnly1(ptr: fn () align(1) i32) i32 {148fn fnExpectsOnly1(ptr: fn () align(1) i32) i32 {
149 return fnExpects4(@alignCast(4, ptr));149 return fnExpects4(@alignCast(4, ptr));
...@@ -160,9 +160,9 @@ test "generic function with align param" {...@@ -160,9 +160,9 @@ test "generic function with align param" {
160 if (builtin.arch == .wasm32 or builtin.arch == .wasm64) return error.SkipZigTest;160 if (builtin.arch == .wasm32 or builtin.arch == .wasm64) return error.SkipZigTest;
161 if (builtin.arch == .thumb) return error.SkipZigTest;161 if (builtin.arch == .thumb) return error.SkipZigTest;
162162
163 expect(whyWouldYouEverDoThis(1) == 0x1);163 try expect(whyWouldYouEverDoThis(1) == 0x1);
164 expect(whyWouldYouEverDoThis(4) == 0x1);164 try expect(whyWouldYouEverDoThis(4) == 0x1);
165 expect(whyWouldYouEverDoThis(8) == 0x1);165 try expect(whyWouldYouEverDoThis(8) == 0x1);
166}166}
167167
168fn whyWouldYouEverDoThis(comptime align_bytes: u8) align(align_bytes) u8 {168fn whyWouldYouEverDoThis(comptime align_bytes: u8) align(align_bytes) u8 {
...@@ -172,49 +172,49 @@ fn whyWouldYouEverDoThis(comptime align_bytes: u8) align(align_bytes) u8 {...@@ -172,49 +172,49 @@ fn whyWouldYouEverDoThis(comptime align_bytes: u8) align(align_bytes) u8 {
172test "@ptrCast preserves alignment of bigger source" {172test "@ptrCast preserves alignment of bigger source" {
173 var x: u32 align(16) = 1234;173 var x: u32 align(16) = 1234;
174 const ptr = @ptrCast(*u8, &x);174 const ptr = @ptrCast(*u8, &x);
175 expect(@TypeOf(ptr) == *align(16) u8);175 try expect(@TypeOf(ptr) == *align(16) u8);
176}176}
177177
178test "runtime known array index has best alignment possible" {178test "runtime known array index has best alignment possible" {
179 // take full advantage of over-alignment179 // take full advantage of over-alignment
180 var array align(4) = [_]u8{ 1, 2, 3, 4 };180 var array align(4) = [_]u8{ 1, 2, 3, 4 };
181 expect(@TypeOf(&array[0]) == *align(4) u8);181 try expect(@TypeOf(&array[0]) == *align(4) u8);
182 expect(@TypeOf(&array[1]) == *u8);182 try expect(@TypeOf(&array[1]) == *u8);
183 expect(@TypeOf(&array[2]) == *align(2) u8);183 try expect(@TypeOf(&array[2]) == *align(2) u8);
184 expect(@TypeOf(&array[3]) == *u8);184 try expect(@TypeOf(&array[3]) == *u8);
185185
186 // because align is too small but we still figure out to use 2186 // because align is too small but we still figure out to use 2
187 var bigger align(2) = [_]u64{ 1, 2, 3, 4 };187 var bigger align(2) = [_]u64{ 1, 2, 3, 4 };
188 expect(@TypeOf(&bigger[0]) == *align(2) u64);188 try expect(@TypeOf(&bigger[0]) == *align(2) u64);
189 expect(@TypeOf(&bigger[1]) == *align(2) u64);189 try expect(@TypeOf(&bigger[1]) == *align(2) u64);
190 expect(@TypeOf(&bigger[2]) == *align(2) u64);190 try expect(@TypeOf(&bigger[2]) == *align(2) u64);
191 expect(@TypeOf(&bigger[3]) == *align(2) u64);191 try expect(@TypeOf(&bigger[3]) == *align(2) u64);
192192
193 // because pointer is align 2 and u32 align % 2 == 0 we can assume align 2193 // because pointer is align 2 and u32 align % 2 == 0 we can assume align 2
194 var smaller align(2) = [_]u32{ 1, 2, 3, 4 };194 var smaller align(2) = [_]u32{ 1, 2, 3, 4 };
195 var runtime_zero: usize = 0;195 var runtime_zero: usize = 0;
196 comptime expect(@TypeOf(smaller[runtime_zero..]) == []align(2) u32);196 comptime try expect(@TypeOf(smaller[runtime_zero..]) == []align(2) u32);
197 comptime expect(@TypeOf(smaller[runtime_zero..].ptr) == [*]align(2) u32);197 comptime try expect(@TypeOf(smaller[runtime_zero..].ptr) == [*]align(2) u32);
198 testIndex(smaller[runtime_zero..].ptr, 0, *align(2) u32);198 try testIndex(smaller[runtime_zero..].ptr, 0, *align(2) u32);
199 testIndex(smaller[runtime_zero..].ptr, 1, *align(2) u32);199 try testIndex(smaller[runtime_zero..].ptr, 1, *align(2) u32);
200 testIndex(smaller[runtime_zero..].ptr, 2, *align(2) u32);200 try testIndex(smaller[runtime_zero..].ptr, 2, *align(2) u32);
201 testIndex(smaller[runtime_zero..].ptr, 3, *align(2) u32);201 try testIndex(smaller[runtime_zero..].ptr, 3, *align(2) u32);
202202
203 // has to use ABI alignment because index known at runtime only203 // has to use ABI alignment because index known at runtime only
204 testIndex2(array[runtime_zero..].ptr, 0, *u8);204 try testIndex2(array[runtime_zero..].ptr, 0, *u8);
205 testIndex2(array[runtime_zero..].ptr, 1, *u8);205 try testIndex2(array[runtime_zero..].ptr, 1, *u8);
206 testIndex2(array[runtime_zero..].ptr, 2, *u8);206 try testIndex2(array[runtime_zero..].ptr, 2, *u8);
207 testIndex2(array[runtime_zero..].ptr, 3, *u8);207 try testIndex2(array[runtime_zero..].ptr, 3, *u8);
208}208}
209fn testIndex(smaller: [*]align(2) u32, index: usize, comptime T: type) void {209fn testIndex(smaller: [*]align(2) u32, index: usize, comptime T: type) !void {
210 comptime expect(@TypeOf(&smaller[index]) == T);210 comptime try expect(@TypeOf(&smaller[index]) == T);
211}211}
212fn testIndex2(ptr: [*]align(4) u8, index: usize, comptime T: type) void {212fn testIndex2(ptr: [*]align(4) u8, index: usize, comptime T: type) !void {
213 comptime expect(@TypeOf(&ptr[index]) == T);213 comptime try expect(@TypeOf(&ptr[index]) == T);
214}214}
215215
216test "alignstack" {216test "alignstack" {
217 expect(fnWithAlignedStack() == 1234);217 try expect(fnWithAlignedStack() == 1234);
218}218}
219219
220fn fnWithAlignedStack() i32 {220fn fnWithAlignedStack() i32 {
...@@ -223,7 +223,7 @@ fn fnWithAlignedStack() i32 {...@@ -223,7 +223,7 @@ fn fnWithAlignedStack() i32 {
223}223}
224224
225test "alignment of structs" {225test "alignment of structs" {
226 expect(@alignOf(struct {226 try expect(@alignOf(struct {
227 a: i32,227 a: i32,
228 b: *i32,228 b: *i32,
229 }) == @alignOf(usize));229 }) == @alignOf(usize));
...@@ -239,37 +239,37 @@ test "alignment of function with c calling convention" {...@@ -239,37 +239,37 @@ test "alignment of function with c calling convention" {
239fn nothing() callconv(.C) void {}239fn nothing() callconv(.C) void {}
240240
241test "return error union with 128-bit integer" {241test "return error union with 128-bit integer" {
242 expect(3 == try give());242 try expect(3 == try give());
243}243}
244fn give() anyerror!u128 {244fn give() anyerror!u128 {
245 return 3;245 return 3;
246}246}
247247
248test "alignment of >= 128-bit integer type" {248test "alignment of >= 128-bit integer type" {
249 expect(@alignOf(u128) == 16);249 try expect(@alignOf(u128) == 16);
250 expect(@alignOf(u129) == 16);250 try expect(@alignOf(u129) == 16);
251}251}
252252
253test "alignment of struct with 128-bit field" {253test "alignment of struct with 128-bit field" {
254 expect(@alignOf(struct {254 try expect(@alignOf(struct {
255 x: u128,255 x: u128,
256 }) == 16);256 }) == 16);
257257
258 comptime {258 comptime {
259 expect(@alignOf(struct {259 try expect(@alignOf(struct {
260 x: u128,260 x: u128,
261 }) == 16);261 }) == 16);
262 }262 }
263}263}
264264
265test "size of extern struct with 128-bit field" {265test "size of extern struct with 128-bit field" {
266 expect(@sizeOf(extern struct {266 try expect(@sizeOf(extern struct {
267 x: u128,267 x: u128,
268 y: u8,268 y: u8,
269 }) == 32);269 }) == 32);
270270
271 comptime {271 comptime {
272 expect(@sizeOf(extern struct {272 try expect(@sizeOf(extern struct {
273 x: u128,273 x: u128,
274 y: u8,274 y: u8,
275 }) == 32);275 }) == 32);
...@@ -286,8 +286,8 @@ test "read 128-bit field from default aligned struct in stack memory" {...@@ -286,8 +286,8 @@ test "read 128-bit field from default aligned struct in stack memory" {
286 .nevermind = 1,286 .nevermind = 1,
287 .badguy = 12,287 .badguy = 12,
288 };288 };
289 expect((@ptrToInt(&default_aligned.badguy) % 16) == 0);289 try expect((@ptrToInt(&default_aligned.badguy) % 16) == 0);
290 expect(12 == default_aligned.badguy);290 try expect(12 == default_aligned.badguy);
291}291}
292292
293var default_aligned_global = DefaultAligned{293var default_aligned_global = DefaultAligned{
...@@ -296,8 +296,8 @@ var default_aligned_global = DefaultAligned{...@@ -296,8 +296,8 @@ var default_aligned_global = DefaultAligned{
296};296};
297297
298test "read 128-bit field from default aligned struct in global memory" {298test "read 128-bit field from default aligned struct in global memory" {
299 expect((@ptrToInt(&default_aligned_global.badguy) % 16) == 0);299 try expect((@ptrToInt(&default_aligned_global.badguy) % 16) == 0);
300 expect(12 == default_aligned_global.badguy);300 try expect(12 == default_aligned_global.badguy);
301}301}
302302
303test "struct field explicit alignment" {303test "struct field explicit alignment" {
...@@ -310,9 +310,9 @@ test "struct field explicit alignment" {...@@ -310,9 +310,9 @@ test "struct field explicit alignment" {
310310
311 var node: S.Node = undefined;311 var node: S.Node = undefined;
312 node.massive_byte = 100;312 node.massive_byte = 100;
313 expect(node.massive_byte == 100);313 try expect(node.massive_byte == 100);
314 comptime expect(@TypeOf(&node.massive_byte) == *align(64) u8);314 comptime try expect(@TypeOf(&node.massive_byte) == *align(64) u8);
315 expect(@ptrToInt(&node.massive_byte) % 64 == 0);315 try expect(@ptrToInt(&node.massive_byte) % 64 == 0);
316}316}
317317
318test "align(@alignOf(T)) T does not force resolution of T" {318test "align(@alignOf(T)) T does not force resolution of T" {
...@@ -334,7 +334,7 @@ test "align(@alignOf(T)) T does not force resolution of T" {...@@ -334,7 +334,7 @@ test "align(@alignOf(T)) T does not force resolution of T" {
334 var ok = false;334 var ok = false;
335 };335 };
336 _ = async S.doTheTest();336 _ = async S.doTheTest();
337 expect(S.ok);337 try expect(S.ok);
338}338}
339339
340test "align(N) on functions" {340test "align(N) on functions" {
...@@ -342,7 +342,7 @@ test "align(N) on functions" {...@@ -342,7 +342,7 @@ test "align(N) on functions" {
342 if (builtin.arch == .wasm32 or builtin.arch == .wasm64) return error.SkipZigTest;342 if (builtin.arch == .wasm32 or builtin.arch == .wasm64) return error.SkipZigTest;
343 if (builtin.arch == .thumb) return error.SkipZigTest;343 if (builtin.arch == .thumb) return error.SkipZigTest;
344344
345 expect((@ptrToInt(overaligned_fn) & (0x1000 - 1)) == 0);345 try expect((@ptrToInt(overaligned_fn) & (0x1000 - 1)) == 0);
346}346}
347fn overaligned_fn() align(0x1000) i32 {347fn overaligned_fn() align(0x1000) i32 {
348 return 42;348 return 42;
test/stage1/behavior/alignof.zig+14-14
...@@ -10,29 +10,29 @@ const Foo = struct {...@@ -10,29 +10,29 @@ const Foo = struct {
10};10};
1111
12test "@alignOf(T) before referencing T" {12test "@alignOf(T) before referencing T" {
13 comptime expect(@alignOf(Foo) != maxInt(usize));13 comptime try expect(@alignOf(Foo) != maxInt(usize));
14 if (builtin.arch == builtin.Arch.x86_64) {14 if (builtin.arch == builtin.Arch.x86_64) {
15 comptime expect(@alignOf(Foo) == 4);15 comptime try expect(@alignOf(Foo) == 4);
16 }16 }
17}17}
1818
19test "comparison of @alignOf(T) against zero" {19test "comparison of @alignOf(T) against zero" {
20 {20 {
21 const T = struct { x: u32 };21 const T = struct { x: u32 };
22 expect(!(@alignOf(T) == 0));22 try expect(!(@alignOf(T) == 0));
23 expect(@alignOf(T) != 0);23 try expect(@alignOf(T) != 0);
24 expect(!(@alignOf(T) < 0));24 try expect(!(@alignOf(T) < 0));
25 expect(!(@alignOf(T) <= 0));25 try expect(!(@alignOf(T) <= 0));
26 expect(@alignOf(T) > 0);26 try expect(@alignOf(T) > 0);
27 expect(@alignOf(T) >= 0);27 try expect(@alignOf(T) >= 0);
28 }28 }
29 {29 {
30 const T = struct {};30 const T = struct {};
31 expect(@alignOf(T) == 0);31 try expect(@alignOf(T) == 0);
32 expect(!(@alignOf(T) != 0));32 try expect(!(@alignOf(T) != 0));
33 expect(!(@alignOf(T) < 0));33 try expect(!(@alignOf(T) < 0));
34 expect(@alignOf(T) <= 0);34 try expect(@alignOf(T) <= 0);
35 expect(!(@alignOf(T) > 0));35 try expect(!(@alignOf(T) > 0));
36 expect(@alignOf(T) >= 0);36 try expect(@alignOf(T) >= 0);
37 }37 }
38}38}
test/stage1/behavior/array.zig+141-141
...@@ -21,8 +21,8 @@ test "arrays" {...@@ -21,8 +21,8 @@ test "arrays" {
21 i += 1;21 i += 1;
22 }22 }
2323
24 expect(accumulator == 15);24 try expect(accumulator == 15);
25 expect(getArrayLen(&array) == 5);25 try expect(getArrayLen(&array) == 5);
26}26}
27fn getArrayLen(a: []const u32) usize {27fn getArrayLen(a: []const u32) usize {
28 return a.len;28 return a.len;
...@@ -30,37 +30,37 @@ fn getArrayLen(a: []const u32) usize {...@@ -30,37 +30,37 @@ fn getArrayLen(a: []const u32) usize {
3030
31test "array with sentinels" {31test "array with sentinels" {
32 const S = struct {32 const S = struct {
33 fn doTheTest(is_ct: bool) void {33 fn doTheTest(is_ct: bool) !void {
34 if (is_ct) {34 if (is_ct) {
35 var zero_sized: [0:0xde]u8 = [_:0xde]u8{};35 var zero_sized: [0:0xde]u8 = [_:0xde]u8{};
36 // Disabled at runtime because of36 // Disabled at runtime because of
37 // https://github.com/ziglang/zig/issues/437237 // https://github.com/ziglang/zig/issues/4372
38 expectEqual(@as(u8, 0xde), zero_sized[0]);38 try expectEqual(@as(u8, 0xde), zero_sized[0]);
39 var reinterpreted = @ptrCast(*[1]u8, &zero_sized);39 var reinterpreted = @ptrCast(*[1]u8, &zero_sized);
40 expectEqual(@as(u8, 0xde), reinterpreted[0]);40 try expectEqual(@as(u8, 0xde), reinterpreted[0]);
41 }41 }
42 var arr: [3:0x55]u8 = undefined;42 var arr: [3:0x55]u8 = undefined;
43 // Make sure the sentinel pointer is pointing after the last element43 // Make sure the sentinel pointer is pointing after the last element
44 if (!is_ct) {44 if (!is_ct) {
45 const sentinel_ptr = @ptrToInt(&arr[3]);45 const sentinel_ptr = @ptrToInt(&arr[3]);
46 const last_elem_ptr = @ptrToInt(&arr[2]);46 const last_elem_ptr = @ptrToInt(&arr[2]);
47 expectEqual(@as(usize, 1), sentinel_ptr - last_elem_ptr);47 try expectEqual(@as(usize, 1), sentinel_ptr - last_elem_ptr);
48 }48 }
49 // Make sure the sentinel is writeable49 // Make sure the sentinel is writeable
50 arr[3] = 0x55;50 arr[3] = 0x55;
51 }51 }
52 };52 };
5353
54 S.doTheTest(false);54 try S.doTheTest(false);
55 comptime S.doTheTest(true);55 comptime try S.doTheTest(true);
56}56}
5757
58test "void arrays" {58test "void arrays" {
59 var array: [4]void = undefined;59 var array: [4]void = undefined;
60 array[0] = void{};60 array[0] = void{};
61 array[1] = array[2];61 array[1] = array[2];
62 expect(@sizeOf(@TypeOf(array)) == 0);62 try expect(@sizeOf(@TypeOf(array)) == 0);
63 expect(array.len == 4);63 try expect(array.len == 4);
64}64}
6565
66test "array literal" {66test "array literal" {
...@@ -71,12 +71,12 @@ test "array literal" {...@@ -71,12 +71,12 @@ test "array literal" {
71 1,71 1,
72 };72 };
7373
74 expect(hex_mult.len == 4);74 try expect(hex_mult.len == 4);
75 expect(hex_mult[1] == 256);75 try expect(hex_mult[1] == 256);
76}76}
7777
78test "array dot len const expr" {78test "array dot len const expr" {
79 expect(comptime x: {79 try expect(comptime x: {
80 break :x some_array.len == 4;80 break :x some_array.len == 4;
81 });81 });
82}82}
...@@ -100,11 +100,11 @@ test "nested arrays" {...@@ -100,11 +100,11 @@ test "nested arrays" {
100 "thing",100 "thing",
101 };101 };
102 for (array_of_strings) |s, i| {102 for (array_of_strings) |s, i| {
103 if (i == 0) expect(mem.eql(u8, s, "hello"));103 if (i == 0) try expect(mem.eql(u8, s, "hello"));
104 if (i == 1) expect(mem.eql(u8, s, "this"));104 if (i == 1) try expect(mem.eql(u8, s, "this"));
105 if (i == 2) expect(mem.eql(u8, s, "is"));105 if (i == 2) try expect(mem.eql(u8, s, "is"));
106 if (i == 3) expect(mem.eql(u8, s, "my"));106 if (i == 3) try expect(mem.eql(u8, s, "my"));
107 if (i == 4) expect(mem.eql(u8, s, "thing"));107 if (i == 4) try expect(mem.eql(u8, s, "thing"));
108 }108 }
109}109}
110110
...@@ -122,9 +122,9 @@ test "set global var array via slice embedded in struct" {...@@ -122,9 +122,9 @@ test "set global var array via slice embedded in struct" {
122 s.a[1].b = 2;122 s.a[1].b = 2;
123 s.a[2].b = 3;123 s.a[2].b = 3;
124124
125 expect(s_array[0].b == 1);125 try expect(s_array[0].b == 1);
126 expect(s_array[1].b == 2);126 try expect(s_array[1].b == 2);
127 expect(s_array[2].b == 3);127 try expect(s_array[2].b == 3);
128}128}
129129
130test "array literal with specified size" {130test "array literal with specified size" {
...@@ -132,34 +132,34 @@ test "array literal with specified size" {...@@ -132,34 +132,34 @@ test "array literal with specified size" {
132 1,132 1,
133 2,133 2,
134 };134 };
135 expect(array[0] == 1);135 try expect(array[0] == 1);
136 expect(array[1] == 2);136 try expect(array[1] == 2);
137}137}
138138
139test "array len field" {139test "array len field" {
140 var arr = [4]u8{ 0, 0, 0, 0 };140 var arr = [4]u8{ 0, 0, 0, 0 };
141 var ptr = &arr;141 var ptr = &arr;
142 expect(arr.len == 4);142 try expect(arr.len == 4);
143 comptime expect(arr.len == 4);143 comptime try expect(arr.len == 4);
144 expect(ptr.len == 4);144 try expect(ptr.len == 4);
145 comptime expect(ptr.len == 4);145 comptime try expect(ptr.len == 4);
146}146}
147147
148test "single-item pointer to array indexing and slicing" {148test "single-item pointer to array indexing and slicing" {
149 testSingleItemPtrArrayIndexSlice();149 try testSingleItemPtrArrayIndexSlice();
150 comptime testSingleItemPtrArrayIndexSlice();150 comptime try testSingleItemPtrArrayIndexSlice();
151}151}
152152
153fn testSingleItemPtrArrayIndexSlice() void {153fn testSingleItemPtrArrayIndexSlice() !void {
154 {154 {
155 var array: [4]u8 = "aaaa".*;155 var array: [4]u8 = "aaaa".*;
156 doSomeMangling(&array);156 doSomeMangling(&array);
157 expect(mem.eql(u8, "azya", &array));157 try expect(mem.eql(u8, "azya", &array));
158 }158 }
159 {159 {
160 var array = "aaaa".*;160 var array = "aaaa".*;
161 doSomeMangling(&array);161 doSomeMangling(&array);
162 expect(mem.eql(u8, "azya", &array));162 try expect(mem.eql(u8, "azya", &array));
163 }163 }
164}164}
165165
...@@ -169,15 +169,15 @@ fn doSomeMangling(array: *[4]u8) void {...@@ -169,15 +169,15 @@ fn doSomeMangling(array: *[4]u8) void {
169}169}
170170
171test "implicit cast single-item pointer" {171test "implicit cast single-item pointer" {
172 testImplicitCastSingleItemPtr();172 try testImplicitCastSingleItemPtr();
173 comptime testImplicitCastSingleItemPtr();173 comptime try testImplicitCastSingleItemPtr();
174}174}
175175
176fn testImplicitCastSingleItemPtr() void {176fn testImplicitCastSingleItemPtr() !void {
177 var byte: u8 = 100;177 var byte: u8 = 100;
178 const slice = @as(*[1]u8, &byte)[0..];178 const slice = @as(*[1]u8, &byte)[0..];
179 slice[0] += 1;179 slice[0] += 1;
180 expect(byte == 101);180 try expect(byte == 101);
181}181}
182182
183fn testArrayByValAtComptime(b: [2]u8) u8 {183fn testArrayByValAtComptime(b: [2]u8) u8 {
...@@ -192,7 +192,7 @@ test "comptime evalutating function that takes array by value" {...@@ -192,7 +192,7 @@ test "comptime evalutating function that takes array by value" {
192192
193test "implicit comptime in array type size" {193test "implicit comptime in array type size" {
194 var arr: [plusOne(10)]bool = undefined;194 var arr: [plusOne(10)]bool = undefined;
195 expect(arr.len == 11);195 try expect(arr.len == 11);
196}196}
197197
198fn plusOne(x: u32) u32 {198fn plusOne(x: u32) u32 {
...@@ -202,52 +202,52 @@ fn plusOne(x: u32) u32 {...@@ -202,52 +202,52 @@ fn plusOne(x: u32) u32 {
202test "runtime initialize array elem and then implicit cast to slice" {202test "runtime initialize array elem and then implicit cast to slice" {
203 var two: i32 = 2;203 var two: i32 = 2;
204 const x: []const i32 = &[_]i32{two};204 const x: []const i32 = &[_]i32{two};
205 expect(x[0] == 2);205 try expect(x[0] == 2);
206}206}
207207
208test "array literal as argument to function" {208test "array literal as argument to function" {
209 const S = struct {209 const S = struct {
210 fn entry(two: i32) void {210 fn entry(two: i32) !void {
211 foo(&[_]i32{211 try foo(&[_]i32{
212 1,212 1,
213 2,213 2,
214 3,214 3,
215 });215 });
216 foo(&[_]i32{216 try foo(&[_]i32{
217 1,217 1,
218 two,218 two,
219 3,219 3,
220 });220 });
221 foo2(true, &[_]i32{221 try foo2(true, &[_]i32{
222 1,222 1,
223 2,223 2,
224 3,224 3,
225 });225 });
226 foo2(true, &[_]i32{226 try foo2(true, &[_]i32{
227 1,227 1,
228 two,228 two,
229 3,229 3,
230 });230 });
231 }231 }
232 fn foo(x: []const i32) void {232 fn foo(x: []const i32) !void {
233 expect(x[0] == 1);233 try expect(x[0] == 1);
234 expect(x[1] == 2);234 try expect(x[1] == 2);
235 expect(x[2] == 3);235 try expect(x[2] == 3);
236 }236 }
237 fn foo2(trash: bool, x: []const i32) void {237 fn foo2(trash: bool, x: []const i32) !void {
238 expect(trash);238 try expect(trash);
239 expect(x[0] == 1);239 try expect(x[0] == 1);
240 expect(x[1] == 2);240 try expect(x[1] == 2);
241 expect(x[2] == 3);241 try expect(x[2] == 3);
242 }242 }
243 };243 };
244 S.entry(2);244 try S.entry(2);
245 comptime S.entry(2);245 comptime try S.entry(2);
246}246}
247247
248test "double nested array to const slice cast in array literal" {248test "double nested array to const slice cast in array literal" {
249 const S = struct {249 const S = struct {
250 fn entry(two: i32) void {250 fn entry(two: i32) !void {
251 const cases = [_][]const []const i32{251 const cases = [_][]const []const i32{
252 &[_][]const i32{&[_]i32{1}},252 &[_][]const i32{&[_]i32{1}},
253 &[_][]const i32{&[_]i32{ 2, 3 }},253 &[_][]const i32{&[_]i32{ 2, 3 }},
...@@ -256,18 +256,18 @@ test "double nested array to const slice cast in array literal" {...@@ -256,18 +256,18 @@ test "double nested array to const slice cast in array literal" {
256 &[_]i32{ 5, 6, 7 },256 &[_]i32{ 5, 6, 7 },
257 },257 },
258 };258 };
259 check(&cases);259 try check(&cases);
260260
261 const cases2 = [_][]const i32{261 const cases2 = [_][]const i32{
262 &[_]i32{1},262 &[_]i32{1},
263 &[_]i32{ two, 3 },263 &[_]i32{ two, 3 },
264 };264 };
265 expect(cases2.len == 2);265 try expect(cases2.len == 2);
266 expect(cases2[0].len == 1);266 try expect(cases2[0].len == 1);
267 expect(cases2[0][0] == 1);267 try expect(cases2[0][0] == 1);
268 expect(cases2[1].len == 2);268 try expect(cases2[1].len == 2);
269 expect(cases2[1][0] == 2);269 try expect(cases2[1][0] == 2);
270 expect(cases2[1][1] == 3);270 try expect(cases2[1][1] == 3);
271271
272 const cases3 = [_][]const []const i32{272 const cases3 = [_][]const []const i32{
273 &[_][]const i32{&[_]i32{1}},273 &[_][]const i32{&[_]i32{1}},
...@@ -277,37 +277,37 @@ test "double nested array to const slice cast in array literal" {...@@ -277,37 +277,37 @@ test "double nested array to const slice cast in array literal" {
277 &[_]i32{ 5, 6, 7 },277 &[_]i32{ 5, 6, 7 },
278 },278 },
279 };279 };
280 check(&cases3);280 try check(&cases3);
281 }281 }
282282
283 fn check(cases: []const []const []const i32) void {283 fn check(cases: []const []const []const i32) !void {
284 expect(cases.len == 3);284 try expect(cases.len == 3);
285 expect(cases[0].len == 1);285 try expect(cases[0].len == 1);
286 expect(cases[0][0].len == 1);286 try expect(cases[0][0].len == 1);
287 expect(cases[0][0][0] == 1);287 try expect(cases[0][0][0] == 1);
288 expect(cases[1].len == 1);288 try expect(cases[1].len == 1);
289 expect(cases[1][0].len == 2);289 try expect(cases[1][0].len == 2);
290 expect(cases[1][0][0] == 2);290 try expect(cases[1][0][0] == 2);
291 expect(cases[1][0][1] == 3);291 try expect(cases[1][0][1] == 3);
292 expect(cases[2].len == 2);292 try expect(cases[2].len == 2);
293 expect(cases[2][0].len == 1);293 try expect(cases[2][0].len == 1);
294 expect(cases[2][0][0] == 4);294 try expect(cases[2][0][0] == 4);
295 expect(cases[2][1].len == 3);295 try expect(cases[2][1].len == 3);
296 expect(cases[2][1][0] == 5);296 try expect(cases[2][1][0] == 5);
297 expect(cases[2][1][1] == 6);297 try expect(cases[2][1][1] == 6);
298 expect(cases[2][1][2] == 7);298 try expect(cases[2][1][2] == 7);
299 }299 }
300 };300 };
301 S.entry(2);301 try S.entry(2);
302 comptime S.entry(2);302 comptime try S.entry(2);
303}303}
304304
305test "read/write through global variable array of struct fields initialized via array mult" {305test "read/write through global variable array of struct fields initialized via array mult" {
306 const S = struct {306 const S = struct {
307 fn doTheTest() void {307 fn doTheTest() !void {
308 expect(storage[0].term == 1);308 try expect(storage[0].term == 1);
309 storage[0] = MyStruct{ .term = 123 };309 storage[0] = MyStruct{ .term = 123 };
310 expect(storage[0].term == 123);310 try expect(storage[0].term == 123);
311 }311 }
312312
313 pub const MyStruct = struct {313 pub const MyStruct = struct {
...@@ -316,34 +316,34 @@ test "read/write through global variable array of struct fields initialized via...@@ -316,34 +316,34 @@ test "read/write through global variable array of struct fields initialized via
316316
317 var storage: [1]MyStruct = [_]MyStruct{MyStruct{ .term = 1 }} ** 1;317 var storage: [1]MyStruct = [_]MyStruct{MyStruct{ .term = 1 }} ** 1;
318 };318 };
319 S.doTheTest();319 try S.doTheTest();
320}320}
321321
322test "implicit cast zero sized array ptr to slice" {322test "implicit cast zero sized array ptr to slice" {
323 {323 {
324 var b = "".*;324 var b = "".*;
325 const c: []const u8 = &b;325 const c: []const u8 = &b;
326 expect(c.len == 0);326 try expect(c.len == 0);
327 }327 }
328 {328 {
329 var b: [0]u8 = "".*;329 var b: [0]u8 = "".*;
330 const c: []const u8 = &b;330 const c: []const u8 = &b;
331 expect(c.len == 0);331 try expect(c.len == 0);
332 }332 }
333}333}
334334
335test "anonymous list literal syntax" {335test "anonymous list literal syntax" {
336 const S = struct {336 const S = struct {
337 fn doTheTest() void {337 fn doTheTest() !void {
338 var array: [4]u8 = .{ 1, 2, 3, 4 };338 var array: [4]u8 = .{ 1, 2, 3, 4 };
339 expect(array[0] == 1);339 try expect(array[0] == 1);
340 expect(array[1] == 2);340 try expect(array[1] == 2);
341 expect(array[2] == 3);341 try expect(array[2] == 3);
342 expect(array[3] == 4);342 try expect(array[3] == 4);
343 }343 }
344 };344 };
345 S.doTheTest();345 try S.doTheTest();
346 comptime S.doTheTest();346 comptime try S.doTheTest();
347}347}
348348
349test "anonymous literal in array" {349test "anonymous literal in array" {
...@@ -352,51 +352,51 @@ test "anonymous literal in array" {...@@ -352,51 +352,51 @@ test "anonymous literal in array" {
352 a: usize = 2,352 a: usize = 2,
353 b: usize = 4,353 b: usize = 4,
354 };354 };
355 fn doTheTest() void {355 fn doTheTest() !void {
356 var array: [2]Foo = .{356 var array: [2]Foo = .{
357 .{ .a = 3 },357 .{ .a = 3 },
358 .{ .b = 3 },358 .{ .b = 3 },
359 };359 };
360 expect(array[0].a == 3);360 try expect(array[0].a == 3);
361 expect(array[0].b == 4);361 try expect(array[0].b == 4);
362 expect(array[1].a == 2);362 try expect(array[1].a == 2);
363 expect(array[1].b == 3);363 try expect(array[1].b == 3);
364 }364 }
365 };365 };
366 S.doTheTest();366 try S.doTheTest();
367 comptime S.doTheTest();367 comptime try S.doTheTest();
368}368}
369369
370test "access the null element of a null terminated array" {370test "access the null element of a null terminated array" {
371 const S = struct {371 const S = struct {
372 fn doTheTest() void {372 fn doTheTest() !void {
373 var array: [4:0]u8 = .{ 'a', 'o', 'e', 'u' };373 var array: [4:0]u8 = .{ 'a', 'o', 'e', 'u' };
374 expect(array[4] == 0);374 try expect(array[4] == 0);
375 var len: usize = 4;375 var len: usize = 4;
376 expect(array[len] == 0);376 try expect(array[len] == 0);
377 }377 }
378 };378 };
379 S.doTheTest();379 try S.doTheTest();
380 comptime S.doTheTest();380 comptime try S.doTheTest();
381}381}
382382
383test "type deduction for array subscript expression" {383test "type deduction for array subscript expression" {
384 const S = struct {384 const S = struct {
385 fn doTheTest() void {385 fn doTheTest() !void {
386 var array = [_]u8{ 0x55, 0xAA };386 var array = [_]u8{ 0x55, 0xAA };
387 var v0 = true;387 var v0 = true;
388 expectEqual(@as(u8, 0xAA), array[if (v0) 1 else 0]);388 try expectEqual(@as(u8, 0xAA), array[if (v0) 1 else 0]);
389 var v1 = false;389 var v1 = false;
390 expectEqual(@as(u8, 0x55), array[if (v1) 1 else 0]);390 try expectEqual(@as(u8, 0x55), array[if (v1) 1 else 0]);
391 }391 }
392 };392 };
393 S.doTheTest();393 try S.doTheTest();
394 comptime S.doTheTest();394 comptime try S.doTheTest();
395}395}
396396
397test "sentinel element count towards the ABI size calculation" {397test "sentinel element count towards the ABI size calculation" {
398 const S = struct {398 const S = struct {
399 fn doTheTest() void {399 fn doTheTest() !void {
400 const T = packed struct {400 const T = packed struct {
401 fill_pre: u8 = 0x55,401 fill_pre: u8 = 0x55,
402 data: [0:0]u8 = undefined,402 data: [0:0]u8 = undefined,
...@@ -404,14 +404,14 @@ test "sentinel element count towards the ABI size calculation" {...@@ -404,14 +404,14 @@ test "sentinel element count towards the ABI size calculation" {
404 };404 };
405 var x = T{};405 var x = T{};
406 var as_slice = mem.asBytes(&x);406 var as_slice = mem.asBytes(&x);
407 expectEqual(@as(usize, 3), as_slice.len);407 try expectEqual(@as(usize, 3), as_slice.len);
408 expectEqual(@as(u8, 0x55), as_slice[0]);408 try expectEqual(@as(u8, 0x55), as_slice[0]);
409 expectEqual(@as(u8, 0xAA), as_slice[2]);409 try expectEqual(@as(u8, 0xAA), as_slice[2]);
410 }410 }
411 };411 };
412412
413 S.doTheTest();413 try S.doTheTest();
414 comptime S.doTheTest();414 comptime try S.doTheTest();
415}415}
416416
417test "zero-sized array with recursive type definition" {417test "zero-sized array with recursive type definition" {
...@@ -429,61 +429,61 @@ test "zero-sized array with recursive type definition" {...@@ -429,61 +429,61 @@ test "zero-sized array with recursive type definition" {
429 };429 };
430430
431 var t: S = .{ .list = .{ .s = undefined } };431 var t: S = .{ .list = .{ .s = undefined } };
432 expectEqual(@as(usize, 0), t.list.x);432 try expectEqual(@as(usize, 0), t.list.x);
433}433}
434434
435test "type coercion of anon struct literal to array" {435test "type coercion of anon struct literal to array" {
436 const S = struct {436 const S = struct {
437 const U = union{437 const U = union {
438 a: u32,438 a: u32,
439 b: bool,439 b: bool,
440 c: []const u8,440 c: []const u8,
441 };441 };
442442
443 fn doTheTest() void {443 fn doTheTest() !void {
444 var x1: u8 = 42;444 var x1: u8 = 42;
445 const t1 = .{ x1, 56, 54 };445 const t1 = .{ x1, 56, 54 };
446 var arr1: [3]u8 = t1;446 var arr1: [3]u8 = t1;
447 expect(arr1[0] == 42);447 try expect(arr1[0] == 42);
448 expect(arr1[1] == 56);448 try expect(arr1[1] == 56);
449 expect(arr1[2] == 54);449 try expect(arr1[2] == 54);
450 450
451 var x2: U = .{ .a = 42 };451 var x2: U = .{ .a = 42 };
452 const t2 = .{ x2, .{ .b = true }, .{ .c = "hello" } };452 const t2 = .{ x2, .{ .b = true }, .{ .c = "hello" } };
453 var arr2: [3]U = t2;453 var arr2: [3]U = t2;
454 expect(arr2[0].a == 42);454 try expect(arr2[0].a == 42);
455 expect(arr2[1].b == true);455 try expect(arr2[1].b == true);
456 expect(mem.eql(u8, arr2[2].c, "hello"));456 try expect(mem.eql(u8, arr2[2].c, "hello"));
457 }457 }
458 };458 };
459 S.doTheTest();459 try S.doTheTest();
460 comptime S.doTheTest();460 comptime try S.doTheTest();
461}461}
462462
463test "type coercion of pointer to anon struct literal to pointer to array" {463test "type coercion of pointer to anon struct literal to pointer to array" {
464 const S = struct {464 const S = struct {
465 const U = union{465 const U = union {
466 a: u32,466 a: u32,
467 b: bool,467 b: bool,
468 c: []const u8,468 c: []const u8,
469 };469 };
470470
471 fn doTheTest() void {471 fn doTheTest() !void {
472 var x1: u8 = 42;472 var x1: u8 = 42;
473 const t1 = &.{ x1, 56, 54 };473 const t1 = &.{ x1, 56, 54 };
474 var arr1: *const[3]u8 = t1;474 var arr1: *const [3]u8 = t1;
475 expect(arr1[0] == 42);475 try expect(arr1[0] == 42);
476 expect(arr1[1] == 56);476 try expect(arr1[1] == 56);
477 expect(arr1[2] == 54);477 try expect(arr1[2] == 54);
478 478
479 var x2: U = .{ .a = 42 };479 var x2: U = .{ .a = 42 };
480 const t2 = &.{ x2, .{ .b = true }, .{ .c = "hello" } };480 const t2 = &.{ x2, .{ .b = true }, .{ .c = "hello" } };
481 var arr2: *const [3]U = t2;481 var arr2: *const [3]U = t2;
482 expect(arr2[0].a == 42);482 try expect(arr2[0].a == 42);
483 expect(arr2[1].b == true);483 try expect(arr2[1].b == true);
484 expect(mem.eql(u8, arr2[2].c, "hello"));484 try expect(mem.eql(u8, arr2[2].c, "hello"));
485 }485 }
486 };486 };
487 S.doTheTest();487 try S.doTheTest();
488 comptime S.doTheTest();488 comptime try S.doTheTest();
489}489}
test/stage1/behavior/asm.zig+1-1
...@@ -15,7 +15,7 @@ comptime {...@@ -15,7 +15,7 @@ comptime {
1515
16test "module level assembly" {16test "module level assembly" {
17 if (is_x86_64_linux) {17 if (is_x86_64_linux) {
18 expect(this_is_my_alias() == 1234);18 try expect(this_is_my_alias() == 1234);
19 }19 }
20}20}
2121
test/stage1/behavior/async_fn.zig+165-165
...@@ -9,12 +9,12 @@ var global_x: i32 = 1;...@@ -9,12 +9,12 @@ var global_x: i32 = 1;
99
10test "simple coroutine suspend and resume" {10test "simple coroutine suspend and resume" {
11 var frame = async simpleAsyncFn();11 var frame = async simpleAsyncFn();
12 expect(global_x == 2);12 try expect(global_x == 2);
13 resume frame;13 resume frame;
14 expect(global_x == 3);14 try expect(global_x == 3);
15 const af: anyframe->void = &frame;15 const af: anyframe->void = &frame;
16 resume frame;16 resume frame;
17 expect(global_x == 4);17 try expect(global_x == 4);
18}18}
19fn simpleAsyncFn() void {19fn simpleAsyncFn() void {
20 global_x += 1;20 global_x += 1;
...@@ -28,9 +28,9 @@ var global_y: i32 = 1;...@@ -28,9 +28,9 @@ var global_y: i32 = 1;
2828
29test "pass parameter to coroutine" {29test "pass parameter to coroutine" {
30 var p = async simpleAsyncFnWithArg(2);30 var p = async simpleAsyncFnWithArg(2);
31 expect(global_y == 3);31 try expect(global_y == 3);
32 resume p;32 resume p;
33 expect(global_y == 5);33 try expect(global_y == 5);
34}34}
35fn simpleAsyncFnWithArg(delta: i32) void {35fn simpleAsyncFnWithArg(delta: i32) void {
36 global_y += delta;36 global_y += delta;
...@@ -42,10 +42,10 @@ test "suspend at end of function" {...@@ -42,10 +42,10 @@ test "suspend at end of function" {
42 const S = struct {42 const S = struct {
43 var x: i32 = 1;43 var x: i32 = 1;
4444
45 fn doTheTest() void {45 fn doTheTest() !void {
46 expect(x == 1);46 try expect(x == 1);
47 const p = async suspendAtEnd();47 const p = async suspendAtEnd();
48 expect(x == 2);48 try expect(x == 2);
49 }49 }
5050
51 fn suspendAtEnd() void {51 fn suspendAtEnd() void {
...@@ -53,23 +53,23 @@ test "suspend at end of function" {...@@ -53,23 +53,23 @@ test "suspend at end of function" {
53 suspend {}53 suspend {}
54 }54 }
55 };55 };
56 S.doTheTest();56 try S.doTheTest();
57}57}
5858
59test "local variable in async function" {59test "local variable in async function" {
60 const S = struct {60 const S = struct {
61 var x: i32 = 0;61 var x: i32 = 0;
6262
63 fn doTheTest() void {63 fn doTheTest() !void {
64 expect(x == 0);64 try expect(x == 0);
65 var p = async add(1, 2);65 var p = async add(1, 2);
66 expect(x == 0);66 try expect(x == 0);
67 resume p;67 resume p;
68 expect(x == 0);68 try expect(x == 0);
69 resume p;69 resume p;
70 expect(x == 0);70 try expect(x == 0);
71 resume p;71 resume p;
72 expect(x == 3);72 try expect(x == 3);
73 }73 }
7474
75 fn add(a: i32, b: i32) void {75 fn add(a: i32, b: i32) void {
...@@ -82,7 +82,7 @@ test "local variable in async function" {...@@ -82,7 +82,7 @@ test "local variable in async function" {
82 x = accum;82 x = accum;
83 }83 }
84 };84 };
85 S.doTheTest();85 try S.doTheTest();
86}86}
8787
88test "calling an inferred async function" {88test "calling an inferred async function" {
...@@ -90,11 +90,11 @@ test "calling an inferred async function" {...@@ -90,11 +90,11 @@ test "calling an inferred async function" {
90 var x: i32 = 1;90 var x: i32 = 1;
91 var other_frame: *@Frame(other) = undefined;91 var other_frame: *@Frame(other) = undefined;
9292
93 fn doTheTest() void {93 fn doTheTest() !void {
94 _ = async first();94 _ = async first();
95 expect(x == 1);95 try expect(x == 1);
96 resume other_frame.*;96 resume other_frame.*;
97 expect(x == 2);97 try expect(x == 2);
98 }98 }
9999
100 fn first() void {100 fn first() void {
...@@ -106,7 +106,7 @@ test "calling an inferred async function" {...@@ -106,7 +106,7 @@ test "calling an inferred async function" {
106 x += 1;106 x += 1;
107 }107 }
108 };108 };
109 S.doTheTest();109 try S.doTheTest();
110}110}
111111
112test "@frameSize" {112test "@frameSize" {
...@@ -114,16 +114,16 @@ test "@frameSize" {...@@ -114,16 +114,16 @@ test "@frameSize" {
114 return error.SkipZigTest;114 return error.SkipZigTest;
115115
116 const S = struct {116 const S = struct {
117 fn doTheTest() void {117 fn doTheTest() !void {
118 {118 {
119 var ptr = @ptrCast(fn (i32) callconv(.Async) void, other);119 var ptr = @ptrCast(fn (i32) callconv(.Async) void, other);
120 const size = @frameSize(ptr);120 const size = @frameSize(ptr);
121 expect(size == @sizeOf(@Frame(other)));121 try expect(size == @sizeOf(@Frame(other)));
122 }122 }
123 {123 {
124 var ptr = @ptrCast(fn () callconv(.Async) void, first);124 var ptr = @ptrCast(fn () callconv(.Async) void, first);
125 const size = @frameSize(ptr);125 const size = @frameSize(ptr);
126 expect(size == @sizeOf(@Frame(first)));126 try expect(size == @sizeOf(@Frame(first)));
127 }127 }
128 }128 }
129129
...@@ -135,20 +135,20 @@ test "@frameSize" {...@@ -135,20 +135,20 @@ test "@frameSize" {
135 suspend {}135 suspend {}
136 }136 }
137 };137 };
138 S.doTheTest();138 try S.doTheTest();
139}139}
140140
141test "coroutine suspend, resume" {141test "coroutine suspend, resume" {
142 const S = struct {142 const S = struct {
143 var frame: anyframe = undefined;143 var frame: anyframe = undefined;
144144
145 fn doTheTest() void {145 fn doTheTest() !void {
146 _ = async amain();146 _ = async amain();
147 seq('d');147 seq('d');
148 resume frame;148 resume frame;
149 seq('h');149 seq('h');
150150
151 expect(std.mem.eql(u8, &points, "abcdefgh"));151 try expect(std.mem.eql(u8, &points, "abcdefgh"));
152 }152 }
153153
154 fn amain() void {154 fn amain() void {
...@@ -176,27 +176,27 @@ test "coroutine suspend, resume" {...@@ -176,27 +176,27 @@ test "coroutine suspend, resume" {
176 index += 1;176 index += 1;
177 }177 }
178 };178 };
179 S.doTheTest();179 try S.doTheTest();
180}180}
181181
182test "coroutine suspend with block" {182test "coroutine suspend with block" {
183 const p = async testSuspendBlock();183 const p = async testSuspendBlock();
184 expect(!global_result);184 try expect(!global_result);
185 resume a_promise;185 resume a_promise;
186 expect(global_result);186 try expect(global_result);
187}187}
188188
189var a_promise: anyframe = undefined;189var a_promise: anyframe = undefined;
190var global_result = false;190var global_result = false;
191fn testSuspendBlock() callconv(.Async) void {191fn testSuspendBlock() callconv(.Async) void {
192 suspend {192 suspend {
193 comptime expect(@TypeOf(@frame()) == *@Frame(testSuspendBlock));193 comptime expect(@TypeOf(@frame()) == *@Frame(testSuspendBlock)) catch unreachable;
194 a_promise = @frame();194 a_promise = @frame();
195 }195 }
196196
197 // Test to make sure that @frame() works as advertised (issue #1296)197 // Test to make sure that @frame() works as advertised (issue #1296)
198 // var our_handle: anyframe = @frame();198 // var our_handle: anyframe = @frame();
199 expect(a_promise == @as(anyframe, @frame()));199 expect(a_promise == @as(anyframe, @frame())) catch @panic("test failed");
200200
201 global_result = true;201 global_result = true;
202}202}
...@@ -210,8 +210,8 @@ test "coroutine await" {...@@ -210,8 +210,8 @@ test "coroutine await" {
210 await_seq('f');210 await_seq('f');
211 resume await_a_promise;211 resume await_a_promise;
212 await_seq('i');212 await_seq('i');
213 expect(await_final_result == 1234);213 try expect(await_final_result == 1234);
214 expect(std.mem.eql(u8, &await_points, "abcdefghi"));214 try expect(std.mem.eql(u8, &await_points, "abcdefghi"));
215}215}
216fn await_amain() callconv(.Async) void {216fn await_amain() callconv(.Async) void {
217 await_seq('b');217 await_seq('b');
...@@ -244,8 +244,8 @@ test "coroutine await early return" {...@@ -244,8 +244,8 @@ test "coroutine await early return" {
244 early_seq('a');244 early_seq('a');
245 var p = async early_amain();245 var p = async early_amain();
246 early_seq('f');246 early_seq('f');
247 expect(early_final_result == 1234);247 try expect(early_final_result == 1234);
248 expect(std.mem.eql(u8, &early_points, "abcdef"));248 try expect(std.mem.eql(u8, &early_points, "abcdef"));
249}249}
250fn early_amain() callconv(.Async) void {250fn early_amain() callconv(.Async) void {
251 early_seq('b');251 early_seq('b');
...@@ -276,7 +276,7 @@ test "async function with dot syntax" {...@@ -276,7 +276,7 @@ test "async function with dot syntax" {
276 }276 }
277 };277 };
278 const p = async S.foo();278 const p = async S.foo();
279 expect(S.y == 2);279 try expect(S.y == 2);
280}280}
281281
282test "async fn pointer in a struct field" {282test "async fn pointer in a struct field" {
...@@ -287,12 +287,12 @@ test "async fn pointer in a struct field" {...@@ -287,12 +287,12 @@ test "async fn pointer in a struct field" {
287 var foo = Foo{ .bar = simpleAsyncFn2 };287 var foo = Foo{ .bar = simpleAsyncFn2 };
288 var bytes: [64]u8 align(16) = undefined;288 var bytes: [64]u8 align(16) = undefined;
289 const f = @asyncCall(&bytes, {}, foo.bar, .{&data});289 const f = @asyncCall(&bytes, {}, foo.bar, .{&data});
290 comptime expect(@TypeOf(f) == anyframe->void);290 comptime try expect(@TypeOf(f) == anyframe->void);
291 expect(data == 2);291 try expect(data == 2);
292 resume f;292 resume f;
293 expect(data == 4);293 try expect(data == 4);
294 _ = async doTheAwait(f);294 _ = async doTheAwait(f);
295 expect(data == 4);295 try expect(data == 4);
296}296}
297297
298fn doTheAwait(f: anyframe->void) void {298fn doTheAwait(f: anyframe->void) void {
...@@ -323,22 +323,22 @@ test "@asyncCall with return type" {...@@ -323,22 +323,22 @@ test "@asyncCall with return type" {
323 var bytes: [150]u8 align(16) = undefined;323 var bytes: [150]u8 align(16) = undefined;
324 var aresult: i32 = 0;324 var aresult: i32 = 0;
325 _ = @asyncCall(&bytes, &aresult, foo.bar, .{});325 _ = @asyncCall(&bytes, &aresult, foo.bar, .{});
326 expect(aresult == 0);326 try expect(aresult == 0);
327 resume Foo.global_frame;327 resume Foo.global_frame;
328 expect(aresult == 1234);328 try expect(aresult == 1234);
329}329}
330330
331test "async fn with inferred error set" {331test "async fn with inferred error set" {
332 const S = struct {332 const S = struct {
333 var global_frame: anyframe = undefined;333 var global_frame: anyframe = undefined;
334334
335 fn doTheTest() void {335 fn doTheTest() !void {
336 var frame: [1]@Frame(middle) = undefined;336 var frame: [1]@Frame(middle) = undefined;
337 var fn_ptr = middle;337 var fn_ptr = middle;
338 var result: @typeInfo(@typeInfo(@TypeOf(fn_ptr)).Fn.return_type.?).ErrorUnion.error_set!void = undefined;338 var result: @typeInfo(@typeInfo(@TypeOf(fn_ptr)).Fn.return_type.?).ErrorUnion.error_set!void = undefined;
339 _ = @asyncCall(std.mem.sliceAsBytes(frame[0..]), &result, fn_ptr, .{});339 _ = @asyncCall(std.mem.sliceAsBytes(frame[0..]), &result, fn_ptr, .{});
340 resume global_frame;340 resume global_frame;
341 std.testing.expectError(error.Fail, result);341 try std.testing.expectError(error.Fail, result);
342 }342 }
343 fn middle() callconv(.Async) !void {343 fn middle() callconv(.Async) !void {
344 var f = async middle2();344 var f = async middle2();
...@@ -355,7 +355,7 @@ test "async fn with inferred error set" {...@@ -355,7 +355,7 @@ test "async fn with inferred error set" {
355 return error.Fail;355 return error.Fail;
356 }356 }
357 };357 };
358 S.doTheTest();358 try S.doTheTest();
359}359}
360360
361test "error return trace across suspend points - early return" {361test "error return trace across suspend points - early return" {
...@@ -383,9 +383,9 @@ fn suspendThenFail() callconv(.Async) anyerror!void {...@@ -383,9 +383,9 @@ fn suspendThenFail() callconv(.Async) anyerror!void {
383}383}
384fn printTrace(p: anyframe->(anyerror!void)) callconv(.Async) void {384fn printTrace(p: anyframe->(anyerror!void)) callconv(.Async) void {
385 (await p) catch |e| {385 (await p) catch |e| {
386 std.testing.expect(e == error.Fail);386 std.testing.expect(e == error.Fail) catch @panic("test failure");
387 if (@errorReturnTrace()) |trace| {387 if (@errorReturnTrace()) |trace| {
388 expect(trace.index == 1);388 expect(trace.index == 1) catch @panic("test failure");
389 } else switch (builtin.mode) {389 } else switch (builtin.mode) {
390 .Debug, .ReleaseSafe => @panic("expected return trace"),390 .Debug, .ReleaseSafe => @panic("expected return trace"),
391 .ReleaseFast, .ReleaseSmall => {},391 .ReleaseFast, .ReleaseSmall => {},
...@@ -396,7 +396,7 @@ fn printTrace(p: anyframe->(anyerror!void)) callconv(.Async) void {...@@ -396,7 +396,7 @@ fn printTrace(p: anyframe->(anyerror!void)) callconv(.Async) void {
396test "break from suspend" {396test "break from suspend" {
397 var my_result: i32 = 1;397 var my_result: i32 = 1;
398 const p = async testBreakFromSuspend(&my_result);398 const p = async testBreakFromSuspend(&my_result);
399 std.testing.expect(my_result == 2);399 try std.testing.expect(my_result == 2);
400}400}
401fn testBreakFromSuspend(my_result: *i32) callconv(.Async) void {401fn testBreakFromSuspend(my_result: *i32) callconv(.Async) void {
402 suspend {402 suspend {
...@@ -415,11 +415,11 @@ test "heap allocated async function frame" {...@@ -415,11 +415,11 @@ test "heap allocated async function frame" {
415 const frame = try std.testing.allocator.create(@Frame(someFunc));415 const frame = try std.testing.allocator.create(@Frame(someFunc));
416 defer std.testing.allocator.destroy(frame);416 defer std.testing.allocator.destroy(frame);
417417
418 expect(x == 42);418 try expect(x == 42);
419 frame.* = async someFunc();419 frame.* = async someFunc();
420 expect(x == 43);420 try expect(x == 43);
421 resume frame;421 resume frame;
422 expect(x == 44);422 try expect(x == 44);
423 }423 }
424424
425 fn someFunc() void {425 fn someFunc() void {
...@@ -436,15 +436,15 @@ test "async function call return value" {...@@ -436,15 +436,15 @@ test "async function call return value" {
436 var frame: anyframe = undefined;436 var frame: anyframe = undefined;
437 var pt = Point{ .x = 10, .y = 11 };437 var pt = Point{ .x = 10, .y = 11 };
438438
439 fn doTheTest() void {439 fn doTheTest() !void {
440 expectEqual(pt.x, 10);440 try expectEqual(pt.x, 10);
441 expectEqual(pt.y, 11);441 try expectEqual(pt.y, 11);
442 _ = async first();442 _ = async first();
443 expectEqual(pt.x, 10);443 try expectEqual(pt.x, 10);
444 expectEqual(pt.y, 11);444 try expectEqual(pt.y, 11);
445 resume frame;445 resume frame;
446 expectEqual(pt.x, 1);446 try expectEqual(pt.x, 1);
447 expectEqual(pt.y, 2);447 try expectEqual(pt.y, 2);
448 }448 }
449449
450 fn first() void {450 fn first() void {
...@@ -469,23 +469,23 @@ test "async function call return value" {...@@ -469,23 +469,23 @@ test "async function call return value" {
469 y: i32,469 y: i32,
470 };470 };
471 };471 };
472 S.doTheTest();472 try S.doTheTest();
473}473}
474474
475test "suspension points inside branching control flow" {475test "suspension points inside branching control flow" {
476 const S = struct {476 const S = struct {
477 var result: i32 = 10;477 var result: i32 = 10;
478478
479 fn doTheTest() void {479 fn doTheTest() !void {
480 expect(10 == result);480 try expect(10 == result);
481 var frame = async func(true);481 var frame = async func(true);
482 expect(10 == result);482 try expect(10 == result);
483 resume frame;483 resume frame;
484 expect(11 == result);484 try expect(11 == result);
485 resume frame;485 resume frame;
486 expect(12 == result);486 try expect(12 == result);
487 resume frame;487 resume frame;
488 expect(13 == result);488 try expect(13 == result);
489 }489 }
490490
491 fn func(b: bool) void {491 fn func(b: bool) void {
...@@ -495,7 +495,7 @@ test "suspension points inside branching control flow" {...@@ -495,7 +495,7 @@ test "suspension points inside branching control flow" {
495 }495 }
496 }496 }
497 };497 };
498 S.doTheTest();498 try S.doTheTest();
499}499}
500500
501test "call async function which has struct return type" {501test "call async function which has struct return type" {
...@@ -509,8 +509,8 @@ test "call async function which has struct return type" {...@@ -509,8 +509,8 @@ test "call async function which has struct return type" {
509509
510 fn atest() void {510 fn atest() void {
511 const result = func();511 const result = func();
512 expect(result.x == 5);512 expect(result.x == 5) catch @panic("test failed");
513 expect(result.y == 6);513 expect(result.y == 6) catch @panic("test failed");
514 }514 }
515515
516 const Point = struct {516 const Point = struct {
...@@ -536,27 +536,27 @@ test "pass string literal to async function" {...@@ -536,27 +536,27 @@ test "pass string literal to async function" {
536 var frame: anyframe = undefined;536 var frame: anyframe = undefined;
537 var ok: bool = false;537 var ok: bool = false;
538538
539 fn doTheTest() void {539 fn doTheTest() !void {
540 _ = async hello("hello");540 _ = async hello("hello");
541 resume frame;541 resume frame;
542 expect(ok);542 try expect(ok);
543 }543 }
544544
545 fn hello(msg: []const u8) void {545 fn hello(msg: []const u8) void {
546 frame = @frame();546 frame = @frame();
547 suspend {}547 suspend {}
548 expectEqualStrings("hello", msg);548 expectEqualStrings("hello", msg) catch @panic("test failed");
549 ok = true;549 ok = true;
550 }550 }
551 };551 };
552 S.doTheTest();552 try S.doTheTest();
553}553}
554554
555test "await inside an errdefer" {555test "await inside an errdefer" {
556 const S = struct {556 const S = struct {
557 var frame: anyframe = undefined;557 var frame: anyframe = undefined;
558558
559 fn doTheTest() void {559 fn doTheTest() !void {
560 _ = async amainWrap();560 _ = async amainWrap();
561 resume frame;561 resume frame;
562 }562 }
...@@ -572,7 +572,7 @@ test "await inside an errdefer" {...@@ -572,7 +572,7 @@ test "await inside an errdefer" {
572 suspend {}572 suspend {}
573 }573 }
574 };574 };
575 S.doTheTest();575 try S.doTheTest();
576}576}
577577
578test "try in an async function with error union and non-zero-bit payload" {578test "try in an async function with error union and non-zero-bit payload" {
...@@ -580,14 +580,14 @@ test "try in an async function with error union and non-zero-bit payload" {...@@ -580,14 +580,14 @@ test "try in an async function with error union and non-zero-bit payload" {
580 var frame: anyframe = undefined;580 var frame: anyframe = undefined;
581 var ok = false;581 var ok = false;
582582
583 fn doTheTest() void {583 fn doTheTest() !void {
584 _ = async amain();584 _ = async amain();
585 resume frame;585 resume frame;
586 expect(ok);586 try expect(ok);
587 }587 }
588588
589 fn amain() void {589 fn amain() void {
590 std.testing.expectError(error.Bad, theProblem());590 std.testing.expectError(error.Bad, theProblem()) catch @panic("test failed");
591 ok = true;591 ok = true;
592 }592 }
593593
...@@ -602,7 +602,7 @@ test "try in an async function with error union and non-zero-bit payload" {...@@ -602,7 +602,7 @@ test "try in an async function with error union and non-zero-bit payload" {
602 return error.Bad;602 return error.Bad;
603 }603 }
604 };604 };
605 S.doTheTest();605 try S.doTheTest();
606}606}
607607
608test "returning a const error from async function" {608test "returning a const error from async function" {
...@@ -610,10 +610,10 @@ test "returning a const error from async function" {...@@ -610,10 +610,10 @@ test "returning a const error from async function" {
610 var frame: anyframe = undefined;610 var frame: anyframe = undefined;
611 var ok = false;611 var ok = false;
612612
613 fn doTheTest() void {613 fn doTheTest() !void {
614 _ = async amain();614 _ = async amain();
615 resume frame;615 resume frame;
616 expect(ok);616 try expect(ok);
617 }617 }
618618
619 fn amain() !void {619 fn amain() !void {
...@@ -630,7 +630,7 @@ test "returning a const error from async function" {...@@ -630,7 +630,7 @@ test "returning a const error from async function" {
630 return error.OutOfMemory;630 return error.OutOfMemory;
631 }631 }
632 };632 };
633 S.doTheTest();633 try S.doTheTest();
634}634}
635635
636test "async/await typical usage" {636test "async/await typical usage" {
...@@ -663,11 +663,11 @@ fn testAsyncAwaitTypicalUsage(...@@ -663,11 +663,11 @@ fn testAsyncAwaitTypicalUsage(
663 }663 }
664 fn amainWrap() void {664 fn amainWrap() void {
665 if (amain()) |_| {665 if (amain()) |_| {
666 expect(!simulate_fail_download);666 expect(!simulate_fail_download) catch @panic("test failure");
667 expect(!simulate_fail_file);667 expect(!simulate_fail_file) catch @panic("test failure");
668 } else |e| switch (e) {668 } else |e| switch (e) {
669 error.NoResponse => expect(simulate_fail_download),669 error.NoResponse => expect(simulate_fail_download) catch @panic("test failure"),
670 error.FileNotFound => expect(simulate_fail_file),670 error.FileNotFound => expect(simulate_fail_file) catch @panic("test failure"),
671 else => @panic("test failure"),671 else => @panic("test failure"),
672 }672 }
673 }673 }
...@@ -694,8 +694,8 @@ fn testAsyncAwaitTypicalUsage(...@@ -694,8 +694,8 @@ fn testAsyncAwaitTypicalUsage(
694 const file_text = try await file_frame;694 const file_text = try await file_frame;
695 defer allocator.free(file_text);695 defer allocator.free(file_text);
696696
697 expect(std.mem.eql(u8, "expected download text", download_text));697 try expect(std.mem.eql(u8, "expected download text", download_text));
698 expect(std.mem.eql(u8, "expected file text", file_text));698 try expect(std.mem.eql(u8, "expected file text", file_text));
699 }699 }
700700
701 var global_download_frame: anyframe = undefined;701 var global_download_frame: anyframe = undefined;
...@@ -728,13 +728,13 @@ fn testAsyncAwaitTypicalUsage(...@@ -728,13 +728,13 @@ fn testAsyncAwaitTypicalUsage(
728728
729test "alignment of local variables in async functions" {729test "alignment of local variables in async functions" {
730 const S = struct {730 const S = struct {
731 fn doTheTest() void {731 fn doTheTest() !void {
732 var y: u8 = 123;732 var y: u8 = 123;
733 var x: u8 align(128) = 1;733 var x: u8 align(128) = 1;
734 expect(@ptrToInt(&x) % 128 == 0);734 try expect(@ptrToInt(&x) % 128 == 0);
735 }735 }
736 };736 };
737 S.doTheTest();737 try S.doTheTest();
738}738}
739739
740test "no reason to resolve frame still works" {740test "no reason to resolve frame still works" {
...@@ -746,10 +746,10 @@ fn simpleNothing() void {...@@ -746,10 +746,10 @@ fn simpleNothing() void {
746746
747test "async call a generic function" {747test "async call a generic function" {
748 const S = struct {748 const S = struct {
749 fn doTheTest() void {749 fn doTheTest() !void {
750 var f = async func(i32, 2);750 var f = async func(i32, 2);
751 const result = await f;751 const result = await f;
752 expect(result == 3);752 try expect(result == 3);
753 }753 }
754754
755 fn func(comptime T: type, inc: T) T {755 fn func(comptime T: type, inc: T) T {
...@@ -766,8 +766,8 @@ test "async call a generic function" {...@@ -766,8 +766,8 @@ test "async call a generic function" {
766766
767test "return from suspend block" {767test "return from suspend block" {
768 const S = struct {768 const S = struct {
769 fn doTheTest() void {769 fn doTheTest() !void {
770 expect(func() == 1234);770 expect(func() == 1234) catch @panic("test failure");
771 }771 }
772 fn func() i32 {772 fn func() i32 {
773 suspend {773 suspend {
...@@ -808,7 +808,7 @@ test "struct parameter to async function is copied to the frame" {...@@ -808,7 +808,7 @@ test "struct parameter to async function is copied to the frame" {
808 var pt = Point{ .x = 1, .y = 2 };808 var pt = Point{ .x = 1, .y = 2 };
809 f.* = async foo(pt);809 f.* = async foo(pt);
810 var result = await f;810 var result = await f;
811 expect(result == 1);811 expect(result == 1) catch @panic("test failure");
812 }812 }
813813
814 fn foo(point: Point) i32 {814 fn foo(point: Point) i32 {
...@@ -833,7 +833,7 @@ test "cast fn to async fn when it is inferred to be async" {...@@ -833,7 +833,7 @@ test "cast fn to async fn when it is inferred to be async" {
833 var result: i32 = undefined;833 var result: i32 = undefined;
834 const f = @asyncCall(&buf, &result, ptr, .{});834 const f = @asyncCall(&buf, &result, ptr, .{});
835 _ = await f;835 _ = await f;
836 expect(result == 1234);836 expect(result == 1234) catch @panic("test failure");
837 ok = true;837 ok = true;
838 }838 }
839839
...@@ -846,7 +846,7 @@ test "cast fn to async fn when it is inferred to be async" {...@@ -846,7 +846,7 @@ test "cast fn to async fn when it is inferred to be async" {
846 };846 };
847 _ = async S.doTheTest();847 _ = async S.doTheTest();
848 resume S.frame;848 resume S.frame;
849 expect(S.ok);849 try expect(S.ok);
850}850}
851851
852test "cast fn to async fn when it is inferred to be async, awaited directly" {852test "cast fn to async fn when it is inferred to be async, awaited directly" {
...@@ -860,7 +860,7 @@ test "cast fn to async fn when it is inferred to be async, awaited directly" {...@@ -860,7 +860,7 @@ test "cast fn to async fn when it is inferred to be async, awaited directly" {
860 var buf: [100]u8 align(16) = undefined;860 var buf: [100]u8 align(16) = undefined;
861 var result: i32 = undefined;861 var result: i32 = undefined;
862 _ = await @asyncCall(&buf, &result, ptr, .{});862 _ = await @asyncCall(&buf, &result, ptr, .{});
863 expect(result == 1234);863 expect(result == 1234) catch @panic("test failure");
864 ok = true;864 ok = true;
865 }865 }
866866
...@@ -873,7 +873,7 @@ test "cast fn to async fn when it is inferred to be async, awaited directly" {...@@ -873,7 +873,7 @@ test "cast fn to async fn when it is inferred to be async, awaited directly" {
873 };873 };
874 _ = async S.doTheTest();874 _ = async S.doTheTest();
875 resume S.frame;875 resume S.frame;
876 expect(S.ok);876 try expect(S.ok);
877}877}
878878
879test "await does not force async if callee is blocking" {879test "await does not force async if callee is blocking" {
...@@ -883,12 +883,12 @@ test "await does not force async if callee is blocking" {...@@ -883,12 +883,12 @@ test "await does not force async if callee is blocking" {
883 }883 }
884 };884 };
885 var x = async S.simple();885 var x = async S.simple();
886 expect(await x == 1234);886 try expect(await x == 1234);
887}887}
888888
889test "recursive async function" {889test "recursive async function" {
890 expect(recursiveAsyncFunctionTest(false).doTheTest() == 55);890 try expect(recursiveAsyncFunctionTest(false).doTheTest() == 55);
891 expect(recursiveAsyncFunctionTest(true).doTheTest() == 55);891 try expect(recursiveAsyncFunctionTest(true).doTheTest() == 55);
892}892}
893893
894fn recursiveAsyncFunctionTest(comptime suspending_implementation: bool) type {894fn recursiveAsyncFunctionTest(comptime suspending_implementation: bool) type {
...@@ -952,12 +952,12 @@ test "@asyncCall with comptime-known function, but not awaited directly" {...@@ -952,12 +952,12 @@ test "@asyncCall with comptime-known function, but not awaited directly" {
952 const S = struct {952 const S = struct {
953 var global_frame: anyframe = undefined;953 var global_frame: anyframe = undefined;
954954
955 fn doTheTest() void {955 fn doTheTest() !void {
956 var frame: [1]@Frame(middle) = undefined;956 var frame: [1]@Frame(middle) = undefined;
957 var result: @typeInfo(@typeInfo(@TypeOf(middle)).Fn.return_type.?).ErrorUnion.error_set!void = undefined;957 var result: @typeInfo(@typeInfo(@TypeOf(middle)).Fn.return_type.?).ErrorUnion.error_set!void = undefined;
958 _ = @asyncCall(std.mem.sliceAsBytes(frame[0..]), &result, middle, .{});958 _ = @asyncCall(std.mem.sliceAsBytes(frame[0..]), &result, middle, .{});
959 resume global_frame;959 resume global_frame;
960 std.testing.expectError(error.Fail, result);960 try std.testing.expectError(error.Fail, result);
961 }961 }
962 fn middle() callconv(.Async) !void {962 fn middle() callconv(.Async) !void {
963 var f = async middle2();963 var f = async middle2();
...@@ -974,7 +974,7 @@ test "@asyncCall with comptime-known function, but not awaited directly" {...@@ -974,7 +974,7 @@ test "@asyncCall with comptime-known function, but not awaited directly" {
974 return error.Fail;974 return error.Fail;
975 }975 }
976 };976 };
977 S.doTheTest();977 try S.doTheTest();
978}978}
979979
980test "@asyncCall with actual frame instead of byte buffer" {980test "@asyncCall with actual frame instead of byte buffer" {
...@@ -988,7 +988,7 @@ test "@asyncCall with actual frame instead of byte buffer" {...@@ -988,7 +988,7 @@ test "@asyncCall with actual frame instead of byte buffer" {
988 var result: i32 = undefined;988 var result: i32 = undefined;
989 const ptr = @asyncCall(&frame, &result, S.func, .{});989 const ptr = @asyncCall(&frame, &result, S.func, .{});
990 resume ptr;990 resume ptr;
991 expect(result == 1234);991 try expect(result == 1234);
992}992}
993993
994test "@asyncCall using the result location inside the frame" {994test "@asyncCall using the result location inside the frame" {
...@@ -1010,19 +1010,19 @@ test "@asyncCall using the result location inside the frame" {...@@ -1010,19 +1010,19 @@ test "@asyncCall using the result location inside the frame" {
1010 var foo = Foo{ .bar = S.simple2 };1010 var foo = Foo{ .bar = S.simple2 };
1011 var bytes: [64]u8 align(16) = undefined;1011 var bytes: [64]u8 align(16) = undefined;
1012 const f = @asyncCall(&bytes, {}, foo.bar, .{&data});1012 const f = @asyncCall(&bytes, {}, foo.bar, .{&data});
1013 comptime expect(@TypeOf(f) == anyframe->i32);1013 comptime try expect(@TypeOf(f) == anyframe->i32);
1014 expect(data == 2);1014 try expect(data == 2);
1015 resume f;1015 resume f;
1016 expect(data == 4);1016 try expect(data == 4);
1017 _ = async S.getAnswer(f, &data);1017 _ = async S.getAnswer(f, &data);
1018 expect(data == 1234);1018 try expect(data == 1234);
1019}1019}
10201020
1021test "@TypeOf an async function call of generic fn with error union type" {1021test "@TypeOf an async function call of generic fn with error union type" {
1022 const S = struct {1022 const S = struct {
1023 fn func(comptime x: anytype) anyerror!i32 {1023 fn func(comptime x: anytype) anyerror!i32 {
1024 const T = @TypeOf(async func(x));1024 const T = @TypeOf(async func(x));
1025 comptime expect(T == @typeInfo(@TypeOf(@frame())).Pointer.child);1025 comptime try expect(T == @typeInfo(@TypeOf(@frame())).Pointer.child);
1026 return undefined;1026 return undefined;
1027 }1027 }
1028 };1028 };
...@@ -1051,7 +1051,7 @@ test "using @TypeOf on a generic function call" {...@@ -1051,7 +1051,7 @@ test "using @TypeOf on a generic function call" {
1051 };1051 };
1052 _ = async S.amain(@as(u32, 1));1052 _ = async S.amain(@as(u32, 1));
1053 resume S.global_frame;1053 resume S.global_frame;
1054 expect(S.global_ok);1054 try expect(S.global_ok);
1055}1055}
10561056
1057test "recursive call of await @asyncCall with struct return type" {1057test "recursive call of await @asyncCall with struct return type" {
...@@ -1084,17 +1084,17 @@ test "recursive call of await @asyncCall with struct return type" {...@@ -1084,17 +1084,17 @@ test "recursive call of await @asyncCall with struct return type" {
1084 var frame: @TypeOf(async S.amain(@as(u32, 1))) = undefined;1084 var frame: @TypeOf(async S.amain(@as(u32, 1))) = undefined;
1085 _ = @asyncCall(&frame, &res, S.amain, .{@as(u32, 1)});1085 _ = @asyncCall(&frame, &res, S.amain, .{@as(u32, 1)});
1086 resume S.global_frame;1086 resume S.global_frame;
1087 expect(S.global_ok);1087 try expect(S.global_ok);
1088 expect(res.x == 1);1088 try expect(res.x == 1);
1089 expect(res.y == 2);1089 try expect(res.y == 2);
1090 expect(res.z == 3);1090 try expect(res.z == 3);
1091}1091}
10921092
1093test "nosuspend function call" {1093test "nosuspend function call" {
1094 const S = struct {1094 const S = struct {
1095 fn doTheTest() void {1095 fn doTheTest() !void {
1096 const result = nosuspend add(50, 100);1096 const result = nosuspend add(50, 100);
1097 expect(result == 150);1097 try expect(result == 150);
1098 }1098 }
1099 fn add(a: i32, b: i32) i32 {1099 fn add(a: i32, b: i32) i32 {
1100 if (a > 100) {1100 if (a > 100) {
...@@ -1103,7 +1103,7 @@ test "nosuspend function call" {...@@ -1103,7 +1103,7 @@ test "nosuspend function call" {
1103 return a + b;1103 return a + b;
1104 }1104 }
1105 };1105 };
1106 S.doTheTest();1106 try S.doTheTest();
1107}1107}
11081108
1109test "await used in expression and awaiting fn with no suspend but async calling convention" {1109test "await used in expression and awaiting fn with no suspend but async calling convention" {
...@@ -1113,7 +1113,7 @@ test "await used in expression and awaiting fn with no suspend but async calling...@@ -1113,7 +1113,7 @@ test "await used in expression and awaiting fn with no suspend but async calling
1113 var f2 = async add(3, 4);1113 var f2 = async add(3, 4);
11141114
1115 const sum = (await f1) + (await f2);1115 const sum = (await f1) + (await f2);
1116 expect(sum == 10);1116 expect(sum == 10) catch @panic("test failure");
1117 }1117 }
1118 fn add(a: i32, b: i32) callconv(.Async) i32 {1118 fn add(a: i32, b: i32) callconv(.Async) i32 {
1119 return a + b;1119 return a + b;
...@@ -1128,7 +1128,7 @@ test "await used in expression after a fn call" {...@@ -1128,7 +1128,7 @@ test "await used in expression after a fn call" {
1128 var f1 = async add(3, 4);1128 var f1 = async add(3, 4);
1129 var sum: i32 = 0;1129 var sum: i32 = 0;
1130 sum = foo() + await f1;1130 sum = foo() + await f1;
1131 expect(sum == 8);1131 expect(sum == 8) catch @panic("test failure");
1132 }1132 }
1133 fn add(a: i32, b: i32) callconv(.Async) i32 {1133 fn add(a: i32, b: i32) callconv(.Async) i32 {
1134 return a + b;1134 return a + b;
...@@ -1145,7 +1145,7 @@ test "async fn call used in expression after a fn call" {...@@ -1145,7 +1145,7 @@ test "async fn call used in expression after a fn call" {
1145 fn atest() void {1145 fn atest() void {
1146 var sum: i32 = 0;1146 var sum: i32 = 0;
1147 sum = foo() + add(3, 4);1147 sum = foo() + add(3, 4);
1148 expect(sum == 8);1148 expect(sum == 8) catch @panic("test failure");
1149 }1149 }
1150 fn add(a: i32, b: i32) callconv(.Async) i32 {1150 fn add(a: i32, b: i32) callconv(.Async) i32 {
1151 return a + b;1151 return a + b;
...@@ -1167,7 +1167,7 @@ test "suspend in for loop" {...@@ -1167,7 +1167,7 @@ test "suspend in for loop" {
1167 }1167 }
11681168
1169 fn atest() void {1169 fn atest() void {
1170 expect(func(&[_]u8{ 1, 2, 3 }) == 6);1170 expect(func(&[_]u8{ 1, 2, 3 }) == 6) catch @panic("test failure");
1171 }1171 }
1172 fn func(stuff: []const u8) u32 {1172 fn func(stuff: []const u8) u32 {
1173 global_frame = @frame();1173 global_frame = @frame();
...@@ -1193,8 +1193,8 @@ test "suspend in while loop" {...@@ -1193,8 +1193,8 @@ test "suspend in while loop" {
1193 }1193 }
11941194
1195 fn atest() void {1195 fn atest() void {
1196 expect(optional(6) == 6);1196 expect(optional(6) == 6) catch @panic("test failure");
1197 expect(errunion(6) == 6);1197 expect(errunion(6) == 6) catch @panic("test failure");
1198 }1198 }
1199 fn optional(stuff: ?u32) u32 {1199 fn optional(stuff: ?u32) u32 {
1200 global_frame = @frame();1200 global_frame = @frame();
...@@ -1223,8 +1223,8 @@ test "correctly spill when returning the error union result of another async fn"...@@ -1223,8 +1223,8 @@ test "correctly spill when returning the error union result of another async fn"
1223 const S = struct {1223 const S = struct {
1224 var global_frame: anyframe = undefined;1224 var global_frame: anyframe = undefined;
12251225
1226 fn doTheTest() void {1226 fn doTheTest() !void {
1227 expect((atest() catch unreachable) == 1234);1227 expect((atest() catch unreachable) == 1234) catch @panic("test failure");
1228 }1228 }
12291229
1230 fn atest() !i32 {1230 fn atest() !i32 {
...@@ -1246,11 +1246,11 @@ test "spill target expr in a for loop" {...@@ -1246,11 +1246,11 @@ test "spill target expr in a for loop" {
1246 const S = struct {1246 const S = struct {
1247 var global_frame: anyframe = undefined;1247 var global_frame: anyframe = undefined;
12481248
1249 fn doTheTest() void {1249 fn doTheTest() !void {
1250 var foo = Foo{1250 var foo = Foo{
1251 .slice = &[_]i32{ 1, 2 },1251 .slice = &[_]i32{ 1, 2 },
1252 };1252 };
1253 expect(atest(&foo) == 3);1253 expect(atest(&foo) == 3) catch @panic("test failure");
1254 }1254 }
12551255
1256 const Foo = struct {1256 const Foo = struct {
...@@ -1277,11 +1277,11 @@ test "spill target expr in a for loop, with a var decl in the loop body" {...@@ -1277,11 +1277,11 @@ test "spill target expr in a for loop, with a var decl in the loop body" {
1277 const S = struct {1277 const S = struct {
1278 var global_frame: anyframe = undefined;1278 var global_frame: anyframe = undefined;
12791279
1280 fn doTheTest() void {1280 fn doTheTest() !void {
1281 var foo = Foo{1281 var foo = Foo{
1282 .slice = &[_]i32{ 1, 2 },1282 .slice = &[_]i32{ 1, 2 },
1283 };1283 };
1284 expect(atest(&foo) == 3);1284 expect(atest(&foo) == 3) catch @panic("test failure");
1285 }1285 }
12861286
1287 const Foo = struct {1287 const Foo = struct {
...@@ -1319,7 +1319,7 @@ test "async call with @call" {...@@ -1319,7 +1319,7 @@ test "async call with @call" {
1319 fn atest() void {1319 fn atest() void {
1320 var frame = @call(.{ .modifier = .async_kw }, afoo, .{});1320 var frame = @call(.{ .modifier = .async_kw }, afoo, .{});
1321 const res = await frame;1321 const res = await frame;
1322 expect(res == 42);1322 expect(res == 42) catch @panic("test failure");
1323 }1323 }
1324 fn afoo() i32 {1324 fn afoo() i32 {
1325 suspend {1325 suspend {
...@@ -1348,7 +1348,7 @@ test "async function passed 0-bit arg after non-0-bit arg" {...@@ -1348,7 +1348,7 @@ test "async function passed 0-bit arg after non-0-bit arg" {
1348 };1348 };
1349 _ = async S.foo();1349 _ = async S.foo();
1350 resume S.global_frame;1350 resume S.global_frame;
1351 expect(S.global_int == 1);1351 try expect(S.global_int == 1);
1352}1352}
13531353
1354test "async function passed align(16) arg after align(8) arg" {1354test "async function passed align(16) arg after align(8) arg" {
...@@ -1362,7 +1362,7 @@ test "async function passed align(16) arg after align(8) arg" {...@@ -1362,7 +1362,7 @@ test "async function passed align(16) arg after align(8) arg" {
1362 }1362 }
13631363
1364 fn bar(x: u64, args: anytype) anyerror!void {1364 fn bar(x: u64, args: anytype) anyerror!void {
1365 expect(x == 10);1365 try expect(x == 10);
1366 global_frame = @frame();1366 global_frame = @frame();
1367 suspend {}1367 suspend {}
1368 global_int = args[0];1368 global_int = args[0];
...@@ -1370,7 +1370,7 @@ test "async function passed align(16) arg after align(8) arg" {...@@ -1370,7 +1370,7 @@ test "async function passed align(16) arg after align(8) arg" {
1370 };1370 };
1371 _ = async S.foo();1371 _ = async S.foo();
1372 resume S.global_frame;1372 resume S.global_frame;
1373 expect(S.global_int == 99);1373 try expect(S.global_int == 99);
1374}1374}
13751375
1376test "async function call resolves target fn frame, comptime func" {1376test "async function call resolves target fn frame, comptime func" {
...@@ -1392,7 +1392,7 @@ test "async function call resolves target fn frame, comptime func" {...@@ -1392,7 +1392,7 @@ test "async function call resolves target fn frame, comptime func" {
1392 };1392 };
1393 _ = async S.foo();1393 _ = async S.foo();
1394 resume S.global_frame;1394 resume S.global_frame;
1395 expect(S.global_int == 10);1395 try expect(S.global_int == 10);
1396}1396}
13971397
1398test "async function call resolves target fn frame, runtime func" {1398test "async function call resolves target fn frame, runtime func" {
...@@ -1415,7 +1415,7 @@ test "async function call resolves target fn frame, runtime func" {...@@ -1415,7 +1415,7 @@ test "async function call resolves target fn frame, runtime func" {
1415 };1415 };
1416 _ = async S.foo();1416 _ = async S.foo();
1417 resume S.global_frame;1417 resume S.global_frame;
1418 expect(S.global_int == 10);1418 try expect(S.global_int == 10);
1419}1419}
14201420
1421test "properly spill optional payload capture value" {1421test "properly spill optional payload capture value" {
...@@ -1439,7 +1439,7 @@ test "properly spill optional payload capture value" {...@@ -1439,7 +1439,7 @@ test "properly spill optional payload capture value" {
1439 };1439 };
1440 _ = async S.foo();1440 _ = async S.foo();
1441 resume S.global_frame;1441 resume S.global_frame;
1442 expect(S.global_int == 1237);1442 try expect(S.global_int == 1237);
1443}1443}
14441444
1445test "handle defer interfering with return value spill" {1445test "handle defer interfering with return value spill" {
...@@ -1449,16 +1449,16 @@ test "handle defer interfering with return value spill" {...@@ -1449,16 +1449,16 @@ test "handle defer interfering with return value spill" {
1449 var finished = false;1449 var finished = false;
1450 var baz_happened = false;1450 var baz_happened = false;
14511451
1452 fn doTheTest() void {1452 fn doTheTest() !void {
1453 _ = async testFoo();1453 _ = async testFoo();
1454 resume global_frame1;1454 resume global_frame1;
1455 resume global_frame2;1455 resume global_frame2;
1456 expect(baz_happened);1456 try expect(baz_happened);
1457 expect(finished);1457 try expect(finished);
1458 }1458 }
14591459
1460 fn testFoo() void {1460 fn testFoo() void {
1461 expectError(error.Bad, foo());1461 expectError(error.Bad, foo()) catch @panic("test failure");
1462 finished = true;1462 finished = true;
1463 }1463 }
14641464
...@@ -1479,7 +1479,7 @@ test "handle defer interfering with return value spill" {...@@ -1479,7 +1479,7 @@ test "handle defer interfering with return value spill" {
1479 baz_happened = true;1479 baz_happened = true;
1480 }1480 }
1481 };1481 };
1482 S.doTheTest();1482 try S.doTheTest();
1483}1483}
14841484
1485test "take address of temporary async frame" {1485test "take address of temporary async frame" {
...@@ -1487,14 +1487,14 @@ test "take address of temporary async frame" {...@@ -1487,14 +1487,14 @@ test "take address of temporary async frame" {
1487 var global_frame: anyframe = undefined;1487 var global_frame: anyframe = undefined;
1488 var finished = false;1488 var finished = false;
14891489
1490 fn doTheTest() void {1490 fn doTheTest() !void {
1491 _ = async asyncDoTheTest();1491 _ = async asyncDoTheTest();
1492 resume global_frame;1492 resume global_frame;
1493 expect(finished);1493 try expect(finished);
1494 }1494 }
14951495
1496 fn asyncDoTheTest() void {1496 fn asyncDoTheTest() void {
1497 expect(finishIt(&async foo(10)) == 1245);1497 expect(finishIt(&async foo(10)) == 1245) catch @panic("test failure");
1498 finished = true;1498 finished = true;
1499 }1499 }
15001500
...@@ -1508,16 +1508,16 @@ test "take address of temporary async frame" {...@@ -1508,16 +1508,16 @@ test "take address of temporary async frame" {
1508 return (await frame) + 1;1508 return (await frame) + 1;
1509 }1509 }
1510 };1510 };
1511 S.doTheTest();1511 try S.doTheTest();
1512}1512}
15131513
1514test "nosuspend await" {1514test "nosuspend await" {
1515 const S = struct {1515 const S = struct {
1516 var finished = false;1516 var finished = false;
15171517
1518 fn doTheTest() void {1518 fn doTheTest() !void {
1519 var frame = async foo(false);1519 var frame = async foo(false);
1520 expect(nosuspend await frame == 42);1520 try expect(nosuspend await frame == 42);
1521 finished = true;1521 finished = true;
1522 }1522 }
15231523
...@@ -1528,8 +1528,8 @@ test "nosuspend await" {...@@ -1528,8 +1528,8 @@ test "nosuspend await" {
1528 return 42;1528 return 42;
1529 }1529 }
1530 };1530 };
1531 S.doTheTest();1531 try S.doTheTest();
1532 expect(S.finished);1532 try expect(S.finished);
1533}1533}
15341534
1535test "nosuspend on function calls" {1535test "nosuspend on function calls" {
...@@ -1544,8 +1544,8 @@ test "nosuspend on function calls" {...@@ -1544,8 +1544,8 @@ test "nosuspend on function calls" {
1544 return S0{};1544 return S0{};
1545 }1545 }
1546 };1546 };
1547 expectEqual(@as(i32, 42), nosuspend S1.c().b);1547 try expectEqual(@as(i32, 42), nosuspend S1.c().b);
1548 expectEqual(@as(i32, 42), (try nosuspend S1.d()).b);1548 try expectEqual(@as(i32, 42), (try nosuspend S1.d()).b);
1549}1549}
15501550
1551test "nosuspend on async function calls" {1551test "nosuspend on async function calls" {
...@@ -1561,9 +1561,9 @@ test "nosuspend on async function calls" {...@@ -1561,9 +1561,9 @@ test "nosuspend on async function calls" {
1561 }1561 }
1562 };1562 };
1563 var frame_c = nosuspend async S1.c();1563 var frame_c = nosuspend async S1.c();
1564 expectEqual(@as(i32, 42), (await frame_c).b);1564 try expectEqual(@as(i32, 42), (await frame_c).b);
1565 var frame_d = nosuspend async S1.d();1565 var frame_d = nosuspend async S1.d();
1566 expectEqual(@as(i32, 42), (try await frame_d).b);1566 try expectEqual(@as(i32, 42), (try await frame_d).b);
1567}1567}
15681568
1569// test "resume nosuspend async function calls" {1569// test "resume nosuspend async function calls" {
...@@ -1582,10 +1582,10 @@ test "nosuspend on async function calls" {...@@ -1582,10 +1582,10 @@ test "nosuspend on async function calls" {
1582// };1582// };
1583// var frame_c = nosuspend async S1.c();1583// var frame_c = nosuspend async S1.c();
1584// resume frame_c;1584// resume frame_c;
1585// expectEqual(@as(i32, 42), (await frame_c).b);1585// try expectEqual(@as(i32, 42), (await frame_c).b);
1586// var frame_d = nosuspend async S1.d();1586// var frame_d = nosuspend async S1.d();
1587// resume frame_d;1587// resume frame_d;
1588// expectEqual(@as(i32, 42), (try await frame_d).b);1588// try expectEqual(@as(i32, 42), (try await frame_d).b);
1589// }1589// }
15901590
1591test "nosuspend resume async function calls" {1591test "nosuspend resume async function calls" {
...@@ -1604,10 +1604,10 @@ test "nosuspend resume async function calls" {...@@ -1604,10 +1604,10 @@ test "nosuspend resume async function calls" {
1604 };1604 };
1605 var frame_c = async S1.c();1605 var frame_c = async S1.c();
1606 nosuspend resume frame_c;1606 nosuspend resume frame_c;
1607 expectEqual(@as(i32, 42), (await frame_c).b);1607 try expectEqual(@as(i32, 42), (await frame_c).b);
1608 var frame_d = async S1.d();1608 var frame_d = async S1.d();
1609 nosuspend resume frame_d;1609 nosuspend resume frame_d;
1610 expectEqual(@as(i32, 42), (try await frame_d).b);1610 try expectEqual(@as(i32, 42), (try await frame_d).b);
1611}1611}
16121612
1613test "avoid forcing frame alignment resolution implicit cast to *c_void" {1613test "avoid forcing frame alignment resolution implicit cast to *c_void" {
...@@ -1623,7 +1623,7 @@ test "avoid forcing frame alignment resolution implicit cast to *c_void" {...@@ -1623,7 +1623,7 @@ test "avoid forcing frame alignment resolution implicit cast to *c_void" {
1623 };1623 };
1624 var frame = async S.foo();1624 var frame = async S.foo();
1625 resume @ptrCast(anyframe->bool, @alignCast(@alignOf(@Frame(S.foo)), S.x));1625 resume @ptrCast(anyframe->bool, @alignCast(@alignOf(@Frame(S.foo)), S.x));
1626 expect(nosuspend await frame);1626 try expect(nosuspend await frame);
1627}1627}
16281628
1629test "@asyncCall with pass-by-value arguments" {1629test "@asyncCall with pass-by-value arguments" {
...@@ -1638,9 +1638,9 @@ test "@asyncCall with pass-by-value arguments" {...@@ -1638,9 +1638,9 @@ test "@asyncCall with pass-by-value arguments" {
1638 pub fn f(_fill0: u64, s: ST, _fill1: u64, a: AT, _fill2: u64) callconv(.Async) void {1638 pub fn f(_fill0: u64, s: ST, _fill1: u64, a: AT, _fill2: u64) callconv(.Async) void {
1639 // Check that the array and struct arguments passed by value don't1639 // Check that the array and struct arguments passed by value don't
1640 // end up overflowing the adjacent fields in the frame structure.1640 // end up overflowing the adjacent fields in the frame structure.
1641 expectEqual(F0, _fill0);1641 expectEqual(F0, _fill0) catch @panic("test failure");
1642 expectEqual(F1, _fill1);1642 expectEqual(F1, _fill1) catch @panic("test failure");
1643 expectEqual(F2, _fill2);1643 expectEqual(F2, _fill2) catch @panic("test failure");
1644 }1644 }
1645 };1645 };
16461646
...@@ -1664,8 +1664,8 @@ test "@asyncCall with arguments having non-standard alignment" {...@@ -1664,8 +1664,8 @@ test "@asyncCall with arguments having non-standard alignment" {
1664 pub fn f(_fill0: u32, s: struct { x: u64 align(16) }, _fill1: u64) callconv(.Async) void {1664 pub fn f(_fill0: u32, s: struct { x: u64 align(16) }, _fill1: u64) callconv(.Async) void {
1665 // The compiler inserts extra alignment for s, check that the1665 // The compiler inserts extra alignment for s, check that the
1666 // generated code picks the right slot for fill1.1666 // generated code picks the right slot for fill1.
1667 expectEqual(F0, _fill0);1667 expectEqual(F0, _fill0) catch @panic("test failure");
1668 expectEqual(F1, _fill1);1668 expectEqual(F1, _fill1) catch @panic("test failure");
1669 }1669 }
1670 };1670 };
16711671
test/stage1/behavior/atomics.zig+69-69
...@@ -4,25 +4,25 @@ const expectEqual = std.testing.expectEqual;...@@ -4,25 +4,25 @@ const expectEqual = std.testing.expectEqual;
4const builtin = @import("builtin");4const builtin = @import("builtin");
55
6test "cmpxchg" {6test "cmpxchg" {
7 testCmpxchg();7 try testCmpxchg();
8 comptime testCmpxchg();8 comptime try testCmpxchg();
9}9}
1010
11fn testCmpxchg() void {11fn testCmpxchg() !void {
12 var x: i32 = 1234;12 var x: i32 = 1234;
13 if (@cmpxchgWeak(i32, &x, 99, 5678, .SeqCst, .SeqCst)) |x1| {13 if (@cmpxchgWeak(i32, &x, 99, 5678, .SeqCst, .SeqCst)) |x1| {
14 expect(x1 == 1234);14 try expect(x1 == 1234);
15 } else {15 } else {
16 @panic("cmpxchg should have failed");16 @panic("cmpxchg should have failed");
17 }17 }
1818
19 while (@cmpxchgWeak(i32, &x, 1234, 5678, .SeqCst, .SeqCst)) |x1| {19 while (@cmpxchgWeak(i32, &x, 1234, 5678, .SeqCst, .SeqCst)) |x1| {
20 expect(x1 == 1234);20 try expect(x1 == 1234);
21 }21 }
22 expect(x == 5678);22 try expect(x == 5678);
2323
24 expect(@cmpxchgStrong(i32, &x, 5678, 42, .SeqCst, .SeqCst) == null);24 try expect(@cmpxchgStrong(i32, &x, 5678, 42, .SeqCst, .SeqCst) == null);
25 expect(x == 42);25 try expect(x == 42);
26}26}
2727
28test "fence" {28test "fence" {
...@@ -33,25 +33,25 @@ test "fence" {...@@ -33,25 +33,25 @@ test "fence" {
3333
34test "atomicrmw and atomicload" {34test "atomicrmw and atomicload" {
35 var data: u8 = 200;35 var data: u8 = 200;
36 testAtomicRmw(&data);36 try testAtomicRmw(&data);
37 expect(data == 42);37 try expect(data == 42);
38 testAtomicLoad(&data);38 try testAtomicLoad(&data);
39}39}
4040
41fn testAtomicRmw(ptr: *u8) void {41fn testAtomicRmw(ptr: *u8) !void {
42 const prev_value = @atomicRmw(u8, ptr, .Xchg, 42, .SeqCst);42 const prev_value = @atomicRmw(u8, ptr, .Xchg, 42, .SeqCst);
43 expect(prev_value == 200);43 try expect(prev_value == 200);
44 comptime {44 comptime {
45 var x: i32 = 1234;45 var x: i32 = 1234;
46 const y: i32 = 12345;46 const y: i32 = 12345;
47 expect(@atomicLoad(i32, &x, .SeqCst) == 1234);47 try expect(@atomicLoad(i32, &x, .SeqCst) == 1234);
48 expect(@atomicLoad(i32, &y, .SeqCst) == 12345);48 try expect(@atomicLoad(i32, &y, .SeqCst) == 12345);
49 }49 }
50}50}
5151
52fn testAtomicLoad(ptr: *u8) void {52fn testAtomicLoad(ptr: *u8) !void {
53 const x = @atomicLoad(u8, ptr, .SeqCst);53 const x = @atomicLoad(u8, ptr, .SeqCst);
54 expect(x == 42);54 try expect(x == 42);
55}55}
5656
57test "cmpxchg with ptr" {57test "cmpxchg with ptr" {
...@@ -60,18 +60,18 @@ test "cmpxchg with ptr" {...@@ -60,18 +60,18 @@ test "cmpxchg with ptr" {
60 var data3: i32 = 9101;60 var data3: i32 = 9101;
61 var x: *i32 = &data1;61 var x: *i32 = &data1;
62 if (@cmpxchgWeak(*i32, &x, &data2, &data3, .SeqCst, .SeqCst)) |x1| {62 if (@cmpxchgWeak(*i32, &x, &data2, &data3, .SeqCst, .SeqCst)) |x1| {
63 expect(x1 == &data1);63 try expect(x1 == &data1);
64 } else {64 } else {
65 @panic("cmpxchg should have failed");65 @panic("cmpxchg should have failed");
66 }66 }
6767
68 while (@cmpxchgWeak(*i32, &x, &data1, &data3, .SeqCst, .SeqCst)) |x1| {68 while (@cmpxchgWeak(*i32, &x, &data1, &data3, .SeqCst, .SeqCst)) |x1| {
69 expect(x1 == &data1);69 try expect(x1 == &data1);
70 }70 }
71 expect(x == &data3);71 try expect(x == &data3);
7272
73 expect(@cmpxchgStrong(*i32, &x, &data3, &data2, .SeqCst, .SeqCst) == null);73 try expect(@cmpxchgStrong(*i32, &x, &data3, &data2, .SeqCst, .SeqCst) == null);
74 expect(x == &data2);74 try expect(x == &data2);
75}75}
7676
77// TODO this test is disabled until this issue is resolved:77// TODO this test is disabled until this issue is resolved:
...@@ -81,18 +81,18 @@ test "cmpxchg with ptr" {...@@ -81,18 +81,18 @@ test "cmpxchg with ptr" {
81//test "128-bit cmpxchg" {81//test "128-bit cmpxchg" {
82// var x: u128 align(16) = 1234; // TODO: https://github.com/ziglang/zig/issues/298782// var x: u128 align(16) = 1234; // TODO: https://github.com/ziglang/zig/issues/2987
83// if (@cmpxchgWeak(u128, &x, 99, 5678, .SeqCst, .SeqCst)) |x1| {83// if (@cmpxchgWeak(u128, &x, 99, 5678, .SeqCst, .SeqCst)) |x1| {
84// expect(x1 == 1234);84// try expect(x1 == 1234);
85// } else {85// } else {
86// @panic("cmpxchg should have failed");86// @panic("cmpxchg should have failed");
87// }87// }
88//88//
89// while (@cmpxchgWeak(u128, &x, 1234, 5678, .SeqCst, .SeqCst)) |x1| {89// while (@cmpxchgWeak(u128, &x, 1234, 5678, .SeqCst, .SeqCst)) |x1| {
90// expect(x1 == 1234);90// try expect(x1 == 1234);
91// }91// }
92// expect(x == 5678);92// try expect(x == 5678);
93//93//
94// expect(@cmpxchgStrong(u128, &x, 5678, 42, .SeqCst, .SeqCst) == null);94// try expect(@cmpxchgStrong(u128, &x, 5678, 42, .SeqCst, .SeqCst) == null);
95// expect(x == 42);95// try expect(x == 42);
96//}96//}
9797
98test "cmpxchg with ignored result" {98test "cmpxchg with ignored result" {
...@@ -101,14 +101,14 @@ test "cmpxchg with ignored result" {...@@ -101,14 +101,14 @@ test "cmpxchg with ignored result" {
101101
102 _ = @cmpxchgStrong(i32, &x, 1234, 5678, .Monotonic, .Monotonic);102 _ = @cmpxchgStrong(i32, &x, 1234, 5678, .Monotonic, .Monotonic);
103103
104 expectEqual(@as(i32, 5678), x);104 try expectEqual(@as(i32, 5678), x);
105}105}
106106
107var a_global_variable = @as(u32, 1234);107var a_global_variable = @as(u32, 1234);
108108
109test "cmpxchg on a global variable" {109test "cmpxchg on a global variable" {
110 _ = @cmpxchgWeak(u32, &a_global_variable, 1234, 42, .Acquire, .Monotonic);110 _ = @cmpxchgWeak(u32, &a_global_variable, 1234, 42, .Acquire, .Monotonic);
111 expectEqual(@as(u32, 42), a_global_variable);111 try expectEqual(@as(u32, 42), a_global_variable);
112}112}
113113
114test "atomic load and rmw with enum" {114test "atomic load and rmw with enum" {
...@@ -119,33 +119,33 @@ test "atomic load and rmw with enum" {...@@ -119,33 +119,33 @@ test "atomic load and rmw with enum" {
119 };119 };
120 var x = Value.a;120 var x = Value.a;
121121
122 expect(@atomicLoad(Value, &x, .SeqCst) != .b);122 try expect(@atomicLoad(Value, &x, .SeqCst) != .b);
123123
124 _ = @atomicRmw(Value, &x, .Xchg, .c, .SeqCst);124 _ = @atomicRmw(Value, &x, .Xchg, .c, .SeqCst);
125 expect(@atomicLoad(Value, &x, .SeqCst) == .c);125 try expect(@atomicLoad(Value, &x, .SeqCst) == .c);
126 expect(@atomicLoad(Value, &x, .SeqCst) != .a);126 try expect(@atomicLoad(Value, &x, .SeqCst) != .a);
127 expect(@atomicLoad(Value, &x, .SeqCst) != .b);127 try expect(@atomicLoad(Value, &x, .SeqCst) != .b);
128}128}
129129
130test "atomic store" {130test "atomic store" {
131 var x: u32 = 0;131 var x: u32 = 0;
132 @atomicStore(u32, &x, 1, .SeqCst);132 @atomicStore(u32, &x, 1, .SeqCst);
133 expect(@atomicLoad(u32, &x, .SeqCst) == 1);133 try expect(@atomicLoad(u32, &x, .SeqCst) == 1);
134 @atomicStore(u32, &x, 12345678, .SeqCst);134 @atomicStore(u32, &x, 12345678, .SeqCst);
135 expect(@atomicLoad(u32, &x, .SeqCst) == 12345678);135 try expect(@atomicLoad(u32, &x, .SeqCst) == 12345678);
136}136}
137137
138test "atomic store comptime" {138test "atomic store comptime" {
139 comptime testAtomicStore();139 comptime try testAtomicStore();
140 testAtomicStore();140 try testAtomicStore();
141}141}
142142
143fn testAtomicStore() void {143fn testAtomicStore() !void {
144 var x: u32 = 0;144 var x: u32 = 0;
145 @atomicStore(u32, &x, 1, .SeqCst);145 @atomicStore(u32, &x, 1, .SeqCst);
146 expect(@atomicLoad(u32, &x, .SeqCst) == 1);146 try expect(@atomicLoad(u32, &x, .SeqCst) == 1);
147 @atomicStore(u32, &x, 12345678, .SeqCst);147 @atomicStore(u32, &x, 12345678, .SeqCst);
148 expect(@atomicLoad(u32, &x, .SeqCst) == 12345678);148 try expect(@atomicLoad(u32, &x, .SeqCst) == 12345678);
149}149}
150150
151test "atomicrmw with floats" {151test "atomicrmw with floats" {
...@@ -154,66 +154,66 @@ test "atomicrmw with floats" {...@@ -154,66 +154,66 @@ test "atomicrmw with floats" {
154 .aarch64, .arm, .thumb, .riscv64 => return error.SkipZigTest,154 .aarch64, .arm, .thumb, .riscv64 => return error.SkipZigTest,
155 else => {},155 else => {},
156 }156 }
157 testAtomicRmwFloat();157 try testAtomicRmwFloat();
158 comptime testAtomicRmwFloat();158 comptime try testAtomicRmwFloat();
159}159}
160160
161fn testAtomicRmwFloat() void {161fn testAtomicRmwFloat() !void {
162 var x: f32 = 0;162 var x: f32 = 0;
163 expect(x == 0);163 try expect(x == 0);
164 _ = @atomicRmw(f32, &x, .Xchg, 1, .SeqCst);164 _ = @atomicRmw(f32, &x, .Xchg, 1, .SeqCst);
165 expect(x == 1);165 try expect(x == 1);
166 _ = @atomicRmw(f32, &x, .Add, 5, .SeqCst);166 _ = @atomicRmw(f32, &x, .Add, 5, .SeqCst);
167 expect(x == 6);167 try expect(x == 6);
168 _ = @atomicRmw(f32, &x, .Sub, 2, .SeqCst);168 _ = @atomicRmw(f32, &x, .Sub, 2, .SeqCst);
169 expect(x == 4);169 try expect(x == 4);
170}170}
171171
172test "atomicrmw with ints" {172test "atomicrmw with ints" {
173 testAtomicRmwInt();173 try testAtomicRmwInt();
174 comptime testAtomicRmwInt();174 comptime try testAtomicRmwInt();
175}175}
176176
177fn testAtomicRmwInt() void {177fn testAtomicRmwInt() !void {
178 var x: u8 = 1;178 var x: u8 = 1;
179 var res = @atomicRmw(u8, &x, .Xchg, 3, .SeqCst);179 var res = @atomicRmw(u8, &x, .Xchg, 3, .SeqCst);
180 expect(x == 3 and res == 1);180 try expect(x == 3 and res == 1);
181 _ = @atomicRmw(u8, &x, .Add, 3, .SeqCst);181 _ = @atomicRmw(u8, &x, .Add, 3, .SeqCst);
182 expect(x == 6);182 try expect(x == 6);
183 _ = @atomicRmw(u8, &x, .Sub, 1, .SeqCst);183 _ = @atomicRmw(u8, &x, .Sub, 1, .SeqCst);
184 expect(x == 5);184 try expect(x == 5);
185 _ = @atomicRmw(u8, &x, .And, 4, .SeqCst);185 _ = @atomicRmw(u8, &x, .And, 4, .SeqCst);
186 expect(x == 4);186 try expect(x == 4);
187 _ = @atomicRmw(u8, &x, .Nand, 4, .SeqCst);187 _ = @atomicRmw(u8, &x, .Nand, 4, .SeqCst);
188 expect(x == 0xfb);188 try expect(x == 0xfb);
189 _ = @atomicRmw(u8, &x, .Or, 6, .SeqCst);189 _ = @atomicRmw(u8, &x, .Or, 6, .SeqCst);
190 expect(x == 0xff);190 try expect(x == 0xff);
191 _ = @atomicRmw(u8, &x, .Xor, 2, .SeqCst);191 _ = @atomicRmw(u8, &x, .Xor, 2, .SeqCst);
192 expect(x == 0xfd);192 try expect(x == 0xfd);
193193
194 _ = @atomicRmw(u8, &x, .Max, 1, .SeqCst);194 _ = @atomicRmw(u8, &x, .Max, 1, .SeqCst);
195 expect(x == 0xfd);195 try expect(x == 0xfd);
196 _ = @atomicRmw(u8, &x, .Min, 1, .SeqCst);196 _ = @atomicRmw(u8, &x, .Min, 1, .SeqCst);
197 expect(x == 1);197 try expect(x == 1);
198}198}
199199
200test "atomics with different types" {200test "atomics with different types" {
201 testAtomicsWithType(bool, true, false);201 try testAtomicsWithType(bool, true, false);
202 inline for (.{ u1, i5, u15 }) |T| {202 inline for (.{ u1, i5, u15 }) |T| {
203 var x: T = 0;203 var x: T = 0;
204 testAtomicsWithType(T, 0, 1);204 try testAtomicsWithType(T, 0, 1);
205 }205 }
206 testAtomicsWithType(u0, 0, 0);206 try testAtomicsWithType(u0, 0, 0);
207 testAtomicsWithType(i0, 0, 0);207 try testAtomicsWithType(i0, 0, 0);
208}208}
209209
210fn testAtomicsWithType(comptime T: type, a: T, b: T) void {210fn testAtomicsWithType(comptime T: type, a: T, b: T) !void {
211 var x: T = b;211 var x: T = b;
212 @atomicStore(T, &x, a, .SeqCst);212 @atomicStore(T, &x, a, .SeqCst);
213 expect(x == a);213 try expect(x == a);
214 expect(@atomicLoad(T, &x, .SeqCst) == a);214 try expect(@atomicLoad(T, &x, .SeqCst) == a);
215 expect(@atomicRmw(T, &x, .Xchg, b, .SeqCst) == a);215 try expect(@atomicRmw(T, &x, .Xchg, b, .SeqCst) == a);
216 expect(@cmpxchgStrong(T, &x, b, a, .SeqCst, .SeqCst) == null);216 try expect(@cmpxchgStrong(T, &x, b, a, .SeqCst, .SeqCst) == null);
217 if (@sizeOf(T) != 0)217 if (@sizeOf(T) != 0)
218 expect(@cmpxchgStrong(T, &x, b, a, .SeqCst, .SeqCst).? == a);218 try expect(@cmpxchgStrong(T, &x, b, a, .SeqCst, .SeqCst).? == a);
219}219}
test/stage1/behavior/await_struct.zig+2-2
...@@ -15,8 +15,8 @@ test "coroutine await struct" {...@@ -15,8 +15,8 @@ test "coroutine await struct" {
15 await_seq('f');15 await_seq('f');
16 resume await_a_promise;16 resume await_a_promise;
17 await_seq('i');17 await_seq('i');
18 expect(await_final_result.x == 1234);18 try expect(await_final_result.x == 1234);
19 expect(std.mem.eql(u8, &await_points, "abcdefghi"));19 try expect(std.mem.eql(u8, &await_points, "abcdefghi"));
20}20}
21fn await_amain() callconv(.Async) void {21fn await_amain() callconv(.Async) void {
22 await_seq('b');22 await_seq('b');
test/stage1/behavior/bit_shifting.zig+14-14
...@@ -3,8 +3,8 @@ const expect = std.testing.expect;...@@ -3,8 +3,8 @@ const expect = std.testing.expect;
33
4fn ShardedTable(comptime Key: type, comptime mask_bit_count: comptime_int, comptime V: type) type {4fn ShardedTable(comptime Key: type, comptime mask_bit_count: comptime_int, comptime V: type) type {
5 const key_bits = @typeInfo(Key).Int.bits;5 const key_bits = @typeInfo(Key).Int.bits;
6 expect(Key == std.meta.Int(.unsigned, key_bits));6 std.debug.assert(Key == std.meta.Int(.unsigned, key_bits));
7 expect(key_bits >= mask_bit_count);7 std.debug.assert(key_bits >= mask_bit_count);
8 const shard_key_bits = mask_bit_count;8 const shard_key_bits = mask_bit_count;
9 const ShardKey = std.meta.Int(.unsigned, mask_bit_count);9 const ShardKey = std.meta.Int(.unsigned, mask_bit_count);
10 const shift_amount = key_bits - shard_key_bits;10 const shift_amount = key_bits - shard_key_bits;
...@@ -61,31 +61,31 @@ fn ShardedTable(comptime Key: type, comptime mask_bit_count: comptime_int, compt...@@ -61,31 +61,31 @@ fn ShardedTable(comptime Key: type, comptime mask_bit_count: comptime_int, compt
6161
62test "sharded table" {62test "sharded table" {
63 // realistic 16-way sharding63 // realistic 16-way sharding
64 testShardedTable(u32, 4, 8);64 try testShardedTable(u32, 4, 8);
6565
66 testShardedTable(u5, 0, 32); // ShardKey == u066 try testShardedTable(u5, 0, 32); // ShardKey == u0
67 testShardedTable(u5, 2, 32);67 try testShardedTable(u5, 2, 32);
68 testShardedTable(u5, 5, 32);68 try testShardedTable(u5, 5, 32);
6969
70 testShardedTable(u1, 0, 2);70 try testShardedTable(u1, 0, 2);
71 testShardedTable(u1, 1, 2); // this does u1 >> u071 try testShardedTable(u1, 1, 2); // this does u1 >> u0
7272
73 testShardedTable(u0, 0, 1);73 try testShardedTable(u0, 0, 1);
74}74}
75fn testShardedTable(comptime Key: type, comptime mask_bit_count: comptime_int, comptime node_count: comptime_int) void {75fn testShardedTable(comptime Key: type, comptime mask_bit_count: comptime_int, comptime node_count: comptime_int) !void {
76 const Table = ShardedTable(Key, mask_bit_count, void);76 const Table = ShardedTable(Key, mask_bit_count, void);
7777
78 var table = Table.create();78 var table = Table.create();
79 var node_buffer: [node_count]Table.Node = undefined;79 var node_buffer: [node_count]Table.Node = undefined;
80 for (node_buffer) |*node, i| {80 for (node_buffer) |*node, i| {
81 const key = @intCast(Key, i);81 const key = @intCast(Key, i);
82 expect(table.get(key) == null);82 try expect(table.get(key) == null);
83 node.init(key, {});83 node.init(key, {});
84 table.put(node);84 table.put(node);
85 }85 }
8686
87 for (node_buffer) |*node, i| {87 for (node_buffer) |*node, i| {
88 expect(table.get(@intCast(Key, i)) == node);88 try expect(table.get(@intCast(Key, i)) == node);
89 }89 }
90}90}
9191
...@@ -93,9 +93,9 @@ fn testShardedTable(comptime Key: type, comptime mask_bit_count: comptime_int, c...@@ -93,9 +93,9 @@ fn testShardedTable(comptime Key: type, comptime mask_bit_count: comptime_int, c
93test "comptime shr of BigInt" {93test "comptime shr of BigInt" {
94 comptime {94 comptime {
95 var n0 = 0xdeadbeef0000000000000000;95 var n0 = 0xdeadbeef0000000000000000;
96 std.debug.assert(n0 >> 64 == 0xdeadbeef);96 try expect(n0 >> 64 == 0xdeadbeef);
97 var n1 = 17908056155735594659;97 var n1 = 17908056155735594659;
98 std.debug.assert(n1 >> 64 == 0);98 try expect(n1 >> 64 == 0);
99 }99 }
100}100}
101101
test/stage1/behavior/bitcast.zig+52-52
...@@ -5,13 +5,13 @@ const expectEqual = std.testing.expectEqual;...@@ -5,13 +5,13 @@ const expectEqual = std.testing.expectEqual;
5const maxInt = std.math.maxInt;5const maxInt = std.math.maxInt;
66
7test "@bitCast i32 -> u32" {7test "@bitCast i32 -> u32" {
8 testBitCast_i32_u32();8 try testBitCast_i32_u32();
9 comptime testBitCast_i32_u32();9 comptime try testBitCast_i32_u32();
10}10}
1111
12fn testBitCast_i32_u32() void {12fn testBitCast_i32_u32() !void {
13 expect(conv(-1) == maxInt(u32));13 try expect(conv(-1) == maxInt(u32));
14 expect(conv2(maxInt(u32)) == -1);14 try expect(conv2(maxInt(u32)) == -1);
15}15}
1616
17fn conv(x: i32) u32 {17fn conv(x: i32) u32 {
...@@ -26,15 +26,15 @@ test "@bitCast extern enum to its integer type" {...@@ -26,15 +26,15 @@ test "@bitCast extern enum to its integer type" {
26 A,26 A,
27 B,27 B,
2828
29 fn testBitCastExternEnum() void {29 fn testBitCastExternEnum() !void {
30 var SOCK_DGRAM = @This().B;30 var SOCK_DGRAM = @This().B;
31 var sock_dgram = @bitCast(c_int, SOCK_DGRAM);31 var sock_dgram = @bitCast(c_int, SOCK_DGRAM);
32 expect(sock_dgram == 1);32 try expect(sock_dgram == 1);
33 }33 }
34 };34 };
3535
36 SOCK.testBitCastExternEnum();36 try SOCK.testBitCastExternEnum();
37 comptime SOCK.testBitCastExternEnum();37 comptime try SOCK.testBitCastExternEnum();
38}38}
3939
40test "@bitCast packed structs at runtime and comptime" {40test "@bitCast packed structs at runtime and comptime" {
...@@ -47,25 +47,25 @@ test "@bitCast packed structs at runtime and comptime" {...@@ -47,25 +47,25 @@ test "@bitCast packed structs at runtime and comptime" {
47 quarter4: u4,47 quarter4: u4,
48 };48 };
49 const S = struct {49 const S = struct {
50 fn doTheTest() void {50 fn doTheTest() !void {
51 var full = Full{ .number = 0x1234 };51 var full = Full{ .number = 0x1234 };
52 var two_halves = @bitCast(Divided, full);52 var two_halves = @bitCast(Divided, full);
53 switch (builtin.endian) {53 switch (builtin.endian) {
54 builtin.Endian.Big => {54 builtin.Endian.Big => {
55 expect(two_halves.half1 == 0x12);55 try expect(two_halves.half1 == 0x12);
56 expect(two_halves.quarter3 == 0x3);56 try expect(two_halves.quarter3 == 0x3);
57 expect(two_halves.quarter4 == 0x4);57 try expect(two_halves.quarter4 == 0x4);
58 },58 },
59 builtin.Endian.Little => {59 builtin.Endian.Little => {
60 expect(two_halves.half1 == 0x34);60 try expect(two_halves.half1 == 0x34);
61 expect(two_halves.quarter3 == 0x2);61 try expect(two_halves.quarter3 == 0x2);
62 expect(two_halves.quarter4 == 0x1);62 try expect(two_halves.quarter4 == 0x1);
63 },63 },
64 }64 }
65 }65 }
66 };66 };
67 S.doTheTest();67 try S.doTheTest();
68 comptime S.doTheTest();68 comptime try S.doTheTest();
69}69}
7070
71test "@bitCast extern structs at runtime and comptime" {71test "@bitCast extern structs at runtime and comptime" {
...@@ -77,23 +77,23 @@ test "@bitCast extern structs at runtime and comptime" {...@@ -77,23 +77,23 @@ test "@bitCast extern structs at runtime and comptime" {
77 half2: u8,77 half2: u8,
78 };78 };
79 const S = struct {79 const S = struct {
80 fn doTheTest() void {80 fn doTheTest() !void {
81 var full = Full{ .number = 0x1234 };81 var full = Full{ .number = 0x1234 };
82 var two_halves = @bitCast(TwoHalves, full);82 var two_halves = @bitCast(TwoHalves, full);
83 switch (builtin.endian) {83 switch (builtin.endian) {
84 builtin.Endian.Big => {84 builtin.Endian.Big => {
85 expect(two_halves.half1 == 0x12);85 try expect(two_halves.half1 == 0x12);
86 expect(two_halves.half2 == 0x34);86 try expect(two_halves.half2 == 0x34);
87 },87 },
88 builtin.Endian.Little => {88 builtin.Endian.Little => {
89 expect(two_halves.half1 == 0x34);89 try expect(two_halves.half1 == 0x34);
90 expect(two_halves.half2 == 0x12);90 try expect(two_halves.half2 == 0x12);
91 },91 },
92 }92 }
93 }93 }
94 };94 };
95 S.doTheTest();95 try S.doTheTest();
96 comptime S.doTheTest();96 comptime try S.doTheTest();
97}97}
9898
99test "bitcast packed struct to integer and back" {99test "bitcast packed struct to integer and back" {
...@@ -102,35 +102,35 @@ test "bitcast packed struct to integer and back" {...@@ -102,35 +102,35 @@ test "bitcast packed struct to integer and back" {
102 level: u7,102 level: u7,
103 };103 };
104 const S = struct {104 const S = struct {
105 fn doTheTest() void {105 fn doTheTest() !void {
106 var move = LevelUpMove{ .move_id = 1, .level = 2 };106 var move = LevelUpMove{ .move_id = 1, .level = 2 };
107 var v = @bitCast(u16, move);107 var v = @bitCast(u16, move);
108 var back_to_a_move = @bitCast(LevelUpMove, v);108 var back_to_a_move = @bitCast(LevelUpMove, v);
109 expect(back_to_a_move.move_id == 1);109 try expect(back_to_a_move.move_id == 1);
110 expect(back_to_a_move.level == 2);110 try expect(back_to_a_move.level == 2);
111 }111 }
112 };112 };
113 S.doTheTest();113 try S.doTheTest();
114 comptime S.doTheTest();114 comptime try S.doTheTest();
115}115}
116116
117test "implicit cast to error union by returning" {117test "implicit cast to error union by returning" {
118 const S = struct {118 const S = struct {
119 fn entry() void {119 fn entry() !void {
120 expect((func(-1) catch unreachable) == maxInt(u64));120 try expect((func(-1) catch unreachable) == maxInt(u64));
121 }121 }
122 pub fn func(sz: i64) anyerror!u64 {122 pub fn func(sz: i64) anyerror!u64 {
123 return @bitCast(u64, sz);123 return @bitCast(u64, sz);
124 }124 }
125 };125 };
126 S.entry();126 try S.entry();
127 comptime S.entry();127 comptime try S.entry();
128}128}
129129
130// issue #3010: compiler segfault130// issue #3010: compiler segfault
131test "bitcast literal [4]u8 param to u32" {131test "bitcast literal [4]u8 param to u32" {
132 const ip = @bitCast(u32, [_]u8{ 255, 255, 255, 255 });132 const ip = @bitCast(u32, [_]u8{ 255, 255, 255, 255 });
133 expect(ip == maxInt(u32));133 try expect(ip == maxInt(u32));
134}134}
135135
136test "bitcast packed struct literal to byte" {136test "bitcast packed struct literal to byte" {
...@@ -138,14 +138,14 @@ test "bitcast packed struct literal to byte" {...@@ -138,14 +138,14 @@ test "bitcast packed struct literal to byte" {
138 value: u8,138 value: u8,
139 };139 };
140 const casted = @bitCast(u8, Foo{ .value = 0xF });140 const casted = @bitCast(u8, Foo{ .value = 0xF });
141 expect(casted == 0xf);141 try expect(casted == 0xf);
142}142}
143143
144test "comptime bitcast used in expression has the correct type" {144test "comptime bitcast used in expression has the correct type" {
145 const Foo = packed struct {145 const Foo = packed struct {
146 value: u8,146 value: u8,
147 };147 };
148 expect(@bitCast(u8, Foo{ .value = 0xF }) == 0xf);148 try expect(@bitCast(u8, Foo{ .value = 0xF }) == 0xf);
149}149}
150150
151test "bitcast result to _" {151test "bitcast result to _" {
...@@ -154,43 +154,43 @@ test "bitcast result to _" {...@@ -154,43 +154,43 @@ test "bitcast result to _" {
154154
155test "nested bitcast" {155test "nested bitcast" {
156 const S = struct {156 const S = struct {
157 fn moo(x: isize) void {157 fn moo(x: isize) !void {
158 @import("std").testing.expectEqual(@intCast(isize, 42), x);158 try @import("std").testing.expectEqual(@intCast(isize, 42), x);
159 }159 }
160160
161 fn foo(x: isize) void {161 fn foo(x: isize) !void {
162 @This().moo(162 try @This().moo(
163 @bitCast(isize, if (x != 0) @bitCast(usize, x) else @bitCast(usize, x)),163 @bitCast(isize, if (x != 0) @bitCast(usize, x) else @bitCast(usize, x)),
164 );164 );
165 }165 }
166 };166 };
167167
168 S.foo(42);168 try S.foo(42);
169 comptime S.foo(42);169 comptime try S.foo(42);
170}170}
171171
172test "bitcast passed as tuple element" {172test "bitcast passed as tuple element" {
173 const S = struct {173 const S = struct {
174 fn foo(args: anytype) void {174 fn foo(args: anytype) !void {
175 comptime expect(@TypeOf(args[0]) == f32);175 comptime try expect(@TypeOf(args[0]) == f32);
176 expect(args[0] == 12.34);176 try expect(args[0] == 12.34);
177 }177 }
178 };178 };
179 S.foo(.{@bitCast(f32, @as(u32, 0x414570A4))});179 try S.foo(.{@bitCast(f32, @as(u32, 0x414570A4))});
180}180}
181181
182test "triple level result location with bitcast sandwich passed as tuple element" {182test "triple level result location with bitcast sandwich passed as tuple element" {
183 const S = struct {183 const S = struct {
184 fn foo(args: anytype) void {184 fn foo(args: anytype) !void {
185 comptime expect(@TypeOf(args[0]) == f64);185 comptime try expect(@TypeOf(args[0]) == f64);
186 expect(args[0] > 12.33 and args[0] < 12.35);186 try expect(args[0] > 12.33 and args[0] < 12.35);
187 }187 }
188 };188 };
189 S.foo(.{@as(f64, @bitCast(f32, @as(u32, 0x414570A4)))});189 try S.foo(.{@as(f64, @bitCast(f32, @as(u32, 0x414570A4)))});
190}190}
191191
192test "bitcast generates a temporary value" {192test "bitcast generates a temporary value" {
193 var y = @as(u16, 0x55AA);193 var y = @as(u16, 0x55AA);
194 const x = @bitCast(u16, @bitCast([2]u8, y));194 const x = @bitCast(u16, @bitCast([2]u8, y));
195 expectEqual(y, x);195 try expectEqual(y, x);
196}196}
test/stage1/behavior/bitreverse.zig+39-39
...@@ -3,67 +3,67 @@ const expect = std.testing.expect;...@@ -3,67 +3,67 @@ const expect = std.testing.expect;
3const minInt = std.math.minInt;3const minInt = std.math.minInt;
44
5test "@bitReverse" {5test "@bitReverse" {
6 comptime testBitReverse();6 comptime try testBitReverse();
7 testBitReverse();7 try testBitReverse();
8}8}
99
10fn testBitReverse() void {10fn testBitReverse() !void {
11 // using comptime_ints, unsigned11 // using comptime_ints, unsigned
12 expect(@bitReverse(u0, 0) == 0);12 try expect(@bitReverse(u0, 0) == 0);
13 expect(@bitReverse(u5, 0x12) == 0x9);13 try expect(@bitReverse(u5, 0x12) == 0x9);
14 expect(@bitReverse(u8, 0x12) == 0x48);14 try expect(@bitReverse(u8, 0x12) == 0x48);
15 expect(@bitReverse(u16, 0x1234) == 0x2c48);15 try expect(@bitReverse(u16, 0x1234) == 0x2c48);
16 expect(@bitReverse(u24, 0x123456) == 0x6a2c48);16 try expect(@bitReverse(u24, 0x123456) == 0x6a2c48);
17 expect(@bitReverse(u32, 0x12345678) == 0x1e6a2c48);17 try expect(@bitReverse(u32, 0x12345678) == 0x1e6a2c48);
18 expect(@bitReverse(u40, 0x123456789a) == 0x591e6a2c48);18 try expect(@bitReverse(u40, 0x123456789a) == 0x591e6a2c48);
19 expect(@bitReverse(u48, 0x123456789abc) == 0x3d591e6a2c48);19 try expect(@bitReverse(u48, 0x123456789abc) == 0x3d591e6a2c48);
20 expect(@bitReverse(u56, 0x123456789abcde) == 0x7b3d591e6a2c48);20 try expect(@bitReverse(u56, 0x123456789abcde) == 0x7b3d591e6a2c48);
21 expect(@bitReverse(u64, 0x123456789abcdef1) == 0x8f7b3d591e6a2c48);21 try expect(@bitReverse(u64, 0x123456789abcdef1) == 0x8f7b3d591e6a2c48);
22 expect(@bitReverse(u128, 0x123456789abcdef11121314151617181) == 0x818e868a828c84888f7b3d591e6a2c48);22 try expect(@bitReverse(u128, 0x123456789abcdef11121314151617181) == 0x818e868a828c84888f7b3d591e6a2c48);
2323
24 // using runtime uints, unsigned24 // using runtime uints, unsigned
25 var num0: u0 = 0;25 var num0: u0 = 0;
26 expect(@bitReverse(u0, num0) == 0);26 try expect(@bitReverse(u0, num0) == 0);
27 var num5: u5 = 0x12;27 var num5: u5 = 0x12;
28 expect(@bitReverse(u5, num5) == 0x9);28 try expect(@bitReverse(u5, num5) == 0x9);
29 var num8: u8 = 0x12;29 var num8: u8 = 0x12;
30 expect(@bitReverse(u8, num8) == 0x48);30 try expect(@bitReverse(u8, num8) == 0x48);
31 var num16: u16 = 0x1234;31 var num16: u16 = 0x1234;
32 expect(@bitReverse(u16, num16) == 0x2c48);32 try expect(@bitReverse(u16, num16) == 0x2c48);
33 var num24: u24 = 0x123456;33 var num24: u24 = 0x123456;
34 expect(@bitReverse(u24, num24) == 0x6a2c48);34 try expect(@bitReverse(u24, num24) == 0x6a2c48);
35 var num32: u32 = 0x12345678;35 var num32: u32 = 0x12345678;
36 expect(@bitReverse(u32, num32) == 0x1e6a2c48);36 try expect(@bitReverse(u32, num32) == 0x1e6a2c48);
37 var num40: u40 = 0x123456789a;37 var num40: u40 = 0x123456789a;
38 expect(@bitReverse(u40, num40) == 0x591e6a2c48);38 try expect(@bitReverse(u40, num40) == 0x591e6a2c48);
39 var num48: u48 = 0x123456789abc;39 var num48: u48 = 0x123456789abc;
40 expect(@bitReverse(u48, num48) == 0x3d591e6a2c48);40 try expect(@bitReverse(u48, num48) == 0x3d591e6a2c48);
41 var num56: u56 = 0x123456789abcde;41 var num56: u56 = 0x123456789abcde;
42 expect(@bitReverse(u56, num56) == 0x7b3d591e6a2c48);42 try expect(@bitReverse(u56, num56) == 0x7b3d591e6a2c48);
43 var num64: u64 = 0x123456789abcdef1;43 var num64: u64 = 0x123456789abcdef1;
44 expect(@bitReverse(u64, num64) == 0x8f7b3d591e6a2c48);44 try expect(@bitReverse(u64, num64) == 0x8f7b3d591e6a2c48);
45 var num128: u128 = 0x123456789abcdef11121314151617181;45 var num128: u128 = 0x123456789abcdef11121314151617181;
46 expect(@bitReverse(u128, num128) == 0x818e868a828c84888f7b3d591e6a2c48);46 try expect(@bitReverse(u128, num128) == 0x818e868a828c84888f7b3d591e6a2c48);
4747
48 // using comptime_ints, signed, positive48 // using comptime_ints, signed, positive
49 expect(@bitReverse(u8, @as(u8, 0)) == 0);49 try expect(@bitReverse(u8, @as(u8, 0)) == 0);
50 expect(@bitReverse(i8, @bitCast(i8, @as(u8, 0x92))) == @bitCast(i8, @as(u8, 0x49)));50 try expect(@bitReverse(i8, @bitCast(i8, @as(u8, 0x92))) == @bitCast(i8, @as(u8, 0x49)));
51 expect(@bitReverse(i16, @bitCast(i16, @as(u16, 0x1234))) == @bitCast(i16, @as(u16, 0x2c48)));51 try expect(@bitReverse(i16, @bitCast(i16, @as(u16, 0x1234))) == @bitCast(i16, @as(u16, 0x2c48)));
52 expect(@bitReverse(i24, @bitCast(i24, @as(u24, 0x123456))) == @bitCast(i24, @as(u24, 0x6a2c48)));52 try expect(@bitReverse(i24, @bitCast(i24, @as(u24, 0x123456))) == @bitCast(i24, @as(u24, 0x6a2c48)));
53 expect(@bitReverse(i32, @bitCast(i32, @as(u32, 0x12345678))) == @bitCast(i32, @as(u32, 0x1e6a2c48)));53 try expect(@bitReverse(i32, @bitCast(i32, @as(u32, 0x12345678))) == @bitCast(i32, @as(u32, 0x1e6a2c48)));
54 expect(@bitReverse(i40, @bitCast(i40, @as(u40, 0x123456789a))) == @bitCast(i40, @as(u40, 0x591e6a2c48)));54 try expect(@bitReverse(i40, @bitCast(i40, @as(u40, 0x123456789a))) == @bitCast(i40, @as(u40, 0x591e6a2c48)));
55 expect(@bitReverse(i48, @bitCast(i48, @as(u48, 0x123456789abc))) == @bitCast(i48, @as(u48, 0x3d591e6a2c48)));55 try expect(@bitReverse(i48, @bitCast(i48, @as(u48, 0x123456789abc))) == @bitCast(i48, @as(u48, 0x3d591e6a2c48)));
56 expect(@bitReverse(i56, @bitCast(i56, @as(u56, 0x123456789abcde))) == @bitCast(i56, @as(u56, 0x7b3d591e6a2c48)));56 try expect(@bitReverse(i56, @bitCast(i56, @as(u56, 0x123456789abcde))) == @bitCast(i56, @as(u56, 0x7b3d591e6a2c48)));
57 expect(@bitReverse(i64, @bitCast(i64, @as(u64, 0x123456789abcdef1))) == @bitCast(i64, @as(u64, 0x8f7b3d591e6a2c48)));57 try expect(@bitReverse(i64, @bitCast(i64, @as(u64, 0x123456789abcdef1))) == @bitCast(i64, @as(u64, 0x8f7b3d591e6a2c48)));
58 expect(@bitReverse(i128, @bitCast(i128, @as(u128, 0x123456789abcdef11121314151617181))) == @bitCast(i128, @as(u128, 0x818e868a828c84888f7b3d591e6a2c48)));58 try expect(@bitReverse(i128, @bitCast(i128, @as(u128, 0x123456789abcdef11121314151617181))) == @bitCast(i128, @as(u128, 0x818e868a828c84888f7b3d591e6a2c48)));
5959
60 // using signed, negative. Compare to runtime ints returned from llvm.60 // using signed, negative. Compare to runtime ints returned from llvm.
61 var neg8: i8 = -18;61 var neg8: i8 = -18;
62 expect(@bitReverse(i8, @as(i8, -18)) == @bitReverse(i8, neg8));62 try expect(@bitReverse(i8, @as(i8, -18)) == @bitReverse(i8, neg8));
63 var neg16: i16 = -32694;63 var neg16: i16 = -32694;
64 expect(@bitReverse(i16, @as(i16, -32694)) == @bitReverse(i16, neg16));64 try expect(@bitReverse(i16, @as(i16, -32694)) == @bitReverse(i16, neg16));
65 var neg24: i24 = -6773785;65 var neg24: i24 = -6773785;
66 expect(@bitReverse(i24, @as(i24, -6773785)) == @bitReverse(i24, neg24));66 try expect(@bitReverse(i24, @as(i24, -6773785)) == @bitReverse(i24, neg24));
67 var neg32: i32 = -16773785;67 var neg32: i32 = -16773785;
68 expect(@bitReverse(i32, @as(i32, -16773785)) == @bitReverse(i32, neg32));68 try expect(@bitReverse(i32, @as(i32, -16773785)) == @bitReverse(i32, neg32));
69}69}
test/stage1/behavior/bool.zig+11-11
...@@ -1,25 +1,25 @@...@@ -1,25 +1,25 @@
1const expect = @import("std").testing.expect;1const expect = @import("std").testing.expect;
22
3test "bool literals" {3test "bool literals" {
4 expect(true);4 try expect(true);
5 expect(!false);5 try expect(!false);
6}6}
77
8test "cast bool to int" {8test "cast bool to int" {
9 const t = true;9 const t = true;
10 const f = false;10 const f = false;
11 expect(@boolToInt(t) == @as(u32, 1));11 try expect(@boolToInt(t) == @as(u32, 1));
12 expect(@boolToInt(f) == @as(u32, 0));12 try expect(@boolToInt(f) == @as(u32, 0));
13 nonConstCastBoolToInt(t, f);13 try nonConstCastBoolToInt(t, f);
14}14}
1515
16fn nonConstCastBoolToInt(t: bool, f: bool) void {16fn nonConstCastBoolToInt(t: bool, f: bool) !void {
17 expect(@boolToInt(t) == @as(u32, 1));17 try expect(@boolToInt(t) == @as(u32, 1));
18 expect(@boolToInt(f) == @as(u32, 0));18 try expect(@boolToInt(f) == @as(u32, 0));
19}19}
2020
21test "bool cmp" {21test "bool cmp" {
22 expect(testBoolCmp(true, false) == false);22 try expect(testBoolCmp(true, false) == false);
23}23}
24fn testBoolCmp(a: bool, b: bool) bool {24fn testBoolCmp(a: bool, b: bool) bool {
25 return a == b;25 return a == b;
...@@ -30,6 +30,6 @@ const global_t = true;...@@ -30,6 +30,6 @@ const global_t = true;
30const not_global_f = !global_f;30const not_global_f = !global_f;
31const not_global_t = !global_t;31const not_global_t = !global_t;
32test "compile time bool not" {32test "compile time bool not" {
33 expect(not_global_f);33 try expect(not_global_f);
34 expect(!not_global_t);34 try expect(!not_global_t);
35}35}
test/stage1/behavior/bugs/1025.zig+1-1
...@@ -8,5 +8,5 @@ fn getA() A {...@@ -8,5 +8,5 @@ fn getA() A {
88
9test "bug 1025" {9test "bug 1025" {
10 const a = getA();10 const a = getA();
11 @import("std").testing.expect(a.B == u8);11 try @import("std").testing.expect(a.B == u8);
12}12}
test/stage1/behavior/bugs/1076.zig+5-5
...@@ -3,21 +3,21 @@ const mem = std.mem;...@@ -3,21 +3,21 @@ const mem = std.mem;
3const expect = std.testing.expect;3const expect = std.testing.expect;
44
5test "comptime code should not modify constant data" {5test "comptime code should not modify constant data" {
6 testCastPtrOfArrayToSliceAndPtr();6 try testCastPtrOfArrayToSliceAndPtr();
7 comptime testCastPtrOfArrayToSliceAndPtr();7 comptime try testCastPtrOfArrayToSliceAndPtr();
8}8}
99
10fn testCastPtrOfArrayToSliceAndPtr() void {10fn testCastPtrOfArrayToSliceAndPtr() !void {
11 {11 {
12 var array = "aoeu".*;12 var array = "aoeu".*;
13 const x: [*]u8 = &array;13 const x: [*]u8 = &array;
14 x[0] += 1;14 x[0] += 1;
15 expect(mem.eql(u8, array[0..], "boeu"));15 try expect(mem.eql(u8, array[0..], "boeu"));
16 }16 }
17 {17 {
18 var array: [4]u8 = "aoeu".*;18 var array: [4]u8 = "aoeu".*;
19 const x: [*]u8 = &array;19 const x: [*]u8 = &array;
20 x[0] += 1;20 x[0] += 1;
21 expect(mem.eql(u8, array[0..], "boeu"));21 try expect(mem.eql(u8, array[0..], "boeu"));
22 }22 }
23}23}
test/stage1/behavior/bugs/1120.zig+1-1
...@@ -19,5 +19,5 @@ test "bug 1120" {...@@ -19,5 +19,5 @@ test "bug 1120" {
19 1 => &b.a,19 1 => &b.a,
20 else => unreachable,20 else => unreachable,
21 };21 };
22 expect(ptr.* == 2);22 try expect(ptr.* == 2);
23}23}
test/stage1/behavior/bugs/1277.zig+1-1
...@@ -11,5 +11,5 @@ fn f() i32 {...@@ -11,5 +11,5 @@ fn f() i32 {
11}11}
1212
13test "don't emit an LLVM global for a const function when it's in an optional in a struct" {13test "don't emit an LLVM global for a const function when it's in an optional in a struct" {
14 std.testing.expect(s.f.?() == 1234);14 try std.testing.expect(s.f.?() == 1234);
15}15}
test/stage1/behavior/bugs/1310.zig+1-1
...@@ -20,5 +20,5 @@ fn agent_callback(_vm: [*]VM, options: [*]u8) callconv(.C) i32 {...@@ -20,5 +20,5 @@ fn agent_callback(_vm: [*]VM, options: [*]u8) callconv(.C) i32 {
20}20}
2121
22test "fixed" {22test "fixed" {
23 expect(agent_callback(undefined, undefined) == 11);23 try expect(agent_callback(undefined, undefined) == 11);
24}24}
test/stage1/behavior/bugs/1322.zig+2-2
...@@ -13,7 +13,7 @@ const C = struct {};...@@ -13,7 +13,7 @@ const C = struct {};
1313
14test "tagged union with all void fields but a meaningful tag" {14test "tagged union with all void fields but a meaningful tag" {
15 var a: A = A{ .b = B{ .c = C{} } };15 var a: A = A{ .b = B{ .c = C{} } };
16 std.testing.expect(@as(std.meta.Tag(B), a.b) == std.meta.Tag(B).c);16 try std.testing.expect(@as(std.meta.Tag(B), a.b) == std.meta.Tag(B).c);
17 a = A{ .b = B.None };17 a = A{ .b = B.None };
18 std.testing.expect(@as(std.meta.Tag(B), a.b) == std.meta.Tag(B).None);18 try std.testing.expect(@as(std.meta.Tag(B), a.b) == std.meta.Tag(B).None);
19}19}
test/stage1/behavior/bugs/1381.zig+1-1
...@@ -17,5 +17,5 @@ test "union that needs padding bytes inside an array" {...@@ -17,5 +17,5 @@ test "union that needs padding bytes inside an array" {
17 };17 };
1818
19 const a = as[0].B;19 const a = as[0].B;
20 std.testing.expect(a.D == 1);20 try std.testing.expect(a.D == 1);
21}21}
test/stage1/behavior/bugs/1421.zig+1-1
...@@ -10,5 +10,5 @@ const S = struct {...@@ -10,5 +10,5 @@ const S = struct {
1010
11test "functions with return type required to be comptime are generic" {11test "functions with return type required to be comptime are generic" {
12 const ti = S.method();12 const ti = S.method();
13 expect(@as(builtin.TypeId, ti) == builtin.TypeId.Struct);13 try expect(@as(builtin.TypeId, ti) == builtin.TypeId.Struct);
14}14}
test/stage1/behavior/bugs/1442.zig+1-1
...@@ -7,5 +7,5 @@ const Union = union(enum) {...@@ -7,5 +7,5 @@ const Union = union(enum) {
77
8test "const error union field alignment" {8test "const error union field alignment" {
9 var union_or_err: anyerror!Union = Union{ .Color = 1234 };9 var union_or_err: anyerror!Union = Union{ .Color = 1234 };
10 std.testing.expect((union_or_err catch unreachable).Color == 1234);10 try std.testing.expect((union_or_err catch unreachable).Color == 1234);
11}11}
test/stage1/behavior/bugs/1486.zig+2-2
...@@ -5,6 +5,6 @@ var global: u64 = 123;...@@ -5,6 +5,6 @@ var global: u64 = 123;
55
6test "constant pointer to global variable causes runtime load" {6test "constant pointer to global variable causes runtime load" {
7 global = 1234;7 global = 1234;
8 expect(&global == ptr);8 try expect(&global == ptr);
9 expect(ptr.* == 1234);9 try expect(ptr.* == 1234);
10}10}
test/stage1/behavior/bugs/1607.zig+4-4
...@@ -3,13 +3,13 @@ const testing = std.testing;...@@ -3,13 +3,13 @@ const testing = std.testing;
33
4const a = [_]u8{ 1, 2, 3 };4const a = [_]u8{ 1, 2, 3 };
55
6fn checkAddress(s: []const u8) void {6fn checkAddress(s: []const u8) !void {
7 for (s) |*i, j| {7 for (s) |*i, j| {
8 testing.expect(i == &a[j]);8 try testing.expect(i == &a[j]);
9 }9 }
10}10}
1111
12test "slices pointing at the same address as global array." {12test "slices pointing at the same address as global array." {
13 checkAddress(&a);13 try checkAddress(&a);
14 comptime checkAddress(&a);14 comptime try checkAddress(&a);
15}15}
test/stage1/behavior/bugs/1735.zig+1-1
...@@ -42,5 +42,5 @@ const a = struct {...@@ -42,5 +42,5 @@ const a = struct {
4242
43test "intialization" {43test "intialization" {
44 var t = a.init();44 var t = a.init();
45 std.testing.expect(t.foo.len == 0);45 try std.testing.expect(t.foo.len == 0);
46}46}
test/stage1/behavior/bugs/1741.zig+1-1
...@@ -2,5 +2,5 @@ const std = @import("std");...@@ -2,5 +2,5 @@ const std = @import("std");
22
3test "fixed" {3test "fixed" {
4 const x: f32 align(128) = 12.34;4 const x: f32 align(128) = 12.34;
5 std.testing.expect(@ptrToInt(&x) % 128 == 0);5 try std.testing.expect(@ptrToInt(&x) % 128 == 0);
6}6}
test/stage1/behavior/bugs/1851.zig+9-9
...@@ -2,25 +2,25 @@ const std = @import("std");...@@ -2,25 +2,25 @@ const std = @import("std");
2const expect = std.testing.expect;2const expect = std.testing.expect;
33
4test "allocation and looping over 3-byte integer" {4test "allocation and looping over 3-byte integer" {
5 expect(@sizeOf(u24) == 4);5 try expect(@sizeOf(u24) == 4);
6 expect(@sizeOf([1]u24) == 4);6 try expect(@sizeOf([1]u24) == 4);
7 expect(@alignOf(u24) == 4);7 try expect(@alignOf(u24) == 4);
8 expect(@alignOf([1]u24) == 4);8 try expect(@alignOf([1]u24) == 4);
99
10 var x = try std.testing.allocator.alloc(u24, 2);10 var x = try std.testing.allocator.alloc(u24, 2);
11 defer std.testing.allocator.free(x);11 defer std.testing.allocator.free(x);
12 expect(x.len == 2);12 try expect(x.len == 2);
13 x[0] = 0xFFFFFF;13 x[0] = 0xFFFFFF;
14 x[1] = 0xFFFFFF;14 x[1] = 0xFFFFFF;
1515
16 const bytes = std.mem.sliceAsBytes(x);16 const bytes = std.mem.sliceAsBytes(x);
17 expect(@TypeOf(bytes) == []align(4) u8);17 try expect(@TypeOf(bytes) == []align(4) u8);
18 expect(bytes.len == 8);18 try expect(bytes.len == 8);
1919
20 for (bytes) |*b| {20 for (bytes) |*b| {
21 b.* = 0x00;21 b.* = 0x00;
22 }22 }
2323
24 expect(x[0] == 0x00);24 try expect(x[0] == 0x00);
25 expect(x[1] == 0x00);25 try expect(x[1] == 0x00);
26}26}
test/stage1/behavior/bugs/2006.zig+2-2
...@@ -7,6 +7,6 @@ const S = struct {...@@ -7,6 +7,6 @@ const S = struct {
7test "bug 2006" {7test "bug 2006" {
8 var a: S = undefined;8 var a: S = undefined;
9 a = S{ .p = undefined };9 a = S{ .p = undefined };
10 expect(@sizeOf(S) != 0);10 try expect(@sizeOf(S) != 0);
11 expect(@sizeOf(*void) == 0);11 try expect(@sizeOf(*void) == 0);
12}12}
test/stage1/behavior/bugs/2114.zig+7-7
...@@ -7,13 +7,13 @@ fn ctz(x: anytype) usize {...@@ -7,13 +7,13 @@ fn ctz(x: anytype) usize {
7}7}
88
9test "fixed" {9test "fixed" {
10 testClz();10 try testClz();
11 comptime testClz();11 comptime try testClz();
12}12}
1313
14fn testClz() void {14fn testClz() !void {
15 expect(ctz(@as(u128, 0x40000000000000000000000000000000)) == 126);15 try expect(ctz(@as(u128, 0x40000000000000000000000000000000)) == 126);
16 expect(math.rotl(u128, @as(u128, 0x40000000000000000000000000000000), @as(u8, 1)) == @as(u128, 0x80000000000000000000000000000000));16 try expect(math.rotl(u128, @as(u128, 0x40000000000000000000000000000000), @as(u8, 1)) == @as(u128, 0x80000000000000000000000000000000));
17 expect(ctz(@as(u128, 0x80000000000000000000000000000000)) == 127);17 try expect(ctz(@as(u128, 0x80000000000000000000000000000000)) == 127);
18 expect(ctz(math.rotl(u128, @as(u128, 0x40000000000000000000000000000000), @as(u8, 1))) == 127);18 try expect(ctz(math.rotl(u128, @as(u128, 0x40000000000000000000000000000000), @as(u8, 1))) == 127);
19}19}
test/stage1/behavior/bugs/2889.zig+1-1
...@@ -27,5 +27,5 @@ fn parseNote() ?i32 {...@@ -27,5 +27,5 @@ fn parseNote() ?i32 {
2727
28test "fixed" {28test "fixed" {
29 const result = parseNote();29 const result = parseNote();
30 std.testing.expect(result.? == 9);30 try std.testing.expect(result.? == 9);
31}31}
test/stage1/behavior/bugs/3007.zig+1-1
...@@ -19,5 +19,5 @@ fn get_foo() Foo.FooError!*Foo {...@@ -19,5 +19,5 @@ fn get_foo() Foo.FooError!*Foo {
1919
20test "fixed" {20test "fixed" {
21 default_foo = get_foo() catch null; // This Line21 default_foo = get_foo() catch null; // This Line
22 std.testing.expect(!default_foo.?.free);22 try std.testing.expect(!default_foo.?.free);
23}23}
test/stage1/behavior/bugs/3046.zig+1-1
...@@ -15,5 +15,5 @@ test "fixed" {...@@ -15,5 +15,5 @@ test "fixed" {
15 some_struct = SomeStruct{15 some_struct = SomeStruct{
16 .field = couldFail() catch |_| @as(i32, 0),16 .field = couldFail() catch |_| @as(i32, 0),
17 };17 };
18 expect(some_struct.field == 1);18 try expect(some_struct.field == 1);
19}19}
test/stage1/behavior/bugs/3112.zig+1-1
...@@ -7,7 +7,7 @@ const State = struct {...@@ -7,7 +7,7 @@ const State = struct {
7};7};
88
9fn prev(p: ?State) void {9fn prev(p: ?State) void {
10 expect(p == null);10 expect(p == null) catch @panic("test failure");
11}11}
1212
13test "zig test crash" {13test "zig test crash" {
test/stage1/behavior/bugs/3384.zig+6-6
...@@ -2,10 +2,10 @@ const std = @import("std");...@@ -2,10 +2,10 @@ const std = @import("std");
2const expect = std.testing.expect;2const expect = std.testing.expect;
33
4test "resolve array slice using builtin" {4test "resolve array slice using builtin" {
5 expect(@hasDecl(@This(), "std") == true);5 try expect(@hasDecl(@This(), "std") == true);
6 expect(@hasDecl(@This(), "std"[0..0]) == false);6 try expect(@hasDecl(@This(), "std"[0..0]) == false);
7 expect(@hasDecl(@This(), "std"[0..1]) == false);7 try expect(@hasDecl(@This(), "std"[0..1]) == false);
8 expect(@hasDecl(@This(), "std"[0..2]) == false);8 try expect(@hasDecl(@This(), "std"[0..2]) == false);
9 expect(@hasDecl(@This(), "std"[0..3]) == true);9 try expect(@hasDecl(@This(), "std"[0..3]) == true);
10 expect(@hasDecl(@This(), "std"[0..]) == true);10 try expect(@hasDecl(@This(), "std"[0..]) == true);
11}11}
test/stage1/behavior/bugs/394.zig+1-1
...@@ -14,5 +14,5 @@ test "bug 394 fixed" {...@@ -14,5 +14,5 @@ test "bug 394 fixed" {
14 .x = 3,14 .x = 3,
15 .y = E{ .B = 1 },15 .y = E{ .B = 1 },
16 };16 };
17 expect(x.x == 3);17 try expect(x.x == 3);
18}18}
test/stage1/behavior/bugs/421.zig+4-4
...@@ -1,12 +1,12 @@...@@ -1,12 +1,12 @@
1const expect = @import("std").testing.expect;1const expect = @import("std").testing.expect;
22
3test "bitCast to array" {3test "bitCast to array" {
4 comptime testBitCastArray();4 comptime try testBitCastArray();
5 testBitCastArray();5 try testBitCastArray();
6}6}
77
8fn testBitCastArray() void {8fn testBitCastArray() !void {
9 expect(extractOne64(0x0123456789abcdef0123456789abcdef) == 0x0123456789abcdef);9 try expect(extractOne64(0x0123456789abcdef0123456789abcdef) == 0x0123456789abcdef);
10}10}
1111
12fn extractOne64(a: u128) u64 {12fn extractOne64(a: u128) u64 {
test/stage1/behavior/bugs/4328.zig+14-14
...@@ -25,14 +25,14 @@ test "Extern function calls in @TypeOf" {...@@ -25,14 +25,14 @@ test "Extern function calls in @TypeOf" {
25 return 1;25 return 1;
26 }26 }
2727
28 fn doTheTest() void {28 fn doTheTest() !void {
29 expectEqual(c_int, @TypeOf(test_fn_1(0, 42)));29 try expectEqual(c_int, @TypeOf(test_fn_1(0, 42)));
30 expectEqual(c_short, @TypeOf(test_fn_2(0)));30 try expectEqual(c_short, @TypeOf(test_fn_2(0)));
31 }31 }
32 };32 };
3333
34 Test.doTheTest();34 try Test.doTheTest();
35 comptime Test.doTheTest();35 comptime try Test.doTheTest();
36}36}
3737
38test "Peer resolution of extern function calls in @TypeOf" {38test "Peer resolution of extern function calls in @TypeOf" {
...@@ -41,13 +41,13 @@ test "Peer resolution of extern function calls in @TypeOf" {...@@ -41,13 +41,13 @@ test "Peer resolution of extern function calls in @TypeOf" {
41 return 0;41 return 0;
42 }42 }
4343
44 fn doTheTest() void {44 fn doTheTest() !void {
45 expectEqual(c_long, @TypeOf(test_fn()));45 try expectEqual(c_long, @TypeOf(test_fn()));
46 }46 }
47 };47 };
4848
49 Test.doTheTest();49 try Test.doTheTest();
50 comptime Test.doTheTest();50 comptime try Test.doTheTest();
51}51}
5252
53test "Extern function calls, dereferences and field access in @TypeOf" {53test "Extern function calls, dereferences and field access in @TypeOf" {
...@@ -60,12 +60,12 @@ test "Extern function calls, dereferences and field access in @TypeOf" {...@@ -60,12 +60,12 @@ test "Extern function calls, dereferences and field access in @TypeOf" {
60 return 255;60 return 255;
61 }61 }
6262
63 fn doTheTest() void {63 fn doTheTest() !void {
64 expectEqual(FILE, @TypeOf(test_fn_1(0)));64 try expectEqual(FILE, @TypeOf(test_fn_1(0)));
65 expectEqual(u8, @TypeOf(test_fn_2(0)));65 try expectEqual(u8, @TypeOf(test_fn_2(0)));
66 }66 }
67 };67 };
6868
69 Test.doTheTest();69 try Test.doTheTest();
70 comptime Test.doTheTest();70 comptime try Test.doTheTest();
71}71}
test/stage1/behavior/bugs/4560.zig+3-3
...@@ -8,9 +8,9 @@ test "fixed" {...@@ -8,9 +8,9 @@ test "fixed" {
8 .max_distance_from_start_index = 456,8 .max_distance_from_start_index = 456,
9 },9 },
10 };10 };
11 std.testing.expect(s.a == 1);11 try std.testing.expect(s.a == 1);
12 std.testing.expect(s.b.size == 123);12 try std.testing.expect(s.b.size == 123);
13 std.testing.expect(s.b.max_distance_from_start_index == 456);13 try std.testing.expect(s.b.max_distance_from_start_index == 456);
14}14}
1515
16const S = struct {16const S = struct {
test/stage1/behavior/bugs/4769_a.zig+1-1
...@@ -1 +1 @@...@@ -1 +1 @@
1//
\ No newline at end of file
1//
test/stage1/behavior/bugs/4769_b.zig+1-1
...@@ -1 +1 @@...@@ -1 +1 @@
1//!
\ No newline at end of file
1//!
test/stage1/behavior/bugs/5398.zig+3-3
...@@ -25,7 +25,7 @@ test "assignment of field with padding" {...@@ -25,7 +25,7 @@ test "assignment of field with padding" {
25 .emits_shadows = false,25 .emits_shadows = false,
26 },26 },
27 };27 };
28 testing.expectEqual(false, renderable.material.transparent);28 try testing.expectEqual(false, renderable.material.transparent);
29 testing.expectEqual(false, renderable.material.emits_shadows);29 try testing.expectEqual(false, renderable.material.emits_shadows);
30 testing.expectEqual(true, renderable.material.render_color);30 try testing.expectEqual(true, renderable.material.render_color);
31}31}
test/stage1/behavior/bugs/5413.zig+2-2
...@@ -1,6 +1,6 @@...@@ -1,6 +1,6 @@
1const expect = @import("std").testing.expect;1const expect = @import("std").testing.expect;
22
3test "Peer type resolution with string literals and unknown length u8 pointers" {3test "Peer type resolution with string literals and unknown length u8 pointers" {
4 expect(@TypeOf("", "a", @as([*:0]const u8, "")) == [*:0]const u8);4 try expect(@TypeOf("", "a", @as([*:0]const u8, "")) == [*:0]const u8);
5 expect(@TypeOf(@as([*:0]const u8, "baz"), "foo", "bar") == [*:0]const u8);5 try expect(@TypeOf(@as([*:0]const u8, "baz"), "foo", "bar") == [*:0]const u8);
6}6}
test/stage1/behavior/bugs/5474.zig+9-9
...@@ -25,33 +25,33 @@ const Box2 = struct {...@@ -25,33 +25,33 @@ const Box2 = struct {
25 };25 };
26};26};
2727
28fn doTest() void {28fn doTest() !void {
29 // var29 // var
30 {30 {
31 var box0: Box0 = .{ .items = undefined };31 var box0: Box0 = .{ .items = undefined };
32 std.testing.expect(@typeInfo(@TypeOf(box0.items[0..])).Pointer.is_const == false);32 try std.testing.expect(@typeInfo(@TypeOf(box0.items[0..])).Pointer.is_const == false);
3333
34 var box1: Box1 = .{ .items = undefined };34 var box1: Box1 = .{ .items = undefined };
35 std.testing.expect(@typeInfo(@TypeOf(box1.items[0..])).Pointer.is_const == false);35 try std.testing.expect(@typeInfo(@TypeOf(box1.items[0..])).Pointer.is_const == false);
3636
37 var box2: Box2 = .{ .items = undefined };37 var box2: Box2 = .{ .items = undefined };
38 std.testing.expect(@typeInfo(@TypeOf(box2.items[0..])).Pointer.is_const == false);38 try std.testing.expect(@typeInfo(@TypeOf(box2.items[0..])).Pointer.is_const == false);
39 }39 }
4040
41 // const41 // const
42 {42 {
43 const box0: Box0 = .{ .items = undefined };43 const box0: Box0 = .{ .items = undefined };
44 std.testing.expect(@typeInfo(@TypeOf(box0.items[0..])).Pointer.is_const == true);44 try std.testing.expect(@typeInfo(@TypeOf(box0.items[0..])).Pointer.is_const == true);
4545
46 const box1: Box1 = .{ .items = undefined };46 const box1: Box1 = .{ .items = undefined };
47 std.testing.expect(@typeInfo(@TypeOf(box1.items[0..])).Pointer.is_const == true);47 try std.testing.expect(@typeInfo(@TypeOf(box1.items[0..])).Pointer.is_const == true);
4848
49 const box2: Box2 = .{ .items = undefined };49 const box2: Box2 = .{ .items = undefined };
50 std.testing.expect(@typeInfo(@TypeOf(box2.items[0..])).Pointer.is_const == true);50 try std.testing.expect(@typeInfo(@TypeOf(box2.items[0..])).Pointer.is_const == true);
51 }51 }
52}52}
5353
54test "pointer-to-array constness for zero-size elements" {54test "pointer-to-array constness for zero-size elements" {
55 doTest();55 try doTest();
56 comptime doTest();56 comptime try doTest();
57}57}
test/stage1/behavior/bugs/624.zig+1-1
...@@ -19,5 +19,5 @@ fn MemoryPool(comptime T: type) type {...@@ -19,5 +19,5 @@ fn MemoryPool(comptime T: type) type {
1919
20test "foo" {20test "foo" {
21 var allocator = ContextAllocator{ .n = 10 };21 var allocator = ContextAllocator{ .n = 10 };
22 expect(allocator.n == 10);22 try expect(allocator.n == 10);
23}23}
test/stage1/behavior/bugs/6456.zig+4-4
...@@ -35,9 +35,9 @@ test "issue 6456" {...@@ -35,9 +35,9 @@ test "issue 6456" {
35 });35 });
3636
37 const gen_fields = @typeInfo(T).Struct.fields;37 const gen_fields = @typeInfo(T).Struct.fields;
38 testing.expectEqual(3, gen_fields.len);38 try testing.expectEqual(3, gen_fields.len);
39 testing.expectEqualStrings("f1", gen_fields[0].name);39 try testing.expectEqualStrings("f1", gen_fields[0].name);
40 testing.expectEqualStrings("f2", gen_fields[1].name);40 try testing.expectEqualStrings("f2", gen_fields[1].name);
41 testing.expectEqualStrings("f3", gen_fields[2].name);41 try testing.expectEqualStrings("f3", gen_fields[2].name);
42 }42 }
43}43}
test/stage1/behavior/bugs/655.zig+4-4
...@@ -3,10 +3,10 @@ const other_file = @import("655_other_file.zig");...@@ -3,10 +3,10 @@ const other_file = @import("655_other_file.zig");
33
4test "function with *const parameter with type dereferenced by namespace" {4test "function with *const parameter with type dereferenced by namespace" {
5 const x: other_file.Integer = 1234;5 const x: other_file.Integer = 1234;
6 comptime std.testing.expect(@TypeOf(&x) == *const other_file.Integer);6 comptime try std.testing.expect(@TypeOf(&x) == *const other_file.Integer);
7 foo(&x);7 try foo(&x);
8}8}
99
10fn foo(x: *const other_file.Integer) void {10fn foo(x: *const other_file.Integer) !void {
11 std.testing.expect(x.* == 1234);11 try std.testing.expect(x.* == 1234);
12}12}
test/stage1/behavior/bugs/656.zig+3-3
...@@ -10,10 +10,10 @@ const Value = struct {...@@ -10,10 +10,10 @@ const Value = struct {
10};10};
1111
12test "optional if after an if in a switch prong of a switch with 2 prongs in an else" {12test "optional if after an if in a switch prong of a switch with 2 prongs in an else" {
13 foo(false, true);13 try foo(false, true);
14}14}
1515
16fn foo(a: bool, b: bool) void {16fn foo(a: bool, b: bool) !void {
17 var prefix_op = PrefixOp{17 var prefix_op = PrefixOp{
18 .AddrOf = Value{ .align_expr = 1234 },18 .AddrOf = Value{ .align_expr = 1234 },
19 };19 };
...@@ -22,7 +22,7 @@ fn foo(a: bool, b: bool) void {...@@ -22,7 +22,7 @@ fn foo(a: bool, b: bool) void {
22 PrefixOp.AddrOf => |addr_of_info| {22 PrefixOp.AddrOf => |addr_of_info| {
23 if (b) {}23 if (b) {}
24 if (addr_of_info.align_expr) |align_expr| {24 if (addr_of_info.align_expr) |align_expr| {
25 expect(align_expr == 1234);25 try expect(align_expr == 1234);
26 }26 }
27 },27 },
28 PrefixOp.Return => {},28 PrefixOp.Return => {},
test/stage1/behavior/bugs/679.zig+1-1
...@@ -13,5 +13,5 @@ const Element = struct {...@@ -13,5 +13,5 @@ const Element = struct {
13test "false dependency loop in struct definition" {13test "false dependency loop in struct definition" {
14 const listType = ElementList;14 const listType = ElementList;
15 var x: listType = 42;15 var x: listType = 42;
16 expect(x == 42);16 try expect(x == 42);
17}17}
test/stage1/behavior/bugs/6850.zig+1-1
...@@ -4,7 +4,7 @@ test "lazy sizeof comparison with zero" {...@@ -4,7 +4,7 @@ test "lazy sizeof comparison with zero" {
4 const Empty = struct {};4 const Empty = struct {};
5 const T = *Empty;5 const T = *Empty;
66
7 std.testing.expect(hasNoBits(T));7 try std.testing.expect(hasNoBits(T));
8}8}
99
10fn hasNoBits(comptime T: type) bool {10fn hasNoBits(comptime T: type) bool {
test/stage1/behavior/bugs/7047.zig+2-2
...@@ -15,8 +15,8 @@ fn S(comptime query: U) type {...@@ -15,8 +15,8 @@ fn S(comptime query: U) type {
1515
16test "compiler doesn't consider equal unions with different 'type' payload" {16test "compiler doesn't consider equal unions with different 'type' payload" {
17 const s1 = S(U{ .T = u32 }).tag();17 const s1 = S(U{ .T = u32 }).tag();
18 std.testing.expectEqual(u32, s1);18 try std.testing.expectEqual(u32, s1);
1919
20 const s2 = S(U{ .T = u64 }).tag();20 const s2 = S(U{ .T = u64 }).tag();
21 std.testing.expectEqual(u64, s2);21 try std.testing.expectEqual(u64, s2);
22}22}
test/stage1/behavior/bugs/718.zig+4-4
...@@ -10,8 +10,8 @@ const Keys = struct {...@@ -10,8 +10,8 @@ const Keys = struct {
10var keys: Keys = undefined;10var keys: Keys = undefined;
11test "zero keys with @memset" {11test "zero keys with @memset" {
12 @memset(@ptrCast([*]u8, &keys), 0, @sizeOf(@TypeOf(keys)));12 @memset(@ptrCast([*]u8, &keys), 0, @sizeOf(@TypeOf(keys)));
13 expect(!keys.up);13 try expect(!keys.up);
14 expect(!keys.down);14 try expect(!keys.down);
15 expect(!keys.left);15 try expect(!keys.left);
16 expect(!keys.right);16 try expect(!keys.right);
17}17}
test/stage1/behavior/bugs/726.zig+2-2
...@@ -3,7 +3,7 @@ const expect = @import("std").testing.expect;...@@ -3,7 +3,7 @@ const expect = @import("std").testing.expect;
3test "@ptrCast from const to nullable" {3test "@ptrCast from const to nullable" {
4 const c: u8 = 4;4 const c: u8 = 4;
5 var x: ?*const u8 = @ptrCast(?*const u8, &c);5 var x: ?*const u8 = @ptrCast(?*const u8, &c);
6 expect(x.?.* == 4);6 try expect(x.?.* == 4);
7}7}
88
9test "@ptrCast from var in empty struct to nullable" {9test "@ptrCast from var in empty struct to nullable" {
...@@ -11,5 +11,5 @@ test "@ptrCast from var in empty struct to nullable" {...@@ -11,5 +11,5 @@ test "@ptrCast from var in empty struct to nullable" {
11 var c: u8 = 4;11 var c: u8 = 4;
12 };12 };
13 var x: ?*const u8 = @ptrCast(?*const u8, &container.c);13 var x: ?*const u8 = @ptrCast(?*const u8, &container.c);
14 expect(x.?.* == 4);14 try expect(x.?.* == 4);
15}15}
test/stage1/behavior/bugs/920.zig+1-1
...@@ -60,6 +60,6 @@ test "bug 920 fixed" {...@@ -60,6 +60,6 @@ test "bug 920 fixed" {
60 };60 };
6161
62 for (NormalDist1.f) |_, i| {62 for (NormalDist1.f) |_, i| {
63 std.testing.expectEqual(NormalDist1.f[i], NormalDist.f[i]);63 try std.testing.expectEqual(NormalDist1.f[i], NormalDist.f[i]);
64 }64 }
65}65}
test/stage1/behavior/byteswap.zig+33-33
...@@ -3,39 +3,39 @@ const expect = std.testing.expect;...@@ -3,39 +3,39 @@ const expect = std.testing.expect;
33
4test "@byteSwap integers" {4test "@byteSwap integers" {
5 const ByteSwapIntTest = struct {5 const ByteSwapIntTest = struct {
6 fn run() void {6 fn run() !void {
7 t(u0, 0, 0);7 try t(u0, 0, 0);
8 t(u8, 0x12, 0x12);8 try t(u8, 0x12, 0x12);
9 t(u16, 0x1234, 0x3412);9 try t(u16, 0x1234, 0x3412);
10 t(u24, 0x123456, 0x563412);10 try t(u24, 0x123456, 0x563412);
11 t(u32, 0x12345678, 0x78563412);11 try t(u32, 0x12345678, 0x78563412);
12 t(u40, 0x123456789a, 0x9a78563412);12 try t(u40, 0x123456789a, 0x9a78563412);
13 t(i48, 0x123456789abc, @bitCast(i48, @as(u48, 0xbc9a78563412)));13 try t(i48, 0x123456789abc, @bitCast(i48, @as(u48, 0xbc9a78563412)));
14 t(u56, 0x123456789abcde, 0xdebc9a78563412);14 try t(u56, 0x123456789abcde, 0xdebc9a78563412);
15 t(u64, 0x123456789abcdef1, 0xf1debc9a78563412);15 try t(u64, 0x123456789abcdef1, 0xf1debc9a78563412);
16 t(u128, 0x123456789abcdef11121314151617181, 0x8171615141312111f1debc9a78563412);16 try t(u128, 0x123456789abcdef11121314151617181, 0x8171615141312111f1debc9a78563412);
1717
18 t(u0, @as(u0, 0), 0);18 try t(u0, @as(u0, 0), 0);
19 t(i8, @as(i8, -50), -50);19 try t(i8, @as(i8, -50), -50);
20 t(i16, @bitCast(i16, @as(u16, 0x1234)), @bitCast(i16, @as(u16, 0x3412)));20 try t(i16, @bitCast(i16, @as(u16, 0x1234)), @bitCast(i16, @as(u16, 0x3412)));
21 t(i24, @bitCast(i24, @as(u24, 0x123456)), @bitCast(i24, @as(u24, 0x563412)));21 try t(i24, @bitCast(i24, @as(u24, 0x123456)), @bitCast(i24, @as(u24, 0x563412)));
22 t(i32, @bitCast(i32, @as(u32, 0x12345678)), @bitCast(i32, @as(u32, 0x78563412)));22 try t(i32, @bitCast(i32, @as(u32, 0x12345678)), @bitCast(i32, @as(u32, 0x78563412)));
23 t(u40, @bitCast(i40, @as(u40, 0x123456789a)), @as(u40, 0x9a78563412));23 try t(u40, @bitCast(i40, @as(u40, 0x123456789a)), @as(u40, 0x9a78563412));
24 t(i48, @bitCast(i48, @as(u48, 0x123456789abc)), @bitCast(i48, @as(u48, 0xbc9a78563412)));24 try t(i48, @bitCast(i48, @as(u48, 0x123456789abc)), @bitCast(i48, @as(u48, 0xbc9a78563412)));
25 t(i56, @bitCast(i56, @as(u56, 0x123456789abcde)), @bitCast(i56, @as(u56, 0xdebc9a78563412)));25 try t(i56, @bitCast(i56, @as(u56, 0x123456789abcde)), @bitCast(i56, @as(u56, 0xdebc9a78563412)));
26 t(i64, @bitCast(i64, @as(u64, 0x123456789abcdef1)), @bitCast(i64, @as(u64, 0xf1debc9a78563412)));26 try t(i64, @bitCast(i64, @as(u64, 0x123456789abcdef1)), @bitCast(i64, @as(u64, 0xf1debc9a78563412)));
27 t(27 try t(
28 i128,28 i128,
29 @bitCast(i128, @as(u128, 0x123456789abcdef11121314151617181)),29 @bitCast(i128, @as(u128, 0x123456789abcdef11121314151617181)),
30 @bitCast(i128, @as(u128, 0x8171615141312111f1debc9a78563412)),30 @bitCast(i128, @as(u128, 0x8171615141312111f1debc9a78563412)),
31 );31 );
32 }32 }
33 fn t(comptime I: type, input: I, expected_output: I) void {33 fn t(comptime I: type, input: I, expected_output: I) !void {
34 std.testing.expectEqual(expected_output, @byteSwap(I, input));34 try std.testing.expectEqual(expected_output, @byteSwap(I, input));
35 }35 }
36 };36 };
37 comptime ByteSwapIntTest.run();37 comptime try ByteSwapIntTest.run();
38 ByteSwapIntTest.run();38 try ByteSwapIntTest.run();
39}39}
4040
41test "@byteSwap vectors" {41test "@byteSwap vectors" {
...@@ -46,10 +46,10 @@ test "@byteSwap vectors" {...@@ -46,10 +46,10 @@ test "@byteSwap vectors" {
46 if (std.Target.current.cpu.arch == .mipsel or std.Target.current.cpu.arch == .mips) return error.SkipZigTest;46 if (std.Target.current.cpu.arch == .mipsel or std.Target.current.cpu.arch == .mips) return error.SkipZigTest;
4747
48 const ByteSwapVectorTest = struct {48 const ByteSwapVectorTest = struct {
49 fn run() void {49 fn run() !void {
50 t(u8, 2, [_]u8{ 0x12, 0x13 }, [_]u8{ 0x12, 0x13 });50 try t(u8, 2, [_]u8{ 0x12, 0x13 }, [_]u8{ 0x12, 0x13 });
51 t(u16, 2, [_]u16{ 0x1234, 0x2345 }, [_]u16{ 0x3412, 0x4523 });51 try t(u16, 2, [_]u16{ 0x1234, 0x2345 }, [_]u16{ 0x3412, 0x4523 });
52 t(u24, 2, [_]u24{ 0x123456, 0x234567 }, [_]u24{ 0x563412, 0x674523 });52 try t(u24, 2, [_]u24{ 0x123456, 0x234567 }, [_]u24{ 0x563412, 0x674523 });
53 }53 }
5454
55 fn t(55 fn t(
...@@ -57,12 +57,12 @@ test "@byteSwap vectors" {...@@ -57,12 +57,12 @@ test "@byteSwap vectors" {
57 comptime n: comptime_int,57 comptime n: comptime_int,
58 input: std.meta.Vector(n, I),58 input: std.meta.Vector(n, I),
59 expected_vector: std.meta.Vector(n, I),59 expected_vector: std.meta.Vector(n, I),
60 ) void {60 ) !void {
61 const actual_output: [n]I = @byteSwap(I, input);61 const actual_output: [n]I = @byteSwap(I, input);
62 const expected_output: [n]I = expected_vector;62 const expected_output: [n]I = expected_vector;
63 std.testing.expectEqual(expected_output, actual_output);63 try std.testing.expectEqual(expected_output, actual_output);
64 }64 }
65 };65 };
66 comptime ByteSwapVectorTest.run();66 comptime try ByteSwapVectorTest.run();
67 ByteSwapVectorTest.run();67 try ByteSwapVectorTest.run();
68}68}
test/stage1/behavior/byval_arg_var.zig+1-1
...@@ -6,7 +6,7 @@ test "pass string literal byvalue to a generic var param" {...@@ -6,7 +6,7 @@ test "pass string literal byvalue to a generic var param" {
6 start();6 start();
7 blowUpStack(10);7 blowUpStack(10);
88
9 std.testing.expect(std.mem.eql(u8, result, "string literal"));9 try std.testing.expect(std.mem.eql(u8, result, "string literal"));
10}10}
1111
12fn start() void {12fn start() void {
test/stage1/behavior/call.zig+19-19
...@@ -8,25 +8,25 @@ test "basic invocations" {...@@ -8,25 +8,25 @@ test "basic invocations" {
8 return 1234;8 return 1234;
9 }9 }
10 }.foo;10 }.foo;
11 expect(@call(.{}, foo, .{}) == 1234);11 try expect(@call(.{}, foo, .{}) == 1234);
12 comptime {12 comptime {
13 // modifiers that allow comptime calls13 // modifiers that allow comptime calls
14 expect(@call(.{}, foo, .{}) == 1234);14 try expect(@call(.{}, foo, .{}) == 1234);
15 expect(@call(.{ .modifier = .no_async }, foo, .{}) == 1234);15 try expect(@call(.{ .modifier = .no_async }, foo, .{}) == 1234);
16 expect(@call(.{ .modifier = .always_tail }, foo, .{}) == 1234);16 try expect(@call(.{ .modifier = .always_tail }, foo, .{}) == 1234);
17 expect(@call(.{ .modifier = .always_inline }, foo, .{}) == 1234);17 try expect(@call(.{ .modifier = .always_inline }, foo, .{}) == 1234);
18 }18 }
19 {19 {
20 // comptime call without comptime keyword20 // comptime call without comptime keyword
21 const result = @call(.{ .modifier = .compile_time }, foo, .{}) == 1234;21 const result = @call(.{ .modifier = .compile_time }, foo, .{}) == 1234;
22 comptime expect(result);22 comptime try expect(result);
23 }23 }
24 {24 {
25 // call of non comptime-known function25 // call of non comptime-known function
26 var alias_foo = foo;26 var alias_foo = foo;
27 expect(@call(.{ .modifier = .no_async }, alias_foo, .{}) == 1234);27 try expect(@call(.{ .modifier = .no_async }, alias_foo, .{}) == 1234);
28 expect(@call(.{ .modifier = .never_tail }, alias_foo, .{}) == 1234);28 try expect(@call(.{ .modifier = .never_tail }, alias_foo, .{}) == 1234);
29 expect(@call(.{ .modifier = .never_inline }, alias_foo, .{}) == 1234);29 try expect(@call(.{ .modifier = .never_inline }, alias_foo, .{}) == 1234);
30 }30 }
31}31}
3232
...@@ -38,20 +38,20 @@ test "tuple parameters" {...@@ -38,20 +38,20 @@ test "tuple parameters" {
38 }.add;38 }.add;
39 var a: i32 = 12;39 var a: i32 = 12;
40 var b: i32 = 34;40 var b: i32 = 34;
41 expect(@call(.{}, add, .{ a, 34 }) == 46);41 try expect(@call(.{}, add, .{ a, 34 }) == 46);
42 expect(@call(.{}, add, .{ 12, b }) == 46);42 try expect(@call(.{}, add, .{ 12, b }) == 46);
43 expect(@call(.{}, add, .{ a, b }) == 46);43 try expect(@call(.{}, add, .{ a, b }) == 46);
44 expect(@call(.{}, add, .{ 12, 34 }) == 46);44 try expect(@call(.{}, add, .{ 12, 34 }) == 46);
45 comptime expect(@call(.{}, add, .{ 12, 34 }) == 46);45 comptime try expect(@call(.{}, add, .{ 12, 34 }) == 46);
46 {46 {
47 const separate_args0 = .{ a, b };47 const separate_args0 = .{ a, b };
48 const separate_args1 = .{ a, 34 };48 const separate_args1 = .{ a, 34 };
49 const separate_args2 = .{ 12, 34 };49 const separate_args2 = .{ 12, 34 };
50 const separate_args3 = .{ 12, b };50 const separate_args3 = .{ 12, b };
51 expect(@call(.{ .modifier = .always_inline }, add, separate_args0) == 46);51 try expect(@call(.{ .modifier = .always_inline }, add, separate_args0) == 46);
52 expect(@call(.{ .modifier = .always_inline }, add, separate_args1) == 46);52 try expect(@call(.{ .modifier = .always_inline }, add, separate_args1) == 46);
53 expect(@call(.{ .modifier = .always_inline }, add, separate_args2) == 46);53 try expect(@call(.{ .modifier = .always_inline }, add, separate_args2) == 46);
54 expect(@call(.{ .modifier = .always_inline }, add, separate_args3) == 46);54 try expect(@call(.{ .modifier = .always_inline }, add, separate_args3) == 46);
55 }55 }
56}56}
5757
...@@ -70,5 +70,5 @@ test "comptime call with bound function as parameter" {...@@ -70,5 +70,5 @@ test "comptime call with bound function as parameter" {
70 };70 };
7171
72 var inst: S = undefined;72 var inst: S = undefined;
73 expectEqual(?i32, S.ReturnType(inst.call_me_maybe));73 try expectEqual(?i32, S.ReturnType(inst.call_me_maybe));
74}74}
test/stage1/behavior/cast.zig+226-226
...@@ -8,12 +8,12 @@ test "int to ptr cast" {...@@ -8,12 +8,12 @@ test "int to ptr cast" {
8 const x = @as(usize, 13);8 const x = @as(usize, 13);
9 const y = @intToPtr(*u8, x);9 const y = @intToPtr(*u8, x);
10 const z = @ptrToInt(y);10 const z = @ptrToInt(y);
11 expect(z == 13);11 try expect(z == 13);
12}12}
1313
14test "integer literal to pointer cast" {14test "integer literal to pointer cast" {
15 const vga_mem = @intToPtr(*u16, 0xB8000);15 const vga_mem = @intToPtr(*u16, 0xB8000);
16 expect(@ptrToInt(vga_mem) == 0xB8000);16 try expect(@ptrToInt(vga_mem) == 0xB8000);
17}17}
1818
19test "pointer reinterpret const float to int" {19test "pointer reinterpret const float to int" {
...@@ -23,9 +23,9 @@ test "pointer reinterpret const float to int" {...@@ -23,9 +23,9 @@ test "pointer reinterpret const float to int" {
23 const int_ptr = @ptrCast(*const i32, float_ptr);23 const int_ptr = @ptrCast(*const i32, float_ptr);
24 const int_val = int_ptr.*;24 const int_val = int_ptr.*;
25 if (std.builtin.endian == .Little)25 if (std.builtin.endian == .Little)
26 expect(int_val == 0x33333303)26 try expect(int_val == 0x33333303)
27 else27 else
28 expect(int_val == 0x3fe33333);28 try expect(int_val == 0x3fe33333);
29}29}
3030
31test "implicitly cast indirect pointer to maybe-indirect pointer" {31test "implicitly cast indirect pointer to maybe-indirect pointer" {
...@@ -49,62 +49,62 @@ test "implicitly cast indirect pointer to maybe-indirect pointer" {...@@ -49,62 +49,62 @@ test "implicitly cast indirect pointer to maybe-indirect pointer" {
49 const p = &s;49 const p = &s;
50 const q = &p;50 const q = &p;
51 const r = &q;51 const r = &q;
52 expect(42 == S.constConst(q));52 try expect(42 == S.constConst(q));
53 expect(42 == S.maybeConstConst(q));53 try expect(42 == S.maybeConstConst(q));
54 expect(42 == S.constConstConst(r));54 try expect(42 == S.constConstConst(r));
55 expect(42 == S.maybeConstConstConst(r));55 try expect(42 == S.maybeConstConstConst(r));
56}56}
5757
58test "explicit cast from integer to error type" {58test "explicit cast from integer to error type" {
59 testCastIntToErr(error.ItBroke);59 try testCastIntToErr(error.ItBroke);
60 comptime testCastIntToErr(error.ItBroke);60 comptime try testCastIntToErr(error.ItBroke);
61}61}
62fn testCastIntToErr(err: anyerror) void {62fn testCastIntToErr(err: anyerror) !void {
63 const x = @errorToInt(err);63 const x = @errorToInt(err);
64 const y = @intToError(x);64 const y = @intToError(x);
65 expect(error.ItBroke == y);65 try expect(error.ItBroke == y);
66}66}
6767
68test "peer resolve arrays of different size to const slice" {68test "peer resolve arrays of different size to const slice" {
69 expect(mem.eql(u8, boolToStr(true), "true"));69 try expect(mem.eql(u8, boolToStr(true), "true"));
70 expect(mem.eql(u8, boolToStr(false), "false"));70 try expect(mem.eql(u8, boolToStr(false), "false"));
71 comptime expect(mem.eql(u8, boolToStr(true), "true"));71 comptime try expect(mem.eql(u8, boolToStr(true), "true"));
72 comptime expect(mem.eql(u8, boolToStr(false), "false"));72 comptime try expect(mem.eql(u8, boolToStr(false), "false"));
73}73}
74fn boolToStr(b: bool) []const u8 {74fn boolToStr(b: bool) []const u8 {
75 return if (b) "true" else "false";75 return if (b) "true" else "false";
76}76}
7777
78test "peer resolve array and const slice" {78test "peer resolve array and const slice" {
79 testPeerResolveArrayConstSlice(true);79 try testPeerResolveArrayConstSlice(true);
80 comptime testPeerResolveArrayConstSlice(true);80 comptime try testPeerResolveArrayConstSlice(true);
81}81}
82fn testPeerResolveArrayConstSlice(b: bool) void {82fn testPeerResolveArrayConstSlice(b: bool) !void {
83 const value1 = if (b) "aoeu" else @as([]const u8, "zz");83 const value1 = if (b) "aoeu" else @as([]const u8, "zz");
84 const value2 = if (b) @as([]const u8, "zz") else "aoeu";84 const value2 = if (b) @as([]const u8, "zz") else "aoeu";
85 expect(mem.eql(u8, value1, "aoeu"));85 try expect(mem.eql(u8, value1, "aoeu"));
86 expect(mem.eql(u8, value2, "zz"));86 try expect(mem.eql(u8, value2, "zz"));
87}87}
8888
89test "implicitly cast from T to anyerror!?T" {89test "implicitly cast from T to anyerror!?T" {
90 castToOptionalTypeError(1);90 try castToOptionalTypeError(1);
91 comptime castToOptionalTypeError(1);91 comptime try castToOptionalTypeError(1);
92}92}
9393
94const A = struct {94const A = struct {
95 a: i32,95 a: i32,
96};96};
97fn castToOptionalTypeError(z: i32) void {97fn castToOptionalTypeError(z: i32) !void {
98 const x = @as(i32, 1);98 const x = @as(i32, 1);
99 const y: anyerror!?i32 = x;99 const y: anyerror!?i32 = x;
100 expect((try y).? == 1);100 try expect((try y).? == 1);
101101
102 const f = z;102 const f = z;
103 const g: anyerror!?i32 = f;103 const g: anyerror!?i32 = f;
104104
105 const a = A{ .a = z };105 const a = A{ .a = z };
106 const b: anyerror!?A = a;106 const b: anyerror!?A = a;
107 expect((b catch unreachable).?.a == 1);107 try expect((b catch unreachable).?.a == 1);
108}108}
109109
110test "implicitly cast from int to anyerror!?T" {110test "implicitly cast from int to anyerror!?T" {
...@@ -119,7 +119,7 @@ fn implicitIntLitToOptional() void {...@@ -119,7 +119,7 @@ fn implicitIntLitToOptional() void {
119test "return null from fn() anyerror!?&T" {119test "return null from fn() anyerror!?&T" {
120 const a = returnNullFromOptionalTypeErrorRef();120 const a = returnNullFromOptionalTypeErrorRef();
121 const b = returnNullLitFromOptionalTypeErrorRef();121 const b = returnNullLitFromOptionalTypeErrorRef();
122 expect((try a) == null and (try b) == null);122 try expect((try a) == null and (try b) == null);
123}123}
124fn returnNullFromOptionalTypeErrorRef() anyerror!?*A {124fn returnNullFromOptionalTypeErrorRef() anyerror!?*A {
125 const a: ?*A = null;125 const a: ?*A = null;
...@@ -130,11 +130,11 @@ fn returnNullLitFromOptionalTypeErrorRef() anyerror!?*A {...@@ -130,11 +130,11 @@ fn returnNullLitFromOptionalTypeErrorRef() anyerror!?*A {
130}130}
131131
132test "peer type resolution: ?T and T" {132test "peer type resolution: ?T and T" {
133 expect(peerTypeTAndOptionalT(true, false).? == 0);133 try expect(peerTypeTAndOptionalT(true, false).? == 0);
134 expect(peerTypeTAndOptionalT(false, false).? == 3);134 try expect(peerTypeTAndOptionalT(false, false).? == 3);
135 comptime {135 comptime {
136 expect(peerTypeTAndOptionalT(true, false).? == 0);136 try expect(peerTypeTAndOptionalT(true, false).? == 0);
137 expect(peerTypeTAndOptionalT(false, false).? == 3);137 try expect(peerTypeTAndOptionalT(false, false).? == 3);
138 }138 }
139}139}
140fn peerTypeTAndOptionalT(c: bool, b: bool) ?usize {140fn peerTypeTAndOptionalT(c: bool, b: bool) ?usize {
...@@ -146,11 +146,11 @@ fn peerTypeTAndOptionalT(c: bool, b: bool) ?usize {...@@ -146,11 +146,11 @@ fn peerTypeTAndOptionalT(c: bool, b: bool) ?usize {
146}146}
147147
148test "peer type resolution: [0]u8 and []const u8" {148test "peer type resolution: [0]u8 and []const u8" {
149 expect(peerTypeEmptyArrayAndSlice(true, "hi").len == 0);149 try expect(peerTypeEmptyArrayAndSlice(true, "hi").len == 0);
150 expect(peerTypeEmptyArrayAndSlice(false, "hi").len == 1);150 try expect(peerTypeEmptyArrayAndSlice(false, "hi").len == 1);
151 comptime {151 comptime {
152 expect(peerTypeEmptyArrayAndSlice(true, "hi").len == 0);152 try expect(peerTypeEmptyArrayAndSlice(true, "hi").len == 0);
153 expect(peerTypeEmptyArrayAndSlice(false, "hi").len == 1);153 try expect(peerTypeEmptyArrayAndSlice(false, "hi").len == 1);
154 }154 }
155}155}
156fn peerTypeEmptyArrayAndSlice(a: bool, slice: []const u8) []const u8 {156fn peerTypeEmptyArrayAndSlice(a: bool, slice: []const u8) []const u8 {
...@@ -162,8 +162,8 @@ fn peerTypeEmptyArrayAndSlice(a: bool, slice: []const u8) []const u8 {...@@ -162,8 +162,8 @@ fn peerTypeEmptyArrayAndSlice(a: bool, slice: []const u8) []const u8 {
162}162}
163163
164test "implicitly cast from [N]T to ?[]const T" {164test "implicitly cast from [N]T to ?[]const T" {
165 expect(mem.eql(u8, castToOptionalSlice().?, "hi"));165 try expect(mem.eql(u8, castToOptionalSlice().?, "hi"));
166 comptime expect(mem.eql(u8, castToOptionalSlice().?, "hi"));166 comptime try expect(mem.eql(u8, castToOptionalSlice().?, "hi"));
167}167}
168168
169fn castToOptionalSlice() ?[]const u8 {169fn castToOptionalSlice() ?[]const u8 {
...@@ -171,12 +171,12 @@ fn castToOptionalSlice() ?[]const u8 {...@@ -171,12 +171,12 @@ fn castToOptionalSlice() ?[]const u8 {
171}171}
172172
173test "implicitly cast from [0]T to anyerror![]T" {173test "implicitly cast from [0]T to anyerror![]T" {
174 testCastZeroArrayToErrSliceMut();174 try testCastZeroArrayToErrSliceMut();
175 comptime testCastZeroArrayToErrSliceMut();175 comptime try testCastZeroArrayToErrSliceMut();
176}176}
177177
178fn testCastZeroArrayToErrSliceMut() void {178fn testCastZeroArrayToErrSliceMut() !void {
179 expect((gimmeErrOrSlice() catch unreachable).len == 0);179 try expect((gimmeErrOrSlice() catch unreachable).len == 0);
180}180}
181181
182fn gimmeErrOrSlice() anyerror![]u8 {182fn gimmeErrOrSlice() anyerror![]u8 {
...@@ -189,19 +189,19 @@ test "peer type resolution: [0]u8, []const u8, and anyerror![]u8" {...@@ -189,19 +189,19 @@ test "peer type resolution: [0]u8, []const u8, and anyerror![]u8" {
189 {189 {
190 var data = "hi".*;190 var data = "hi".*;
191 const slice = data[0..];191 const slice = data[0..];
192 expect((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0);192 try expect((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0);
193 expect((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1);193 try expect((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1);
194 }194 }
195 {195 {
196 var data: [2]u8 = "hi".*;196 var data: [2]u8 = "hi".*;
197 const slice = data[0..];197 const slice = data[0..];
198 expect((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0);198 try expect((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0);
199 expect((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1);199 try expect((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1);
200 }200 }
201 }201 }
202 };202 };
203 try S.doTheTest();203 try S.doTheTest();
204 try comptime S.doTheTest();204 comptime try S.doTheTest();
205}205}
206fn peerTypeEmptyArrayAndSliceAndError(a: bool, slice: []u8) anyerror![]u8 {206fn peerTypeEmptyArrayAndSliceAndError(a: bool, slice: []u8) anyerror![]u8 {
207 if (a) {207 if (a) {
...@@ -212,43 +212,43 @@ fn peerTypeEmptyArrayAndSliceAndError(a: bool, slice: []u8) anyerror![]u8 {...@@ -212,43 +212,43 @@ fn peerTypeEmptyArrayAndSliceAndError(a: bool, slice: []u8) anyerror![]u8 {
212}212}
213213
214test "resolve undefined with integer" {214test "resolve undefined with integer" {
215 testResolveUndefWithInt(true, 1234);215 try testResolveUndefWithInt(true, 1234);
216 comptime testResolveUndefWithInt(true, 1234);216 comptime try testResolveUndefWithInt(true, 1234);
217}217}
218fn testResolveUndefWithInt(b: bool, x: i32) void {218fn testResolveUndefWithInt(b: bool, x: i32) !void {
219 const value = if (b) x else undefined;219 const value = if (b) x else undefined;
220 if (b) {220 if (b) {
221 expect(value == x);221 try expect(value == x);
222 }222 }
223}223}
224224
225test "implicit cast from &const [N]T to []const T" {225test "implicit cast from &const [N]T to []const T" {
226 testCastConstArrayRefToConstSlice();226 try testCastConstArrayRefToConstSlice();
227 comptime testCastConstArrayRefToConstSlice();227 comptime try testCastConstArrayRefToConstSlice();
228}228}
229229
230fn testCastConstArrayRefToConstSlice() void {230fn testCastConstArrayRefToConstSlice() !void {
231 {231 {
232 const blah = "aoeu".*;232 const blah = "aoeu".*;
233 const const_array_ref = &blah;233 const const_array_ref = &blah;
234 expect(@TypeOf(const_array_ref) == *const [4:0]u8);234 try expect(@TypeOf(const_array_ref) == *const [4:0]u8);
235 const slice: []const u8 = const_array_ref;235 const slice: []const u8 = const_array_ref;
236 expect(mem.eql(u8, slice, "aoeu"));236 try expect(mem.eql(u8, slice, "aoeu"));
237 }237 }
238 {238 {
239 const blah: [4]u8 = "aoeu".*;239 const blah: [4]u8 = "aoeu".*;
240 const const_array_ref = &blah;240 const const_array_ref = &blah;
241 expect(@TypeOf(const_array_ref) == *const [4]u8);241 try expect(@TypeOf(const_array_ref) == *const [4]u8);
242 const slice: []const u8 = const_array_ref;242 const slice: []const u8 = const_array_ref;
243 expect(mem.eql(u8, slice, "aoeu"));243 try expect(mem.eql(u8, slice, "aoeu"));
244 }244 }
245}245}
246246
247test "peer type resolution: error and [N]T" {247test "peer type resolution: error and [N]T" {
248 expect(mem.eql(u8, try testPeerErrorAndArray(0), "OK"));248 try expect(mem.eql(u8, try testPeerErrorAndArray(0), "OK"));
249 comptime expect(mem.eql(u8, try testPeerErrorAndArray(0), "OK"));249 comptime try expect(mem.eql(u8, try testPeerErrorAndArray(0), "OK"));
250 expect(mem.eql(u8, try testPeerErrorAndArray2(1), "OKK"));250 try expect(mem.eql(u8, try testPeerErrorAndArray2(1), "OKK"));
251 comptime expect(mem.eql(u8, try testPeerErrorAndArray2(1), "OKK"));251 comptime try expect(mem.eql(u8, try testPeerErrorAndArray2(1), "OKK"));
252}252}
253253
254fn testPeerErrorAndArray(x: u8) anyerror![]const u8 {254fn testPeerErrorAndArray(x: u8) anyerror![]const u8 {
...@@ -266,35 +266,35 @@ fn testPeerErrorAndArray2(x: u8) anyerror![]const u8 {...@@ -266,35 +266,35 @@ fn testPeerErrorAndArray2(x: u8) anyerror![]const u8 {
266}266}
267267
268test "@floatToInt" {268test "@floatToInt" {
269 testFloatToInts();269 try testFloatToInts();
270 comptime testFloatToInts();270 comptime try testFloatToInts();
271}271}
272272
273fn testFloatToInts() void {273fn testFloatToInts() !void {
274 const x = @as(i32, 1e4);274 const x = @as(i32, 1e4);
275 expect(x == 10000);275 try expect(x == 10000);
276 const y = @floatToInt(i32, @as(f32, 1e4));276 const y = @floatToInt(i32, @as(f32, 1e4));
277 expect(y == 10000);277 try expect(y == 10000);
278 expectFloatToInt(f16, 255.1, u8, 255);278 try expectFloatToInt(f16, 255.1, u8, 255);
279 expectFloatToInt(f16, 127.2, i8, 127);279 try expectFloatToInt(f16, 127.2, i8, 127);
280 expectFloatToInt(f16, -128.2, i8, -128);280 try expectFloatToInt(f16, -128.2, i8, -128);
281 expectFloatToInt(f32, 255.1, u8, 255);281 try expectFloatToInt(f32, 255.1, u8, 255);
282 expectFloatToInt(f32, 127.2, i8, 127);282 try expectFloatToInt(f32, 127.2, i8, 127);
283 expectFloatToInt(f32, -128.2, i8, -128);283 try expectFloatToInt(f32, -128.2, i8, -128);
284 expectFloatToInt(comptime_int, 1234, i16, 1234);284 try expectFloatToInt(comptime_int, 1234, i16, 1234);
285}285}
286286
287fn expectFloatToInt(comptime F: type, f: F, comptime I: type, i: I) void {287fn expectFloatToInt(comptime F: type, f: F, comptime I: type, i: I) !void {
288 expect(@floatToInt(I, f) == i);288 try expect(@floatToInt(I, f) == i);
289}289}
290290
291test "cast u128 to f128 and back" {291test "cast u128 to f128 and back" {
292 comptime testCast128();292 comptime try testCast128();
293 testCast128();293 try testCast128();
294}294}
295295
296fn testCast128() void {296fn testCast128() !void {
297 expect(cast128Int(cast128Float(0x7fff0000000000000000000000000000)) == 0x7fff0000000000000000000000000000);297 try expect(cast128Int(cast128Float(0x7fff0000000000000000000000000000)) == 0x7fff0000000000000000000000000000);
298}298}
299299
300fn cast128Int(x: f128) u128 {300fn cast128Int(x: f128) u128 {
...@@ -306,69 +306,69 @@ fn cast128Float(x: u128) f128 {...@@ -306,69 +306,69 @@ fn cast128Float(x: u128) f128 {
306}306}
307307
308test "single-item pointer of array to slice and to unknown length pointer" {308test "single-item pointer of array to slice and to unknown length pointer" {
309 testCastPtrOfArrayToSliceAndPtr();309 try testCastPtrOfArrayToSliceAndPtr();
310 comptime testCastPtrOfArrayToSliceAndPtr();310 comptime try testCastPtrOfArrayToSliceAndPtr();
311}311}
312312
313fn testCastPtrOfArrayToSliceAndPtr() void {313fn testCastPtrOfArrayToSliceAndPtr() !void {
314 {314 {
315 var array = "aoeu".*;315 var array = "aoeu".*;
316 const x: [*]u8 = &array;316 const x: [*]u8 = &array;
317 x[0] += 1;317 x[0] += 1;
318 expect(mem.eql(u8, array[0..], "boeu"));318 try expect(mem.eql(u8, array[0..], "boeu"));
319 const y: []u8 = &array;319 const y: []u8 = &array;
320 y[0] += 1;320 y[0] += 1;
321 expect(mem.eql(u8, array[0..], "coeu"));321 try expect(mem.eql(u8, array[0..], "coeu"));
322 }322 }
323 {323 {
324 var array: [4]u8 = "aoeu".*;324 var array: [4]u8 = "aoeu".*;
325 const x: [*]u8 = &array;325 const x: [*]u8 = &array;
326 x[0] += 1;326 x[0] += 1;
327 expect(mem.eql(u8, array[0..], "boeu"));327 try expect(mem.eql(u8, array[0..], "boeu"));
328 const y: []u8 = &array;328 const y: []u8 = &array;
329 y[0] += 1;329 y[0] += 1;
330 expect(mem.eql(u8, array[0..], "coeu"));330 try expect(mem.eql(u8, array[0..], "coeu"));
331 }331 }
332}332}
333333
334test "cast *[1][*]const u8 to [*]const ?[*]const u8" {334test "cast *[1][*]const u8 to [*]const ?[*]const u8" {
335 const window_name = [1][*]const u8{"window name"};335 const window_name = [1][*]const u8{"window name"};
336 const x: [*]const ?[*]const u8 = &window_name;336 const x: [*]const ?[*]const u8 = &window_name;
337 expect(mem.eql(u8, std.mem.spanZ(@ptrCast([*:0]const u8, x[0].?)), "window name"));337 try expect(mem.eql(u8, std.mem.spanZ(@ptrCast([*:0]const u8, x[0].?)), "window name"));
338}338}
339339
340test "@intCast comptime_int" {340test "@intCast comptime_int" {
341 const result = @intCast(i32, 1234);341 const result = @intCast(i32, 1234);
342 expect(@TypeOf(result) == i32);342 try expect(@TypeOf(result) == i32);
343 expect(result == 1234);343 try expect(result == 1234);
344}344}
345345
346test "@floatCast comptime_int and comptime_float" {346test "@floatCast comptime_int and comptime_float" {
347 {347 {
348 const result = @floatCast(f16, 1234);348 const result = @floatCast(f16, 1234);
349 expect(@TypeOf(result) == f16);349 try expect(@TypeOf(result) == f16);
350 expect(result == 1234.0);350 try expect(result == 1234.0);
351 }351 }
352 {352 {
353 const result = @floatCast(f16, 1234.0);353 const result = @floatCast(f16, 1234.0);
354 expect(@TypeOf(result) == f16);354 try expect(@TypeOf(result) == f16);
355 expect(result == 1234.0);355 try expect(result == 1234.0);
356 }356 }
357 {357 {
358 const result = @floatCast(f32, 1234);358 const result = @floatCast(f32, 1234);
359 expect(@TypeOf(result) == f32);359 try expect(@TypeOf(result) == f32);
360 expect(result == 1234.0);360 try expect(result == 1234.0);
361 }361 }
362 {362 {
363 const result = @floatCast(f32, 1234.0);363 const result = @floatCast(f32, 1234.0);
364 expect(@TypeOf(result) == f32);364 try expect(@TypeOf(result) == f32);
365 expect(result == 1234.0);365 try expect(result == 1234.0);
366 }366 }
367}367}
368368
369test "vector casts" {369test "vector casts" {
370 const S = struct {370 const S = struct {
371 fn doTheTest() void {371 fn doTheTest() !void {
372 // Upcast (implicit, equivalent to @intCast)372 // Upcast (implicit, equivalent to @intCast)
373 var up0: Vector(2, u8) = [_]u8{ 0x55, 0xaa };373 var up0: Vector(2, u8) = [_]u8{ 0x55, 0xaa };
374 var up1 = @as(Vector(2, u16), up0);374 var up1 = @as(Vector(2, u16), up0);
...@@ -380,55 +380,55 @@ test "vector casts" {...@@ -380,55 +380,55 @@ test "vector casts" {
380 var down2 = @intCast(Vector(2, u16), down0);380 var down2 = @intCast(Vector(2, u16), down0);
381 var down3 = @intCast(Vector(2, u8), down0);381 var down3 = @intCast(Vector(2, u8), down0);
382382
383 expect(mem.eql(u16, &@as([2]u16, up1), &[2]u16{ 0x55, 0xaa }));383 try expect(mem.eql(u16, &@as([2]u16, up1), &[2]u16{ 0x55, 0xaa }));
384 expect(mem.eql(u32, &@as([2]u32, up2), &[2]u32{ 0x55, 0xaa }));384 try expect(mem.eql(u32, &@as([2]u32, up2), &[2]u32{ 0x55, 0xaa }));
385 expect(mem.eql(u64, &@as([2]u64, up3), &[2]u64{ 0x55, 0xaa }));385 try expect(mem.eql(u64, &@as([2]u64, up3), &[2]u64{ 0x55, 0xaa }));
386386
387 expect(mem.eql(u32, &@as([2]u32, down1), &[2]u32{ 0x55, 0xaa }));387 try expect(mem.eql(u32, &@as([2]u32, down1), &[2]u32{ 0x55, 0xaa }));
388 expect(mem.eql(u16, &@as([2]u16, down2), &[2]u16{ 0x55, 0xaa }));388 try expect(mem.eql(u16, &@as([2]u16, down2), &[2]u16{ 0x55, 0xaa }));
389 expect(mem.eql(u8, &@as([2]u8, down3), &[2]u8{ 0x55, 0xaa }));389 try expect(mem.eql(u8, &@as([2]u8, down3), &[2]u8{ 0x55, 0xaa }));
390 }390 }
391391
392 fn doTheTestFloat() void {392 fn doTheTestFloat() !void {
393 var vec = @splat(2, @as(f32, 1234.0));393 var vec = @splat(2, @as(f32, 1234.0));
394 var wider: Vector(2, f64) = vec;394 var wider: Vector(2, f64) = vec;
395 expect(wider[0] == 1234.0);395 try expect(wider[0] == 1234.0);
396 expect(wider[1] == 1234.0);396 try expect(wider[1] == 1234.0);
397 }397 }
398 };398 };
399399
400 S.doTheTest();400 try S.doTheTest();
401 comptime S.doTheTest();401 comptime try S.doTheTest();
402 S.doTheTestFloat();402 try S.doTheTestFloat();
403 comptime S.doTheTestFloat();403 comptime try S.doTheTestFloat();
404}404}
405405
406test "comptime_int @intToFloat" {406test "comptime_int @intToFloat" {
407 {407 {
408 const result = @intToFloat(f16, 1234);408 const result = @intToFloat(f16, 1234);
409 expect(@TypeOf(result) == f16);409 try expect(@TypeOf(result) == f16);
410 expect(result == 1234.0);410 try expect(result == 1234.0);
411 }411 }
412 {412 {
413 const result = @intToFloat(f32, 1234);413 const result = @intToFloat(f32, 1234);
414 expect(@TypeOf(result) == f32);414 try expect(@TypeOf(result) == f32);
415 expect(result == 1234.0);415 try expect(result == 1234.0);
416 }416 }
417 {417 {
418 const result = @intToFloat(f64, 1234);418 const result = @intToFloat(f64, 1234);
419 expect(@TypeOf(result) == f64);419 try expect(@TypeOf(result) == f64);
420 expect(result == 1234.0);420 try expect(result == 1234.0);
421 }421 }
422 {422 {
423 const result = @intToFloat(f128, 1234);423 const result = @intToFloat(f128, 1234);
424 expect(@TypeOf(result) == f128);424 try expect(@TypeOf(result) == f128);
425 expect(result == 1234.0);425 try expect(result == 1234.0);
426 }426 }
427 // big comptime_int (> 64 bits) to f128 conversion427 // big comptime_int (> 64 bits) to f128 conversion
428 {428 {
429 const result = @intToFloat(f128, 0x1_0000_0000_0000_0000);429 const result = @intToFloat(f128, 0x1_0000_0000_0000_0000);
430 expect(@TypeOf(result) == f128);430 try expect(@TypeOf(result) == f128);
431 expect(result == 0x1_0000_0000_0000_0000.0);431 try expect(result == 0x1_0000_0000_0000_0000.0);
432 }432 }
433}433}
434434
...@@ -436,25 +436,25 @@ test "@intCast i32 to u7" {...@@ -436,25 +436,25 @@ test "@intCast i32 to u7" {
436 var x: u128 = maxInt(u128);436 var x: u128 = maxInt(u128);
437 var y: i32 = 120;437 var y: i32 = 120;
438 var z = x >> @intCast(u7, y);438 var z = x >> @intCast(u7, y);
439 expect(z == 0xff);439 try expect(z == 0xff);
440}440}
441441
442test "@floatCast cast down" {442test "@floatCast cast down" {
443 {443 {
444 var double: f64 = 0.001534;444 var double: f64 = 0.001534;
445 var single = @floatCast(f32, double);445 var single = @floatCast(f32, double);
446 expect(single == 0.001534);446 try expect(single == 0.001534);
447 }447 }
448 {448 {
449 const double: f64 = 0.001534;449 const double: f64 = 0.001534;
450 const single = @floatCast(f32, double);450 const single = @floatCast(f32, double);
451 expect(single == 0.001534);451 try expect(single == 0.001534);
452 }452 }
453}453}
454454
455test "implicit cast undefined to optional" {455test "implicit cast undefined to optional" {
456 expect(MakeType(void).getNull() == null);456 try expect(MakeType(void).getNull() == null);
457 expect(MakeType(void).getNonNull() != null);457 try expect(MakeType(void).getNonNull() != null);
458}458}
459459
460fn MakeType(comptime T: type) type {460fn MakeType(comptime T: type) type {
...@@ -474,26 +474,26 @@ test "implicit cast from *[N]T to ?[*]T" {...@@ -474,26 +474,26 @@ test "implicit cast from *[N]T to ?[*]T" {
474 var y: [4]u16 = [4]u16{ 0, 1, 2, 3 };474 var y: [4]u16 = [4]u16{ 0, 1, 2, 3 };
475475
476 x = &y;476 x = &y;
477 expect(std.mem.eql(u16, x.?[0..4], y[0..4]));477 try expect(std.mem.eql(u16, x.?[0..4], y[0..4]));
478 x.?[0] = 8;478 x.?[0] = 8;
479 y[3] = 6;479 y[3] = 6;
480 expect(std.mem.eql(u16, x.?[0..4], y[0..4]));480 try expect(std.mem.eql(u16, x.?[0..4], y[0..4]));
481}481}
482482
483test "implicit cast from *[N]T to [*c]T" {483test "implicit cast from *[N]T to [*c]T" {
484 var x: [4]u16 = [4]u16{ 0, 1, 2, 3 };484 var x: [4]u16 = [4]u16{ 0, 1, 2, 3 };
485 var y: [*c]u16 = &x;485 var y: [*c]u16 = &x;
486486
487 expect(std.mem.eql(u16, x[0..4], y[0..4]));487 try expect(std.mem.eql(u16, x[0..4], y[0..4]));
488 x[0] = 8;488 x[0] = 8;
489 y[3] = 6;489 y[3] = 6;
490 expect(std.mem.eql(u16, x[0..4], y[0..4]));490 try expect(std.mem.eql(u16, x[0..4], y[0..4]));
491}491}
492492
493test "implicit cast from *T to ?*c_void" {493test "implicit cast from *T to ?*c_void" {
494 var a: u8 = 1;494 var a: u8 = 1;
495 incrementVoidPtrValue(&a);495 incrementVoidPtrValue(&a);
496 std.testing.expect(a == 2);496 try std.testing.expect(a == 2);
497}497}
498498
499fn incrementVoidPtrValue(value: ?*c_void) void {499fn incrementVoidPtrValue(value: ?*c_void) void {
...@@ -504,7 +504,7 @@ test "implicit cast from [*]T to ?*c_void" {...@@ -504,7 +504,7 @@ test "implicit cast from [*]T to ?*c_void" {
504 var a = [_]u8{ 3, 2, 1 };504 var a = [_]u8{ 3, 2, 1 };
505 var runtime_zero: usize = 0;505 var runtime_zero: usize = 0;
506 incrementVoidPtrArray(a[runtime_zero..].ptr, 3);506 incrementVoidPtrArray(a[runtime_zero..].ptr, 3);
507 expect(std.mem.eql(u8, &a, &[_]u8{ 4, 3, 2 }));507 try expect(std.mem.eql(u8, &a, &[_]u8{ 4, 3, 2 }));
508}508}
509509
510fn incrementVoidPtrArray(array: ?*c_void, len: usize) void {510fn incrementVoidPtrArray(array: ?*c_void, len: usize) void {
...@@ -521,34 +521,34 @@ test "*usize to *void" {...@@ -521,34 +521,34 @@ test "*usize to *void" {
521}521}
522522
523test "compile time int to ptr of function" {523test "compile time int to ptr of function" {
524 foobar(FUNCTION_CONSTANT);524 try foobar(FUNCTION_CONSTANT);
525}525}
526526
527pub const FUNCTION_CONSTANT = @intToPtr(PFN_void, maxInt(usize));527pub const FUNCTION_CONSTANT = @intToPtr(PFN_void, maxInt(usize));
528pub const PFN_void = fn (*c_void) callconv(.C) void;528pub const PFN_void = fn (*c_void) callconv(.C) void;
529529
530fn foobar(func: PFN_void) void {530fn foobar(func: PFN_void) !void {
531 std.testing.expect(@ptrToInt(func) == maxInt(usize));531 try std.testing.expect(@ptrToInt(func) == maxInt(usize));
532}532}
533533
534test "implicit ptr to *c_void" {534test "implicit ptr to *c_void" {
535 var a: u32 = 1;535 var a: u32 = 1;
536 var ptr: *align(@alignOf(u32)) c_void = &a;536 var ptr: *align(@alignOf(u32)) c_void = &a;
537 var b: *u32 = @ptrCast(*u32, ptr);537 var b: *u32 = @ptrCast(*u32, ptr);
538 expect(b.* == 1);538 try expect(b.* == 1);
539 var ptr2: ?*align(@alignOf(u32)) c_void = &a;539 var ptr2: ?*align(@alignOf(u32)) c_void = &a;
540 var c: *u32 = @ptrCast(*u32, ptr2.?);540 var c: *u32 = @ptrCast(*u32, ptr2.?);
541 expect(c.* == 1);541 try expect(c.* == 1);
542}542}
543543
544test "@intCast to comptime_int" {544test "@intCast to comptime_int" {
545 expect(@intCast(comptime_int, 0) == 0);545 try expect(@intCast(comptime_int, 0) == 0);
546}546}
547547
548test "implicit cast comptime numbers to any type when the value fits" {548test "implicit cast comptime numbers to any type when the value fits" {
549 const a: u64 = 255;549 const a: u64 = 255;
550 var b: u8 = a;550 var b: u8 = a;
551 expect(b == 255);551 try expect(b == 255);
552}552}
553553
554test "@intToEnum passed a comptime_int to an enum with one item" {554test "@intToEnum passed a comptime_int to an enum with one item" {
...@@ -556,7 +556,7 @@ test "@intToEnum passed a comptime_int to an enum with one item" {...@@ -556,7 +556,7 @@ test "@intToEnum passed a comptime_int to an enum with one item" {
556 A,556 A,
557 };557 };
558 const x = @intToEnum(E, 0);558 const x = @intToEnum(E, 0);
559 expect(x == E.A);559 try expect(x == E.A);
560}560}
561561
562test "@intToEnum runtime to an extern enum with duplicate values" {562test "@intToEnum runtime to an extern enum with duplicate values" {
...@@ -566,33 +566,33 @@ test "@intToEnum runtime to an extern enum with duplicate values" {...@@ -566,33 +566,33 @@ test "@intToEnum runtime to an extern enum with duplicate values" {
566 };566 };
567 var a: u8 = 1;567 var a: u8 = 1;
568 var x = @intToEnum(E, a);568 var x = @intToEnum(E, a);
569 expect(x == E.A);569 try expect(x == E.A);
570 expect(x == E.B);570 try expect(x == E.B);
571}571}
572572
573test "@intCast to u0 and use the result" {573test "@intCast to u0 and use the result" {
574 const S = struct {574 const S = struct {
575 fn doTheTest(zero: u1, one: u1, bigzero: i32) void {575 fn doTheTest(zero: u1, one: u1, bigzero: i32) !void {
576 expect((one << @intCast(u0, bigzero)) == 1);576 try expect((one << @intCast(u0, bigzero)) == 1);
577 expect((zero << @intCast(u0, bigzero)) == 0);577 try expect((zero << @intCast(u0, bigzero)) == 0);
578 }578 }
579 };579 };
580 S.doTheTest(0, 1, 0);580 try S.doTheTest(0, 1, 0);
581 comptime S.doTheTest(0, 1, 0);581 comptime try S.doTheTest(0, 1, 0);
582}582}
583583
584test "peer type resolution: unreachable, null, slice" {584test "peer type resolution: unreachable, null, slice" {
585 const S = struct {585 const S = struct {
586 fn doTheTest(num: usize, word: []const u8) void {586 fn doTheTest(num: usize, word: []const u8) !void {
587 const result = switch (num) {587 const result = switch (num) {
588 0 => null,588 0 => null,
589 1 => word,589 1 => word,
590 else => unreachable,590 else => unreachable,
591 };591 };
592 expect(mem.eql(u8, result.?, "hi"));592 try expect(mem.eql(u8, result.?, "hi"));
593 }593 }
594 };594 };
595 S.doTheTest(1, "hi");595 try S.doTheTest(1, "hi");
596}596}
597597
598test "peer type resolution: unreachable, error set, unreachable" {598test "peer type resolution: unreachable, error set, unreachable" {
...@@ -615,17 +615,17 @@ test "peer type resolution: unreachable, error set, unreachable" {...@@ -615,17 +615,17 @@ test "peer type resolution: unreachable, error set, unreachable" {
615 error.FileDescriptorIncompatibleWithEpoll => unreachable,615 error.FileDescriptorIncompatibleWithEpoll => unreachable,
616 error.Unexpected => unreachable,616 error.Unexpected => unreachable,
617 };617 };
618 expect(transformed_err == error.SystemResources);618 try expect(transformed_err == error.SystemResources);
619}619}
620620
621test "implicit cast comptime_int to comptime_float" {621test "implicit cast comptime_int to comptime_float" {
622 comptime expect(@as(comptime_float, 10) == @as(f32, 10));622 comptime try expect(@as(comptime_float, 10) == @as(f32, 10));
623 expect(2 == 2.0);623 try expect(2 == 2.0);
624}624}
625625
626test "implicit cast *[0]T to E![]const u8" {626test "implicit cast *[0]T to E![]const u8" {
627 var x = @as(anyerror![]const u8, &[0]u8{});627 var x = @as(anyerror![]const u8, &[0]u8{});
628 expect((x catch unreachable).len == 0);628 try expect((x catch unreachable).len == 0);
629}629}
630630
631test "peer cast *[0]T to E![]const T" {631test "peer cast *[0]T to E![]const T" {
...@@ -633,7 +633,7 @@ test "peer cast *[0]T to E![]const T" {...@@ -633,7 +633,7 @@ test "peer cast *[0]T to E![]const T" {
633 var buf: anyerror![]const u8 = buffer[0..];633 var buf: anyerror![]const u8 = buffer[0..];
634 var b = false;634 var b = false;
635 var y = if (b) &[0]u8{} else buf;635 var y = if (b) &[0]u8{} else buf;
636 expect(mem.eql(u8, "abcde", y catch unreachable));636 try expect(mem.eql(u8, "abcde", y catch unreachable));
637}637}
638638
639test "peer cast *[0]T to []const T" {639test "peer cast *[0]T to []const T" {
...@@ -641,25 +641,25 @@ test "peer cast *[0]T to []const T" {...@@ -641,25 +641,25 @@ test "peer cast *[0]T to []const T" {
641 var buf: []const u8 = buffer[0..];641 var buf: []const u8 = buffer[0..];
642 var b = false;642 var b = false;
643 var y = if (b) &[0]u8{} else buf;643 var y = if (b) &[0]u8{} else buf;
644 expect(mem.eql(u8, "abcde", y));644 try expect(mem.eql(u8, "abcde", y));
645}645}
646646
647var global_array: [4]u8 = undefined;647var global_array: [4]u8 = undefined;
648test "cast from array reference to fn" {648test "cast from array reference to fn" {
649 const f = @ptrCast(fn () callconv(.C) void, &global_array);649 const f = @ptrCast(fn () callconv(.C) void, &global_array);
650 expect(@ptrToInt(f) == @ptrToInt(&global_array));650 try expect(@ptrToInt(f) == @ptrToInt(&global_array));
651}651}
652652
653test "*const [N]null u8 to ?[]const u8" {653test "*const [N]null u8 to ?[]const u8" {
654 const S = struct {654 const S = struct {
655 fn doTheTest() void {655 fn doTheTest() !void {
656 var a = "Hello";656 var a = "Hello";
657 var b: ?[]const u8 = a;657 var b: ?[]const u8 = a;
658 expect(mem.eql(u8, b.?, "Hello"));658 try expect(mem.eql(u8, b.?, "Hello"));
659 }659 }
660 };660 };
661 S.doTheTest();661 try S.doTheTest();
662 comptime S.doTheTest();662 comptime try S.doTheTest();
663}663}
664664
665test "peer resolution of string literals" {665test "peer resolution of string literals" {
...@@ -671,54 +671,54 @@ test "peer resolution of string literals" {...@@ -671,54 +671,54 @@ test "peer resolution of string literals" {
671 d,671 d,
672 };672 };
673673
674 fn doTheTest(e: E) void {674 fn doTheTest(e: E) !void {
675 const cmd = switch (e) {675 const cmd = switch (e) {
676 .a => "one",676 .a => "one",
677 .b => "two",677 .b => "two",
678 .c => "three",678 .c => "three",
679 .d => "four",679 .d => "four",
680 };680 };
681 expect(mem.eql(u8, cmd, "two"));681 try expect(mem.eql(u8, cmd, "two"));
682 }682 }
683 };683 };
684 S.doTheTest(.b);684 try S.doTheTest(.b);
685 comptime S.doTheTest(.b);685 comptime try S.doTheTest(.b);
686}686}
687687
688test "type coercion related to sentinel-termination" {688test "type coercion related to sentinel-termination" {
689 const S = struct {689 const S = struct {
690 fn doTheTest() void {690 fn doTheTest() !void {
691 // [:x]T to []T691 // [:x]T to []T
692 {692 {
693 var array = [4:0]i32{ 1, 2, 3, 4 };693 var array = [4:0]i32{ 1, 2, 3, 4 };
694 var slice: [:0]i32 = &array;694 var slice: [:0]i32 = &array;
695 var dest: []i32 = slice;695 var dest: []i32 = slice;
696 expect(mem.eql(i32, dest, &[_]i32{ 1, 2, 3, 4 }));696 try expect(mem.eql(i32, dest, &[_]i32{ 1, 2, 3, 4 }));
697 }697 }
698698
699 // [*:x]T to [*]T699 // [*:x]T to [*]T
700 {700 {
701 var array = [4:99]i32{ 1, 2, 3, 4 };701 var array = [4:99]i32{ 1, 2, 3, 4 };
702 var dest: [*]i32 = &array;702 var dest: [*]i32 = &array;
703 expect(dest[0] == 1);703 try expect(dest[0] == 1);
704 expect(dest[1] == 2);704 try expect(dest[1] == 2);
705 expect(dest[2] == 3);705 try expect(dest[2] == 3);
706 expect(dest[3] == 4);706 try expect(dest[3] == 4);
707 expect(dest[4] == 99);707 try expect(dest[4] == 99);
708 }708 }
709709
710 // [N:x]T to [N]T710 // [N:x]T to [N]T
711 {711 {
712 var array = [4:0]i32{ 1, 2, 3, 4 };712 var array = [4:0]i32{ 1, 2, 3, 4 };
713 var dest: [4]i32 = array;713 var dest: [4]i32 = array;
714 expect(mem.eql(i32, &dest, &[_]i32{ 1, 2, 3, 4 }));714 try expect(mem.eql(i32, &dest, &[_]i32{ 1, 2, 3, 4 }));
715 }715 }
716716
717 // *[N:x]T to *[N]T717 // *[N:x]T to *[N]T
718 {718 {
719 var array = [4:0]i32{ 1, 2, 3, 4 };719 var array = [4:0]i32{ 1, 2, 3, 4 };
720 var dest: *[4]i32 = &array;720 var dest: *[4]i32 = &array;
721 expect(mem.eql(i32, dest, &[_]i32{ 1, 2, 3, 4 }));721 try expect(mem.eql(i32, dest, &[_]i32{ 1, 2, 3, 4 }));
722 }722 }
723723
724 // [:x]T to [*:x]T724 // [:x]T to [*:x]T
...@@ -726,24 +726,24 @@ test "type coercion related to sentinel-termination" {...@@ -726,24 +726,24 @@ test "type coercion related to sentinel-termination" {
726 var array = [4:0]i32{ 1, 2, 3, 4 };726 var array = [4:0]i32{ 1, 2, 3, 4 };
727 var slice: [:0]i32 = &array;727 var slice: [:0]i32 = &array;
728 var dest: [*:0]i32 = slice;728 var dest: [*:0]i32 = slice;
729 expect(dest[0] == 1);729 try expect(dest[0] == 1);
730 expect(dest[1] == 2);730 try expect(dest[1] == 2);
731 expect(dest[2] == 3);731 try expect(dest[2] == 3);
732 expect(dest[3] == 4);732 try expect(dest[3] == 4);
733 expect(dest[4] == 0);733 try expect(dest[4] == 0);
734 }734 }
735 }735 }
736 };736 };
737 S.doTheTest();737 try S.doTheTest();
738 comptime S.doTheTest();738 comptime try S.doTheTest();
739}739}
740740
741test "cast i8 fn call peers to i32 result" {741test "cast i8 fn call peers to i32 result" {
742 const S = struct {742 const S = struct {
743 fn doTheTest() void {743 fn doTheTest() !void {
744 var cond = true;744 var cond = true;
745 const value: i32 = if (cond) smallBoi() else bigBoi();745 const value: i32 = if (cond) smallBoi() else bigBoi();
746 expect(value == 123);746 try expect(value == 123);
747 }747 }
748 fn smallBoi() i8 {748 fn smallBoi() i8 {
749 return 123;749 return 123;
...@@ -752,21 +752,21 @@ test "cast i8 fn call peers to i32 result" {...@@ -752,21 +752,21 @@ test "cast i8 fn call peers to i32 result" {
752 return 1234;752 return 1234;
753 }753 }
754 };754 };
755 S.doTheTest();755 try S.doTheTest();
756 comptime S.doTheTest();756 comptime try S.doTheTest();
757}757}
758758
759test "return u8 coercing into ?u32 return type" {759test "return u8 coercing into ?u32 return type" {
760 const S = struct {760 const S = struct {
761 fn doTheTest() void {761 fn doTheTest() !void {
762 expect(foo(123).? == 123);762 try expect(foo(123).? == 123);
763 }763 }
764 fn foo(arg: u8) ?u32 {764 fn foo(arg: u8) ?u32 {
765 return arg;765 return arg;
766 }766 }
767 };767 };
768 S.doTheTest();768 try S.doTheTest();
769 comptime S.doTheTest();769 comptime try S.doTheTest();
770}770}
771771
772test "peer result null and comptime_int" {772test "peer result null and comptime_int" {
...@@ -782,17 +782,17 @@ test "peer result null and comptime_int" {...@@ -782,17 +782,17 @@ test "peer result null and comptime_int" {
782 }782 }
783 };783 };
784784
785 expect(S.blah(0) == null);785 try expect(S.blah(0) == null);
786 comptime expect(S.blah(0) == null);786 comptime try expect(S.blah(0) == null);
787 expect(S.blah(10).? == 1);787 try expect(S.blah(10).? == 1);
788 comptime expect(S.blah(10).? == 1);788 comptime try expect(S.blah(10).? == 1);
789 expect(S.blah(-10).? == -1);789 try expect(S.blah(-10).? == -1);
790 comptime expect(S.blah(-10).? == -1);790 comptime try expect(S.blah(-10).? == -1);
791}791}
792792
793test "peer type resolution implicit cast to return type" {793test "peer type resolution implicit cast to return type" {
794 const S = struct {794 const S = struct {
795 fn doTheTest() void {795 fn doTheTest() !void {
796 for ("hello") |c| _ = f(c);796 for ("hello") |c| _ = f(c);
797 }797 }
798 fn f(c: u8) []const u8 {798 fn f(c: u8) []const u8 {
...@@ -803,13 +803,13 @@ test "peer type resolution implicit cast to return type" {...@@ -803,13 +803,13 @@ test "peer type resolution implicit cast to return type" {
803 };803 };
804 }804 }
805 };805 };
806 S.doTheTest();806 try S.doTheTest();
807 comptime S.doTheTest();807 comptime try S.doTheTest();
808}808}
809809
810test "peer type resolution implicit cast to variable type" {810test "peer type resolution implicit cast to variable type" {
811 const S = struct {811 const S = struct {
812 fn doTheTest() void {812 fn doTheTest() !void {
813 var x: []const u8 = undefined;813 var x: []const u8 = undefined;
814 for ("hello") |c| x = switch (c) {814 for ("hello") |c| x = switch (c) {
815 'h', 'e' => &[_]u8{c}, // should cast to slice815 'h', 'e' => &[_]u8{c}, // should cast to slice
...@@ -818,14 +818,14 @@ test "peer type resolution implicit cast to variable type" {...@@ -818,14 +818,14 @@ test "peer type resolution implicit cast to variable type" {
818 };818 };
819 }819 }
820 };820 };
821 S.doTheTest();821 try S.doTheTest();
822 comptime S.doTheTest();822 comptime try S.doTheTest();
823}823}
824824
825test "variable initialization uses result locations properly with regards to the type" {825test "variable initialization uses result locations properly with regards to the type" {
826 var b = true;826 var b = true;
827 const x: i32 = if (b) 1 else 2;827 const x: i32 = if (b) 1 else 2;
828 expect(x == 1);828 try expect(x == 1);
829}829}
830830
831test "cast between [*c]T and ?[*:0]T on fn parameter" {831test "cast between [*c]T and ?[*:0]T on fn parameter" {
...@@ -847,27 +847,27 @@ test "cast between C pointer with different but compatible types" {...@@ -847,27 +847,27 @@ test "cast between C pointer with different but compatible types" {
847 fn foo(arg: [*]c_ushort) u16 {847 fn foo(arg: [*]c_ushort) u16 {
848 return arg[0];848 return arg[0];
849 }849 }
850 fn doTheTest() void {850 fn doTheTest() !void {
851 var x = [_]u16{ 4, 2, 1, 3 };851 var x = [_]u16{ 4, 2, 1, 3 };
852 expect(foo(@ptrCast([*]u16, &x)) == 4);852 try expect(foo(@ptrCast([*]u16, &x)) == 4);
853 }853 }
854 };854 };
855 S.doTheTest();855 try S.doTheTest();
856}856}
857857
858var global_struct: struct { f0: usize } = undefined;858var global_struct: struct { f0: usize } = undefined;
859859
860test "assignment to optional pointer result loc" {860test "assignment to optional pointer result loc" {
861 var foo: struct { ptr: ?*c_void } = .{ .ptr = &global_struct };861 var foo: struct { ptr: ?*c_void } = .{ .ptr = &global_struct };
862 expect(foo.ptr.? == @ptrCast(*c_void, &global_struct));862 try expect(foo.ptr.? == @ptrCast(*c_void, &global_struct));
863}863}
864864
865test "peer type resolve string lit with sentinel-terminated mutable slice" {865test "peer type resolve string lit with sentinel-terminated mutable slice" {
866 var array: [4:0]u8 = undefined;866 var array: [4:0]u8 = undefined;
867 array[4] = 0; // TODO remove this when #4372 is solved867 array[4] = 0; // TODO remove this when #4372 is solved
868 var slice: [:0]u8 = array[0..4 :0];868 var slice: [:0]u8 = array[0..4 :0];
869 comptime expect(@TypeOf(slice, "hi") == [:0]const u8);869 comptime try expect(@TypeOf(slice, "hi") == [:0]const u8);
870 comptime expect(@TypeOf("hi", slice) == [:0]const u8);870 comptime try expect(@TypeOf("hi", slice) == [:0]const u8);
871}871}
872872
873test "peer type unsigned int to signed" {873test "peer type unsigned int to signed" {
...@@ -875,15 +875,15 @@ test "peer type unsigned int to signed" {...@@ -875,15 +875,15 @@ test "peer type unsigned int to signed" {
875 var x: u8 = 7;875 var x: u8 = 7;
876 var y: i32 = -5;876 var y: i32 = -5;
877 var a = w + y + x;877 var a = w + y + x;
878 comptime expect(@TypeOf(a) == i32);878 comptime try expect(@TypeOf(a) == i32);
879 expect(a == 7);879 try expect(a == 7);
880}880}
881881
882test "peer type resolve array pointers, one of them const" {882test "peer type resolve array pointers, one of them const" {
883 var array1: [4]u8 = undefined;883 var array1: [4]u8 = undefined;
884 const array2: [5]u8 = undefined;884 const array2: [5]u8 = undefined;
885 comptime expect(@TypeOf(&array1, &array2) == []const u8);885 comptime try expect(@TypeOf(&array1, &array2) == []const u8);
886 comptime expect(@TypeOf(&array2, &array1) == []const u8);886 comptime try expect(@TypeOf(&array2, &array1) == []const u8);
887}887}
888888
889test "peer type resolve array pointer and unknown pointer" {889test "peer type resolve array pointer and unknown pointer" {
...@@ -892,35 +892,35 @@ test "peer type resolve array pointer and unknown pointer" {...@@ -892,35 +892,35 @@ test "peer type resolve array pointer and unknown pointer" {
892 var const_ptr: [*]const u8 = undefined;892 var const_ptr: [*]const u8 = undefined;
893 var ptr: [*]u8 = undefined;893 var ptr: [*]u8 = undefined;
894894
895 comptime expect(@TypeOf(&array, ptr) == [*]u8);895 comptime try expect(@TypeOf(&array, ptr) == [*]u8);
896 comptime expect(@TypeOf(ptr, &array) == [*]u8);896 comptime try expect(@TypeOf(ptr, &array) == [*]u8);
897897
898 comptime expect(@TypeOf(&const_array, ptr) == [*]const u8);898 comptime try expect(@TypeOf(&const_array, ptr) == [*]const u8);
899 comptime expect(@TypeOf(ptr, &const_array) == [*]const u8);899 comptime try expect(@TypeOf(ptr, &const_array) == [*]const u8);
900900
901 comptime expect(@TypeOf(&array, const_ptr) == [*]const u8);901 comptime try expect(@TypeOf(&array, const_ptr) == [*]const u8);
902 comptime expect(@TypeOf(const_ptr, &array) == [*]const u8);902 comptime try expect(@TypeOf(const_ptr, &array) == [*]const u8);
903903
904 comptime expect(@TypeOf(&const_array, const_ptr) == [*]const u8);904 comptime try expect(@TypeOf(&const_array, const_ptr) == [*]const u8);
905 comptime expect(@TypeOf(const_ptr, &const_array) == [*]const u8);905 comptime try expect(@TypeOf(const_ptr, &const_array) == [*]const u8);
906}906}
907907
908test "comptime float casts" {908test "comptime float casts" {
909 const a = @intToFloat(comptime_float, 1);909 const a = @intToFloat(comptime_float, 1);
910 expect(a == 1);910 try expect(a == 1);
911 expect(@TypeOf(a) == comptime_float);911 try expect(@TypeOf(a) == comptime_float);
912 const b = @floatToInt(comptime_int, 2);912 const b = @floatToInt(comptime_int, 2);
913 expect(b == 2);913 try expect(b == 2);
914 expect(@TypeOf(b) == comptime_int);914 try expect(@TypeOf(b) == comptime_int);
915}915}
916916
917test "cast from ?[*]T to ??[*]T" {917test "cast from ?[*]T to ??[*]T" {
918 const a: ??[*]u8 = @as(?[*]u8, null);918 const a: ??[*]u8 = @as(?[*]u8, null);
919 expect(a != null and a.? == null);919 try expect(a != null and a.? == null);
920}920}
921921
922test "cast between *[N]void and []void" {922test "cast between *[N]void and []void" {
923 var a: [4]void = undefined;923 var a: [4]void = undefined;
924 var b: []void = &a;924 var b: []void = &a;
925 expect(b.len == 4);925 try expect(b.len == 4);
926}926}
test/stage1/behavior/const_slice_child.zig+8-8
...@@ -12,24 +12,24 @@ test "const slice child" {...@@ -12,24 +12,24 @@ test "const slice child" {
12 "three",12 "three",
13 };13 };
14 argv = &strs;14 argv = &strs;
15 bar(strs.len);15 try bar(strs.len);
16}16}
1717
18fn foo(args: [][]const u8) void {18fn foo(args: [][]const u8) !void {
19 expect(args.len == 3);19 try expect(args.len == 3);
20 expect(streql(args[0], "one"));20 try expect(streql(args[0], "one"));
21 expect(streql(args[1], "two"));21 try expect(streql(args[1], "two"));
22 expect(streql(args[2], "three"));22 try expect(streql(args[2], "three"));
23}23}
2424
25fn bar(argc: usize) void {25fn bar(argc: usize) !void {
26 const args = testing.allocator.alloc([]const u8, argc) catch unreachable;26 const args = testing.allocator.alloc([]const u8, argc) catch unreachable;
27 defer testing.allocator.free(args);27 defer testing.allocator.free(args);
28 for (args) |_, i| {28 for (args) |_, i| {
29 const ptr = argv[i];29 const ptr = argv[i];
30 args[i] = ptr[0..strlen(ptr)];30 args[i] = ptr[0..strlen(ptr)];
31 }31 }
32 foo(args);32 try foo(args);
33}33}
3434
35fn strlen(ptr: [*]const u8) usize {35fn strlen(ptr: [*]const u8) usize {
test/stage1/behavior/defer.zig+20-20
...@@ -24,18 +24,18 @@ fn runSomeErrorDefers(x: bool) !bool {...@@ -24,18 +24,18 @@ fn runSomeErrorDefers(x: bool) !bool {
24}24}
2525
26test "mixing normal and error defers" {26test "mixing normal and error defers" {
27 expect(runSomeErrorDefers(true) catch unreachable);27 try expect(runSomeErrorDefers(true) catch unreachable);
28 expect(result[0] == 'c');28 try expect(result[0] == 'c');
29 expect(result[1] == 'a');29 try expect(result[1] == 'a');
3030
31 const ok = runSomeErrorDefers(false) catch |err| x: {31 const ok = runSomeErrorDefers(false) catch |err| x: {
32 expect(err == error.FalseNotAllowed);32 try expect(err == error.FalseNotAllowed);
33 break :x true;33 break :x true;
34 };34 };
35 expect(ok);35 try expect(ok);
36 expect(result[0] == 'c');36 try expect(result[0] == 'c');
37 expect(result[1] == 'b');37 try expect(result[1] == 'b');
38 expect(result[2] == 'a');38 try expect(result[2] == 'a');
39}39}
4040
41test "break and continue inside loop inside defer expression" {41test "break and continue inside loop inside defer expression" {
...@@ -50,7 +50,7 @@ fn testBreakContInDefer(x: usize) void {...@@ -50,7 +50,7 @@ fn testBreakContInDefer(x: usize) void {
50 if (i < 5) continue;50 if (i < 5) continue;
51 if (i == 5) break;51 if (i == 5) break;
52 }52 }
53 expect(i == 5);53 expect(i == 5) catch @panic("test failure");
54 }54 }
55}55}
5656
...@@ -62,11 +62,11 @@ test "defer and labeled break" {...@@ -62,11 +62,11 @@ test "defer and labeled break" {
62 break :blk;62 break :blk;
63 }63 }
6464
65 expect(i == 1);65 try expect(i == 1);
66}66}
6767
68test "errdefer does not apply to fn inside fn" {68test "errdefer does not apply to fn inside fn" {
69 if (testNestedFnErrDefer()) |_| @panic("expected error") else |e| expect(e == error.Bad);69 if (testNestedFnErrDefer()) |_| @panic("expected error") else |e| try expect(e == error.Bad);
70}70}
7171
72fn testNestedFnErrDefer() anyerror!void {72fn testNestedFnErrDefer() anyerror!void {
...@@ -82,8 +82,8 @@ fn testNestedFnErrDefer() anyerror!void {...@@ -82,8 +82,8 @@ fn testNestedFnErrDefer() anyerror!void {
8282
83test "return variable while defer expression in scope to modify it" {83test "return variable while defer expression in scope to modify it" {
84 const S = struct {84 const S = struct {
85 fn doTheTest() void {85 fn doTheTest() !void {
86 expect(notNull().? == 1);86 try expect(notNull().? == 1);
87 }87 }
8888
89 fn notNull() ?u8 {89 fn notNull() ?u8 {
...@@ -93,22 +93,22 @@ test "return variable while defer expression in scope to modify it" {...@@ -93,22 +93,22 @@ test "return variable while defer expression in scope to modify it" {
93 }93 }
94 };94 };
9595
96 S.doTheTest();96 try S.doTheTest();
97 comptime S.doTheTest();97 comptime try S.doTheTest();
98}98}
9999
100test "errdefer with payload" {100test "errdefer with payload" {
101 const S = struct {101 const S = struct {
102 fn foo() !i32 {102 fn foo() !i32 {
103 errdefer |a| {103 errdefer |a| {
104 expectEqual(error.One, a);104 expectEqual(error.One, a) catch @panic("test failure");
105 }105 }
106 return error.One;106 return error.One;
107 }107 }
108 fn doTheTest() void {108 fn doTheTest() !void {
109 expectError(error.One, foo());109 try expectError(error.One, foo());
110 }110 }
111 };111 };
112 S.doTheTest();112 try S.doTheTest();
113 comptime S.doTheTest();113 comptime try S.doTheTest();
114}114}
test/stage1/behavior/enum.zig+112-112
...@@ -29,41 +29,41 @@ test "non-exhaustive enum" {...@@ -29,41 +29,41 @@ test "non-exhaustive enum" {
29 b,29 b,
30 _,30 _,
31 };31 };
32 fn doTheTest(y: u8) void {32 fn doTheTest(y: u8) !void {
33 var e: E = .b;33 var e: E = .b;
34 expect(switch (e) {34 try expect(switch (e) {
35 .a => false,35 .a => false,
36 .b => true,36 .b => true,
37 _ => false,37 _ => false,
38 });38 });
39 e = @intToEnum(E, 12);39 e = @intToEnum(E, 12);
40 expect(switch (e) {40 try expect(switch (e) {
41 .a => false,41 .a => false,
42 .b => false,42 .b => false,
43 _ => true,43 _ => true,
44 });44 });
4545
46 expect(switch (e) {46 try expect(switch (e) {
47 .a => false,47 .a => false,
48 .b => false,48 .b => false,
49 else => true,49 else => true,
50 });50 });
51 e = .b;51 e = .b;
52 expect(switch (e) {52 try expect(switch (e) {
53 .a => false,53 .a => false,
54 else => true,54 else => true,
55 });55 });
5656
57 expect(@typeInfo(E).Enum.fields.len == 2);57 try expect(@typeInfo(E).Enum.fields.len == 2);
58 e = @intToEnum(E, 12);58 e = @intToEnum(E, 12);
59 expect(@enumToInt(e) == 12);59 try expect(@enumToInt(e) == 12);
60 e = @intToEnum(E, y);60 e = @intToEnum(E, y);
61 expect(@enumToInt(e) == 52);61 try expect(@enumToInt(e) == 52);
62 expect(@typeInfo(E).Enum.is_exhaustive == false);62 try expect(@typeInfo(E).Enum.is_exhaustive == false);
63 }63 }
64 };64 };
65 S.doTheTest(52);65 try S.doTheTest(52);
66 comptime S.doTheTest(52);66 comptime try S.doTheTest(52);
67}67}
6868
69test "empty non-exhaustive enum" {69test "empty non-exhaustive enum" {
...@@ -71,19 +71,19 @@ test "empty non-exhaustive enum" {...@@ -71,19 +71,19 @@ test "empty non-exhaustive enum" {
71 const E = enum(u8) {71 const E = enum(u8) {
72 _,72 _,
73 };73 };
74 fn doTheTest(y: u8) void {74 fn doTheTest(y: u8) !void {
75 var e = @intToEnum(E, y);75 var e = @intToEnum(E, y);
76 expect(switch (e) {76 try expect(switch (e) {
77 _ => true,77 _ => true,
78 });78 });
79 expect(@enumToInt(e) == y);79 try expect(@enumToInt(e) == y);
8080
81 expect(@typeInfo(E).Enum.fields.len == 0);81 try expect(@typeInfo(E).Enum.fields.len == 0);
82 expect(@typeInfo(E).Enum.is_exhaustive == false);82 try expect(@typeInfo(E).Enum.is_exhaustive == false);
83 }83 }
84 };84 };
85 S.doTheTest(42);85 try S.doTheTest(42);
86 comptime S.doTheTest(42);86 comptime try S.doTheTest(42);
87}87}
8888
89test "single field non-exhaustive enum" {89test "single field non-exhaustive enum" {
...@@ -92,35 +92,35 @@ test "single field non-exhaustive enum" {...@@ -92,35 +92,35 @@ test "single field non-exhaustive enum" {
92 a,92 a,
93 _,93 _,
94 };94 };
95 fn doTheTest(y: u8) void {95 fn doTheTest(y: u8) !void {
96 var e: E = .a;96 var e: E = .a;
97 expect(switch (e) {97 try expect(switch (e) {
98 .a => true,98 .a => true,
99 _ => false,99 _ => false,
100 });100 });
101 e = @intToEnum(E, 12);101 e = @intToEnum(E, 12);
102 expect(switch (e) {102 try expect(switch (e) {
103 .a => false,103 .a => false,
104 _ => true,104 _ => true,
105 });105 });
106106
107 expect(switch (e) {107 try expect(switch (e) {
108 .a => false,108 .a => false,
109 else => true,109 else => true,
110 });110 });
111 e = .a;111 e = .a;
112 expect(switch (e) {112 try expect(switch (e) {
113 .a => true,113 .a => true,
114 else => false,114 else => false,
115 });115 });
116116
117 expect(@enumToInt(@intToEnum(E, y)) == y);117 try expect(@enumToInt(@intToEnum(E, y)) == y);
118 expect(@typeInfo(E).Enum.fields.len == 1);118 try expect(@typeInfo(E).Enum.fields.len == 1);
119 expect(@typeInfo(E).Enum.is_exhaustive == false);119 try expect(@typeInfo(E).Enum.is_exhaustive == false);
120 }120 }
121 };121 };
122 S.doTheTest(23);122 try S.doTheTest(23);
123 comptime S.doTheTest(23);123 comptime try S.doTheTest(23);
124}124}
125125
126test "enum type" {126test "enum type" {
...@@ -133,16 +133,16 @@ test "enum type" {...@@ -133,16 +133,16 @@ test "enum type" {
133 };133 };
134 const bar = Bar.B;134 const bar = Bar.B;
135135
136 expect(bar == Bar.B);136 try expect(bar == Bar.B);
137 expect(@typeInfo(Foo).Union.fields.len == 3);137 try expect(@typeInfo(Foo).Union.fields.len == 3);
138 expect(@typeInfo(Bar).Enum.fields.len == 4);138 try expect(@typeInfo(Bar).Enum.fields.len == 4);
139 expect(@sizeOf(Foo) == @sizeOf(FooNoVoid));139 try expect(@sizeOf(Foo) == @sizeOf(FooNoVoid));
140 expect(@sizeOf(Bar) == 1);140 try expect(@sizeOf(Bar) == 1);
141}141}
142142
143test "enum as return value" {143test "enum as return value" {
144 switch (returnAnInt(13)) {144 switch (returnAnInt(13)) {
145 Foo.One => |value| expect(value == 13),145 Foo.One => |value| try expect(value == 13),
146 else => unreachable,146 else => unreachable,
147 }147 }
148}148}
...@@ -206,22 +206,22 @@ const Number = enum {...@@ -206,22 +206,22 @@ const Number = enum {
206};206};
207207
208test "enum to int" {208test "enum to int" {
209 shouldEqual(Number.Zero, 0);209 try shouldEqual(Number.Zero, 0);
210 shouldEqual(Number.One, 1);210 try shouldEqual(Number.One, 1);
211 shouldEqual(Number.Two, 2);211 try shouldEqual(Number.Two, 2);
212 shouldEqual(Number.Three, 3);212 try shouldEqual(Number.Three, 3);
213 shouldEqual(Number.Four, 4);213 try shouldEqual(Number.Four, 4);
214}214}
215215
216fn shouldEqual(n: Number, expected: u3) void {216fn shouldEqual(n: Number, expected: u3) !void {
217 expect(@enumToInt(n) == expected);217 try expect(@enumToInt(n) == expected);
218}218}
219219
220test "int to enum" {220test "int to enum" {
221 testIntToEnumEval(3);221 try testIntToEnumEval(3);
222}222}
223fn testIntToEnumEval(x: i32) void {223fn testIntToEnumEval(x: i32) !void {
224 expect(@intToEnum(IntToEnumNumber, @intCast(u3, x)) == IntToEnumNumber.Three);224 try expect(@intToEnum(IntToEnumNumber, @intCast(u3, x)) == IntToEnumNumber.Three);
225}225}
226const IntToEnumNumber = enum {226const IntToEnumNumber = enum {
227 Zero,227 Zero,
...@@ -232,18 +232,18 @@ const IntToEnumNumber = enum {...@@ -232,18 +232,18 @@ const IntToEnumNumber = enum {
232};232};
233233
234test "@tagName" {234test "@tagName" {
235 expect(mem.eql(u8, testEnumTagNameBare(BareNumber.Three), "Three"));235 try expect(mem.eql(u8, testEnumTagNameBare(BareNumber.Three), "Three"));
236 comptime expect(mem.eql(u8, testEnumTagNameBare(BareNumber.Three), "Three"));236 comptime try expect(mem.eql(u8, testEnumTagNameBare(BareNumber.Three), "Three"));
237}237}
238238
239test "@tagName extern enum with duplicates" {239test "@tagName extern enum with duplicates" {
240 expect(mem.eql(u8, testEnumTagNameBare(ExternDuplicates.B), "A"));240 try expect(mem.eql(u8, testEnumTagNameBare(ExternDuplicates.B), "A"));
241 comptime expect(mem.eql(u8, testEnumTagNameBare(ExternDuplicates.B), "A"));241 comptime try expect(mem.eql(u8, testEnumTagNameBare(ExternDuplicates.B), "A"));
242}242}
243243
244test "@tagName non-exhaustive enum" {244test "@tagName non-exhaustive enum" {
245 expect(mem.eql(u8, testEnumTagNameBare(NonExhaustive.B), "B"));245 try expect(mem.eql(u8, testEnumTagNameBare(NonExhaustive.B), "B"));
246 comptime expect(mem.eql(u8, testEnumTagNameBare(NonExhaustive.B), "B"));246 comptime try expect(mem.eql(u8, testEnumTagNameBare(NonExhaustive.B), "B"));
247}247}
248248
249fn testEnumTagNameBare(n: anytype) []const u8 {249fn testEnumTagNameBare(n: anytype) []const u8 {
...@@ -269,8 +269,8 @@ const NonExhaustive = enum(u8) {...@@ -269,8 +269,8 @@ const NonExhaustive = enum(u8) {
269269
270test "enum alignment" {270test "enum alignment" {
271 comptime {271 comptime {
272 expect(@alignOf(AlignTestEnum) >= @alignOf([9]u8));272 try expect(@alignOf(AlignTestEnum) >= @alignOf([9]u8));
273 expect(@alignOf(AlignTestEnum) >= @alignOf(u64));273 try expect(@alignOf(AlignTestEnum) >= @alignOf(u64));
274 }274 }
275}275}
276276
...@@ -806,10 +806,10 @@ const ValueCount257 = enum {...@@ -806,10 +806,10 @@ const ValueCount257 = enum {
806806
807test "enum sizes" {807test "enum sizes" {
808 comptime {808 comptime {
809 expect(@sizeOf(ValueCount1) == 0);809 try expect(@sizeOf(ValueCount1) == 0);
810 expect(@sizeOf(ValueCount2) == 1);810 try expect(@sizeOf(ValueCount2) == 1);
811 expect(@sizeOf(ValueCount256) == 1);811 try expect(@sizeOf(ValueCount256) == 1);
812 expect(@sizeOf(ValueCount257) == 2);812 try expect(@sizeOf(ValueCount257) == 2);
813 }813 }
814}814}
815815
...@@ -828,12 +828,12 @@ test "set enum tag type" {...@@ -828,12 +828,12 @@ test "set enum tag type" {
828 {828 {
829 var x = Small.One;829 var x = Small.One;
830 x = Small.Two;830 x = Small.Two;
831 comptime expect(Tag(Small) == u2);831 comptime try expect(Tag(Small) == u2);
832 }832 }
833 {833 {
834 var x = Small2.One;834 var x = Small2.One;
835 x = Small2.Two;835 x = Small2.Two;
836 comptime expect(Tag(Small2) == u2);836 comptime try expect(Tag(Small2) == u2);
837 }837 }
838}838}
839839
...@@ -880,17 +880,17 @@ const bit_field_1 = BitFieldOfEnums{...@@ -880,17 +880,17 @@ const bit_field_1 = BitFieldOfEnums{
880880
881test "bit field access with enum fields" {881test "bit field access with enum fields" {
882 var data = bit_field_1;882 var data = bit_field_1;
883 expect(getA(&data) == A.Two);883 try expect(getA(&data) == A.Two);
884 expect(getB(&data) == B.Three3);884 try expect(getB(&data) == B.Three3);
885 expect(getC(&data) == C.Four4);885 try expect(getC(&data) == C.Four4);
886 comptime expect(@sizeOf(BitFieldOfEnums) == 1);886 comptime try expect(@sizeOf(BitFieldOfEnums) == 1);
887887
888 data.b = B.Four3;888 data.b = B.Four3;
889 expect(data.b == B.Four3);889 try expect(data.b == B.Four3);
890890
891 data.a = A.Three;891 data.a = A.Three;
892 expect(data.a == A.Three);892 try expect(data.a == A.Three);
893 expect(data.b == B.Four3);893 try expect(data.b == B.Four3);
894}894}
895895
896fn getA(data: *const BitFieldOfEnums) A {896fn getA(data: *const BitFieldOfEnums) A {
...@@ -906,12 +906,12 @@ fn getC(data: *const BitFieldOfEnums) C {...@@ -906,12 +906,12 @@ fn getC(data: *const BitFieldOfEnums) C {
906}906}
907907
908test "casting enum to its tag type" {908test "casting enum to its tag type" {
909 testCastEnumTag(Small2.Two);909 try testCastEnumTag(Small2.Two);
910 comptime testCastEnumTag(Small2.Two);910 comptime try testCastEnumTag(Small2.Two);
911}911}
912912
913fn testCastEnumTag(value: Small2) void {913fn testCastEnumTag(value: Small2) !void {
914 expect(@enumToInt(value) == 1);914 try expect(@enumToInt(value) == 1);
915}915}
916916
917const MultipleChoice = enum(u32) {917const MultipleChoice = enum(u32) {
...@@ -922,13 +922,13 @@ const MultipleChoice = enum(u32) {...@@ -922,13 +922,13 @@ const MultipleChoice = enum(u32) {
922};922};
923923
924test "enum with specified tag values" {924test "enum with specified tag values" {
925 testEnumWithSpecifiedTagValues(MultipleChoice.C);925 try testEnumWithSpecifiedTagValues(MultipleChoice.C);
926 comptime testEnumWithSpecifiedTagValues(MultipleChoice.C);926 comptime try testEnumWithSpecifiedTagValues(MultipleChoice.C);
927}927}
928928
929fn testEnumWithSpecifiedTagValues(x: MultipleChoice) void {929fn testEnumWithSpecifiedTagValues(x: MultipleChoice) !void {
930 expect(@enumToInt(x) == 60);930 try expect(@enumToInt(x) == 60);
931 expect(1234 == switch (x) {931 try expect(1234 == switch (x) {
932 MultipleChoice.A => 1,932 MultipleChoice.A => 1,
933 MultipleChoice.B => 2,933 MultipleChoice.B => 2,
934 MultipleChoice.C => @as(u32, 1234),934 MultipleChoice.C => @as(u32, 1234),
...@@ -949,13 +949,13 @@ const MultipleChoice2 = enum(u32) {...@@ -949,13 +949,13 @@ const MultipleChoice2 = enum(u32) {
949};949};
950950
951test "enum with specified and unspecified tag values" {951test "enum with specified and unspecified tag values" {
952 testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2.D);952 try testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2.D);
953 comptime testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2.D);953 comptime try testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2.D);
954}954}
955955
956fn testEnumWithSpecifiedAndUnspecifiedTagValues(x: MultipleChoice2) void {956fn testEnumWithSpecifiedAndUnspecifiedTagValues(x: MultipleChoice2) !void {
957 expect(@enumToInt(x) == 1000);957 try expect(@enumToInt(x) == 1000);
958 expect(1234 == switch (x) {958 try expect(1234 == switch (x) {
959 MultipleChoice2.A => 1,959 MultipleChoice2.A => 1,
960 MultipleChoice2.B => 2,960 MultipleChoice2.B => 2,
961 MultipleChoice2.C => 3,961 MultipleChoice2.C => 3,
...@@ -969,8 +969,8 @@ fn testEnumWithSpecifiedAndUnspecifiedTagValues(x: MultipleChoice2) void {...@@ -969,8 +969,8 @@ fn testEnumWithSpecifiedAndUnspecifiedTagValues(x: MultipleChoice2) void {
969}969}
970970
971test "cast integer literal to enum" {971test "cast integer literal to enum" {
972 expect(@intToEnum(MultipleChoice2, 0) == MultipleChoice2.Unspecified1);972 try expect(@intToEnum(MultipleChoice2, 0) == MultipleChoice2.Unspecified1);
973 expect(@intToEnum(MultipleChoice2, 40) == MultipleChoice2.B);973 try expect(@intToEnum(MultipleChoice2, 40) == MultipleChoice2.B);
974}974}
975975
976const EnumWithOneMember = enum {976const EnumWithOneMember = enum {
...@@ -1008,14 +1008,14 @@ const EnumWithTagValues = enum(u4) {...@@ -1008,14 +1008,14 @@ const EnumWithTagValues = enum(u4) {
1008 D = 1 << 3,1008 D = 1 << 3,
1009};1009};
1010test "enum with tag values don't require parens" {1010test "enum with tag values don't require parens" {
1011 expect(@enumToInt(EnumWithTagValues.C) == 0b0100);1011 try expect(@enumToInt(EnumWithTagValues.C) == 0b0100);
1012}1012}
10131013
1014test "enum with 1 field but explicit tag type should still have the tag type" {1014test "enum with 1 field but explicit tag type should still have the tag type" {
1015 const Enum = enum(u8) {1015 const Enum = enum(u8) {
1016 B = 2,1016 B = 2,
1017 };1017 };
1018 comptime @import("std").testing.expect(@sizeOf(Enum) == @sizeOf(u8));1018 comptime try expect(@sizeOf(Enum) == @sizeOf(u8));
1019}1019}
10201020
1021test "empty extern enum with members" {1021test "empty extern enum with members" {
...@@ -1024,7 +1024,7 @@ test "empty extern enum with members" {...@@ -1024,7 +1024,7 @@ test "empty extern enum with members" {
1024 B,1024 B,
1025 C,1025 C,
1026 };1026 };
1027 expect(@sizeOf(E) == @sizeOf(c_int));1027 try expect(@sizeOf(E) == @sizeOf(c_int));
1028}1028}
10291029
1030test "tag name with assigned enum values" {1030test "tag name with assigned enum values" {
...@@ -1033,7 +1033,7 @@ test "tag name with assigned enum values" {...@@ -1033,7 +1033,7 @@ test "tag name with assigned enum values" {
1033 B = 0,1033 B = 0,
1034 };1034 };
1035 var b = LocalFoo.B;1035 var b = LocalFoo.B;
1036 expect(mem.eql(u8, @tagName(b), "B"));1036 try expect(mem.eql(u8, @tagName(b), "B"));
1037}1037}
10381038
1039test "enum literal equality" {1039test "enum literal equality" {
...@@ -1041,8 +1041,8 @@ test "enum literal equality" {...@@ -1041,8 +1041,8 @@ test "enum literal equality" {
1041 const y = .ok;1041 const y = .ok;
1042 const z = .hi;1042 const z = .hi;
10431043
1044 expect(x != y);1044 try expect(x != y);
1045 expect(x == z);1045 try expect(x == z);
1046}1046}
10471047
1048test "enum literal cast to enum" {1048test "enum literal cast to enum" {
...@@ -1054,7 +1054,7 @@ test "enum literal cast to enum" {...@@ -1054,7 +1054,7 @@ test "enum literal cast to enum" {
10541054
1055 var color1: Color = .Auto;1055 var color1: Color = .Auto;
1056 var color2 = Color.Auto;1056 var color2 = Color.Auto;
1057 expect(color1 == color2);1057 try expect(color1 == color2);
1058}1058}
10591059
1060test "peer type resolution with enum literal" {1060test "peer type resolution with enum literal" {
...@@ -1063,8 +1063,8 @@ test "peer type resolution with enum literal" {...@@ -1063,8 +1063,8 @@ test "peer type resolution with enum literal" {
1063 two,1063 two,
1064 };1064 };
10651065
1066 expect(Items.two == .two);1066 try expect(Items.two == .two);
1067 expect(.two == Items.two);1067 try expect(.two == Items.two);
1068}1068}
10691069
1070test "enum literal in array literal" {1070test "enum literal in array literal" {
...@@ -1078,8 +1078,8 @@ test "enum literal in array literal" {...@@ -1078,8 +1078,8 @@ test "enum literal in array literal" {
1078 .two,1078 .two,
1079 };1079 };
10801080
1081 expect(array[0] == .one);1081 try expect(array[0] == .one);
1082 expect(array[1] == .two);1082 try expect(array[1] == .two);
1083}1083}
10841084
1085test "signed integer as enum tag" {1085test "signed integer as enum tag" {
...@@ -1089,9 +1089,9 @@ test "signed integer as enum tag" {...@@ -1089,9 +1089,9 @@ test "signed integer as enum tag" {
1089 A2 = 1,1089 A2 = 1,
1090 };1090 };
10911091
1092 expect(@enumToInt(SignedEnum.A0) == -1);1092 try expect(@enumToInt(SignedEnum.A0) == -1);
1093 expect(@enumToInt(SignedEnum.A1) == 0);1093 try expect(@enumToInt(SignedEnum.A1) == 0);
1094 expect(@enumToInt(SignedEnum.A2) == 1);1094 try expect(@enumToInt(SignedEnum.A2) == 1);
1095}1095}
10961096
1097test "enum value allocation" {1097test "enum value allocation" {
...@@ -1101,9 +1101,9 @@ test "enum value allocation" {...@@ -1101,9 +1101,9 @@ test "enum value allocation" {
1101 A2,1101 A2,
1102 };1102 };
11031103
1104 expect(@enumToInt(LargeEnum.A0) == 0x80000000);1104 try expect(@enumToInt(LargeEnum.A0) == 0x80000000);
1105 expect(@enumToInt(LargeEnum.A1) == 0x80000001);1105 try expect(@enumToInt(LargeEnum.A1) == 0x80000001);
1106 expect(@enumToInt(LargeEnum.A2) == 0x80000002);1106 try expect(@enumToInt(LargeEnum.A2) == 0x80000002);
1107}1107}
11081108
1109test "enum literal casting to tagged union" {1109test "enum literal casting to tagged union" {
...@@ -1130,32 +1130,32 @@ test "enum with one member and custom tag type" {...@@ -1130,32 +1130,32 @@ test "enum with one member and custom tag type" {
1130 const E = enum(u2) {1130 const E = enum(u2) {
1131 One,1131 One,
1132 };1132 };
1133 expect(@enumToInt(E.One) == 0);1133 try expect(@enumToInt(E.One) == 0);
1134 const E2 = enum(u2) {1134 const E2 = enum(u2) {
1135 One = 2,1135 One = 2,
1136 };1136 };
1137 expect(@enumToInt(E2.One) == 2);1137 try expect(@enumToInt(E2.One) == 2);
1138}1138}
11391139
1140test "enum literal casting to optional" {1140test "enum literal casting to optional" {
1141 var bar: ?Bar = undefined;1141 var bar: ?Bar = undefined;
1142 bar = .B;1142 bar = .B;
11431143
1144 expect(bar.? == Bar.B);1144 try expect(bar.? == Bar.B);
1145}1145}
11461146
1147test "enum literal casting to error union with payload enum" {1147test "enum literal casting to error union with payload enum" {
1148 var bar: error{B}!Bar = undefined;1148 var bar: error{B}!Bar = undefined;
1149 bar = .B; // should never cast to the error set1149 bar = .B; // should never cast to the error set
11501150
1151 expect((try bar) == Bar.B);1151 try expect((try bar) == Bar.B);
1152}1152}
11531153
1154test "enum with one member and u1 tag type @enumToInt" {1154test "enum with one member and u1 tag type @enumToInt" {
1155 const Enum = enum(u1) {1155 const Enum = enum(u1) {
1156 Test,1156 Test,
1157 };1157 };
1158 expect(@enumToInt(Enum.Test) == 0);1158 try expect(@enumToInt(Enum.Test) == 0);
1159}1159}
11601160
1161test "enum with comptime_int tag type" {1161test "enum with comptime_int tag type" {
...@@ -1164,19 +1164,19 @@ test "enum with comptime_int tag type" {...@@ -1164,19 +1164,19 @@ test "enum with comptime_int tag type" {
1164 Two = 2,1164 Two = 2,
1165 Three = 1,1165 Three = 1,
1166 };1166 };
1167 comptime expect(Tag(Enum) == comptime_int);1167 comptime try expect(Tag(Enum) == comptime_int);
1168}1168}
11691169
1170test "enum with one member default to u0 tag type" {1170test "enum with one member default to u0 tag type" {
1171 const E0 = enum {1171 const E0 = enum {
1172 X,1172 X,
1173 };1173 };
1174 comptime expect(Tag(E0) == u0);1174 comptime try expect(Tag(E0) == u0);
1175}1175}
11761176
1177test "tagName on enum literals" {1177test "tagName on enum literals" {
1178 expect(mem.eql(u8, @tagName(.FooBar), "FooBar"));1178 try expect(mem.eql(u8, @tagName(.FooBar), "FooBar"));
1179 comptime expect(mem.eql(u8, @tagName(.FooBar), "FooBar"));1179 comptime try expect(mem.eql(u8, @tagName(.FooBar), "FooBar"));
1180}1180}
11811181
1182test "method call on an enum" {1182test "method call on an enum" {
...@@ -1193,12 +1193,12 @@ test "method call on an enum" {...@@ -1193,12 +1193,12 @@ test "method call on an enum" {
1193 return self.* == .two and foo == bool;1193 return self.* == .two and foo == bool;
1194 }1194 }
1195 };1195 };
1196 fn doTheTest() void {1196 fn doTheTest() !void {
1197 var e = E.two;1197 var e = E.two;
1198 expect(e.method());1198 try expect(e.method());
1199 expect(e.generic_method(bool));1199 try expect(e.generic_method(bool));
1200 }1200 }
1201 };1201 };
1202 S.doTheTest();1202 try S.doTheTest();
1203 comptime S.doTheTest();1203 comptime try S.doTheTest();
1204}1204}
test/stage1/behavior/enum_with_members.zig+4-4
...@@ -19,9 +19,9 @@ test "enum with members" {...@@ -19,9 +19,9 @@ test "enum with members" {
19 const b = ET{ .UINT = 42 };19 const b = ET{ .UINT = 42 };
20 var buf: [20]u8 = undefined;20 var buf: [20]u8 = undefined;
2121
22 expect((a.print(buf[0..]) catch unreachable) == 3);22 try expect((a.print(buf[0..]) catch unreachable) == 3);
23 expect(mem.eql(u8, buf[0..3], "-42"));23 try expect(mem.eql(u8, buf[0..3], "-42"));
2424
25 expect((b.print(buf[0..]) catch unreachable) == 2);25 try expect((b.print(buf[0..]) catch unreachable) == 2);
26 expect(mem.eql(u8, buf[0..2], "42"));26 try expect(mem.eql(u8, buf[0..2], "42"));
27}27}
test/stage1/behavior/error.zig+60-60
...@@ -19,7 +19,7 @@ pub fn baz() anyerror!i32 {...@@ -19,7 +19,7 @@ pub fn baz() anyerror!i32 {
19}19}
2020
21test "error wrapping" {21test "error wrapping" {
22 expect((baz() catch unreachable) == 15);22 try expect((baz() catch unreachable) == 15);
23}23}
2424
25fn gimmeItBroke() []const u8 {25fn gimmeItBroke() []const u8 {
...@@ -27,14 +27,14 @@ fn gimmeItBroke() []const u8 {...@@ -27,14 +27,14 @@ fn gimmeItBroke() []const u8 {
27}27}
2828
29test "@errorName" {29test "@errorName" {
30 expect(mem.eql(u8, @errorName(error.AnError), "AnError"));30 try expect(mem.eql(u8, @errorName(error.AnError), "AnError"));
31 expect(mem.eql(u8, @errorName(error.ALongerErrorName), "ALongerErrorName"));31 try expect(mem.eql(u8, @errorName(error.ALongerErrorName), "ALongerErrorName"));
32}32}
3333
34test "error values" {34test "error values" {
35 const a = @errorToInt(error.err1);35 const a = @errorToInt(error.err1);
36 const b = @errorToInt(error.err2);36 const b = @errorToInt(error.err2);
37 expect(a != b);37 try expect(a != b);
38}38}
3939
40test "redefinition of error values allowed" {40test "redefinition of error values allowed" {
...@@ -47,8 +47,8 @@ fn shouldBeNotEqual(a: anyerror, b: anyerror) void {...@@ -47,8 +47,8 @@ fn shouldBeNotEqual(a: anyerror, b: anyerror) void {
47test "error binary operator" {47test "error binary operator" {
48 const a = errBinaryOperatorG(true) catch 3;48 const a = errBinaryOperatorG(true) catch 3;
49 const b = errBinaryOperatorG(false) catch 3;49 const b = errBinaryOperatorG(false) catch 3;
50 expect(a == 3);50 try expect(a == 3);
51 expect(b == 10);51 try expect(b == 10);
52}52}
53fn errBinaryOperatorG(x: bool) anyerror!isize {53fn errBinaryOperatorG(x: bool) anyerror!isize {
54 return if (x) error.ItBroke else @as(isize, 10);54 return if (x) error.ItBroke else @as(isize, 10);
...@@ -56,7 +56,7 @@ fn errBinaryOperatorG(x: bool) anyerror!isize {...@@ -56,7 +56,7 @@ fn errBinaryOperatorG(x: bool) anyerror!isize {
5656
57test "unwrap simple value from error" {57test "unwrap simple value from error" {
58 const i = unwrapSimpleValueFromErrorDo() catch unreachable;58 const i = unwrapSimpleValueFromErrorDo() catch unreachable;
59 expect(i == 13);59 try expect(i == 13);
60}60}
61fn unwrapSimpleValueFromErrorDo() anyerror!isize {61fn unwrapSimpleValueFromErrorDo() anyerror!isize {
62 return 13;62 return 13;
...@@ -76,21 +76,21 @@ fn makeANonErr() anyerror!i32 {...@@ -76,21 +76,21 @@ fn makeANonErr() anyerror!i32 {
76}76}
7777
78test "error union type " {78test "error union type " {
79 testErrorUnionType();79 try testErrorUnionType();
80 comptime testErrorUnionType();80 comptime try testErrorUnionType();
81}81}
8282
83fn testErrorUnionType() void {83fn testErrorUnionType() !void {
84 const x: anyerror!i32 = 1234;84 const x: anyerror!i32 = 1234;
85 if (x) |value| expect(value == 1234) else |_| unreachable;85 if (x) |value| try expect(value == 1234) else |_| unreachable;
86 expect(@typeInfo(@TypeOf(x)) == .ErrorUnion);86 try expect(@typeInfo(@TypeOf(x)) == .ErrorUnion);
87 expect(@typeInfo(@typeInfo(@TypeOf(x)).ErrorUnion.error_set) == .ErrorSet);87 try expect(@typeInfo(@typeInfo(@TypeOf(x)).ErrorUnion.error_set) == .ErrorSet);
88 expect(@typeInfo(@TypeOf(x)).ErrorUnion.error_set == anyerror);88 try expect(@typeInfo(@TypeOf(x)).ErrorUnion.error_set == anyerror);
89}89}
9090
91test "error set type" {91test "error set type" {
92 testErrorSetType();92 try testErrorSetType();
93 comptime testErrorSetType();93 comptime try testErrorSetType();
94}94}
9595
96const MyErrSet = error{96const MyErrSet = error{
...@@ -98,21 +98,21 @@ const MyErrSet = error{...@@ -98,21 +98,21 @@ const MyErrSet = error{
98 FileNotFound,98 FileNotFound,
99};99};
100100
101fn testErrorSetType() void {101fn testErrorSetType() !void {
102 expect(@typeInfo(MyErrSet).ErrorSet.?.len == 2);102 try expect(@typeInfo(MyErrSet).ErrorSet.?.len == 2);
103103
104 const a: MyErrSet!i32 = 5678;104 const a: MyErrSet!i32 = 5678;
105 const b: MyErrSet!i32 = MyErrSet.OutOfMemory;105 const b: MyErrSet!i32 = MyErrSet.OutOfMemory;
106106
107 if (a) |value| expect(value == 5678) else |err| switch (err) {107 if (a) |value| try expect(value == 5678) else |err| switch (err) {
108 error.OutOfMemory => unreachable,108 error.OutOfMemory => unreachable,
109 error.FileNotFound => unreachable,109 error.FileNotFound => unreachable,
110 }110 }
111}111}
112112
113test "explicit error set cast" {113test "explicit error set cast" {
114 testExplicitErrorSetCast(Set1.A);114 try testExplicitErrorSetCast(Set1.A);
115 comptime testExplicitErrorSetCast(Set1.A);115 comptime try testExplicitErrorSetCast(Set1.A);
116}116}
117117
118const Set1 = error{118const Set1 = error{
...@@ -124,26 +124,26 @@ const Set2 = error{...@@ -124,26 +124,26 @@ const Set2 = error{
124 C,124 C,
125};125};
126126
127fn testExplicitErrorSetCast(set1: Set1) void {127fn testExplicitErrorSetCast(set1: Set1) !void {
128 var x = @errSetCast(Set2, set1);128 var x = @errSetCast(Set2, set1);
129 var y = @errSetCast(Set1, x);129 var y = @errSetCast(Set1, x);
130 expect(y == error.A);130 try expect(y == error.A);
131}131}
132132
133test "comptime test error for empty error set" {133test "comptime test error for empty error set" {
134 testComptimeTestErrorEmptySet(1234);134 try testComptimeTestErrorEmptySet(1234);
135 comptime testComptimeTestErrorEmptySet(1234);135 comptime try testComptimeTestErrorEmptySet(1234);
136}136}
137137
138const EmptyErrorSet = error{};138const EmptyErrorSet = error{};
139139
140fn testComptimeTestErrorEmptySet(x: EmptyErrorSet!i32) void {140fn testComptimeTestErrorEmptySet(x: EmptyErrorSet!i32) !void {
141 if (x) |v| expect(v == 1234) else |err| @compileError("bad");141 if (x) |v| try expect(v == 1234) else |err| @compileError("bad");
142}142}
143143
144test "syntax: optional operator in front of error union operator" {144test "syntax: optional operator in front of error union operator" {
145 comptime {145 comptime {
146 expect(?(anyerror!i32) == ?(anyerror!i32));146 try expect(?(anyerror!i32) == ?(anyerror!i32));
147 }147 }
148}148}
149149
...@@ -165,10 +165,10 @@ test "empty error union" {...@@ -165,10 +165,10 @@ test "empty error union" {
165}165}
166166
167test "error union peer type resolution" {167test "error union peer type resolution" {
168 testErrorUnionPeerTypeResolution(1);168 try testErrorUnionPeerTypeResolution(1);
169}169}
170170
171fn testErrorUnionPeerTypeResolution(x: i32) void {171fn testErrorUnionPeerTypeResolution(x: i32) !void {
172 const y = switch (x) {172 const y = switch (x) {
173 1 => bar_1(),173 1 => bar_1(),
174 2 => baz_1(),174 2 => baz_1(),
...@@ -177,7 +177,7 @@ fn testErrorUnionPeerTypeResolution(x: i32) void {...@@ -177,7 +177,7 @@ fn testErrorUnionPeerTypeResolution(x: i32) void {
177 if (y) |_| {177 if (y) |_| {
178 @panic("expected error");178 @panic("expected error");
179 } else |e| {179 } else |e| {
180 expect(e == error.A);180 try expect(e == error.A);
181 }181 }
182}182}
183183
...@@ -286,13 +286,13 @@ test "nested error union function call in optional unwrap" {...@@ -286,13 +286,13 @@ test "nested error union function call in optional unwrap" {
286 return null;286 return null;
287 }287 }
288 };288 };
289 expect((try S.errorable()) == 1234);289 try expect((try S.errorable()) == 1234);
290 expectError(error.Failure, S.errorable2());290 try expectError(error.Failure, S.errorable2());
291 expectError(error.Other, S.errorable3());291 try expectError(error.Other, S.errorable3());
292 comptime {292 comptime {
293 expect((try S.errorable()) == 1234);293 try expect((try S.errorable()) == 1234);
294 expectError(error.Failure, S.errorable2());294 try expectError(error.Failure, S.errorable2());
295 expectError(error.Other, S.errorable3());295 try expectError(error.Other, S.errorable3());
296 }296 }
297}297}
298298
...@@ -307,7 +307,7 @@ test "widen cast integer payload of error union function call" {...@@ -307,7 +307,7 @@ test "widen cast integer payload of error union function call" {
307 return 1234;307 return 1234;
308 }308 }
309 };309 };
310 expect((try S.errorable()) == 1234);310 try expect((try S.errorable()) == 1234);
311}311}
312312
313test "return function call to error set from error union function" {313test "return function call to error set from error union function" {
...@@ -320,19 +320,19 @@ test "return function call to error set from error union function" {...@@ -320,19 +320,19 @@ test "return function call to error set from error union function" {
320 return error.Failure;320 return error.Failure;
321 }321 }
322 };322 };
323 expectError(error.Failure, S.errorable());323 try expectError(error.Failure, S.errorable());
324 comptime expectError(error.Failure, S.errorable());324 comptime try expectError(error.Failure, S.errorable());
325}325}
326326
327test "optional error set is the same size as error set" {327test "optional error set is the same size as error set" {
328 comptime expect(@sizeOf(?anyerror) == @sizeOf(anyerror));328 comptime try expect(@sizeOf(?anyerror) == @sizeOf(anyerror));
329 const S = struct {329 const S = struct {
330 fn returnsOptErrSet() ?anyerror {330 fn returnsOptErrSet() ?anyerror {
331 return null;331 return null;
332 }332 }
333 };333 };
334 expect(S.returnsOptErrSet() == null);334 try expect(S.returnsOptErrSet() == null);
335 comptime expect(S.returnsOptErrSet() == null);335 comptime try expect(S.returnsOptErrSet() == null);
336}336}
337337
338test "debug info for optional error set" {338test "debug info for optional error set" {
...@@ -342,8 +342,8 @@ test "debug info for optional error set" {...@@ -342,8 +342,8 @@ test "debug info for optional error set" {
342342
343test "nested catch" {343test "nested catch" {
344 const S = struct {344 const S = struct {
345 fn entry() void {345 fn entry() !void {
346 expectError(error.Bad, func());346 try expectError(error.Bad, func());
347 }347 }
348 fn fail() anyerror!Foo {348 fn fail() anyerror!Foo {
349 return error.Wrong;349 return error.Wrong;
...@@ -358,16 +358,16 @@ test "nested catch" {...@@ -358,16 +358,16 @@ test "nested catch" {
358 field: i32,358 field: i32,
359 };359 };
360 };360 };
361 S.entry();361 try S.entry();
362 comptime S.entry();362 comptime try S.entry();
363}363}
364364
365test "implicit cast to optional to error union to return result loc" {365test "implicit cast to optional to error union to return result loc" {
366 const S = struct {366 const S = struct {
367 fn entry() void {367 fn entry() !void {
368 var x: Foo = undefined;368 var x: Foo = undefined;
369 if (func(&x)) |opt| {369 if (func(&x)) |opt| {
370 expect(opt != null);370 try expect(opt != null);
371 } else |_| @panic("expected non error");371 } else |_| @panic("expected non error");
372 }372 }
373 fn func(f: *Foo) anyerror!?*Foo {373 fn func(f: *Foo) anyerror!?*Foo {
...@@ -377,7 +377,7 @@ test "implicit cast to optional to error union to return result loc" {...@@ -377,7 +377,7 @@ test "implicit cast to optional to error union to return result loc" {
377 field: i32,377 field: i32,
378 };378 };
379 };379 };
380 S.entry();380 try S.entry();
381 //comptime S.entry(); TODO381 //comptime S.entry(); TODO
382}382}
383383
...@@ -393,23 +393,23 @@ test "function pointer with return type that is error union with payload which i...@@ -393,23 +393,23 @@ test "function pointer with return type that is error union with payload which i
393 return Err.UnspecifiedErr;393 return Err.UnspecifiedErr;
394 }394 }
395395
396 fn doTheTest() void {396 fn doTheTest() !void {
397 var x = Foo{ .fun = bar };397 var x = Foo{ .fun = bar };
398 expectError(error.UnspecifiedErr, x.fun(1));398 try expectError(error.UnspecifiedErr, x.fun(1));
399 }399 }
400 };400 };
401 S.doTheTest();401 try S.doTheTest();
402}402}
403403
404test "return result loc as peer result loc in inferred error set function" {404test "return result loc as peer result loc in inferred error set function" {
405 const S = struct {405 const S = struct {
406 fn doTheTest() void {406 fn doTheTest() !void {
407 if (foo(2)) |x| {407 if (foo(2)) |x| {
408 expect(x.Two);408 try expect(x.Two);
409 } else |e| switch (e) {409 } else |e| switch (e) {
410 error.Whatever => @panic("fail"),410 error.Whatever => @panic("fail"),
411 }411 }
412 expectError(error.Whatever, foo(99));412 try expectError(error.Whatever, foo(99));
413 }413 }
414 const FormValue = union(enum) {414 const FormValue = union(enum) {
415 One: void,415 One: void,
...@@ -424,8 +424,8 @@ test "return result loc as peer result loc in inferred error set function" {...@@ -424,8 +424,8 @@ test "return result loc as peer result loc in inferred error set function" {
424 };424 };
425 }425 }
426 };426 };
427 S.doTheTest();427 try S.doTheTest();
428 comptime S.doTheTest();428 comptime try S.doTheTest();
429}429}
430430
431test "error payload type is correctly resolved" {431test "error payload type is correctly resolved" {
...@@ -439,7 +439,7 @@ test "error payload type is correctly resolved" {...@@ -439,7 +439,7 @@ test "error payload type is correctly resolved" {
439 }439 }
440 };440 };
441441
442 expectEqual(MyIntWrapper{ .x = 42 }, try MyIntWrapper.create());442 try expectEqual(MyIntWrapper{ .x = 42 }, try MyIntWrapper.create());
443}443}
444444
445test "error union comptime caching" {445test "error union comptime caching" {
...@@ -449,4 +449,4 @@ test "error union comptime caching" {...@@ -449,4 +449,4 @@ test "error union comptime caching" {
449449
450 S.foo(@as(anyerror!void, {}));450 S.foo(@as(anyerror!void, {}));
451 S.foo(@as(anyerror!void, {}));451 S.foo(@as(anyerror!void, {}));
452}
\ No newline at end of file
452}
test/stage1/behavior/eval.zig+119-119
...@@ -4,7 +4,7 @@ const expectEqual = std.testing.expectEqual;...@@ -4,7 +4,7 @@ const expectEqual = std.testing.expectEqual;
4const builtin = @import("builtin");4const builtin = @import("builtin");
55
6test "compile time recursion" {6test "compile time recursion" {
7 expect(some_data.len == 21);7 try expect(some_data.len == 21);
8}8}
9var some_data: [@intCast(usize, fibonacci(7))]u8 = undefined;9var some_data: [@intCast(usize, fibonacci(7))]u8 = undefined;
10fn fibonacci(x: i32) i32 {10fn fibonacci(x: i32) i32 {
...@@ -17,7 +17,7 @@ fn unwrapAndAddOne(blah: ?i32) i32 {...@@ -17,7 +17,7 @@ fn unwrapAndAddOne(blah: ?i32) i32 {
17}17}
18const should_be_1235 = unwrapAndAddOne(1234);18const should_be_1235 = unwrapAndAddOne(1234);
19test "static add one" {19test "static add one" {
20 expect(should_be_1235 == 1235);20 try expect(should_be_1235 == 1235);
21}21}
2222
23test "inlined loop" {23test "inlined loop" {
...@@ -25,7 +25,7 @@ test "inlined loop" {...@@ -25,7 +25,7 @@ test "inlined loop" {
25 comptime var sum = 0;25 comptime var sum = 0;
26 inline while (i <= 5) : (i += 1)26 inline while (i <= 5) : (i += 1)
27 sum += i;27 sum += i;
28 expect(sum == 15);28 try expect(sum == 15);
29}29}
3030
31fn gimme1or2(comptime a: bool) i32 {31fn gimme1or2(comptime a: bool) i32 {
...@@ -35,12 +35,12 @@ fn gimme1or2(comptime a: bool) i32 {...@@ -35,12 +35,12 @@ fn gimme1or2(comptime a: bool) i32 {
35 return z;35 return z;
36}36}
37test "inline variable gets result of const if" {37test "inline variable gets result of const if" {
38 expect(gimme1or2(true) == 1);38 try expect(gimme1or2(true) == 1);
39 expect(gimme1or2(false) == 2);39 try expect(gimme1or2(false) == 2);
40}40}
4141
42test "static function evaluation" {42test "static function evaluation" {
43 expect(statically_added_number == 3);43 try expect(statically_added_number == 3);
44}44}
45const statically_added_number = staticAdd(1, 2);45const statically_added_number = staticAdd(1, 2);
46fn staticAdd(a: i32, b: i32) i32 {46fn staticAdd(a: i32, b: i32) i32 {
...@@ -48,8 +48,8 @@ fn staticAdd(a: i32, b: i32) i32 {...@@ -48,8 +48,8 @@ fn staticAdd(a: i32, b: i32) i32 {
48}48}
4949
50test "const expr eval on single expr blocks" {50test "const expr eval on single expr blocks" {
51 expect(constExprEvalOnSingleExprBlocksFn(1, true) == 3);51 try expect(constExprEvalOnSingleExprBlocksFn(1, true) == 3);
52 comptime expect(constExprEvalOnSingleExprBlocksFn(1, true) == 3);52 comptime try expect(constExprEvalOnSingleExprBlocksFn(1, true) == 3);
53}53}
5454
55fn constExprEvalOnSingleExprBlocksFn(x: i32, b: bool) i32 {55fn constExprEvalOnSingleExprBlocksFn(x: i32, b: bool) i32 {
...@@ -65,10 +65,10 @@ fn constExprEvalOnSingleExprBlocksFn(x: i32, b: bool) i32 {...@@ -65,10 +65,10 @@ fn constExprEvalOnSingleExprBlocksFn(x: i32, b: bool) i32 {
65}65}
6666
67test "statically initialized list" {67test "statically initialized list" {
68 expect(static_point_list[0].x == 1);68 try expect(static_point_list[0].x == 1);
69 expect(static_point_list[0].y == 2);69 try expect(static_point_list[0].y == 2);
70 expect(static_point_list[1].x == 3);70 try expect(static_point_list[1].x == 3);
71 expect(static_point_list[1].y == 4);71 try expect(static_point_list[1].y == 4);
72}72}
73const Point = struct {73const Point = struct {
74 x: i32,74 x: i32,
...@@ -86,8 +86,8 @@ fn makePoint(x: i32, y: i32) Point {...@@ -86,8 +86,8 @@ fn makePoint(x: i32, y: i32) Point {
86}86}
8787
88test "static eval list init" {88test "static eval list init" {
89 expect(static_vec3.data[2] == 1.0);89 try expect(static_vec3.data[2] == 1.0);
90 expect(vec3(0.0, 0.0, 3.0).data[2] == 3.0);90 try expect(vec3(0.0, 0.0, 3.0).data[2] == 3.0);
91}91}
92const static_vec3 = vec3(0.0, 0.0, 1.0);92const static_vec3 = vec3(0.0, 0.0, 1.0);
93pub const Vec3 = struct {93pub const Vec3 = struct {
...@@ -105,12 +105,12 @@ pub fn vec3(x: f32, y: f32, z: f32) Vec3 {...@@ -105,12 +105,12 @@ pub fn vec3(x: f32, y: f32, z: f32) Vec3 {
105105
106test "constant expressions" {106test "constant expressions" {
107 var array: [array_size]u8 = undefined;107 var array: [array_size]u8 = undefined;
108 expect(@sizeOf(@TypeOf(array)) == 20);108 try expect(@sizeOf(@TypeOf(array)) == 20);
109}109}
110const array_size: u8 = 20;110const array_size: u8 = 20;
111111
112test "constant struct with negation" {112test "constant struct with negation" {
113 expect(vertices[0].x == -0.6);113 try expect(vertices[0].x == -0.6);
114}114}
115const Vertex = struct {115const Vertex = struct {
116 x: f32,116 x: f32,
...@@ -145,7 +145,7 @@ const vertices = [_]Vertex{...@@ -145,7 +145,7 @@ const vertices = [_]Vertex{
145145
146test "statically initialized struct" {146test "statically initialized struct" {
147 st_init_str_foo.x += 1;147 st_init_str_foo.x += 1;
148 expect(st_init_str_foo.x == 14);148 try expect(st_init_str_foo.x == 14);
149}149}
150const StInitStrFoo = struct {150const StInitStrFoo = struct {
151 x: i32,151 x: i32,
...@@ -158,7 +158,7 @@ var st_init_str_foo = StInitStrFoo{...@@ -158,7 +158,7 @@ var st_init_str_foo = StInitStrFoo{
158158
159test "statically initalized array literal" {159test "statically initalized array literal" {
160 const y: [4]u8 = st_init_arr_lit_x;160 const y: [4]u8 = st_init_arr_lit_x;
161 expect(y[3] == 4);161 try expect(y[3] == 4);
162}162}
163const st_init_arr_lit_x = [_]u8{163const st_init_arr_lit_x = [_]u8{
164 1,164 1,
...@@ -170,15 +170,15 @@ const st_init_arr_lit_x = [_]u8{...@@ -170,15 +170,15 @@ const st_init_arr_lit_x = [_]u8{
170test "const slice" {170test "const slice" {
171 comptime {171 comptime {
172 const a = "1234567890";172 const a = "1234567890";
173 expect(a.len == 10);173 try expect(a.len == 10);
174 const b = a[1..2];174 const b = a[1..2];
175 expect(b.len == 1);175 try expect(b.len == 1);
176 expect(b[0] == '2');176 try expect(b[0] == '2');
177 }177 }
178}178}
179179
180test "try to trick eval with runtime if" {180test "try to trick eval with runtime if" {
181 expect(testTryToTrickEvalWithRuntimeIf(true) == 10);181 try expect(testTryToTrickEvalWithRuntimeIf(true) == 10);
182}182}
183183
184fn testTryToTrickEvalWithRuntimeIf(b: bool) usize {184fn testTryToTrickEvalWithRuntimeIf(b: bool) usize {
...@@ -198,7 +198,7 @@ test "inlined loop has array literal with elided runtime scope on first iteratio...@@ -198,7 +198,7 @@ test "inlined loop has array literal with elided runtime scope on first iteratio
198 const result = if (i == 0) [1]i32{2} else runtime;198 const result = if (i == 0) [1]i32{2} else runtime;
199 }199 }
200 comptime {200 comptime {
201 expect(i == 2);201 try expect(i == 2);
202 }202 }
203}203}
204204
...@@ -215,16 +215,16 @@ fn letsTryToCompareBools(a: bool, b: bool) bool {...@@ -215,16 +215,16 @@ fn letsTryToCompareBools(a: bool, b: bool) bool {
215 return max(bool, a, b);215 return max(bool, a, b);
216}216}
217test "inlined block and runtime block phi" {217test "inlined block and runtime block phi" {
218 expect(letsTryToCompareBools(true, true));218 try expect(letsTryToCompareBools(true, true));
219 expect(letsTryToCompareBools(true, false));219 try expect(letsTryToCompareBools(true, false));
220 expect(letsTryToCompareBools(false, true));220 try expect(letsTryToCompareBools(false, true));
221 expect(!letsTryToCompareBools(false, false));221 try expect(!letsTryToCompareBools(false, false));
222222
223 comptime {223 comptime {
224 expect(letsTryToCompareBools(true, true));224 try expect(letsTryToCompareBools(true, true));
225 expect(letsTryToCompareBools(true, false));225 try expect(letsTryToCompareBools(true, false));
226 expect(letsTryToCompareBools(false, true));226 try expect(letsTryToCompareBools(false, true));
227 expect(!letsTryToCompareBools(false, false));227 try expect(!letsTryToCompareBools(false, false));
228 }228 }
229}229}
230230
...@@ -269,14 +269,14 @@ fn performFn(comptime prefix_char: u8, start_value: i32) i32 {...@@ -269,14 +269,14 @@ fn performFn(comptime prefix_char: u8, start_value: i32) i32 {
269}269}
270270
271test "comptime iterate over fn ptr list" {271test "comptime iterate over fn ptr list" {
272 expect(performFn('t', 1) == 6);272 try expect(performFn('t', 1) == 6);
273 expect(performFn('o', 0) == 1);273 try expect(performFn('o', 0) == 1);
274 expect(performFn('w', 99) == 99);274 try expect(performFn('w', 99) == 99);
275}275}
276276
277test "eval @setRuntimeSafety at compile-time" {277test "eval @setRuntimeSafety at compile-time" {
278 const result = comptime fnWithSetRuntimeSafety();278 const result = comptime fnWithSetRuntimeSafety();
279 expect(result == 1234);279 try expect(result == 1234);
280}280}
281281
282fn fnWithSetRuntimeSafety() i32 {282fn fnWithSetRuntimeSafety() i32 {
...@@ -286,7 +286,7 @@ fn fnWithSetRuntimeSafety() i32 {...@@ -286,7 +286,7 @@ fn fnWithSetRuntimeSafety() i32 {
286286
287test "eval @setFloatMode at compile-time" {287test "eval @setFloatMode at compile-time" {
288 const result = comptime fnWithFloatMode();288 const result = comptime fnWithFloatMode();
289 expect(result == 1234.0);289 try expect(result == 1234.0);
290}290}
291291
292fn fnWithFloatMode() f32 {292fn fnWithFloatMode() f32 {
...@@ -307,15 +307,15 @@ var simple_struct = SimpleStruct{ .field = 1234 };...@@ -307,15 +307,15 @@ var simple_struct = SimpleStruct{ .field = 1234 };
307const bound_fn = simple_struct.method;307const bound_fn = simple_struct.method;
308308
309test "call method on bound fn referring to var instance" {309test "call method on bound fn referring to var instance" {
310 expect(bound_fn() == 1237);310 try expect(bound_fn() == 1237);
311}311}
312312
313test "ptr to local array argument at comptime" {313test "ptr to local array argument at comptime" {
314 comptime {314 comptime {
315 var bytes: [10]u8 = undefined;315 var bytes: [10]u8 = undefined;
316 modifySomeBytes(bytes[0..]);316 modifySomeBytes(bytes[0..]);
317 expect(bytes[0] == 'a');317 try expect(bytes[0] == 'a');
318 expect(bytes[9] == 'b');318 try expect(bytes[9] == 'b');
319 }319 }
320}320}
321321
...@@ -343,9 +343,9 @@ fn testCompTimeUIntComparisons(x: u32) void {...@@ -343,9 +343,9 @@ fn testCompTimeUIntComparisons(x: u32) void {
343}343}
344344
345test "const ptr to variable data changes at runtime" {345test "const ptr to variable data changes at runtime" {
346 expect(foo_ref.name[0] == 'a');346 try expect(foo_ref.name[0] == 'a');
347 foo_ref.name = "b";347 foo_ref.name = "b";
348 expect(foo_ref.name[0] == 'b');348 try expect(foo_ref.name[0] == 'b');
349}349}
350350
351const Foo = struct {351const Foo = struct {
...@@ -356,8 +356,8 @@ var foo_contents = Foo{ .name = "a" };...@@ -356,8 +356,8 @@ var foo_contents = Foo{ .name = "a" };
356const foo_ref = &foo_contents;356const foo_ref = &foo_contents;
357357
358test "create global array with for loop" {358test "create global array with for loop" {
359 expect(global_array[5] == 5 * 5);359 try expect(global_array[5] == 5 * 5);
360 expect(global_array[9] == 9 * 9);360 try expect(global_array[9] == 9 * 9);
361}361}
362362
363const global_array = x: {363const global_array = x: {
...@@ -372,18 +372,18 @@ test "compile-time downcast when the bits fit" {...@@ -372,18 +372,18 @@ test "compile-time downcast when the bits fit" {
372 comptime {372 comptime {
373 const spartan_count: u16 = 255;373 const spartan_count: u16 = 255;
374 const byte = @intCast(u8, spartan_count);374 const byte = @intCast(u8, spartan_count);
375 expect(byte == 255);375 try expect(byte == 255);
376 }376 }
377}377}
378378
379const hi1 = "hi";379const hi1 = "hi";
380const hi2 = hi1;380const hi2 = hi1;
381test "const global shares pointer with other same one" {381test "const global shares pointer with other same one" {
382 assertEqualPtrs(&hi1[0], &hi2[0]);382 try assertEqualPtrs(&hi1[0], &hi2[0]);
383 comptime expect(&hi1[0] == &hi2[0]);383 comptime try expect(&hi1[0] == &hi2[0]);
384}384}
385fn assertEqualPtrs(ptr1: *const u8, ptr2: *const u8) void {385fn assertEqualPtrs(ptr1: *const u8, ptr2: *const u8) !void {
386 expect(ptr1 == ptr2);386 try expect(ptr1 == ptr2);
387}387}
388388
389test "@setEvalBranchQuota" {389test "@setEvalBranchQuota" {
...@@ -395,29 +395,29 @@ test "@setEvalBranchQuota" {...@@ -395,29 +395,29 @@ test "@setEvalBranchQuota" {
395 while (i < 1001) : (i += 1) {395 while (i < 1001) : (i += 1) {
396 sum += i;396 sum += i;
397 }397 }
398 expect(sum == 500500);398 try expect(sum == 500500);
399 }399 }
400}400}
401401
402test "float literal at compile time not lossy" {402test "float literal at compile time not lossy" {
403 expect(16777216.0 + 1.0 == 16777217.0);403 try expect(16777216.0 + 1.0 == 16777217.0);
404 expect(9007199254740992.0 + 1.0 == 9007199254740993.0);404 try expect(9007199254740992.0 + 1.0 == 9007199254740993.0);
405}405}
406406
407test "f32 at compile time is lossy" {407test "f32 at compile time is lossy" {
408 expect(@as(f32, 1 << 24) + 1 == 1 << 24);408 try expect(@as(f32, 1 << 24) + 1 == 1 << 24);
409}409}
410410
411test "f64 at compile time is lossy" {411test "f64 at compile time is lossy" {
412 expect(@as(f64, 1 << 53) + 1 == 1 << 53);412 try expect(@as(f64, 1 << 53) + 1 == 1 << 53);
413}413}
414414
415test "f128 at compile time is lossy" {415test "f128 at compile time is lossy" {
416 expect(@as(f128, 10384593717069655257060992658440192.0) + 1 == 10384593717069655257060992658440192.0);416 try expect(@as(f128, 10384593717069655257060992658440192.0) + 1 == 10384593717069655257060992658440192.0);
417}417}
418418
419comptime {419comptime {
420 expect(@as(f128, 1 << 113) == 10384593717069655257060992658440192);420 try expect(@as(f128, 1 << 113) == 10384593717069655257060992658440192);
421}421}
422422
423pub fn TypeWithCompTimeSlice(comptime field_name: []const u8) type {423pub fn TypeWithCompTimeSlice(comptime field_name: []const u8) type {
...@@ -429,15 +429,15 @@ pub fn TypeWithCompTimeSlice(comptime field_name: []const u8) type {...@@ -429,15 +429,15 @@ pub fn TypeWithCompTimeSlice(comptime field_name: []const u8) type {
429test "string literal used as comptime slice is memoized" {429test "string literal used as comptime slice is memoized" {
430 const a = "link";430 const a = "link";
431 const b = "link";431 const b = "link";
432 comptime expect(TypeWithCompTimeSlice(a).Node == TypeWithCompTimeSlice(b).Node);432 comptime try expect(TypeWithCompTimeSlice(a).Node == TypeWithCompTimeSlice(b).Node);
433 comptime expect(TypeWithCompTimeSlice("link").Node == TypeWithCompTimeSlice("link").Node);433 comptime try expect(TypeWithCompTimeSlice("link").Node == TypeWithCompTimeSlice("link").Node);
434}434}
435435
436test "comptime slice of undefined pointer of length 0" {436test "comptime slice of undefined pointer of length 0" {
437 const slice1 = @as([*]i32, undefined)[0..0];437 const slice1 = @as([*]i32, undefined)[0..0];
438 expect(slice1.len == 0);438 try expect(slice1.len == 0);
439 const slice2 = @as([*]i32, undefined)[100..100];439 const slice2 = @as([*]i32, undefined)[100..100];
440 expect(slice2.len == 0);440 try expect(slice2.len == 0);
441}441}
442442
443fn copyWithPartialInline(s: []u32, b: []u8) void {443fn copyWithPartialInline(s: []u32, b: []u8) void {
...@@ -459,16 +459,16 @@ test "binary math operator in partially inlined function" {...@@ -459,16 +459,16 @@ test "binary math operator in partially inlined function" {
459 r.* = @intCast(u8, i + 1);459 r.* = @intCast(u8, i + 1);
460460
461 copyWithPartialInline(s[0..], b[0..]);461 copyWithPartialInline(s[0..], b[0..]);
462 expect(s[0] == 0x1020304);462 try expect(s[0] == 0x1020304);
463 expect(s[1] == 0x5060708);463 try expect(s[1] == 0x5060708);
464 expect(s[2] == 0x90a0b0c);464 try expect(s[2] == 0x90a0b0c);
465 expect(s[3] == 0xd0e0f10);465 try expect(s[3] == 0xd0e0f10);
466}466}
467467
468test "comptime function with the same args is memoized" {468test "comptime function with the same args is memoized" {
469 comptime {469 comptime {
470 expect(MakeType(i32) == MakeType(i32));470 try expect(MakeType(i32) == MakeType(i32));
471 expect(MakeType(i32) != MakeType(f64));471 try expect(MakeType(i32) != MakeType(f64));
472 }472 }
473}473}
474474
...@@ -484,7 +484,7 @@ test "comptime function with mutable pointer is not memoized" {...@@ -484,7 +484,7 @@ test "comptime function with mutable pointer is not memoized" {
484 const ptr = &x;484 const ptr = &x;
485 increment(ptr);485 increment(ptr);
486 increment(ptr);486 increment(ptr);
487 expect(x == 3);487 try expect(x == 3);
488 }488 }
489}489}
490490
...@@ -510,14 +510,14 @@ fn doesAlotT(comptime T: type, value: usize) T {...@@ -510,14 +510,14 @@ fn doesAlotT(comptime T: type, value: usize) T {
510}510}
511511
512test "@setEvalBranchQuota at same scope as generic function call" {512test "@setEvalBranchQuota at same scope as generic function call" {
513 expect(doesAlotT(u32, 2) == 2);513 try expect(doesAlotT(u32, 2) == 2);
514}514}
515515
516test "comptime slice of slice preserves comptime var" {516test "comptime slice of slice preserves comptime var" {
517 comptime {517 comptime {
518 var buff: [10]u8 = undefined;518 var buff: [10]u8 = undefined;
519 buff[0..][0..][0] = 1;519 buff[0..][0..][0] = 1;
520 expect(buff[0..][0..][0] == 1);520 try expect(buff[0..][0..][0] == 1);
521 }521 }
522}522}
523523
...@@ -526,7 +526,7 @@ test "comptime slice of pointer preserves comptime var" {...@@ -526,7 +526,7 @@ test "comptime slice of pointer preserves comptime var" {
526 var buff: [10]u8 = undefined;526 var buff: [10]u8 = undefined;
527 var a = @ptrCast([*]u8, &buff);527 var a = @ptrCast([*]u8, &buff);
528 a[0..1][0] = 1;528 a[0..1][0] = 1;
529 expect(buff[0..][0..][0] == 1);529 try expect(buff[0..][0..][0] == 1);
530 }530 }
531}531}
532532
...@@ -540,9 +540,9 @@ const SingleFieldStruct = struct {...@@ -540,9 +540,9 @@ const SingleFieldStruct = struct {
540test "const ptr to comptime mutable data is not memoized" {540test "const ptr to comptime mutable data is not memoized" {
541 comptime {541 comptime {
542 var foo = SingleFieldStruct{ .x = 1 };542 var foo = SingleFieldStruct{ .x = 1 };
543 expect(foo.read_x() == 1);543 try expect(foo.read_x() == 1);
544 foo.x = 2;544 foo.x = 2;
545 expect(foo.read_x() == 2);545 try expect(foo.read_x() == 2);
546 }546 }
547}547}
548548
...@@ -551,7 +551,7 @@ test "array concat of slices gives slice" {...@@ -551,7 +551,7 @@ test "array concat of slices gives slice" {
551 var a: []const u8 = "aoeu";551 var a: []const u8 = "aoeu";
552 var b: []const u8 = "asdf";552 var b: []const u8 = "asdf";
553 const c = a ++ b;553 const c = a ++ b;
554 expect(std.mem.eql(u8, c, "aoeuasdf"));554 try expect(std.mem.eql(u8, c, "aoeuasdf"));
555 }555 }
556}556}
557557
...@@ -568,14 +568,14 @@ test "comptime shlWithOverflow" {...@@ -568,14 +568,14 @@ test "comptime shlWithOverflow" {
568 break :amt amt;568 break :amt amt;
569 };569 };
570570
571 expect(ct_shifted == rt_shifted);571 try expect(ct_shifted == rt_shifted);
572}572}
573573
574test "runtime 128 bit integer division" {574test "runtime 128 bit integer division" {
575 var a: u128 = 152313999999999991610955792383;575 var a: u128 = 152313999999999991610955792383;
576 var b: u128 = 10000000000000000000;576 var b: u128 = 10000000000000000000;
577 var c = a / b;577 var c = a / b;
578 expect(c == 15231399999);578 try expect(c == 15231399999);
579}579}
580580
581pub const Info = struct {581pub const Info = struct {
...@@ -588,20 +588,20 @@ test "comptime modification of const struct field" {...@@ -588,20 +588,20 @@ test "comptime modification of const struct field" {
588 comptime {588 comptime {
589 var res = diamond_info;589 var res = diamond_info;
590 res.version = 1;590 res.version = 1;
591 expect(diamond_info.version == 0);591 try expect(diamond_info.version == 0);
592 expect(res.version == 1);592 try expect(res.version == 1);
593 }593 }
594}594}
595595
596test "pointer to type" {596test "pointer to type" {
597 comptime {597 comptime {
598 var T: type = i32;598 var T: type = i32;
599 expect(T == i32);599 try expect(T == i32);
600 var ptr = &T;600 var ptr = &T;
601 expect(@TypeOf(ptr) == *type);601 try expect(@TypeOf(ptr) == *type);
602 ptr.* = f32;602 ptr.* = f32;
603 expect(T == f32);603 try expect(T == f32);
604 expect(*T == *f32);604 try expect(*T == *f32);
605 }605 }
606}606}
607607
...@@ -610,17 +610,17 @@ test "slice of type" {...@@ -610,17 +610,17 @@ test "slice of type" {
610 var types_array = [_]type{ i32, f64, type };610 var types_array = [_]type{ i32, f64, type };
611 for (types_array) |T, i| {611 for (types_array) |T, i| {
612 switch (i) {612 switch (i) {
613 0 => expect(T == i32),613 0 => try expect(T == i32),
614 1 => expect(T == f64),614 1 => try expect(T == f64),
615 2 => expect(T == type),615 2 => try expect(T == type),
616 else => unreachable,616 else => unreachable,
617 }617 }
618 }618 }
619 for (types_array[0..]) |T, i| {619 for (types_array[0..]) |T, i| {
620 switch (i) {620 switch (i) {
621 0 => expect(T == i32),621 0 => try expect(T == i32),
622 1 => expect(T == f64),622 1 => try expect(T == f64),
623 2 => expect(T == type),623 2 => try expect(T == type),
624 else => unreachable,624 else => unreachable,
625 }625 }
626 }626 }
...@@ -637,7 +637,7 @@ fn wrap(comptime T: type) Wrapper {...@@ -637,7 +637,7 @@ fn wrap(comptime T: type) Wrapper {
637637
638test "function which returns struct with type field causes implicit comptime" {638test "function which returns struct with type field causes implicit comptime" {
639 const ty = wrap(i32).T;639 const ty = wrap(i32).T;
640 expect(ty == i32);640 try expect(ty == i32);
641}641}
642642
643test "call method with comptime pass-by-non-copying-value self parameter" {643test "call method with comptime pass-by-non-copying-value self parameter" {
...@@ -651,12 +651,12 @@ test "call method with comptime pass-by-non-copying-value self parameter" {...@@ -651,12 +651,12 @@ test "call method with comptime pass-by-non-copying-value self parameter" {
651651
652 const s = S{ .a = 2 };652 const s = S{ .a = 2 };
653 var b = s.b();653 var b = s.b();
654 expect(b == 2);654 try expect(b == 2);
655}655}
656656
657test "@tagName of @typeInfo" {657test "@tagName of @typeInfo" {
658 const str = @tagName(@typeInfo(u8));658 const str = @tagName(@typeInfo(u8));
659 expect(std.mem.eql(u8, str, "Int"));659 try expect(std.mem.eql(u8, str, "Int"));
660}660}
661661
662test "setting backward branch quota just before a generic fn call" {662test "setting backward branch quota just before a generic fn call" {
...@@ -670,15 +670,15 @@ fn loopNTimes(comptime n: usize) void {...@@ -670,15 +670,15 @@ fn loopNTimes(comptime n: usize) void {
670}670}
671671
672test "variable inside inline loop that has different types on different iterations" {672test "variable inside inline loop that has different types on different iterations" {
673 testVarInsideInlineLoop(.{ true, @as(u32, 42) });673 try testVarInsideInlineLoop(.{ true, @as(u32, 42) });
674}674}
675675
676fn testVarInsideInlineLoop(args: anytype) void {676fn testVarInsideInlineLoop(args: anytype) !void {
677 comptime var i = 0;677 comptime var i = 0;
678 inline while (i < args.len) : (i += 1) {678 inline while (i < args.len) : (i += 1) {
679 const x = args[i];679 const x = args[i];
680 if (i == 0) expect(x);680 if (i == 0) try expect(x);
681 if (i == 1) expect(x == 42);681 if (i == 1) try expect(x == 42);
682 }682 }
683}683}
684684
...@@ -688,7 +688,7 @@ test "inline for with same type but different values" {...@@ -688,7 +688,7 @@ test "inline for with same type but different values" {
688 var a: T = undefined;688 var a: T = undefined;
689 res += a.len;689 res += a.len;
690 }690 }
691 expect(res == 5);691 try expect(res == 5);
692}692}
693693
694test "refer to the type of a generic function" {694test "refer to the type of a generic function" {
...@@ -702,13 +702,13 @@ fn doNothingWithType(comptime T: type) void {}...@@ -702,13 +702,13 @@ fn doNothingWithType(comptime T: type) void {}
702test "zero extend from u0 to u1" {702test "zero extend from u0 to u1" {
703 var zero_u0: u0 = 0;703 var zero_u0: u0 = 0;
704 var zero_u1: u1 = zero_u0;704 var zero_u1: u1 = zero_u0;
705 expect(zero_u1 == 0);705 try expect(zero_u1 == 0);
706}706}
707707
708test "bit shift a u1" {708test "bit shift a u1" {
709 var x: u1 = 1;709 var x: u1 = 1;
710 var y = x << 0;710 var y = x << 0;
711 expect(y == 1);711 try expect(y == 1);
712}712}
713713
714test "comptime pointer cast array and then slice" {714test "comptime pointer cast array and then slice" {
...@@ -720,8 +720,8 @@ test "comptime pointer cast array and then slice" {...@@ -720,8 +720,8 @@ test "comptime pointer cast array and then slice" {
720 const ptrB: [*]const u8 = &array;720 const ptrB: [*]const u8 = &array;
721 const sliceB: []const u8 = ptrB[0..2];721 const sliceB: []const u8 = ptrB[0..2];
722722
723 expect(sliceA[1] == 2);723 try expect(sliceA[1] == 2);
724 expect(sliceB[1] == 2);724 try expect(sliceB[1] == 2);
725}725}
726726
727test "slice bounds in comptime concatenation" {727test "slice bounds in comptime concatenation" {
...@@ -730,46 +730,46 @@ test "slice bounds in comptime concatenation" {...@@ -730,46 +730,46 @@ test "slice bounds in comptime concatenation" {
730 break :blk b[8..9];730 break :blk b[8..9];
731 };731 };
732 const str = "" ++ bs;732 const str = "" ++ bs;
733 expect(str.len == 1);733 try expect(str.len == 1);
734 expect(std.mem.eql(u8, str, "1"));734 try expect(std.mem.eql(u8, str, "1"));
735735
736 const str2 = bs ++ "";736 const str2 = bs ++ "";
737 expect(str2.len == 1);737 try expect(str2.len == 1);
738 expect(std.mem.eql(u8, str2, "1"));738 try expect(std.mem.eql(u8, str2, "1"));
739}739}
740740
741test "comptime bitwise operators" {741test "comptime bitwise operators" {
742 comptime {742 comptime {
743 expect(3 & 1 == 1);743 try expect(3 & 1 == 1);
744 expect(3 & -1 == 3);744 try expect(3 & -1 == 3);
745 expect(-3 & -1 == -3);745 try expect(-3 & -1 == -3);
746 expect(3 | -1 == -1);746 try expect(3 | -1 == -1);
747 expect(-3 | -1 == -1);747 try expect(-3 | -1 == -1);
748 expect(3 ^ -1 == -4);748 try expect(3 ^ -1 == -4);
749 expect(-3 ^ -1 == 2);749 try expect(-3 ^ -1 == 2);
750 expect(~@as(i8, -1) == 0);750 try expect(~@as(i8, -1) == 0);
751 expect(~@as(i128, -1) == 0);751 try expect(~@as(i128, -1) == 0);
752 expect(18446744073709551615 & 18446744073709551611 == 18446744073709551611);752 try expect(18446744073709551615 & 18446744073709551611 == 18446744073709551611);
753 expect(-18446744073709551615 & -18446744073709551611 == -18446744073709551615);753 try expect(-18446744073709551615 & -18446744073709551611 == -18446744073709551615);
754 expect(~@as(u128, 0) == 0xffffffffffffffffffffffffffffffff);754 try expect(~@as(u128, 0) == 0xffffffffffffffffffffffffffffffff);
755 }755 }
756}756}
757757
758test "*align(1) u16 is the same as *align(1:0:2) u16" {758test "*align(1) u16 is the same as *align(1:0:2) u16" {
759 comptime {759 comptime {
760 expect(*align(1:0:2) u16 == *align(1) u16);760 try expect(*align(1:0:2) u16 == *align(1) u16);
761 expect(*align(2:0:2) u16 == *u16);761 try expect(*align(2:0:2) u16 == *u16);
762 }762 }
763}763}
764764
765test "array concatenation forces comptime" {765test "array concatenation forces comptime" {
766 var a = oneItem(3) ++ oneItem(4);766 var a = oneItem(3) ++ oneItem(4);
767 expect(std.mem.eql(i32, &a, &[_]i32{ 3, 4 }));767 try expect(std.mem.eql(i32, &a, &[_]i32{ 3, 4 }));
768}768}
769769
770test "array multiplication forces comptime" {770test "array multiplication forces comptime" {
771 var a = oneItem(3) ** scalar(2);771 var a = oneItem(3) ** scalar(2);
772 expect(std.mem.eql(i32, &a, &[_]i32{ 3, 3 }));772 try expect(std.mem.eql(i32, &a, &[_]i32{ 3, 3 }));
773}773}
774774
775fn oneItem(x: i32) [1]i32 {775fn oneItem(x: i32) [1]i32 {
...@@ -791,7 +791,7 @@ test "comptime assign int to optional int" {...@@ -791,7 +791,7 @@ test "comptime assign int to optional int" {
791 var x: ?i32 = null;791 var x: ?i32 = null;
792 x = 2;792 x = 2;
793 x.? *= 10;793 x.? *= 10;
794 expectEqual(20, x.?);794 try expectEqual(20, x.?);
795 }795 }
796}796}
797797
test/stage1/behavior/field_parent_ptr.zig+12-12
...@@ -1,13 +1,13 @@...@@ -1,13 +1,13 @@
1const expect = @import("std").testing.expect;1const expect = @import("std").testing.expect;
22
3test "@fieldParentPtr non-first field" {3test "@fieldParentPtr non-first field" {
4 testParentFieldPtr(&foo.c);4 try testParentFieldPtr(&foo.c);
5 comptime testParentFieldPtr(&foo.c);5 comptime try testParentFieldPtr(&foo.c);
6}6}
77
8test "@fieldParentPtr first field" {8test "@fieldParentPtr first field" {
9 testParentFieldPtrFirst(&foo.a);9 try testParentFieldPtrFirst(&foo.a);
10 comptime testParentFieldPtrFirst(&foo.a);10 comptime try testParentFieldPtrFirst(&foo.a);
11}11}
1212
13const Foo = struct {13const Foo = struct {
...@@ -24,18 +24,18 @@ const foo = Foo{...@@ -24,18 +24,18 @@ const foo = Foo{
24 .d = -10,24 .d = -10,
25};25};
2626
27fn testParentFieldPtr(c: *const i32) void {27fn testParentFieldPtr(c: *const i32) !void {
28 expect(c == &foo.c);28 try expect(c == &foo.c);
2929
30 const base = @fieldParentPtr(Foo, "c", c);30 const base = @fieldParentPtr(Foo, "c", c);
31 expect(base == &foo);31 try expect(base == &foo);
32 expect(&base.c == c);32 try expect(&base.c == c);
33}33}
3434
35fn testParentFieldPtrFirst(a: *const bool) void {35fn testParentFieldPtrFirst(a: *const bool) !void {
36 expect(a == &foo.a);36 try expect(a == &foo.a);
3737
38 const base = @fieldParentPtr(Foo, "a", a);38 const base = @fieldParentPtr(Foo, "a", a);
39 expect(base == &foo);39 try expect(base == &foo);
40 expect(&base.a == a);40 try expect(&base.a == a);
41}41}
test/stage1/behavior/floatop.zig+161-161
...@@ -8,441 +8,441 @@ const Vector = std.meta.Vector;...@@ -8,441 +8,441 @@ const Vector = std.meta.Vector;
8const epsilon = 0.000001;8const epsilon = 0.000001;
99
10test "@sqrt" {10test "@sqrt" {
11 comptime testSqrt();11 comptime try testSqrt();
12 testSqrt();12 try testSqrt();
13}13}
1414
15fn testSqrt() void {15fn testSqrt() !void {
16 {16 {
17 var a: f16 = 4;17 var a: f16 = 4;
18 expect(@sqrt(a) == 2);18 try expect(@sqrt(a) == 2);
19 }19 }
20 {20 {
21 var a: f32 = 9;21 var a: f32 = 9;
22 expect(@sqrt(a) == 3);22 try expect(@sqrt(a) == 3);
23 var b: f32 = 1.1;23 var b: f32 = 1.1;
24 expect(math.approxEqAbs(f32, @sqrt(b), 1.0488088481701516, epsilon));24 try expect(math.approxEqAbs(f32, @sqrt(b), 1.0488088481701516, epsilon));
25 }25 }
26 {26 {
27 var a: f64 = 25;27 var a: f64 = 25;
28 expect(@sqrt(a) == 5);28 try expect(@sqrt(a) == 5);
29 }29 }
30 {30 {
31 const a: comptime_float = 25.0;31 const a: comptime_float = 25.0;
32 expect(@sqrt(a) == 5.0);32 try expect(@sqrt(a) == 5.0);
33 }33 }
34 // TODO https://github.com/ziglang/zig/issues/402634 // TODO https://github.com/ziglang/zig/issues/4026
35 //{35 //{
36 // var a: f128 = 49;36 // var a: f128 = 49;
37 // expect(@sqrt(a) == 7);37 //try expect(@sqrt(a) == 7);
38 //}38 //}
39 {39 {
40 var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 3.3, 4.4 };40 var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 3.3, 4.4 };
41 var result = @sqrt(v);41 var result = @sqrt(v);
42 expect(math.approxEqAbs(f32, @sqrt(@as(f32, 1.1)), result[0], epsilon));42 try expect(math.approxEqAbs(f32, @sqrt(@as(f32, 1.1)), result[0], epsilon));
43 expect(math.approxEqAbs(f32, @sqrt(@as(f32, 2.2)), result[1], epsilon));43 try expect(math.approxEqAbs(f32, @sqrt(@as(f32, 2.2)), result[1], epsilon));
44 expect(math.approxEqAbs(f32, @sqrt(@as(f32, 3.3)), result[2], epsilon));44 try expect(math.approxEqAbs(f32, @sqrt(@as(f32, 3.3)), result[2], epsilon));
45 expect(math.approxEqAbs(f32, @sqrt(@as(f32, 4.4)), result[3], epsilon));45 try expect(math.approxEqAbs(f32, @sqrt(@as(f32, 4.4)), result[3], epsilon));
46 }46 }
47}47}
4848
49test "more @sqrt f16 tests" {49test "more @sqrt f16 tests" {
50 // TODO these are not all passing at comptime50 // TODO these are not all passing at comptime
51 expect(@sqrt(@as(f16, 0.0)) == 0.0);51 try expect(@sqrt(@as(f16, 0.0)) == 0.0);
52 expect(math.approxEqAbs(f16, @sqrt(@as(f16, 2.0)), 1.414214, epsilon));52 try expect(math.approxEqAbs(f16, @sqrt(@as(f16, 2.0)), 1.414214, epsilon));
53 expect(math.approxEqAbs(f16, @sqrt(@as(f16, 3.6)), 1.897367, epsilon));53 try expect(math.approxEqAbs(f16, @sqrt(@as(f16, 3.6)), 1.897367, epsilon));
54 expect(@sqrt(@as(f16, 4.0)) == 2.0);54 try expect(@sqrt(@as(f16, 4.0)) == 2.0);
55 expect(math.approxEqAbs(f16, @sqrt(@as(f16, 7.539840)), 2.745877, epsilon));55 try expect(math.approxEqAbs(f16, @sqrt(@as(f16, 7.539840)), 2.745877, epsilon));
56 expect(math.approxEqAbs(f16, @sqrt(@as(f16, 19.230934)), 4.385309, epsilon));56 try expect(math.approxEqAbs(f16, @sqrt(@as(f16, 19.230934)), 4.385309, epsilon));
57 expect(@sqrt(@as(f16, 64.0)) == 8.0);57 try expect(@sqrt(@as(f16, 64.0)) == 8.0);
58 expect(math.approxEqAbs(f16, @sqrt(@as(f16, 64.1)), 8.006248, epsilon));58 try expect(math.approxEqAbs(f16, @sqrt(@as(f16, 64.1)), 8.006248, epsilon));
59 expect(math.approxEqAbs(f16, @sqrt(@as(f16, 8942.230469)), 94.563370, epsilon));59 try expect(math.approxEqAbs(f16, @sqrt(@as(f16, 8942.230469)), 94.563370, epsilon));
6060
61 // special cases61 // special cases
62 expect(math.isPositiveInf(@sqrt(@as(f16, math.inf(f16)))));62 try expect(math.isPositiveInf(@sqrt(@as(f16, math.inf(f16)))));
63 expect(@sqrt(@as(f16, 0.0)) == 0.0);63 try expect(@sqrt(@as(f16, 0.0)) == 0.0);
64 expect(@sqrt(@as(f16, -0.0)) == -0.0);64 try expect(@sqrt(@as(f16, -0.0)) == -0.0);
65 expect(math.isNan(@sqrt(@as(f16, -1.0))));65 try expect(math.isNan(@sqrt(@as(f16, -1.0))));
66 expect(math.isNan(@sqrt(@as(f16, math.nan(f16)))));66 try expect(math.isNan(@sqrt(@as(f16, math.nan(f16)))));
67}67}
6868
69test "@sin" {69test "@sin" {
70 comptime testSin();70 comptime try testSin();
71 testSin();71 try testSin();
72}72}
7373
74fn testSin() void {74fn testSin() !void {
75 // TODO test f128, and c_longdouble75 // TODO test f128, and c_longdouble
76 // https://github.com/ziglang/zig/issues/402676 // https://github.com/ziglang/zig/issues/4026
77 {77 {
78 var a: f16 = 0;78 var a: f16 = 0;
79 expect(@sin(a) == 0);79 try expect(@sin(a) == 0);
80 }80 }
81 {81 {
82 var a: f32 = 0;82 var a: f32 = 0;
83 expect(@sin(a) == 0);83 try expect(@sin(a) == 0);
84 }84 }
85 {85 {
86 var a: f64 = 0;86 var a: f64 = 0;
87 expect(@sin(a) == 0);87 try expect(@sin(a) == 0);
88 }88 }
89 {89 {
90 var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 3.3, 4.4 };90 var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 3.3, 4.4 };
91 var result = @sin(v);91 var result = @sin(v);
92 expect(math.approxEqAbs(f32, @sin(@as(f32, 1.1)), result[0], epsilon));92 try expect(math.approxEqAbs(f32, @sin(@as(f32, 1.1)), result[0], epsilon));
93 expect(math.approxEqAbs(f32, @sin(@as(f32, 2.2)), result[1], epsilon));93 try expect(math.approxEqAbs(f32, @sin(@as(f32, 2.2)), result[1], epsilon));
94 expect(math.approxEqAbs(f32, @sin(@as(f32, 3.3)), result[2], epsilon));94 try expect(math.approxEqAbs(f32, @sin(@as(f32, 3.3)), result[2], epsilon));
95 expect(math.approxEqAbs(f32, @sin(@as(f32, 4.4)), result[3], epsilon));95 try expect(math.approxEqAbs(f32, @sin(@as(f32, 4.4)), result[3], epsilon));
96 }96 }
97}97}
9898
99test "@cos" {99test "@cos" {
100 comptime testCos();100 comptime try testCos();
101 testCos();101 try testCos();
102}102}
103103
104fn testCos() void {104fn testCos() !void {
105 // TODO test f128, and c_longdouble105 // TODO test f128, and c_longdouble
106 // https://github.com/ziglang/zig/issues/4026106 // https://github.com/ziglang/zig/issues/4026
107 {107 {
108 var a: f16 = 0;108 var a: f16 = 0;
109 expect(@cos(a) == 1);109 try expect(@cos(a) == 1);
110 }110 }
111 {111 {
112 var a: f32 = 0;112 var a: f32 = 0;
113 expect(@cos(a) == 1);113 try expect(@cos(a) == 1);
114 }114 }
115 {115 {
116 var a: f64 = 0;116 var a: f64 = 0;
117 expect(@cos(a) == 1);117 try expect(@cos(a) == 1);
118 }118 }
119 {119 {
120 var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 3.3, 4.4 };120 var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 3.3, 4.4 };
121 var result = @cos(v);121 var result = @cos(v);
122 expect(math.approxEqAbs(f32, @cos(@as(f32, 1.1)), result[0], epsilon));122 try expect(math.approxEqAbs(f32, @cos(@as(f32, 1.1)), result[0], epsilon));
123 expect(math.approxEqAbs(f32, @cos(@as(f32, 2.2)), result[1], epsilon));123 try expect(math.approxEqAbs(f32, @cos(@as(f32, 2.2)), result[1], epsilon));
124 expect(math.approxEqAbs(f32, @cos(@as(f32, 3.3)), result[2], epsilon));124 try expect(math.approxEqAbs(f32, @cos(@as(f32, 3.3)), result[2], epsilon));
125 expect(math.approxEqAbs(f32, @cos(@as(f32, 4.4)), result[3], epsilon));125 try expect(math.approxEqAbs(f32, @cos(@as(f32, 4.4)), result[3], epsilon));
126 }126 }
127}127}
128128
129test "@exp" {129test "@exp" {
130 comptime testExp();130 comptime try testExp();
131 testExp();131 try testExp();
132}132}
133133
134fn testExp() void {134fn testExp() !void {
135 // TODO test f128, and c_longdouble135 // TODO test f128, and c_longdouble
136 // https://github.com/ziglang/zig/issues/4026136 // https://github.com/ziglang/zig/issues/4026
137 {137 {
138 var a: f16 = 0;138 var a: f16 = 0;
139 expect(@exp(a) == 1);139 try expect(@exp(a) == 1);
140 }140 }
141 {141 {
142 var a: f32 = 0;142 var a: f32 = 0;
143 expect(@exp(a) == 1);143 try expect(@exp(a) == 1);
144 }144 }
145 {145 {
146 var a: f64 = 0;146 var a: f64 = 0;
147 expect(@exp(a) == 1);147 try expect(@exp(a) == 1);
148 }148 }
149 {149 {
150 var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 };150 var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 };
151 var result = @exp(v);151 var result = @exp(v);
152 expect(math.approxEqAbs(f32, @exp(@as(f32, 1.1)), result[0], epsilon));152 try expect(math.approxEqAbs(f32, @exp(@as(f32, 1.1)), result[0], epsilon));
153 expect(math.approxEqAbs(f32, @exp(@as(f32, 2.2)), result[1], epsilon));153 try expect(math.approxEqAbs(f32, @exp(@as(f32, 2.2)), result[1], epsilon));
154 expect(math.approxEqAbs(f32, @exp(@as(f32, 0.3)), result[2], epsilon));154 try expect(math.approxEqAbs(f32, @exp(@as(f32, 0.3)), result[2], epsilon));
155 expect(math.approxEqAbs(f32, @exp(@as(f32, 0.4)), result[3], epsilon));155 try expect(math.approxEqAbs(f32, @exp(@as(f32, 0.4)), result[3], epsilon));
156 }156 }
157}157}
158158
159test "@exp2" {159test "@exp2" {
160 comptime testExp2();160 comptime try testExp2();
161 testExp2();161 try testExp2();
162}162}
163163
164fn testExp2() void {164fn testExp2() !void {
165 // TODO test f128, and c_longdouble165 // TODO test f128, and c_longdouble
166 // https://github.com/ziglang/zig/issues/4026166 // https://github.com/ziglang/zig/issues/4026
167 {167 {
168 var a: f16 = 2;168 var a: f16 = 2;
169 expect(@exp2(a) == 4);169 try expect(@exp2(a) == 4);
170 }170 }
171 {171 {
172 var a: f32 = 2;172 var a: f32 = 2;
173 expect(@exp2(a) == 4);173 try expect(@exp2(a) == 4);
174 }174 }
175 {175 {
176 var a: f64 = 2;176 var a: f64 = 2;
177 expect(@exp2(a) == 4);177 try expect(@exp2(a) == 4);
178 }178 }
179 {179 {
180 var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 };180 var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 };
181 var result = @exp2(v);181 var result = @exp2(v);
182 expect(math.approxEqAbs(f32, @exp2(@as(f32, 1.1)), result[0], epsilon));182 try expect(math.approxEqAbs(f32, @exp2(@as(f32, 1.1)), result[0], epsilon));
183 expect(math.approxEqAbs(f32, @exp2(@as(f32, 2.2)), result[1], epsilon));183 try expect(math.approxEqAbs(f32, @exp2(@as(f32, 2.2)), result[1], epsilon));
184 expect(math.approxEqAbs(f32, @exp2(@as(f32, 0.3)), result[2], epsilon));184 try expect(math.approxEqAbs(f32, @exp2(@as(f32, 0.3)), result[2], epsilon));
185 expect(math.approxEqAbs(f32, @exp2(@as(f32, 0.4)), result[3], epsilon));185 try expect(math.approxEqAbs(f32, @exp2(@as(f32, 0.4)), result[3], epsilon));
186 }186 }
187}187}
188188
189test "@log" {189test "@log" {
190 // Old musl (and glibc?), and our current math.ln implementation do not return 1190 // Old musl (and glibc?), and our current math.ln implementation do not return 1
191 // so also accept those values.191 // so also accept those values.
192 comptime testLog();192 comptime try testLog();
193 testLog();193 try testLog();
194}194}
195195
196fn testLog() void {196fn testLog() !void {
197 // TODO test f128, and c_longdouble197 // TODO test f128, and c_longdouble
198 // https://github.com/ziglang/zig/issues/4026198 // https://github.com/ziglang/zig/issues/4026
199 {199 {
200 var a: f16 = e;200 var a: f16 = e;
201 expect(math.approxEqAbs(f16, @log(a), 1, epsilon));201 try expect(math.approxEqAbs(f16, @log(a), 1, epsilon));
202 }202 }
203 {203 {
204 var a: f32 = e;204 var a: f32 = e;
205 expect(@log(a) == 1 or @log(a) == @bitCast(f32, @as(u32, 0x3f7fffff)));205 try expect(@log(a) == 1 or @log(a) == @bitCast(f32, @as(u32, 0x3f7fffff)));
206 }206 }
207 {207 {
208 var a: f64 = e;208 var a: f64 = e;
209 expect(@log(a) == 1 or @log(a) == @bitCast(f64, @as(u64, 0x3ff0000000000000)));209 try expect(@log(a) == 1 or @log(a) == @bitCast(f64, @as(u64, 0x3ff0000000000000)));
210 }210 }
211 {211 {
212 var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 };212 var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 };
213 var result = @log(v);213 var result = @log(v);
214 expect(math.approxEqAbs(f32, @log(@as(f32, 1.1)), result[0], epsilon));214 try expect(math.approxEqAbs(f32, @log(@as(f32, 1.1)), result[0], epsilon));
215 expect(math.approxEqAbs(f32, @log(@as(f32, 2.2)), result[1], epsilon));215 try expect(math.approxEqAbs(f32, @log(@as(f32, 2.2)), result[1], epsilon));
216 expect(math.approxEqAbs(f32, @log(@as(f32, 0.3)), result[2], epsilon));216 try expect(math.approxEqAbs(f32, @log(@as(f32, 0.3)), result[2], epsilon));
217 expect(math.approxEqAbs(f32, @log(@as(f32, 0.4)), result[3], epsilon));217 try expect(math.approxEqAbs(f32, @log(@as(f32, 0.4)), result[3], epsilon));
218 }218 }
219}219}
220220
221test "@log2" {221test "@log2" {
222 comptime testLog2();222 comptime try testLog2();
223 testLog2();223 try testLog2();
224}224}
225225
226fn testLog2() void {226fn testLog2() !void {
227 // TODO test f128, and c_longdouble227 // TODO test f128, and c_longdouble
228 // https://github.com/ziglang/zig/issues/4026228 // https://github.com/ziglang/zig/issues/4026
229 {229 {
230 var a: f16 = 4;230 var a: f16 = 4;
231 expect(@log2(a) == 2);231 try expect(@log2(a) == 2);
232 }232 }
233 {233 {
234 var a: f32 = 4;234 var a: f32 = 4;
235 expect(@log2(a) == 2);235 try expect(@log2(a) == 2);
236 }236 }
237 {237 {
238 var a: f64 = 4;238 var a: f64 = 4;
239 expect(@log2(a) == 2);239 try expect(@log2(a) == 2);
240 }240 }
241 {241 {
242 var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 };242 var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 };
243 var result = @log2(v);243 var result = @log2(v);
244 expect(math.approxEqAbs(f32, @log2(@as(f32, 1.1)), result[0], epsilon));244 try expect(math.approxEqAbs(f32, @log2(@as(f32, 1.1)), result[0], epsilon));
245 expect(math.approxEqAbs(f32, @log2(@as(f32, 2.2)), result[1], epsilon));245 try expect(math.approxEqAbs(f32, @log2(@as(f32, 2.2)), result[1], epsilon));
246 expect(math.approxEqAbs(f32, @log2(@as(f32, 0.3)), result[2], epsilon));246 try expect(math.approxEqAbs(f32, @log2(@as(f32, 0.3)), result[2], epsilon));
247 expect(math.approxEqAbs(f32, @log2(@as(f32, 0.4)), result[3], epsilon));247 try expect(math.approxEqAbs(f32, @log2(@as(f32, 0.4)), result[3], epsilon));
248 }248 }
249}249}
250250
251test "@log10" {251test "@log10" {
252 comptime testLog10();252 comptime try testLog10();
253 testLog10();253 try testLog10();
254}254}
255255
256fn testLog10() void {256fn testLog10() !void {
257 // TODO test f128, and c_longdouble257 // TODO test f128, and c_longdouble
258 // https://github.com/ziglang/zig/issues/4026258 // https://github.com/ziglang/zig/issues/4026
259 {259 {
260 var a: f16 = 100;260 var a: f16 = 100;
261 expect(@log10(a) == 2);261 try expect(@log10(a) == 2);
262 }262 }
263 {263 {
264 var a: f32 = 100;264 var a: f32 = 100;
265 expect(@log10(a) == 2);265 try expect(@log10(a) == 2);
266 }266 }
267 {267 {
268 var a: f64 = 1000;268 var a: f64 = 1000;
269 expect(@log10(a) == 3);269 try expect(@log10(a) == 3);
270 }270 }
271 {271 {
272 var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 };272 var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 };
273 var result = @log10(v);273 var result = @log10(v);
274 expect(math.approxEqAbs(f32, @log10(@as(f32, 1.1)), result[0], epsilon));274 try expect(math.approxEqAbs(f32, @log10(@as(f32, 1.1)), result[0], epsilon));
275 expect(math.approxEqAbs(f32, @log10(@as(f32, 2.2)), result[1], epsilon));275 try expect(math.approxEqAbs(f32, @log10(@as(f32, 2.2)), result[1], epsilon));
276 expect(math.approxEqAbs(f32, @log10(@as(f32, 0.3)), result[2], epsilon));276 try expect(math.approxEqAbs(f32, @log10(@as(f32, 0.3)), result[2], epsilon));
277 expect(math.approxEqAbs(f32, @log10(@as(f32, 0.4)), result[3], epsilon));277 try expect(math.approxEqAbs(f32, @log10(@as(f32, 0.4)), result[3], epsilon));
278 }278 }
279}279}
280280
281test "@fabs" {281test "@fabs" {
282 comptime testFabs();282 comptime try testFabs();
283 testFabs();283 try testFabs();
284}284}
285285
286fn testFabs() void {286fn testFabs() !void {
287 // TODO test f128, and c_longdouble287 // TODO test f128, and c_longdouble
288 // https://github.com/ziglang/zig/issues/4026288 // https://github.com/ziglang/zig/issues/4026
289 {289 {
290 var a: f16 = -2.5;290 var a: f16 = -2.5;
291 var b: f16 = 2.5;291 var b: f16 = 2.5;
292 expect(@fabs(a) == 2.5);292 try expect(@fabs(a) == 2.5);
293 expect(@fabs(b) == 2.5);293 try expect(@fabs(b) == 2.5);
294 }294 }
295 {295 {
296 var a: f32 = -2.5;296 var a: f32 = -2.5;
297 var b: f32 = 2.5;297 var b: f32 = 2.5;
298 expect(@fabs(a) == 2.5);298 try expect(@fabs(a) == 2.5);
299 expect(@fabs(b) == 2.5);299 try expect(@fabs(b) == 2.5);
300 }300 }
301 {301 {
302 var a: f64 = -2.5;302 var a: f64 = -2.5;
303 var b: f64 = 2.5;303 var b: f64 = 2.5;
304 expect(@fabs(a) == 2.5);304 try expect(@fabs(a) == 2.5);
305 expect(@fabs(b) == 2.5);305 try expect(@fabs(b) == 2.5);
306 }306 }
307 {307 {
308 var v: Vector(4, f32) = [_]f32{ 1.1, -2.2, 0.3, -0.4 };308 var v: Vector(4, f32) = [_]f32{ 1.1, -2.2, 0.3, -0.4 };
309 var result = @fabs(v);309 var result = @fabs(v);
310 expect(math.approxEqAbs(f32, @fabs(@as(f32, 1.1)), result[0], epsilon));310 try expect(math.approxEqAbs(f32, @fabs(@as(f32, 1.1)), result[0], epsilon));
311 expect(math.approxEqAbs(f32, @fabs(@as(f32, -2.2)), result[1], epsilon));311 try expect(math.approxEqAbs(f32, @fabs(@as(f32, -2.2)), result[1], epsilon));
312 expect(math.approxEqAbs(f32, @fabs(@as(f32, 0.3)), result[2], epsilon));312 try expect(math.approxEqAbs(f32, @fabs(@as(f32, 0.3)), result[2], epsilon));
313 expect(math.approxEqAbs(f32, @fabs(@as(f32, -0.4)), result[3], epsilon));313 try expect(math.approxEqAbs(f32, @fabs(@as(f32, -0.4)), result[3], epsilon));
314 }314 }
315}315}
316316
317test "@floor" {317test "@floor" {
318 comptime testFloor();318 comptime try testFloor();
319 testFloor();319 try testFloor();
320}320}
321321
322fn testFloor() void {322fn testFloor() !void {
323 // TODO test f128, and c_longdouble323 // TODO test f128, and c_longdouble
324 // https://github.com/ziglang/zig/issues/4026324 // https://github.com/ziglang/zig/issues/4026
325 {325 {
326 var a: f16 = 2.1;326 var a: f16 = 2.1;
327 expect(@floor(a) == 2);327 try expect(@floor(a) == 2);
328 }328 }
329 {329 {
330 var a: f32 = 2.1;330 var a: f32 = 2.1;
331 expect(@floor(a) == 2);331 try expect(@floor(a) == 2);
332 }332 }
333 {333 {
334 var a: f64 = 3.5;334 var a: f64 = 3.5;
335 expect(@floor(a) == 3);335 try expect(@floor(a) == 3);
336 }336 }
337 {337 {
338 var v: Vector(4, f32) = [_]f32{ 1.1, -2.2, 0.3, -0.4 };338 var v: Vector(4, f32) = [_]f32{ 1.1, -2.2, 0.3, -0.4 };
339 var result = @floor(v);339 var result = @floor(v);
340 expect(math.approxEqAbs(f32, @floor(@as(f32, 1.1)), result[0], epsilon));340 try expect(math.approxEqAbs(f32, @floor(@as(f32, 1.1)), result[0], epsilon));
341 expect(math.approxEqAbs(f32, @floor(@as(f32, -2.2)), result[1], epsilon));341 try expect(math.approxEqAbs(f32, @floor(@as(f32, -2.2)), result[1], epsilon));
342 expect(math.approxEqAbs(f32, @floor(@as(f32, 0.3)), result[2], epsilon));342 try expect(math.approxEqAbs(f32, @floor(@as(f32, 0.3)), result[2], epsilon));
343 expect(math.approxEqAbs(f32, @floor(@as(f32, -0.4)), result[3], epsilon));343 try expect(math.approxEqAbs(f32, @floor(@as(f32, -0.4)), result[3], epsilon));
344 }344 }
345}345}
346346
347test "@ceil" {347test "@ceil" {
348 comptime testCeil();348 comptime try testCeil();
349 testCeil();349 try testCeil();
350}350}
351351
352fn testCeil() void {352fn testCeil() !void {
353 // TODO test f128, and c_longdouble353 // TODO test f128, and c_longdouble
354 // https://github.com/ziglang/zig/issues/4026354 // https://github.com/ziglang/zig/issues/4026
355 {355 {
356 var a: f16 = 2.1;356 var a: f16 = 2.1;
357 expect(@ceil(a) == 3);357 try expect(@ceil(a) == 3);
358 }358 }
359 {359 {
360 var a: f32 = 2.1;360 var a: f32 = 2.1;
361 expect(@ceil(a) == 3);361 try expect(@ceil(a) == 3);
362 }362 }
363 {363 {
364 var a: f64 = 3.5;364 var a: f64 = 3.5;
365 expect(@ceil(a) == 4);365 try expect(@ceil(a) == 4);
366 }366 }
367 {367 {
368 var v: Vector(4, f32) = [_]f32{ 1.1, -2.2, 0.3, -0.4 };368 var v: Vector(4, f32) = [_]f32{ 1.1, -2.2, 0.3, -0.4 };
369 var result = @ceil(v);369 var result = @ceil(v);
370 expect(math.approxEqAbs(f32, @ceil(@as(f32, 1.1)), result[0], epsilon));370 try expect(math.approxEqAbs(f32, @ceil(@as(f32, 1.1)), result[0], epsilon));
371 expect(math.approxEqAbs(f32, @ceil(@as(f32, -2.2)), result[1], epsilon));371 try expect(math.approxEqAbs(f32, @ceil(@as(f32, -2.2)), result[1], epsilon));
372 expect(math.approxEqAbs(f32, @ceil(@as(f32, 0.3)), result[2], epsilon));372 try expect(math.approxEqAbs(f32, @ceil(@as(f32, 0.3)), result[2], epsilon));
373 expect(math.approxEqAbs(f32, @ceil(@as(f32, -0.4)), result[3], epsilon));373 try expect(math.approxEqAbs(f32, @ceil(@as(f32, -0.4)), result[3], epsilon));
374 }374 }
375}375}
376376
377test "@trunc" {377test "@trunc" {
378 comptime testTrunc();378 comptime try testTrunc();
379 testTrunc();379 try testTrunc();
380}380}
381381
382fn testTrunc() void {382fn testTrunc() !void {
383 // TODO test f128, and c_longdouble383 // TODO test f128, and c_longdouble
384 // https://github.com/ziglang/zig/issues/4026384 // https://github.com/ziglang/zig/issues/4026
385 {385 {
386 var a: f16 = 2.1;386 var a: f16 = 2.1;
387 expect(@trunc(a) == 2);387 try expect(@trunc(a) == 2);
388 }388 }
389 {389 {
390 var a: f32 = 2.1;390 var a: f32 = 2.1;
391 expect(@trunc(a) == 2);391 try expect(@trunc(a) == 2);
392 }392 }
393 {393 {
394 var a: f64 = -3.5;394 var a: f64 = -3.5;
395 expect(@trunc(a) == -3);395 try expect(@trunc(a) == -3);
396 }396 }
397 {397 {
398 var v: Vector(4, f32) = [_]f32{ 1.1, -2.2, 0.3, -0.4 };398 var v: Vector(4, f32) = [_]f32{ 1.1, -2.2, 0.3, -0.4 };
399 var result = @trunc(v);399 var result = @trunc(v);
400 expect(math.approxEqAbs(f32, @trunc(@as(f32, 1.1)), result[0], epsilon));400 try expect(math.approxEqAbs(f32, @trunc(@as(f32, 1.1)), result[0], epsilon));
401 expect(math.approxEqAbs(f32, @trunc(@as(f32, -2.2)), result[1], epsilon));401 try expect(math.approxEqAbs(f32, @trunc(@as(f32, -2.2)), result[1], epsilon));
402 expect(math.approxEqAbs(f32, @trunc(@as(f32, 0.3)), result[2], epsilon));402 try expect(math.approxEqAbs(f32, @trunc(@as(f32, 0.3)), result[2], epsilon));
403 expect(math.approxEqAbs(f32, @trunc(@as(f32, -0.4)), result[3], epsilon));403 try expect(math.approxEqAbs(f32, @trunc(@as(f32, -0.4)), result[3], epsilon));
404 }404 }
405}405}
406406
407test "floating point comparisons" {407test "floating point comparisons" {
408 testFloatComparisons();408 try testFloatComparisons();
409 comptime testFloatComparisons();409 comptime try testFloatComparisons();
410}410}
411411
412fn testFloatComparisons() void {412fn testFloatComparisons() !void {
413 inline for ([_]type{ f16, f32, f64, f128 }) |ty| {413 inline for ([_]type{ f16, f32, f64, f128 }) |ty| {
414 // No decimal part414 // No decimal part
415 {415 {
416 const x: ty = 1.0;416 const x: ty = 1.0;
417 expect(x == 1);417 try expect(x == 1);
418 expect(x != 0);418 try expect(x != 0);
419 expect(x > 0);419 try expect(x > 0);
420 expect(x < 2);420 try expect(x < 2);
421 expect(x >= 1);421 try expect(x >= 1);
422 expect(x <= 1);422 try expect(x <= 1);
423 }423 }
424 // Non-zero decimal part424 // Non-zero decimal part
425 {425 {
426 const x: ty = 1.5;426 const x: ty = 1.5;
427 expect(x != 1);427 try expect(x != 1);
428 expect(x != 2);428 try expect(x != 2);
429 expect(x > 1);429 try expect(x > 1);
430 expect(x < 2);430 try expect(x < 2);
431 expect(x >= 1);431 try expect(x >= 1);
432 expect(x <= 2);432 try expect(x <= 2);
433 }433 }
434 }434 }
435}435}
436436
437test "different sized float comparisons" {437test "different sized float comparisons" {
438 testDifferentSizedFloatComparisons();438 try testDifferentSizedFloatComparisons();
439 comptime testDifferentSizedFloatComparisons();439 comptime try testDifferentSizedFloatComparisons();
440}440}
441441
442fn testDifferentSizedFloatComparisons() void {442fn testDifferentSizedFloatComparisons() !void {
443 var a: f16 = 1;443 var a: f16 = 1;
444 var b: f64 = 2;444 var b: f64 = 2;
445 expect(a < b);445 try expect(a < b);
446}446}
447447
448// TODO This is waiting on library support for the Windows build (not sure why the other's don't need it)448// TODO This is waiting on library support for the Windows build (not sure why the other's don't need it)
...@@ -456,10 +456,10 @@ fn testDifferentSizedFloatComparisons() void {...@@ -456,10 +456,10 @@ fn testDifferentSizedFloatComparisons() void {
456// // https://github.com/ziglang/zig/issues/4026456// // https://github.com/ziglang/zig/issues/4026
457// {457// {
458// var a: f32 = 2.1;458// var a: f32 = 2.1;
459// expect(@nearbyint(a) == 2);459// try expect(@nearbyint(a) == 2);
460// }460// }
461// {461// {
462// var a: f64 = -3.75;462// var a: f64 = -3.75;
463// expect(@nearbyint(a) == -4);463// try expect(@nearbyint(a) == -4);
464// }464// }
465//}465//}
test/stage1/behavior/fn.zig+41-41
...@@ -4,7 +4,7 @@ const expect = testing.expect;...@@ -4,7 +4,7 @@ const expect = testing.expect;
4const expectEqual = testing.expectEqual;4const expectEqual = testing.expectEqual;
55
6test "params" {6test "params" {
7 expect(testParamsAdd(22, 11) == 33);7 try expect(testParamsAdd(22, 11) == 33);
8}8}
9fn testParamsAdd(a: i32, b: i32) i32 {9fn testParamsAdd(a: i32, b: i32) i32 {
10 return a + b;10 return a + b;
...@@ -19,37 +19,37 @@ fn testLocVars(b: i32) void {...@@ -19,37 +19,37 @@ fn testLocVars(b: i32) void {
19}19}
2020
21test "void parameters" {21test "void parameters" {
22 voidFun(1, void{}, 2, {});22 try voidFun(1, void{}, 2, {});
23}23}
24fn voidFun(a: i32, b: void, c: i32, d: void) void {24fn voidFun(a: i32, b: void, c: i32, d: void) !void {
25 const v = b;25 const v = b;
26 const vv: void = if (a == 1) v else {};26 const vv: void = if (a == 1) v else {};
27 expect(a + c == 3);27 try expect(a + c == 3);
28 return vv;28 return vv;
29}29}
3030
31test "mutable local variables" {31test "mutable local variables" {
32 var zero: i32 = 0;32 var zero: i32 = 0;
33 expect(zero == 0);33 try expect(zero == 0);
3434
35 var i = @as(i32, 0);35 var i = @as(i32, 0);
36 while (i != 3) {36 while (i != 3) {
37 i += 1;37 i += 1;
38 }38 }
39 expect(i == 3);39 try expect(i == 3);
40}40}
4141
42test "separate block scopes" {42test "separate block scopes" {
43 {43 {
44 const no_conflict: i32 = 5;44 const no_conflict: i32 = 5;
45 expect(no_conflict == 5);45 try expect(no_conflict == 5);
46 }46 }
4747
48 const c = x: {48 const c = x: {
49 const no_conflict = @as(i32, 10);49 const no_conflict = @as(i32, 10);
50 break :x no_conflict;50 break :x no_conflict;
51 };51 };
52 expect(c == 10);52 try expect(c == 10);
53}53}
5454
55test "call function with empty string" {55test "call function with empty string" {
...@@ -62,7 +62,7 @@ fn @"weird function name"() i32 {...@@ -62,7 +62,7 @@ fn @"weird function name"() i32 {
62 return 1234;62 return 1234;
63}63}
64test "weird function name" {64test "weird function name" {
65 expect(@"weird function name"() == 1234);65 try expect(@"weird function name"() == 1234);
66}66}
6767
68test "implicit cast function unreachable return" {68test "implicit cast function unreachable return" {
...@@ -83,7 +83,7 @@ test "function pointers" {...@@ -83,7 +83,7 @@ test "function pointers" {
83 fn4,83 fn4,
84 };84 };
85 for (fns) |f, i| {85 for (fns) |f, i| {
86 expect(f() == @intCast(u32, i) + 5);86 try expect(f() == @intCast(u32, i) + 5);
87 }87 }
88}88}
89fn fn1() u32 {89fn fn1() u32 {
...@@ -100,12 +100,12 @@ fn fn4() u32 {...@@ -100,12 +100,12 @@ fn fn4() u32 {
100}100}
101101
102test "number literal as an argument" {102test "number literal as an argument" {
103 numberLiteralArg(3);103 try numberLiteralArg(3);
104 comptime numberLiteralArg(3);104 comptime try numberLiteralArg(3);
105}105}
106106
107fn numberLiteralArg(a: anytype) void {107fn numberLiteralArg(a: anytype) !void {
108 expect(a == 3);108 try expect(a == 3);
109}109}
110110
111test "assign inline fn to const variable" {111test "assign inline fn to const variable" {
...@@ -116,7 +116,7 @@ test "assign inline fn to const variable" {...@@ -116,7 +116,7 @@ test "assign inline fn to const variable" {
116fn inlineFn() callconv(.Inline) void {}116fn inlineFn() callconv(.Inline) void {}
117117
118test "pass by non-copying value" {118test "pass by non-copying value" {
119 expect(addPointCoords(Point{ .x = 1, .y = 2 }) == 3);119 try expect(addPointCoords(Point{ .x = 1, .y = 2 }) == 3);
120}120}
121121
122const Point = struct {122const Point = struct {
...@@ -129,17 +129,17 @@ fn addPointCoords(pt: Point) i32 {...@@ -129,17 +129,17 @@ fn addPointCoords(pt: Point) i32 {
129}129}
130130
131test "pass by non-copying value through var arg" {131test "pass by non-copying value through var arg" {
132 expect(addPointCoordsVar(Point{ .x = 1, .y = 2 }) == 3);132 try expect((try addPointCoordsVar(Point{ .x = 1, .y = 2 })) == 3);
133}133}
134134
135fn addPointCoordsVar(pt: anytype) i32 {135fn addPointCoordsVar(pt: anytype) !i32 {
136 comptime expect(@TypeOf(pt) == Point);136 comptime try expect(@TypeOf(pt) == Point);
137 return pt.x + pt.y;137 return pt.x + pt.y;
138}138}
139139
140test "pass by non-copying value as method" {140test "pass by non-copying value as method" {
141 var pt = Point2{ .x = 1, .y = 2 };141 var pt = Point2{ .x = 1, .y = 2 };
142 expect(pt.addPointCoords() == 3);142 try expect(pt.addPointCoords() == 3);
143}143}
144144
145const Point2 = struct {145const Point2 = struct {
...@@ -153,7 +153,7 @@ const Point2 = struct {...@@ -153,7 +153,7 @@ const Point2 = struct {
153153
154test "pass by non-copying value as method, which is generic" {154test "pass by non-copying value as method, which is generic" {
155 var pt = Point3{ .x = 1, .y = 2 };155 var pt = Point3{ .x = 1, .y = 2 };
156 expect(pt.addPointCoords(i32) == 3);156 try expect(pt.addPointCoords(i32) == 3);
157}157}
158158
159const Point3 = struct {159const Point3 = struct {
...@@ -168,7 +168,7 @@ const Point3 = struct {...@@ -168,7 +168,7 @@ const Point3 = struct {
168test "pass by non-copying value as method, at comptime" {168test "pass by non-copying value as method, at comptime" {
169 comptime {169 comptime {
170 var pt = Point2{ .x = 1, .y = 2 };170 var pt = Point2{ .x = 1, .y = 2 };
171 expect(pt.addPointCoords() == 3);171 try expect(pt.addPointCoords() == 3);
172 }172 }
173}173}
174174
...@@ -184,7 +184,7 @@ fn outer(y: u32) fn (u32) u32 {...@@ -184,7 +184,7 @@ fn outer(y: u32) fn (u32) u32 {
184184
185test "return inner function which references comptime variable of outer function" {185test "return inner function which references comptime variable of outer function" {
186 var func = outer(10);186 var func = outer(10);
187 expect(func(3) == 7);187 try expect(func(3) == 7);
188}188}
189189
190test "extern struct with stdcallcc fn pointer" {190test "extern struct with stdcallcc fn pointer" {
...@@ -198,16 +198,16 @@ test "extern struct with stdcallcc fn pointer" {...@@ -198,16 +198,16 @@ test "extern struct with stdcallcc fn pointer" {
198198
199 var s: S = undefined;199 var s: S = undefined;
200 s.ptr = S.foo;200 s.ptr = S.foo;
201 expect(s.ptr() == 1234);201 try expect(s.ptr() == 1234);
202}202}
203203
204test "implicit cast fn call result to optional in field result" {204test "implicit cast fn call result to optional in field result" {
205 const S = struct {205 const S = struct {
206 fn entry() void {206 fn entry() !void {
207 var x = Foo{207 var x = Foo{
208 .field = optionalPtr(),208 .field = optionalPtr(),
209 };209 };
210 expect(x.field.?.* == 999);210 try expect(x.field.?.* == 999);
211 }211 }
212212
213 const glob: i32 = 999;213 const glob: i32 = 999;
...@@ -220,8 +220,8 @@ test "implicit cast fn call result to optional in field result" {...@@ -220,8 +220,8 @@ test "implicit cast fn call result to optional in field result" {
220 field: ?*const i32,220 field: ?*const i32,
221 };221 };
222 };222 };
223 S.entry();223 try S.entry();
224 comptime S.entry();224 comptime try S.entry();
225}225}
226226
227test "discard the result of a function that returns a struct" {227test "discard the result of a function that returns a struct" {
...@@ -245,26 +245,26 @@ test "discard the result of a function that returns a struct" {...@@ -245,26 +245,26 @@ test "discard the result of a function that returns a struct" {
245245
246test "function call with anon list literal" {246test "function call with anon list literal" {
247 const S = struct {247 const S = struct {
248 fn doTheTest() void {248 fn doTheTest() !void {
249 consumeVec(.{ 9, 8, 7 });249 try consumeVec(.{ 9, 8, 7 });
250 }250 }
251251
252 fn consumeVec(vec: [3]f32) void {252 fn consumeVec(vec: [3]f32) !void {
253 expect(vec[0] == 9);253 try expect(vec[0] == 9);
254 expect(vec[1] == 8);254 try expect(vec[1] == 8);
255 expect(vec[2] == 7);255 try expect(vec[2] == 7);
256 }256 }
257 };257 };
258 S.doTheTest();258 try S.doTheTest();
259 comptime S.doTheTest();259 comptime try S.doTheTest();
260}260}
261261
262test "ability to give comptime types and non comptime types to same parameter" {262test "ability to give comptime types and non comptime types to same parameter" {
263 const S = struct {263 const S = struct {
264 fn doTheTest() void {264 fn doTheTest() !void {
265 var x: i32 = 1;265 var x: i32 = 1;
266 expect(foo(x) == 10);266 try expect(foo(x) == 10);
267 expect(foo(i32) == 20);267 try expect(foo(i32) == 20);
268 }268 }
269269
270 fn foo(arg: anytype) i32 {270 fn foo(arg: anytype) i32 {
...@@ -272,8 +272,8 @@ test "ability to give comptime types and non comptime types to same parameter" {...@@ -272,8 +272,8 @@ test "ability to give comptime types and non comptime types to same parameter" {
272 return 9 + arg;272 return 9 + arg;
273 }273 }
274 };274 };
275 S.doTheTest();275 try S.doTheTest();
276 comptime S.doTheTest();276 comptime try S.doTheTest();
277}277}
278278
279test "function with inferred error set but returning no error" {279test "function with inferred error set but returning no error" {
...@@ -282,5 +282,5 @@ test "function with inferred error set but returning no error" {...@@ -282,5 +282,5 @@ test "function with inferred error set but returning no error" {
282 };282 };
283283
284 const return_ty = @typeInfo(@TypeOf(S.foo)).Fn.return_type.?;284 const return_ty = @typeInfo(@TypeOf(S.foo)).Fn.return_type.?;
285 expectEqual(0, @typeInfo(@typeInfo(return_ty).ErrorUnion.error_set).ErrorSet.?.len);285 try expectEqual(0, @typeInfo(@typeInfo(return_ty).ErrorUnion.error_set).ErrorSet.?.len);
286}286}
test/stage1/behavior/fn_delegation.zig+4-4
...@@ -32,8 +32,8 @@ fn custom(comptime T: type, comptime num: u64) fn (T) u64 {...@@ -32,8 +32,8 @@ fn custom(comptime T: type, comptime num: u64) fn (T) u64 {
3232
33test "fn delegation" {33test "fn delegation" {
34 const foo = Foo{};34 const foo = Foo{};
35 expect(foo.one() == 11);35 try expect(foo.one() == 11);
36 expect(foo.two() == 12);36 try expect(foo.two() == 12);
37 expect(foo.three() == 13);37 try expect(foo.three() == 13);
38 expect(foo.four() == 14);38 try expect(foo.four() == 14);
39}39}
test/stage1/behavior/fn_in_struct_in_comptime.zig+1-1
...@@ -13,5 +13,5 @@ fn get_foo() fn (*u8) usize {...@@ -13,5 +13,5 @@ fn get_foo() fn (*u8) usize {
1313
14test "define a function in an anonymous struct in comptime" {14test "define a function in an anonymous struct in comptime" {
15 const foo = get_foo();15 const foo = get_foo();
16 expect(foo(@intToPtr(*u8, 12345)) == 12345);16 try expect(foo(@intToPtr(*u8, 12345)) == 12345);
17}17}
test/stage1/behavior/for.zig+28-28
...@@ -27,12 +27,12 @@ test "for loop with pointer elem var" {...@@ -27,12 +27,12 @@ test "for loop with pointer elem var" {
27 var target: [source.len]u8 = undefined;27 var target: [source.len]u8 = undefined;
28 mem.copy(u8, target[0..], source);28 mem.copy(u8, target[0..], source);
29 mangleString(target[0..]);29 mangleString(target[0..]);
30 expect(mem.eql(u8, &target, "bcdefgh"));30 try expect(mem.eql(u8, &target, "bcdefgh"));
3131
32 for (source) |*c, i|32 for (source) |*c, i|
33 expect(@TypeOf(c) == *const u8);33 try expect(@TypeOf(c) == *const u8);
34 for (target) |*c, i|34 for (target) |*c, i|
35 expect(@TypeOf(c) == *u8);35 try expect(@TypeOf(c) == *u8);
36}36}
3737
38fn mangleString(s: []u8) void {38fn mangleString(s: []u8) void {
...@@ -75,15 +75,15 @@ test "basic for loop" {...@@ -75,15 +75,15 @@ test "basic for loop" {
75 buf_index += 1;75 buf_index += 1;
76 }76 }
7777
78 expect(mem.eql(u8, buffer[0..buf_index], &expected_result));78 try expect(mem.eql(u8, buffer[0..buf_index], &expected_result));
79}79}
8080
81test "break from outer for loop" {81test "break from outer for loop" {
82 testBreakOuter();82 try testBreakOuter();
83 comptime testBreakOuter();83 comptime try testBreakOuter();
84}84}
8585
86fn testBreakOuter() void {86fn testBreakOuter() !void {
87 var array = "aoeu";87 var array = "aoeu";
88 var count: usize = 0;88 var count: usize = 0;
89 outer: for (array) |_| {89 outer: for (array) |_| {
...@@ -92,15 +92,15 @@ fn testBreakOuter() void {...@@ -92,15 +92,15 @@ fn testBreakOuter() void {
92 break :outer;92 break :outer;
93 }93 }
94 }94 }
95 expect(count == 1);95 try expect(count == 1);
96}96}
9797
98test "continue outer for loop" {98test "continue outer for loop" {
99 testContinueOuter();99 try testContinueOuter();
100 comptime testContinueOuter();100 comptime try testContinueOuter();
101}101}
102102
103fn testContinueOuter() void {103fn testContinueOuter() !void {
104 var array = "aoeu";104 var array = "aoeu";
105 var counter: usize = 0;105 var counter: usize = 0;
106 outer: for (array) |_| {106 outer: for (array) |_| {
...@@ -109,28 +109,28 @@ fn testContinueOuter() void {...@@ -109,28 +109,28 @@ fn testContinueOuter() void {
109 continue :outer;109 continue :outer;
110 }110 }
111 }111 }
112 expect(counter == array.len);112 try expect(counter == array.len);
113}113}
114114
115test "2 break statements and an else" {115test "2 break statements and an else" {
116 const S = struct {116 const S = struct {
117 fn entry(t: bool, f: bool) void {117 fn entry(t: bool, f: bool) !void {
118 var buf: [10]u8 = undefined;118 var buf: [10]u8 = undefined;
119 var ok = false;119 var ok = false;
120 ok = for (buf) |item| {120 ok = for (buf) |item| {
121 if (f) break false;121 if (f) break false;
122 if (t) break true;122 if (t) break true;
123 } else false;123 } else false;
124 expect(ok);124 try expect(ok);
125 }125 }
126 };126 };
127 S.entry(true, false);127 try S.entry(true, false);
128 comptime S.entry(true, false);128 comptime try S.entry(true, false);
129}129}
130130
131test "for with null and T peer types and inferred result location type" {131test "for with null and T peer types and inferred result location type" {
132 const S = struct {132 const S = struct {
133 fn doTheTest(slice: []const u8) void {133 fn doTheTest(slice: []const u8) !void {
134 if (for (slice) |item| {134 if (for (slice) |item| {
135 if (item == 10) {135 if (item == 10) {
136 break item;136 break item;
...@@ -140,33 +140,33 @@ test "for with null and T peer types and inferred result location type" {...@@ -140,33 +140,33 @@ test "for with null and T peer types and inferred result location type" {
140 }140 }
141 }141 }
142 };142 };
143 S.doTheTest(&[_]u8{ 1, 2 });143 try S.doTheTest(&[_]u8{ 1, 2 });
144 comptime S.doTheTest(&[_]u8{ 1, 2 });144 comptime try S.doTheTest(&[_]u8{ 1, 2 });
145}145}
146146
147test "for copies its payload" {147test "for copies its payload" {
148 const S = struct {148 const S = struct {
149 fn doTheTest() void {149 fn doTheTest() !void {
150 var x = [_]usize{ 1, 2, 3 };150 var x = [_]usize{ 1, 2, 3 };
151 for (x) |value, i| {151 for (x) |value, i| {
152 // Modify the original array152 // Modify the original array
153 x[i] += 99;153 x[i] += 99;
154 expectEqual(value, i + 1);154 try expectEqual(value, i + 1);
155 }155 }
156 }156 }
157 };157 };
158 S.doTheTest();158 try S.doTheTest();
159 comptime S.doTheTest();159 comptime try S.doTheTest();
160}160}
161161
162test "for on slice with allowzero ptr" {162test "for on slice with allowzero ptr" {
163 const S = struct {163 const S = struct {
164 fn doTheTest(slice: []const u8) void {164 fn doTheTest(slice: []const u8) !void {
165 var ptr = @ptrCast([*]allowzero const u8, slice.ptr)[0..slice.len];165 var ptr = @ptrCast([*]allowzero const u8, slice.ptr)[0..slice.len];
166 for (ptr) |x, i| expect(x == i + 1);166 for (ptr) |x, i| try expect(x == i + 1);
167 for (ptr) |*x, i| expect(x.* == i + 1);167 for (ptr) |*x, i| try expect(x.* == i + 1);
168 }168 }
169 };169 };
170 S.doTheTest(&[_]u8{ 1, 2, 3, 4 });170 try S.doTheTest(&[_]u8{ 1, 2, 3, 4 });
171 comptime S.doTheTest(&[_]u8{ 1, 2, 3, 4 });171 comptime try S.doTheTest(&[_]u8{ 1, 2, 3, 4 });
172}172}
test/stage1/behavior/generics.zig+25-25
...@@ -4,9 +4,9 @@ const expect = testing.expect;...@@ -4,9 +4,9 @@ const expect = testing.expect;
4const expectEqual = testing.expectEqual;4const expectEqual = testing.expectEqual;
55
6test "simple generic fn" {6test "simple generic fn" {
7 expect(max(i32, 3, -1) == 3);7 try expect(max(i32, 3, -1) == 3);
8 expect(max(f32, 0.123, 0.456) == 0.456);8 try expect(max(f32, 0.123, 0.456) == 0.456);
9 expect(add(2, 3) == 5);9 try expect(add(2, 3) == 5);
10}10}
1111
12fn max(comptime T: type, a: T, b: T) T {12fn max(comptime T: type, a: T, b: T) T {
...@@ -19,7 +19,7 @@ fn add(comptime a: i32, b: i32) i32 {...@@ -19,7 +19,7 @@ fn add(comptime a: i32, b: i32) i32 {
1919
20const the_max = max(u32, 1234, 5678);20const the_max = max(u32, 1234, 5678);
21test "compile time generic eval" {21test "compile time generic eval" {
22 expect(the_max == 5678);22 try expect(the_max == 5678);
23}23}
2424
25fn gimmeTheBigOne(a: u32, b: u32) u32 {25fn gimmeTheBigOne(a: u32, b: u32) u32 {
...@@ -35,19 +35,19 @@ fn sameButWithFloats(a: f64, b: f64) f64 {...@@ -35,19 +35,19 @@ fn sameButWithFloats(a: f64, b: f64) f64 {
35}35}
3636
37test "fn with comptime args" {37test "fn with comptime args" {
38 expect(gimmeTheBigOne(1234, 5678) == 5678);38 try expect(gimmeTheBigOne(1234, 5678) == 5678);
39 expect(shouldCallSameInstance(34, 12) == 34);39 try expect(shouldCallSameInstance(34, 12) == 34);
40 expect(sameButWithFloats(0.43, 0.49) == 0.49);40 try expect(sameButWithFloats(0.43, 0.49) == 0.49);
41}41}
4242
43test "var params" {43test "var params" {
44 expect(max_i32(12, 34) == 34);44 try expect(max_i32(12, 34) == 34);
45 expect(max_f64(1.2, 3.4) == 3.4);45 try expect(max_f64(1.2, 3.4) == 3.4);
46}46}
4747
48comptime {48comptime {
49 expect(max_i32(12, 34) == 34);49 try expect(max_i32(12, 34) == 34);
50 expect(max_f64(1.2, 3.4) == 3.4);50 try expect(max_f64(1.2, 3.4) == 3.4);
51}51}
5252
53fn max_var(a: anytype, b: anytype) @TypeOf(a + b) {53fn max_var(a: anytype, b: anytype) @TypeOf(a + b) {
...@@ -79,8 +79,8 @@ test "function with return type type" {...@@ -79,8 +79,8 @@ test "function with return type type" {
79 var list2: List(i32) = undefined;79 var list2: List(i32) = undefined;
80 list.length = 10;80 list.length = 10;
81 list2.length = 10;81 list2.length = 10;
82 expect(list.prealloc_items.len == 8);82 try expect(list.prealloc_items.len == 8);
83 expect(list2.prealloc_items.len == 8);83 try expect(list2.prealloc_items.len == 8);
84}84}
8585
86test "generic struct" {86test "generic struct" {
...@@ -92,9 +92,9 @@ test "generic struct" {...@@ -92,9 +92,9 @@ test "generic struct" {
92 .value = true,92 .value = true,
93 .next = null,93 .next = null,
94 };94 };
95 expect(a1.value == 13);95 try expect(a1.value == 13);
96 expect(a1.value == a1.getVal());96 try expect(a1.value == a1.getVal());
97 expect(b1.getVal());97 try expect(b1.getVal());
98}98}
99fn GenNode(comptime T: type) type {99fn GenNode(comptime T: type) type {
100 return struct {100 return struct {
...@@ -107,7 +107,7 @@ fn GenNode(comptime T: type) type {...@@ -107,7 +107,7 @@ fn GenNode(comptime T: type) type {
107}107}
108108
109test "const decls in struct" {109test "const decls in struct" {
110 expect(GenericDataThing(3).count_plus_one == 4);110 try expect(GenericDataThing(3).count_plus_one == 4);
111}111}
112fn GenericDataThing(comptime count: isize) type {112fn GenericDataThing(comptime count: isize) type {
113 return struct {113 return struct {
...@@ -116,15 +116,15 @@ fn GenericDataThing(comptime count: isize) type {...@@ -116,15 +116,15 @@ fn GenericDataThing(comptime count: isize) type {
116}116}
117117
118test "use generic param in generic param" {118test "use generic param in generic param" {
119 expect(aGenericFn(i32, 3, 4) == 7);119 try expect(aGenericFn(i32, 3, 4) == 7);
120}120}
121fn aGenericFn(comptime T: type, comptime a: T, b: T) T {121fn aGenericFn(comptime T: type, comptime a: T, b: T) T {
122 return a + b;122 return a + b;
123}123}
124124
125test "generic fn with implicit cast" {125test "generic fn with implicit cast" {
126 expect(getFirstByte(u8, &[_]u8{13}) == 13);126 try expect(getFirstByte(u8, &[_]u8{13}) == 13);
127 expect(getFirstByte(u16, &[_]u16{127 try expect(getFirstByte(u16, &[_]u16{
128 0,128 0,
129 13,129 13,
130 }) == 0);130 }) == 0);
...@@ -149,21 +149,21 @@ fn foo2(arg: anytype) bool {...@@ -149,21 +149,21 @@ fn foo2(arg: anytype) bool {
149}149}
150150
151test "array of generic fns" {151test "array of generic fns" {
152 expect(foos[0](true));152 try expect(foos[0](true));
153 expect(!foos[1](true));153 try expect(!foos[1](true));
154}154}
155155
156test "generic fn keeps non-generic parameter types" {156test "generic fn keeps non-generic parameter types" {
157 const A = 128;157 const A = 128;
158158
159 const S = struct {159 const S = struct {
160 fn f(comptime T: type, s: []T) void {160 fn f(comptime T: type, s: []T) !void {
161 expect(A != @typeInfo(@TypeOf(s)).Pointer.alignment);161 try expect(A != @typeInfo(@TypeOf(s)).Pointer.alignment);
162 }162 }
163 };163 };
164164
165 // The compiler monomorphizes `S.f` for `T=u8` on its first use, check that165 // The compiler monomorphizes `S.f` for `T=u8` on its first use, check that
166 // `x` type not affect `s` parameter type.166 // `x` type not affect `s` parameter type.
167 var x: [16]u8 align(A) = undefined;167 var x: [16]u8 align(A) = undefined;
168 S.f(u8, &x);168 try S.f(u8, &x);
169}169}
test/stage1/behavior/hasdecl.zig+6-6
...@@ -11,11 +11,11 @@ const Bar = struct {...@@ -11,11 +11,11 @@ const Bar = struct {
11};11};
1212
13test "@hasDecl" {13test "@hasDecl" {
14 expect(@hasDecl(Foo, "public_thing"));14 try expect(@hasDecl(Foo, "public_thing"));
15 expect(!@hasDecl(Foo, "private_thing"));15 try expect(!@hasDecl(Foo, "private_thing"));
16 expect(!@hasDecl(Foo, "no_thing"));16 try expect(!@hasDecl(Foo, "no_thing"));
1717
18 expect(@hasDecl(Bar, "hi"));18 try expect(@hasDecl(Bar, "hi"));
19 expect(@hasDecl(Bar, "blah"));19 try expect(@hasDecl(Bar, "blah"));
20 expect(!@hasDecl(Bar, "nope"));20 try expect(!@hasDecl(Bar, "nope"));
21}21}
test/stage1/behavior/hasfield.zig+12-12
...@@ -8,10 +8,10 @@ test "@hasField" {...@@ -8,10 +8,10 @@ test "@hasField" {
88
9 pub const nope = 1;9 pub const nope = 1;
10 };10 };
11 expect(@hasField(struc, "a") == true);11 try expect(@hasField(struc, "a") == true);
12 expect(@hasField(struc, "b") == true);12 try expect(@hasField(struc, "b") == true);
13 expect(@hasField(struc, "non-existant") == false);13 try expect(@hasField(struc, "non-existant") == false);
14 expect(@hasField(struc, "nope") == false);14 try expect(@hasField(struc, "nope") == false);
1515
16 const unin = union {16 const unin = union {
17 a: u64,17 a: u64,
...@@ -19,10 +19,10 @@ test "@hasField" {...@@ -19,10 +19,10 @@ test "@hasField" {
1919
20 pub const nope = 1;20 pub const nope = 1;
21 };21 };
22 expect(@hasField(unin, "a") == true);22 try expect(@hasField(unin, "a") == true);
23 expect(@hasField(unin, "b") == true);23 try expect(@hasField(unin, "b") == true);
24 expect(@hasField(unin, "non-existant") == false);24 try expect(@hasField(unin, "non-existant") == false);
25 expect(@hasField(unin, "nope") == false);25 try expect(@hasField(unin, "nope") == false);
2626
27 const enm = enum {27 const enm = enum {
28 a,28 a,
...@@ -30,8 +30,8 @@ test "@hasField" {...@@ -30,8 +30,8 @@ test "@hasField" {
3030
31 pub const nope = 1;31 pub const nope = 1;
32 };32 };
33 expect(@hasField(enm, "a") == true);33 try expect(@hasField(enm, "a") == true);
34 expect(@hasField(enm, "b") == true);34 try expect(@hasField(enm, "b") == true);
35 expect(@hasField(enm, "non-existant") == false);35 try expect(@hasField(enm, "non-existant") == false);
36 expect(@hasField(enm, "nope") == false);36 try expect(@hasField(enm, "nope") == false);
37}37}
test/stage1/behavior/if.zig+14-14
...@@ -26,7 +26,7 @@ fn firstEqlThird(a: i32, b: i32, c: i32) void {...@@ -26,7 +26,7 @@ fn firstEqlThird(a: i32, b: i32, c: i32) void {
26}26}
2727
28test "else if expression" {28test "else if expression" {
29 expect(elseIfExpressionF(1) == 1);29 try expect(elseIfExpressionF(1) == 1);
30}30}
31fn elseIfExpressionF(c: u8) u8 {31fn elseIfExpressionF(c: u8) u8 {
32 if (c == 0) {32 if (c == 0) {
...@@ -44,14 +44,14 @@ var global_with_err: anyerror!u32 = error.SomeError;...@@ -44,14 +44,14 @@ var global_with_err: anyerror!u32 = error.SomeError;
4444
45test "unwrap mutable global var" {45test "unwrap mutable global var" {
46 if (global_with_val) |v| {46 if (global_with_val) |v| {
47 expect(v == 0);47 try expect(v == 0);
48 } else |e| {48 } else |e| {
49 unreachable;49 unreachable;
50 }50 }
51 if (global_with_err) |_| {51 if (global_with_err) |_| {
52 unreachable;52 unreachable;
53 } else |e| {53 } else |e| {
54 expect(e == error.SomeError);54 try expect(e == error.SomeError);
55 }55 }
56}56}
5757
...@@ -63,7 +63,7 @@ test "labeled break inside comptime if inside runtime if" {...@@ -63,7 +63,7 @@ test "labeled break inside comptime if inside runtime if" {
63 break :blk @as(i32, 42);63 break :blk @as(i32, 42);
64 };64 };
65 }65 }
66 expect(answer == 42);66 try expect(answer == 42);
67}67}
6868
69test "const result loc, runtime if cond, else unreachable" {69test "const result loc, runtime if cond, else unreachable" {
...@@ -74,36 +74,36 @@ test "const result loc, runtime if cond, else unreachable" {...@@ -74,36 +74,36 @@ test "const result loc, runtime if cond, else unreachable" {
7474
75 var t = true;75 var t = true;
76 const x = if (t) Num.Two else unreachable;76 const x = if (t) Num.Two else unreachable;
77 expect(x == .Two);77 try expect(x == .Two);
78}78}
7979
80test "if prongs cast to expected type instead of peer type resolution" {80test "if prongs cast to expected type instead of peer type resolution" {
81 const S = struct {81 const S = struct {
82 fn doTheTest(f: bool) void {82 fn doTheTest(f: bool) !void {
83 var x: i32 = 0;83 var x: i32 = 0;
84 x = if (f) 1 else 2;84 x = if (f) 1 else 2;
85 expect(x == 2);85 try expect(x == 2);
8686
87 var b = true;87 var b = true;
88 const y: i32 = if (b) 1 else 2;88 const y: i32 = if (b) 1 else 2;
89 expect(y == 1);89 try expect(y == 1);
90 }90 }
91 };91 };
92 S.doTheTest(false);92 try S.doTheTest(false);
93 comptime S.doTheTest(false);93 comptime try S.doTheTest(false);
94}94}
9595
96test "while copies its payload" {96test "while copies its payload" {
97 const S = struct {97 const S = struct {
98 fn doTheTest() void {98 fn doTheTest() !void {
99 var tmp: ?i32 = 10;99 var tmp: ?i32 = 10;
100 if (tmp) |value| {100 if (tmp) |value| {
101 // Modify the original variable101 // Modify the original variable
102 tmp = null;102 tmp = null;
103 expectEqual(@as(i32, 10), value);103 try expectEqual(@as(i32, 10), value);
104 } else unreachable;104 } else unreachable;
105 }105 }
106 };106 };
107 S.doTheTest();107 try S.doTheTest();
108 comptime S.doTheTest();108 comptime try S.doTheTest();
109}109}
test/stage1/behavior/import.zig+3-3
...@@ -3,18 +3,18 @@ const expectEqual = @import("std").testing.expectEqual;...@@ -3,18 +3,18 @@ const expectEqual = @import("std").testing.expectEqual;
3const a_namespace = @import("import/a_namespace.zig");3const a_namespace = @import("import/a_namespace.zig");
44
5test "call fn via namespace lookup" {5test "call fn via namespace lookup" {
6 expectEqual(@as(i32, 1234), a_namespace.foo());6 try expectEqual(@as(i32, 1234), a_namespace.foo());
7}7}
88
9test "importing the same thing gives the same import" {9test "importing the same thing gives the same import" {
10 expect(@import("std") == @import("std"));10 try expect(@import("std") == @import("std"));
11}11}
1212
13test "import in non-toplevel scope" {13test "import in non-toplevel scope" {
14 const S = struct {14 const S = struct {
15 usingnamespace @import("import/a_namespace.zig");15 usingnamespace @import("import/a_namespace.zig");
16 };16 };
17 expectEqual(@as(i32, 1234), S.foo());17 try expectEqual(@as(i32, 1234), S.foo());
18}18}
1919
20test "import empty file" {20test "import empty file" {
test/stage1/behavior/incomplete_struct_param_tld.zig+1-1
...@@ -26,5 +26,5 @@ test "incomplete struct param top level declaration" {...@@ -26,5 +26,5 @@ test "incomplete struct param top level declaration" {
26 .c = C{ .x = 13 },26 .c = C{ .x = 13 },
27 },27 },
28 };28 };
29 expect(foo(a) == 13);29 try expect(foo(a) == 13);
30}30}
test/stage1/behavior/inttoptr.zig-4
...@@ -1,7 +1,3 @@...@@ -1,7 +1,3 @@
1const builtin = @import("builtin");
2const std = @import("std");
3const expect = std.testing.expect;
4
5test "casting random address to function pointer" {1test "casting random address to function pointer" {
6 randomAddressToFunction();2 randomAddressToFunction();
7 comptime randomAddressToFunction();3 comptime randomAddressToFunction();
test/stage1/behavior/ir_block_deps.zig+2-2
...@@ -16,6 +16,6 @@ fn getErrInt() anyerror!i32 {...@@ -16,6 +16,6 @@ fn getErrInt() anyerror!i32 {
16}16}
1717
18test "ir block deps" {18test "ir block deps" {
19 expect((foo(1) catch unreachable) == 0);19 try expect((foo(1) catch unreachable) == 0);
20 expect((foo(2) catch unreachable) == 0);20 try expect((foo(2) catch unreachable) == 0);
21}21}
test/stage1/behavior/math.zig+357-357
...@@ -7,71 +7,71 @@ const minInt = std.math.minInt;...@@ -7,71 +7,71 @@ const minInt = std.math.minInt;
7const mem = std.mem;7const mem = std.mem;
88
9test "division" {9test "division" {
10 testDivision();10 try testDivision();
11 comptime testDivision();11 comptime try testDivision();
12}12}
13fn testDivision() void {13fn testDivision() !void {
14 expect(div(u32, 13, 3) == 4);14 try expect(div(u32, 13, 3) == 4);
15 expect(div(f16, 1.0, 2.0) == 0.5);15 try expect(div(f16, 1.0, 2.0) == 0.5);
16 expect(div(f32, 1.0, 2.0) == 0.5);16 try expect(div(f32, 1.0, 2.0) == 0.5);
1717
18 expect(divExact(u32, 55, 11) == 5);18 try expect(divExact(u32, 55, 11) == 5);
19 expect(divExact(i32, -55, 11) == -5);19 try expect(divExact(i32, -55, 11) == -5);
20 expect(divExact(f16, 55.0, 11.0) == 5.0);20 try expect(divExact(f16, 55.0, 11.0) == 5.0);
21 expect(divExact(f16, -55.0, 11.0) == -5.0);21 try expect(divExact(f16, -55.0, 11.0) == -5.0);
22 expect(divExact(f32, 55.0, 11.0) == 5.0);22 try expect(divExact(f32, 55.0, 11.0) == 5.0);
23 expect(divExact(f32, -55.0, 11.0) == -5.0);23 try expect(divExact(f32, -55.0, 11.0) == -5.0);
2424
25 expect(divFloor(i32, 5, 3) == 1);25 try expect(divFloor(i32, 5, 3) == 1);
26 expect(divFloor(i32, -5, 3) == -2);26 try expect(divFloor(i32, -5, 3) == -2);
27 expect(divFloor(f16, 5.0, 3.0) == 1.0);27 try expect(divFloor(f16, 5.0, 3.0) == 1.0);
28 expect(divFloor(f16, -5.0, 3.0) == -2.0);28 try expect(divFloor(f16, -5.0, 3.0) == -2.0);
29 expect(divFloor(f32, 5.0, 3.0) == 1.0);29 try expect(divFloor(f32, 5.0, 3.0) == 1.0);
30 expect(divFloor(f32, -5.0, 3.0) == -2.0);30 try expect(divFloor(f32, -5.0, 3.0) == -2.0);
31 expect(divFloor(i32, -0x80000000, -2) == 0x40000000);31 try expect(divFloor(i32, -0x80000000, -2) == 0x40000000);
32 expect(divFloor(i32, 0, -0x80000000) == 0);32 try expect(divFloor(i32, 0, -0x80000000) == 0);
33 expect(divFloor(i32, -0x40000001, 0x40000000) == -2);33 try expect(divFloor(i32, -0x40000001, 0x40000000) == -2);
34 expect(divFloor(i32, -0x80000000, 1) == -0x80000000);34 try expect(divFloor(i32, -0x80000000, 1) == -0x80000000);
35 expect(divFloor(i32, 10, 12) == 0);35 try expect(divFloor(i32, 10, 12) == 0);
36 expect(divFloor(i32, -14, 12) == -2);36 try expect(divFloor(i32, -14, 12) == -2);
37 expect(divFloor(i32, -2, 12) == -1);37 try expect(divFloor(i32, -2, 12) == -1);
3838
39 expect(divTrunc(i32, 5, 3) == 1);39 try expect(divTrunc(i32, 5, 3) == 1);
40 expect(divTrunc(i32, -5, 3) == -1);40 try expect(divTrunc(i32, -5, 3) == -1);
41 expect(divTrunc(f16, 5.0, 3.0) == 1.0);41 try expect(divTrunc(f16, 5.0, 3.0) == 1.0);
42 expect(divTrunc(f16, -5.0, 3.0) == -1.0);42 try expect(divTrunc(f16, -5.0, 3.0) == -1.0);
43 expect(divTrunc(f32, 5.0, 3.0) == 1.0);43 try expect(divTrunc(f32, 5.0, 3.0) == 1.0);
44 expect(divTrunc(f32, -5.0, 3.0) == -1.0);44 try expect(divTrunc(f32, -5.0, 3.0) == -1.0);
45 expect(divTrunc(f64, 5.0, 3.0) == 1.0);45 try expect(divTrunc(f64, 5.0, 3.0) == 1.0);
46 expect(divTrunc(f64, -5.0, 3.0) == -1.0);46 try expect(divTrunc(f64, -5.0, 3.0) == -1.0);
47 expect(divTrunc(i32, 10, 12) == 0);47 try expect(divTrunc(i32, 10, 12) == 0);
48 expect(divTrunc(i32, -14, 12) == -1);48 try expect(divTrunc(i32, -14, 12) == -1);
49 expect(divTrunc(i32, -2, 12) == 0);49 try expect(divTrunc(i32, -2, 12) == 0);
5050
51 expect(mod(i32, 10, 12) == 10);51 try expect(mod(i32, 10, 12) == 10);
52 expect(mod(i32, -14, 12) == 10);52 try expect(mod(i32, -14, 12) == 10);
53 expect(mod(i32, -2, 12) == 10);53 try expect(mod(i32, -2, 12) == 10);
5454
55 comptime {55 comptime {
56 expect(56 try expect(
57 1194735857077236777412821811143690633098347576 % 508740759824825164163191790951174292733114988 == 177254337427586449086438229241342047632117600,57 1194735857077236777412821811143690633098347576 % 508740759824825164163191790951174292733114988 == 177254337427586449086438229241342047632117600,
58 );58 );
59 expect(59 try expect(
60 @rem(-1194735857077236777412821811143690633098347576, 508740759824825164163191790951174292733114988) == -177254337427586449086438229241342047632117600,60 @rem(-1194735857077236777412821811143690633098347576, 508740759824825164163191790951174292733114988) == -177254337427586449086438229241342047632117600,
61 );61 );
62 expect(62 try expect(
63 1194735857077236777412821811143690633098347576 / 508740759824825164163191790951174292733114988 == 2,63 1194735857077236777412821811143690633098347576 / 508740759824825164163191790951174292733114988 == 2,
64 );64 );
65 expect(65 try expect(
66 @divTrunc(-1194735857077236777412821811143690633098347576, 508740759824825164163191790951174292733114988) == -2,66 @divTrunc(-1194735857077236777412821811143690633098347576, 508740759824825164163191790951174292733114988) == -2,
67 );67 );
68 expect(68 try expect(
69 @divTrunc(1194735857077236777412821811143690633098347576, -508740759824825164163191790951174292733114988) == -2,69 @divTrunc(1194735857077236777412821811143690633098347576, -508740759824825164163191790951174292733114988) == -2,
70 );70 );
71 expect(71 try expect(
72 @divTrunc(-1194735857077236777412821811143690633098347576, -508740759824825164163191790951174292733114988) == 2,72 @divTrunc(-1194735857077236777412821811143690633098347576, -508740759824825164163191790951174292733114988) == 2,
73 );73 );
74 expect(74 try expect(
75 4126227191251978491697987544882340798050766755606969681711 % 10 == 1,75 4126227191251978491697987544882340798050766755606969681711 % 10 == 1,
76 );76 );
77 }77 }
...@@ -94,9 +94,9 @@ fn mod(comptime T: type, a: T, b: T) T {...@@ -94,9 +94,9 @@ fn mod(comptime T: type, a: T, b: T) T {
9494
95test "@addWithOverflow" {95test "@addWithOverflow" {
96 var result: u8 = undefined;96 var result: u8 = undefined;
97 expect(@addWithOverflow(u8, 250, 100, &result));97 try expect(@addWithOverflow(u8, 250, 100, &result));
98 expect(!@addWithOverflow(u8, 100, 150, &result));98 try expect(!@addWithOverflow(u8, 100, 150, &result));
99 expect(result == 250);99 try expect(result == 250);
100}100}
101101
102// TODO test mulWithOverflow102// TODO test mulWithOverflow
...@@ -104,31 +104,31 @@ test "@addWithOverflow" {...@@ -104,31 +104,31 @@ test "@addWithOverflow" {
104104
105test "@shlWithOverflow" {105test "@shlWithOverflow" {
106 var result: u16 = undefined;106 var result: u16 = undefined;
107 expect(@shlWithOverflow(u16, 0b0010111111111111, 3, &result));107 try expect(@shlWithOverflow(u16, 0b0010111111111111, 3, &result));
108 expect(!@shlWithOverflow(u16, 0b0010111111111111, 2, &result));108 try expect(!@shlWithOverflow(u16, 0b0010111111111111, 2, &result));
109 expect(result == 0b1011111111111100);109 try expect(result == 0b1011111111111100);
110}110}
111111
112test "@*WithOverflow with u0 values" {112test "@*WithOverflow with u0 values" {
113 var result: u0 = undefined;113 var result: u0 = undefined;
114 expect(!@addWithOverflow(u0, 0, 0, &result));114 try expect(!@addWithOverflow(u0, 0, 0, &result));
115 expect(!@subWithOverflow(u0, 0, 0, &result));115 try expect(!@subWithOverflow(u0, 0, 0, &result));
116 expect(!@mulWithOverflow(u0, 0, 0, &result));116 try expect(!@mulWithOverflow(u0, 0, 0, &result));
117 expect(!@shlWithOverflow(u0, 0, 0, &result));117 try expect(!@shlWithOverflow(u0, 0, 0, &result));
118}118}
119119
120test "@clz" {120test "@clz" {
121 testClz();121 try testClz();
122 comptime testClz();122 comptime try testClz();
123}123}
124124
125fn testClz() void {125fn testClz() !void {
126 expect(clz(u8, 0b10001010) == 0);126 try expect(clz(u8, 0b10001010) == 0);
127 expect(clz(u8, 0b00001010) == 4);127 try expect(clz(u8, 0b00001010) == 4);
128 expect(clz(u8, 0b00011010) == 3);128 try expect(clz(u8, 0b00011010) == 3);
129 expect(clz(u8, 0b00000000) == 8);129 try expect(clz(u8, 0b00000000) == 8);
130 expect(clz(u128, 0xffffffffffffffff) == 64);130 try expect(clz(u128, 0xffffffffffffffff) == 64);
131 expect(clz(u128, 0x10000000000000000) == 63);131 try expect(clz(u128, 0x10000000000000000) == 63);
132}132}
133133
134fn clz(comptime T: type, x: T) usize {134fn clz(comptime T: type, x: T) usize {
...@@ -136,15 +136,15 @@ fn clz(comptime T: type, x: T) usize {...@@ -136,15 +136,15 @@ fn clz(comptime T: type, x: T) usize {
136}136}
137137
138test "@ctz" {138test "@ctz" {
139 testCtz();139 try testCtz();
140 comptime testCtz();140 comptime try testCtz();
141}141}
142142
143fn testCtz() void {143fn testCtz() !void {
144 expect(ctz(u8, 0b10100000) == 5);144 try expect(ctz(u8, 0b10100000) == 5);
145 expect(ctz(u8, 0b10001010) == 1);145 try expect(ctz(u8, 0b10001010) == 1);
146 expect(ctz(u8, 0b00000000) == 8);146 try expect(ctz(u8, 0b00000000) == 8);
147 expect(ctz(u16, 0b00000000) == 16);147 try expect(ctz(u16, 0b00000000) == 16);
148}148}
149149
150fn ctz(comptime T: type, x: T) usize {150fn ctz(comptime T: type, x: T) usize {
...@@ -154,109 +154,109 @@ fn ctz(comptime T: type, x: T) usize {...@@ -154,109 +154,109 @@ fn ctz(comptime T: type, x: T) usize {
154test "assignment operators" {154test "assignment operators" {
155 var i: u32 = 0;155 var i: u32 = 0;
156 i += 5;156 i += 5;
157 expect(i == 5);157 try expect(i == 5);
158 i -= 2;158 i -= 2;
159 expect(i == 3);159 try expect(i == 3);
160 i *= 20;160 i *= 20;
161 expect(i == 60);161 try expect(i == 60);
162 i /= 3;162 i /= 3;
163 expect(i == 20);163 try expect(i == 20);
164 i %= 11;164 i %= 11;
165 expect(i == 9);165 try expect(i == 9);
166 i <<= 1;166 i <<= 1;
167 expect(i == 18);167 try expect(i == 18);
168 i >>= 2;168 i >>= 2;
169 expect(i == 4);169 try expect(i == 4);
170 i = 6;170 i = 6;
171 i &= 5;171 i &= 5;
172 expect(i == 4);172 try expect(i == 4);
173 i ^= 6;173 i ^= 6;
174 expect(i == 2);174 try expect(i == 2);
175 i = 6;175 i = 6;
176 i |= 3;176 i |= 3;
177 expect(i == 7);177 try expect(i == 7);
178}178}
179179
180test "three expr in a row" {180test "three expr in a row" {
181 testThreeExprInARow(false, true);181 try testThreeExprInARow(false, true);
182 comptime testThreeExprInARow(false, true);182 comptime try testThreeExprInARow(false, true);
183}183}
184fn testThreeExprInARow(f: bool, t: bool) void {184fn testThreeExprInARow(f: bool, t: bool) !void {
185 assertFalse(f or f or f);185 try assertFalse(f or f or f);
186 assertFalse(t and t and f);186 try assertFalse(t and t and f);
187 assertFalse(1 | 2 | 4 != 7);187 try assertFalse(1 | 2 | 4 != 7);
188 assertFalse(3 ^ 6 ^ 8 != 13);188 try assertFalse(3 ^ 6 ^ 8 != 13);
189 assertFalse(7 & 14 & 28 != 4);189 try assertFalse(7 & 14 & 28 != 4);
190 assertFalse(9 << 1 << 2 != 9 << 3);190 try assertFalse(9 << 1 << 2 != 9 << 3);
191 assertFalse(90 >> 1 >> 2 != 90 >> 3);191 try assertFalse(90 >> 1 >> 2 != 90 >> 3);
192 assertFalse(100 - 1 + 1000 != 1099);192 try assertFalse(100 - 1 + 1000 != 1099);
193 assertFalse(5 * 4 / 2 % 3 != 1);193 try assertFalse(5 * 4 / 2 % 3 != 1);
194 assertFalse(@as(i32, @as(i32, 5)) != 5);194 try assertFalse(@as(i32, @as(i32, 5)) != 5);
195 assertFalse(!!false);195 try assertFalse(!!false);
196 assertFalse(@as(i32, 7) != --(@as(i32, 7)));196 try assertFalse(@as(i32, 7) != --(@as(i32, 7)));
197}197}
198fn assertFalse(b: bool) void {198fn assertFalse(b: bool) !void {
199 expect(!b);199 try expect(!b);
200}200}
201201
202test "const number literal" {202test "const number literal" {
203 const one = 1;203 const one = 1;
204 const eleven = ten + one;204 const eleven = ten + one;
205205
206 expect(eleven == 11);206 try expect(eleven == 11);
207}207}
208const ten = 10;208const ten = 10;
209209
210test "unsigned wrapping" {210test "unsigned wrapping" {
211 testUnsignedWrappingEval(maxInt(u32));211 try testUnsignedWrappingEval(maxInt(u32));
212 comptime testUnsignedWrappingEval(maxInt(u32));212 comptime try testUnsignedWrappingEval(maxInt(u32));
213}213}
214fn testUnsignedWrappingEval(x: u32) void {214fn testUnsignedWrappingEval(x: u32) !void {
215 const zero = x +% 1;215 const zero = x +% 1;
216 expect(zero == 0);216 try expect(zero == 0);
217 const orig = zero -% 1;217 const orig = zero -% 1;
218 expect(orig == maxInt(u32));218 try expect(orig == maxInt(u32));
219}219}
220220
221test "signed wrapping" {221test "signed wrapping" {
222 testSignedWrappingEval(maxInt(i32));222 try testSignedWrappingEval(maxInt(i32));
223 comptime testSignedWrappingEval(maxInt(i32));223 comptime try testSignedWrappingEval(maxInt(i32));
224}224}
225fn testSignedWrappingEval(x: i32) void {225fn testSignedWrappingEval(x: i32) !void {
226 const min_val = x +% 1;226 const min_val = x +% 1;
227 expect(min_val == minInt(i32));227 try expect(min_val == minInt(i32));
228 const max_val = min_val -% 1;228 const max_val = min_val -% 1;
229 expect(max_val == maxInt(i32));229 try expect(max_val == maxInt(i32));
230}230}
231231
232test "signed negation wrapping" {232test "signed negation wrapping" {
233 testSignedNegationWrappingEval(minInt(i16));233 try testSignedNegationWrappingEval(minInt(i16));
234 comptime testSignedNegationWrappingEval(minInt(i16));234 comptime try testSignedNegationWrappingEval(minInt(i16));
235}235}
236fn testSignedNegationWrappingEval(x: i16) void {236fn testSignedNegationWrappingEval(x: i16) !void {
237 expect(x == -32768);237 try expect(x == -32768);
238 const neg = -%x;238 const neg = -%x;
239 expect(neg == -32768);239 try expect(neg == -32768);
240}240}
241241
242test "unsigned negation wrapping" {242test "unsigned negation wrapping" {
243 testUnsignedNegationWrappingEval(1);243 try testUnsignedNegationWrappingEval(1);
244 comptime testUnsignedNegationWrappingEval(1);244 comptime try testUnsignedNegationWrappingEval(1);
245}245}
246fn testUnsignedNegationWrappingEval(x: u16) void {246fn testUnsignedNegationWrappingEval(x: u16) !void {
247 expect(x == 1);247 try expect(x == 1);
248 const neg = -%x;248 const neg = -%x;
249 expect(neg == maxInt(u16));249 try expect(neg == maxInt(u16));
250}250}
251251
252test "unsigned 64-bit division" {252test "unsigned 64-bit division" {
253 test_u64_div();253 try test_u64_div();
254 comptime test_u64_div();254 comptime try test_u64_div();
255}255}
256fn test_u64_div() void {256fn test_u64_div() !void {
257 const result = divWithResult(1152921504606846976, 34359738365);257 const result = divWithResult(1152921504606846976, 34359738365);
258 expect(result.quotient == 33554432);258 try expect(result.quotient == 33554432);
259 expect(result.remainder == 100663296);259 try expect(result.remainder == 100663296);
260}260}
261fn divWithResult(a: u64, b: u64) DivResult {261fn divWithResult(a: u64, b: u64) DivResult {
262 return DivResult{262 return DivResult{
...@@ -270,62 +270,62 @@ const DivResult = struct {...@@ -270,62 +270,62 @@ const DivResult = struct {
270};270};
271271
272test "binary not" {272test "binary not" {
273 expect(comptime x: {273 try expect(comptime x: {
274 break :x ~@as(u16, 0b1010101010101010) == 0b0101010101010101;274 break :x ~@as(u16, 0b1010101010101010) == 0b0101010101010101;
275 });275 });
276 expect(comptime x: {276 try expect(comptime x: {
277 break :x ~@as(u64, 2147483647) == 18446744071562067968;277 break :x ~@as(u64, 2147483647) == 18446744071562067968;
278 });278 });
279 testBinaryNot(0b1010101010101010);279 try testBinaryNot(0b1010101010101010);
280}280}
281281
282fn testBinaryNot(x: u16) void {282fn testBinaryNot(x: u16) !void {
283 expect(~x == 0b0101010101010101);283 try expect(~x == 0b0101010101010101);
284}284}
285285
286test "small int addition" {286test "small int addition" {
287 var x: u2 = 0;287 var x: u2 = 0;
288 expect(x == 0);288 try expect(x == 0);
289289
290 x += 1;290 x += 1;
291 expect(x == 1);291 try expect(x == 1);
292292
293 x += 1;293 x += 1;
294 expect(x == 2);294 try expect(x == 2);
295295
296 x += 1;296 x += 1;
297 expect(x == 3);297 try expect(x == 3);
298298
299 var result: @TypeOf(x) = 3;299 var result: @TypeOf(x) = 3;
300 expect(@addWithOverflow(@TypeOf(x), x, 1, &result));300 try expect(@addWithOverflow(@TypeOf(x), x, 1, &result));
301301
302 expect(result == 0);302 try expect(result == 0);
303}303}
304304
305test "float equality" {305test "float equality" {
306 const x: f64 = 0.012;306 const x: f64 = 0.012;
307 const y: f64 = x + 1.0;307 const y: f64 = x + 1.0;
308308
309 testFloatEqualityImpl(x, y);309 try testFloatEqualityImpl(x, y);
310 comptime testFloatEqualityImpl(x, y);310 comptime try testFloatEqualityImpl(x, y);
311}311}
312312
313fn testFloatEqualityImpl(x: f64, y: f64) void {313fn testFloatEqualityImpl(x: f64, y: f64) !void {
314 const y2 = x + 1.0;314 const y2 = x + 1.0;
315 expect(y == y2);315 try expect(y == y2);
316}316}
317317
318test "allow signed integer division/remainder when values are comptime known and positive or exact" {318test "allow signed integer division/remainder when values are comptime known and positive or exact" {
319 expect(5 / 3 == 1);319 try expect(5 / 3 == 1);
320 expect(-5 / -3 == 1);320 try expect(-5 / -3 == 1);
321 expect(-6 / 3 == -2);321 try expect(-6 / 3 == -2);
322322
323 expect(5 % 3 == 2);323 try expect(5 % 3 == 2);
324 expect(-6 % 3 == 0);324 try expect(-6 % 3 == 0);
325}325}
326326
327test "hex float literal parsing" {327test "hex float literal parsing" {
328 comptime expect(0x1.0 == 1.0);328 comptime try expect(0x1.0 == 1.0);
329}329}
330330
331test "quad hex float literal parsing in range" {331test "quad hex float literal parsing in range" {
...@@ -340,29 +340,29 @@ test "quad hex float literal parsing accurate" {...@@ -340,29 +340,29 @@ test "quad hex float literal parsing accurate" {
340340
341 // implied 1 is dropped, with an exponent of 0 (0x3fff) after biasing.341 // implied 1 is dropped, with an exponent of 0 (0x3fff) after biasing.
342 const expected: u128 = 0x3fff1111222233334444555566667777;342 const expected: u128 = 0x3fff1111222233334444555566667777;
343 expect(@bitCast(u128, a) == expected);343 try expect(@bitCast(u128, a) == expected);
344344
345 // non-normalized345 // non-normalized
346 const b: f128 = 0x11.111222233334444555566667777p-4;346 const b: f128 = 0x11.111222233334444555566667777p-4;
347 expect(@bitCast(u128, b) == expected);347 try expect(@bitCast(u128, b) == expected);
348348
349 const S = struct {349 const S = struct {
350 fn doTheTest() void {350 fn doTheTest() !void {
351 {351 {
352 var f: f128 = 0x1.2eab345678439abcdefea56782346p+5;352 var f: f128 = 0x1.2eab345678439abcdefea56782346p+5;
353 expect(@bitCast(u128, f) == 0x40042eab345678439abcdefea5678234);353 try expect(@bitCast(u128, f) == 0x40042eab345678439abcdefea5678234);
354 }354 }
355 {355 {
356 var f: f128 = 0x1.edcb34a235253948765432134674fp-1;356 var f: f128 = 0x1.edcb34a235253948765432134674fp-1;
357 expect(@bitCast(u128, f) == 0x3ffeedcb34a235253948765432134674);357 try expect(@bitCast(u128, f) == 0x3ffeedcb34a235253948765432134674);
358 }358 }
359 {359 {
360 var f: f128 = 0x1.353e45674d89abacc3a2ebf3ff4ffp-50;360 var f: f128 = 0x1.353e45674d89abacc3a2ebf3ff4ffp-50;
361 expect(@bitCast(u128, f) == 0x3fcd353e45674d89abacc3a2ebf3ff50);361 try expect(@bitCast(u128, f) == 0x3fcd353e45674d89abacc3a2ebf3ff50);
362 }362 }
363 {363 {
364 var f: f128 = 0x1.ed8764648369535adf4be3214567fp-9;364 var f: f128 = 0x1.ed8764648369535adf4be3214567fp-9;
365 expect(@bitCast(u128, f) == 0x3ff6ed8764648369535adf4be3214568);365 try expect(@bitCast(u128, f) == 0x3ff6ed8764648369535adf4be3214568);
366 }366 }
367 const exp2ft = [_]f64{367 const exp2ft = [_]f64{
368 0x1.6a09e667f3bcdp-1,368 0x1.6a09e667f3bcdp-1,
...@@ -417,40 +417,40 @@ test "quad hex float literal parsing accurate" {...@@ -417,40 +417,40 @@ test "quad hex float literal parsing accurate" {
417 };417 };
418418
419 for (exp2ft) |x, i| {419 for (exp2ft) |x, i| {
420 expect(@bitCast(u64, x) == answers[i]);420 try expect(@bitCast(u64, x) == answers[i]);
421 }421 }
422 }422 }
423 };423 };
424 S.doTheTest();424 try S.doTheTest();
425 comptime S.doTheTest();425 comptime try S.doTheTest();
426}426}
427427
428test "underscore separator parsing" {428test "underscore separator parsing" {
429 expect(0_0_0_0 == 0);429 try expect(0_0_0_0 == 0);
430 expect(1_234_567 == 1234567);430 try expect(1_234_567 == 1234567);
431 expect(001_234_567 == 1234567);431 try expect(001_234_567 == 1234567);
432 expect(0_0_1_2_3_4_5_6_7 == 1234567);432 try expect(0_0_1_2_3_4_5_6_7 == 1234567);
433433
434 expect(0b0_0_0_0 == 0);434 try expect(0b0_0_0_0 == 0);
435 expect(0b1010_1010 == 0b10101010);435 try expect(0b1010_1010 == 0b10101010);
436 expect(0b0000_1010_1010 == 0b10101010);436 try expect(0b0000_1010_1010 == 0b10101010);
437 expect(0b1_0_1_0_1_0_1_0 == 0b10101010);437 try expect(0b1_0_1_0_1_0_1_0 == 0b10101010);
438438
439 expect(0o0_0_0_0 == 0);439 try expect(0o0_0_0_0 == 0);
440 expect(0o1010_1010 == 0o10101010);440 try expect(0o1010_1010 == 0o10101010);
441 expect(0o0000_1010_1010 == 0o10101010);441 try expect(0o0000_1010_1010 == 0o10101010);
442 expect(0o1_0_1_0_1_0_1_0 == 0o10101010);442 try expect(0o1_0_1_0_1_0_1_0 == 0o10101010);
443443
444 expect(0x0_0_0_0 == 0);444 try expect(0x0_0_0_0 == 0);
445 expect(0x1010_1010 == 0x10101010);445 try expect(0x1010_1010 == 0x10101010);
446 expect(0x0000_1010_1010 == 0x10101010);446 try expect(0x0000_1010_1010 == 0x10101010);
447 expect(0x1_0_1_0_1_0_1_0 == 0x10101010);447 try expect(0x1_0_1_0_1_0_1_0 == 0x10101010);
448448
449 expect(123_456.789_000e1_0 == 123456.789000e10);449 try expect(123_456.789_000e1_0 == 123456.789000e10);
450 expect(0_1_2_3_4_5_6.7_8_9_0_0_0e0_0_1_0 == 123456.789000e10);450 try expect(0_1_2_3_4_5_6.7_8_9_0_0_0e0_0_1_0 == 123456.789000e10);
451451
452 expect(0x1234_5678.9ABC_DEF0p-1_0 == 0x12345678.9ABCDEF0p-10);452 try expect(0x1234_5678.9ABC_DEF0p-1_0 == 0x12345678.9ABCDEF0p-10);
453 expect(0x1_2_3_4_5_6_7_8.9_A_B_C_D_E_F_0p-0_0_0_1_0 == 0x12345678.9ABCDEF0p-10);453 try expect(0x1_2_3_4_5_6_7_8.9_A_B_C_D_E_F_0p-0_0_0_1_0 == 0x12345678.9ABCDEF0p-10);
454}454}
455455
456test "hex float literal within range" {456test "hex float literal within range" {
...@@ -460,73 +460,73 @@ test "hex float literal within range" {...@@ -460,73 +460,73 @@ test "hex float literal within range" {
460}460}
461461
462test "truncating shift left" {462test "truncating shift left" {
463 testShlTrunc(maxInt(u16));463 try testShlTrunc(maxInt(u16));
464 comptime testShlTrunc(maxInt(u16));464 comptime try testShlTrunc(maxInt(u16));
465}465}
466fn testShlTrunc(x: u16) void {466fn testShlTrunc(x: u16) !void {
467 const shifted = x << 1;467 const shifted = x << 1;
468 expect(shifted == 65534);468 try expect(shifted == 65534);
469}469}
470470
471test "truncating shift right" {471test "truncating shift right" {
472 testShrTrunc(maxInt(u16));472 try testShrTrunc(maxInt(u16));
473 comptime testShrTrunc(maxInt(u16));473 comptime try testShrTrunc(maxInt(u16));
474}474}
475fn testShrTrunc(x: u16) void {475fn testShrTrunc(x: u16) !void {
476 const shifted = x >> 1;476 const shifted = x >> 1;
477 expect(shifted == 32767);477 try expect(shifted == 32767);
478}478}
479479
480test "exact shift left" {480test "exact shift left" {
481 testShlExact(0b00110101);481 try testShlExact(0b00110101);
482 comptime testShlExact(0b00110101);482 comptime try testShlExact(0b00110101);
483}483}
484fn testShlExact(x: u8) void {484fn testShlExact(x: u8) !void {
485 const shifted = @shlExact(x, 2);485 const shifted = @shlExact(x, 2);
486 expect(shifted == 0b11010100);486 try expect(shifted == 0b11010100);
487}487}
488488
489test "exact shift right" {489test "exact shift right" {
490 testShrExact(0b10110100);490 try testShrExact(0b10110100);
491 comptime testShrExact(0b10110100);491 comptime try testShrExact(0b10110100);
492}492}
493fn testShrExact(x: u8) void {493fn testShrExact(x: u8) !void {
494 const shifted = @shrExact(x, 2);494 const shifted = @shrExact(x, 2);
495 expect(shifted == 0b00101101);495 try expect(shifted == 0b00101101);
496}496}
497497
498test "shift left/right on u0 operand" {498test "shift left/right on u0 operand" {
499 const S = struct {499 const S = struct {
500 fn doTheTest() void {500 fn doTheTest() !void {
501 var x: u0 = 0;501 var x: u0 = 0;
502 var y: u0 = 0;502 var y: u0 = 0;
503 expectEqual(@as(u0, 0), x << 0);503 try expectEqual(@as(u0, 0), x << 0);
504 expectEqual(@as(u0, 0), x >> 0);504 try expectEqual(@as(u0, 0), x >> 0);
505 expectEqual(@as(u0, 0), x << y);505 try expectEqual(@as(u0, 0), x << y);
506 expectEqual(@as(u0, 0), x >> y);506 try expectEqual(@as(u0, 0), x >> y);
507 expectEqual(@as(u0, 0), @shlExact(x, 0));507 try expectEqual(@as(u0, 0), @shlExact(x, 0));
508 expectEqual(@as(u0, 0), @shrExact(x, 0));508 try expectEqual(@as(u0, 0), @shrExact(x, 0));
509 expectEqual(@as(u0, 0), @shlExact(x, y));509 try expectEqual(@as(u0, 0), @shlExact(x, y));
510 expectEqual(@as(u0, 0), @shrExact(x, y));510 try expectEqual(@as(u0, 0), @shrExact(x, y));
511 }511 }
512 };512 };
513 S.doTheTest();513 try S.doTheTest();
514 comptime S.doTheTest();514 comptime try S.doTheTest();
515}515}
516516
517test "comptime_int addition" {517test "comptime_int addition" {
518 comptime {518 comptime {
519 expect(35361831660712422535336160538497375248 + 101752735581729509668353361206450473702 == 137114567242441932203689521744947848950);519 try expect(35361831660712422535336160538497375248 + 101752735581729509668353361206450473702 == 137114567242441932203689521744947848950);
520 expect(594491908217841670578297176641415611445982232488944558774612 + 390603545391089362063884922208143568023166603618446395589768 == 985095453608931032642182098849559179469148836107390954364380);520 try expect(594491908217841670578297176641415611445982232488944558774612 + 390603545391089362063884922208143568023166603618446395589768 == 985095453608931032642182098849559179469148836107390954364380);
521 }521 }
522}522}
523523
524test "comptime_int multiplication" {524test "comptime_int multiplication" {
525 comptime {525 comptime {
526 expect(526 try expect(
527 45960427431263824329884196484953148229 * 128339149605334697009938835852565949723 == 5898522172026096622534201617172456926982464453350084962781392314016180490567,527 45960427431263824329884196484953148229 * 128339149605334697009938835852565949723 == 5898522172026096622534201617172456926982464453350084962781392314016180490567,
528 );528 );
529 expect(529 try expect(
530 594491908217841670578297176641415611445982232488944558774612 * 390603545391089362063884922208143568023166603618446395589768 == 232210647056203049913662402532976186578842425262306016094292237500303028346593132411865381225871291702600263463125370016,530 594491908217841670578297176641415611445982232488944558774612 * 390603545391089362063884922208143568023166603618446395589768 == 232210647056203049913662402532976186578842425262306016094292237500303028346593132411865381225871291702600263463125370016,
531 );531 );
532 }532 }
...@@ -534,7 +534,7 @@ test "comptime_int multiplication" {...@@ -534,7 +534,7 @@ test "comptime_int multiplication" {
534534
535test "comptime_int shifting" {535test "comptime_int shifting" {
536 comptime {536 comptime {
537 expect((@as(u128, 1) << 127) == 0x80000000000000000000000000000000);537 try expect((@as(u128, 1) << 127) == 0x80000000000000000000000000000000);
538 }538 }
539}539}
540540
...@@ -542,16 +542,16 @@ test "comptime_int multi-limb shift and mask" {...@@ -542,16 +542,16 @@ test "comptime_int multi-limb shift and mask" {
542 comptime {542 comptime {
543 var a = 0xefffffffa0000001eeeeeeefaaaaaaab;543 var a = 0xefffffffa0000001eeeeeeefaaaaaaab;
544544
545 expect(@as(u32, a & 0xffffffff) == 0xaaaaaaab);545 try expect(@as(u32, a & 0xffffffff) == 0xaaaaaaab);
546 a >>= 32;546 a >>= 32;
547 expect(@as(u32, a & 0xffffffff) == 0xeeeeeeef);547 try expect(@as(u32, a & 0xffffffff) == 0xeeeeeeef);
548 a >>= 32;548 a >>= 32;
549 expect(@as(u32, a & 0xffffffff) == 0xa0000001);549 try expect(@as(u32, a & 0xffffffff) == 0xa0000001);
550 a >>= 32;550 a >>= 32;
551 expect(@as(u32, a & 0xffffffff) == 0xefffffff);551 try expect(@as(u32, a & 0xffffffff) == 0xefffffff);
552 a >>= 32;552 a >>= 32;
553553
554 expect(a == 0);554 try expect(a == 0);
555 }555 }
556}556}
557557
...@@ -559,227 +559,227 @@ test "comptime_int multi-limb partial shift right" {...@@ -559,227 +559,227 @@ test "comptime_int multi-limb partial shift right" {
559 comptime {559 comptime {
560 var a = 0x1ffffffffeeeeeeee;560 var a = 0x1ffffffffeeeeeeee;
561 a >>= 16;561 a >>= 16;
562 expect(a == 0x1ffffffffeeee);562 try expect(a == 0x1ffffffffeeee);
563 }563 }
564}564}
565565
566test "xor" {566test "xor" {
567 test_xor();567 try test_xor();
568 comptime test_xor();568 comptime try test_xor();
569}569}
570570
571fn test_xor() void {571fn test_xor() !void {
572 expect(0xFF ^ 0x00 == 0xFF);572 try expect(0xFF ^ 0x00 == 0xFF);
573 expect(0xF0 ^ 0x0F == 0xFF);573 try expect(0xF0 ^ 0x0F == 0xFF);
574 expect(0xFF ^ 0xF0 == 0x0F);574 try expect(0xFF ^ 0xF0 == 0x0F);
575 expect(0xFF ^ 0x0F == 0xF0);575 try expect(0xFF ^ 0x0F == 0xF0);
576 expect(0xFF ^ 0xFF == 0x00);576 try expect(0xFF ^ 0xFF == 0x00);
577}577}
578578
579test "comptime_int xor" {579test "comptime_int xor" {
580 comptime {580 comptime {
581 expect(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF ^ 0x00000000000000000000000000000000 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);581 try expect(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF ^ 0x00000000000000000000000000000000 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);
582 expect(0xFFFFFFFFFFFFFFFF0000000000000000 ^ 0x0000000000000000FFFFFFFFFFFFFFFF == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);582 try expect(0xFFFFFFFFFFFFFFFF0000000000000000 ^ 0x0000000000000000FFFFFFFFFFFFFFFF == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);
583 expect(0xFFFFFFFFFFFFFFFF0000000000000000 ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0x0000000000000000FFFFFFFFFFFFFFFF);583 try expect(0xFFFFFFFFFFFFFFFF0000000000000000 ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0x0000000000000000FFFFFFFFFFFFFFFF);
584 expect(0x0000000000000000FFFFFFFFFFFFFFFF ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0xFFFFFFFFFFFFFFFF0000000000000000);584 try expect(0x0000000000000000FFFFFFFFFFFFFFFF ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0xFFFFFFFFFFFFFFFF0000000000000000);
585 expect(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0x00000000000000000000000000000000);585 try expect(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0x00000000000000000000000000000000);
586 expect(0xFFFFFFFF00000000FFFFFFFF00000000 ^ 0x00000000FFFFFFFF00000000FFFFFFFF == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);586 try expect(0xFFFFFFFF00000000FFFFFFFF00000000 ^ 0x00000000FFFFFFFF00000000FFFFFFFF == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);
587 expect(0xFFFFFFFF00000000FFFFFFFF00000000 ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0x00000000FFFFFFFF00000000FFFFFFFF);587 try expect(0xFFFFFFFF00000000FFFFFFFF00000000 ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0x00000000FFFFFFFF00000000FFFFFFFF);
588 expect(0x00000000FFFFFFFF00000000FFFFFFFF ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0xFFFFFFFF00000000FFFFFFFF00000000);588 try expect(0x00000000FFFFFFFF00000000FFFFFFFF ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0xFFFFFFFF00000000FFFFFFFF00000000);
589 }589 }
590}590}
591591
592test "f128" {592test "f128" {
593 test_f128();593 try test_f128();
594 comptime test_f128();594 comptime try test_f128();
595}595}
596596
597fn make_f128(x: f128) f128 {597fn make_f128(x: f128) f128 {
598 return x;598 return x;
599}599}
600600
601fn test_f128() void {601fn test_f128() !void {
602 expect(@sizeOf(f128) == 16);602 try expect(@sizeOf(f128) == 16);
603 expect(make_f128(1.0) == 1.0);603 try expect(make_f128(1.0) == 1.0);
604 expect(make_f128(1.0) != 1.1);604 try expect(make_f128(1.0) != 1.1);
605 expect(make_f128(1.0) > 0.9);605 try expect(make_f128(1.0) > 0.9);
606 expect(make_f128(1.0) >= 0.9);606 try expect(make_f128(1.0) >= 0.9);
607 expect(make_f128(1.0) >= 1.0);607 try expect(make_f128(1.0) >= 1.0);
608 should_not_be_zero(1.0);608 try should_not_be_zero(1.0);
609}609}
610610
611fn should_not_be_zero(x: f128) void {611fn should_not_be_zero(x: f128) !void {
612 expect(x != 0.0);612 try expect(x != 0.0);
613}613}
614614
615test "comptime float rem int" {615test "comptime float rem int" {
616 comptime {616 comptime {
617 var x = @as(f32, 1) % 2;617 var x = @as(f32, 1) % 2;
618 expect(x == 1.0);618 try expect(x == 1.0);
619 }619 }
620}620}
621621
622test "remainder division" {622test "remainder division" {
623 comptime remdiv(f16);623 comptime try remdiv(f16);
624 comptime remdiv(f32);624 comptime try remdiv(f32);
625 comptime remdiv(f64);625 comptime try remdiv(f64);
626 comptime remdiv(f128);626 comptime try remdiv(f128);
627 remdiv(f16);627 try remdiv(f16);
628 remdiv(f64);628 try remdiv(f64);
629 remdiv(f128);629 try remdiv(f128);
630}630}
631631
632fn remdiv(comptime T: type) void {632fn remdiv(comptime T: type) !void {
633 expect(@as(T, 1) == @as(T, 1) % @as(T, 2));633 try expect(@as(T, 1) == @as(T, 1) % @as(T, 2));
634 expect(@as(T, 1) == @as(T, 7) % @as(T, 3));634 try expect(@as(T, 1) == @as(T, 7) % @as(T, 3));
635}635}
636636
637test "@sqrt" {637test "@sqrt" {
638 testSqrt(f64, 12.0);638 try testSqrt(f64, 12.0);
639 comptime testSqrt(f64, 12.0);639 comptime try testSqrt(f64, 12.0);
640 testSqrt(f32, 13.0);640 try testSqrt(f32, 13.0);
641 comptime testSqrt(f32, 13.0);641 comptime try testSqrt(f32, 13.0);
642 testSqrt(f16, 13.0);642 try testSqrt(f16, 13.0);
643 comptime testSqrt(f16, 13.0);643 comptime try testSqrt(f16, 13.0);
644644
645 const x = 14.0;645 const x = 14.0;
646 const y = x * x;646 const y = x * x;
647 const z = @sqrt(y);647 const z = @sqrt(y);
648 comptime expect(z == x);648 comptime try expect(z == x);
649}649}
650650
651fn testSqrt(comptime T: type, x: T) void {651fn testSqrt(comptime T: type, x: T) !void {
652 expect(@sqrt(x * x) == x);652 try expect(@sqrt(x * x) == x);
653}653}
654654
655test "@fabs" {655test "@fabs" {
656 testFabs(f128, 12.0);656 try testFabs(f128, 12.0);
657 comptime testFabs(f128, 12.0);657 comptime try testFabs(f128, 12.0);
658 testFabs(f64, 12.0);658 try testFabs(f64, 12.0);
659 comptime testFabs(f64, 12.0);659 comptime try testFabs(f64, 12.0);
660 testFabs(f32, 12.0);660 try testFabs(f32, 12.0);
661 comptime testFabs(f32, 12.0);661 comptime try testFabs(f32, 12.0);
662 testFabs(f16, 12.0);662 try testFabs(f16, 12.0);
663 comptime testFabs(f16, 12.0);663 comptime try testFabs(f16, 12.0);
664664
665 const x = 14.0;665 const x = 14.0;
666 const y = -x;666 const y = -x;
667 const z = @fabs(y);667 const z = @fabs(y);
668 comptime expectEqual(x, z);668 comptime try expectEqual(x, z);
669}669}
670670
671fn testFabs(comptime T: type, x: T) void {671fn testFabs(comptime T: type, x: T) !void {
672 const y = -x;672 const y = -x;
673 const z = @fabs(y);673 const z = @fabs(y);
674 expectEqual(x, z);674 try expectEqual(x, z);
675}675}
676676
677test "@floor" {677test "@floor" {
678 // FIXME: Generates a floorl function call678 // FIXME: Generates a floorl function call
679 // testFloor(f128, 12.0);679 // testFloor(f128, 12.0);
680 comptime testFloor(f128, 12.0);680 comptime try testFloor(f128, 12.0);
681 testFloor(f64, 12.0);681 try testFloor(f64, 12.0);
682 comptime testFloor(f64, 12.0);682 comptime try testFloor(f64, 12.0);
683 testFloor(f32, 12.0);683 try testFloor(f32, 12.0);
684 comptime testFloor(f32, 12.0);684 comptime try testFloor(f32, 12.0);
685 testFloor(f16, 12.0);685 try testFloor(f16, 12.0);
686 comptime testFloor(f16, 12.0);686 comptime try testFloor(f16, 12.0);
687687
688 const x = 14.0;688 const x = 14.0;
689 const y = x + 0.7;689 const y = x + 0.7;
690 const z = @floor(y);690 const z = @floor(y);
691 comptime expectEqual(x, z);691 comptime try expectEqual(x, z);
692}692}
693693
694fn testFloor(comptime T: type, x: T) void {694fn testFloor(comptime T: type, x: T) !void {
695 const y = x + 0.6;695 const y = x + 0.6;
696 const z = @floor(y);696 const z = @floor(y);
697 expectEqual(x, z);697 try expectEqual(x, z);
698}698}
699699
700test "@ceil" {700test "@ceil" {
701 // FIXME: Generates a ceill function call701 // FIXME: Generates a ceill function call
702 //testCeil(f128, 12.0);702 //testCeil(f128, 12.0);
703 comptime testCeil(f128, 12.0);703 comptime try testCeil(f128, 12.0);
704 testCeil(f64, 12.0);704 try testCeil(f64, 12.0);
705 comptime testCeil(f64, 12.0);705 comptime try testCeil(f64, 12.0);
706 testCeil(f32, 12.0);706 try testCeil(f32, 12.0);
707 comptime testCeil(f32, 12.0);707 comptime try testCeil(f32, 12.0);
708 testCeil(f16, 12.0);708 try testCeil(f16, 12.0);
709 comptime testCeil(f16, 12.0);709 comptime try testCeil(f16, 12.0);
710710
711 const x = 14.0;711 const x = 14.0;
712 const y = x - 0.7;712 const y = x - 0.7;
713 const z = @ceil(y);713 const z = @ceil(y);
714 comptime expectEqual(x, z);714 comptime try expectEqual(x, z);
715}715}
716716
717fn testCeil(comptime T: type, x: T) void {717fn testCeil(comptime T: type, x: T) !void {
718 const y = x - 0.8;718 const y = x - 0.8;
719 const z = @ceil(y);719 const z = @ceil(y);
720 expectEqual(x, z);720 try expectEqual(x, z);
721}721}
722722
723test "@trunc" {723test "@trunc" {
724 // FIXME: Generates a truncl function call724 // FIXME: Generates a truncl function call
725 //testTrunc(f128, 12.0);725 //testTrunc(f128, 12.0);
726 comptime testTrunc(f128, 12.0);726 comptime try testTrunc(f128, 12.0);
727 testTrunc(f64, 12.0);727 try testTrunc(f64, 12.0);
728 comptime testTrunc(f64, 12.0);728 comptime try testTrunc(f64, 12.0);
729 testTrunc(f32, 12.0);729 try testTrunc(f32, 12.0);
730 comptime testTrunc(f32, 12.0);730 comptime try testTrunc(f32, 12.0);
731 testTrunc(f16, 12.0);731 try testTrunc(f16, 12.0);
732 comptime testTrunc(f16, 12.0);732 comptime try testTrunc(f16, 12.0);
733733
734 const x = 14.0;734 const x = 14.0;
735 const y = x + 0.7;735 const y = x + 0.7;
736 const z = @trunc(y);736 const z = @trunc(y);
737 comptime expectEqual(x, z);737 comptime try expectEqual(x, z);
738}738}
739739
740fn testTrunc(comptime T: type, x: T) void {740fn testTrunc(comptime T: type, x: T) !void {
741 {741 {
742 const y = x + 0.8;742 const y = x + 0.8;
743 const z = @trunc(y);743 const z = @trunc(y);
744 expectEqual(x, z);744 try expectEqual(x, z);
745 }745 }
746746
747 {747 {
748 const y = -x - 0.8;748 const y = -x - 0.8;
749 const z = @trunc(y);749 const z = @trunc(y);
750 expectEqual(-x, z);750 try expectEqual(-x, z);
751 }751 }
752}752}
753753
754test "@round" {754test "@round" {
755 // FIXME: Generates a roundl function call755 // FIXME: Generates a roundl function call
756 //testRound(f128, 12.0);756 //testRound(f128, 12.0);
757 comptime testRound(f128, 12.0);757 comptime try testRound(f128, 12.0);
758 testRound(f64, 12.0);758 try testRound(f64, 12.0);
759 comptime testRound(f64, 12.0);759 comptime try testRound(f64, 12.0);
760 testRound(f32, 12.0);760 try testRound(f32, 12.0);
761 comptime testRound(f32, 12.0);761 comptime try testRound(f32, 12.0);
762 testRound(f16, 12.0);762 try testRound(f16, 12.0);
763 comptime testRound(f16, 12.0);763 comptime try testRound(f16, 12.0);
764764
765 const x = 14.0;765 const x = 14.0;
766 const y = x + 0.4;766 const y = x + 0.4;
767 const z = @round(y);767 const z = @round(y);
768 comptime expectEqual(x, z);768 comptime try expectEqual(x, z);
769}769}
770770
771fn testRound(comptime T: type, x: T) void {771fn testRound(comptime T: type, x: T) !void {
772 const y = x - 0.5;772 const y = x - 0.5;
773 const z = @round(y);773 const z = @round(y);
774 expectEqual(x, z);774 try expectEqual(x, z);
775}775}
776776
777test "comptime_int param and return" {777test "comptime_int param and return" {
778 const a = comptimeAdd(35361831660712422535336160538497375248, 101752735581729509668353361206450473702);778 const a = comptimeAdd(35361831660712422535336160538497375248, 101752735581729509668353361206450473702);
779 expect(a == 137114567242441932203689521744947848950);779 try expect(a == 137114567242441932203689521744947848950);
780780
781 const b = comptimeAdd(594491908217841670578297176641415611445982232488944558774612, 390603545391089362063884922208143568023166603618446395589768);781 const b = comptimeAdd(594491908217841670578297176641415611445982232488944558774612, 390603545391089362063884922208143568023166603618446395589768);
782 expect(b == 985095453608931032642182098849559179469148836107390954364380);782 try expect(b == 985095453608931032642182098849559179469148836107390954364380);
783}783}
784784
785fn comptimeAdd(comptime a: comptime_int, comptime b: comptime_int) comptime_int {785fn comptimeAdd(comptime a: comptime_int, comptime b: comptime_int) comptime_int {
...@@ -788,85 +788,85 @@ fn comptimeAdd(comptime a: comptime_int, comptime b: comptime_int) comptime_int...@@ -788,85 +788,85 @@ fn comptimeAdd(comptime a: comptime_int, comptime b: comptime_int) comptime_int
788788
789test "vector integer addition" {789test "vector integer addition" {
790 const S = struct {790 const S = struct {
791 fn doTheTest() void {791 fn doTheTest() !void {
792 var a: std.meta.Vector(4, i32) = [_]i32{ 1, 2, 3, 4 };792 var a: std.meta.Vector(4, i32) = [_]i32{ 1, 2, 3, 4 };
793 var b: std.meta.Vector(4, i32) = [_]i32{ 5, 6, 7, 8 };793 var b: std.meta.Vector(4, i32) = [_]i32{ 5, 6, 7, 8 };
794 var result = a + b;794 var result = a + b;
795 var result_array: [4]i32 = result;795 var result_array: [4]i32 = result;
796 const expected = [_]i32{ 6, 8, 10, 12 };796 const expected = [_]i32{ 6, 8, 10, 12 };
797 expectEqualSlices(i32, &expected, &result_array);797 try expectEqualSlices(i32, &expected, &result_array);
798 }798 }
799 };799 };
800 S.doTheTest();800 try S.doTheTest();
801 comptime S.doTheTest();801 comptime try S.doTheTest();
802}802}
803803
804test "NaN comparison" {804test "NaN comparison" {
805 testNanEqNan(f16);805 try testNanEqNan(f16);
806 testNanEqNan(f32);806 try testNanEqNan(f32);
807 testNanEqNan(f64);807 try testNanEqNan(f64);
808 testNanEqNan(f128);808 try testNanEqNan(f128);
809 comptime testNanEqNan(f16);809 comptime try testNanEqNan(f16);
810 comptime testNanEqNan(f32);810 comptime try testNanEqNan(f32);
811 comptime testNanEqNan(f64);811 comptime try testNanEqNan(f64);
812 comptime testNanEqNan(f128);812 comptime try testNanEqNan(f128);
813}813}
814814
815fn testNanEqNan(comptime F: type) void {815fn testNanEqNan(comptime F: type) !void {
816 var nan1 = std.math.nan(F);816 var nan1 = std.math.nan(F);
817 var nan2 = std.math.nan(F);817 var nan2 = std.math.nan(F);
818 expect(nan1 != nan2);818 try expect(nan1 != nan2);
819 expect(!(nan1 == nan2));819 try expect(!(nan1 == nan2));
820 expect(!(nan1 > nan2));820 try expect(!(nan1 > nan2));
821 expect(!(nan1 >= nan2));821 try expect(!(nan1 >= nan2));
822 expect(!(nan1 < nan2));822 try expect(!(nan1 < nan2));
823 expect(!(nan1 <= nan2));823 try expect(!(nan1 <= nan2));
824}824}
825825
826test "128-bit multiplication" {826test "128-bit multiplication" {
827 var a: i128 = 3;827 var a: i128 = 3;
828 var b: i128 = 2;828 var b: i128 = 2;
829 var c = a * b;829 var c = a * b;
830 expect(c == 6);830 try expect(c == 6);
831}831}
832832
833test "vector comparison" {833test "vector comparison" {
834 const S = struct {834 const S = struct {
835 fn doTheTest() void {835 fn doTheTest() !void {
836 var a: std.meta.Vector(6, i32) = [_]i32{ 1, 3, -1, 5, 7, 9 };836 var a: std.meta.Vector(6, i32) = [_]i32{ 1, 3, -1, 5, 7, 9 };
837 var b: std.meta.Vector(6, i32) = [_]i32{ -1, 3, 0, 6, 10, -10 };837 var b: std.meta.Vector(6, i32) = [_]i32{ -1, 3, 0, 6, 10, -10 };
838 expect(mem.eql(bool, &@as([6]bool, a < b), &[_]bool{ false, false, true, true, true, false }));838 try expect(mem.eql(bool, &@as([6]bool, a < b), &[_]bool{ false, false, true, true, true, false }));
839 expect(mem.eql(bool, &@as([6]bool, a <= b), &[_]bool{ false, true, true, true, true, false }));839 try expect(mem.eql(bool, &@as([6]bool, a <= b), &[_]bool{ false, true, true, true, true, false }));
840 expect(mem.eql(bool, &@as([6]bool, a == b), &[_]bool{ false, true, false, false, false, false }));840 try expect(mem.eql(bool, &@as([6]bool, a == b), &[_]bool{ false, true, false, false, false, false }));
841 expect(mem.eql(bool, &@as([6]bool, a != b), &[_]bool{ true, false, true, true, true, true }));841 try expect(mem.eql(bool, &@as([6]bool, a != b), &[_]bool{ true, false, true, true, true, true }));
842 expect(mem.eql(bool, &@as([6]bool, a > b), &[_]bool{ true, false, false, false, false, true }));842 try expect(mem.eql(bool, &@as([6]bool, a > b), &[_]bool{ true, false, false, false, false, true }));
843 expect(mem.eql(bool, &@as([6]bool, a >= b), &[_]bool{ true, true, false, false, false, true }));843 try expect(mem.eql(bool, &@as([6]bool, a >= b), &[_]bool{ true, true, false, false, false, true }));
844 }844 }
845 };845 };
846 S.doTheTest();846 try S.doTheTest();
847 comptime S.doTheTest();847 comptime try S.doTheTest();
848}848}
849849
850test "compare undefined literal with comptime_int" {850test "compare undefined literal with comptime_int" {
851 var x = undefined == 1;851 var x = undefined == 1;
852 // x is now undefined with type bool852 // x is now undefined with type bool
853 x = true;853 x = true;
854 expect(x);854 try expect(x);
855}855}
856856
857test "signed zeros are represented properly" {857test "signed zeros are represented properly" {
858 const S = struct {858 const S = struct {
859 fn doTheTest() void {859 fn doTheTest() !void {
860 inline for ([_]type{ f16, f32, f64, f128 }) |T| {860 inline for ([_]type{ f16, f32, f64, f128 }) |T| {
861 const ST = std.meta.Int(.unsigned, @typeInfo(T).Float.bits);861 const ST = std.meta.Int(.unsigned, @typeInfo(T).Float.bits);
862 var as_fp_val = -@as(T, 0.0);862 var as_fp_val = -@as(T, 0.0);
863 var as_uint_val = @bitCast(ST, as_fp_val);863 var as_uint_val = @bitCast(ST, as_fp_val);
864 // Ensure the sign bit is set.864 // Ensure the sign bit is set.
865 expect(as_uint_val >> (@typeInfo(T).Float.bits - 1) == 1);865 try expect(as_uint_val >> (@typeInfo(T).Float.bits - 1) == 1);
866 }866 }
867 }867 }
868 };868 };
869869
870 S.doTheTest();870 try S.doTheTest();
871 comptime S.doTheTest();871 comptime try S.doTheTest();
872}872}
test/stage1/behavior/misc.zig+107-107
...@@ -25,18 +25,18 @@ test "call disabled extern fn" {...@@ -25,18 +25,18 @@ test "call disabled extern fn" {
25}25}
2626
27test "short circuit" {27test "short circuit" {
28 testShortCircuit(false, true);28 try testShortCircuit(false, true);
29 comptime testShortCircuit(false, true);29 comptime try testShortCircuit(false, true);
30}30}
3131
32fn testShortCircuit(f: bool, t: bool) void {32fn testShortCircuit(f: bool, t: bool) !void {
33 var hit_1 = f;33 var hit_1 = f;
34 var hit_2 = f;34 var hit_2 = f;
35 var hit_3 = f;35 var hit_3 = f;
36 var hit_4 = f;36 var hit_4 = f;
3737
38 if (t or x: {38 if (t or x: {
39 expect(f);39 try expect(f);
40 break :x f;40 break :x f;
41 }) {41 }) {
42 hit_1 = t;42 hit_1 = t;
...@@ -45,31 +45,31 @@ fn testShortCircuit(f: bool, t: bool) void {...@@ -45,31 +45,31 @@ fn testShortCircuit(f: bool, t: bool) void {
45 hit_2 = t;45 hit_2 = t;
46 break :x f;46 break :x f;
47 }) {47 }) {
48 expect(f);48 try expect(f);
49 }49 }
5050
51 if (t and x: {51 if (t and x: {
52 hit_3 = t;52 hit_3 = t;
53 break :x f;53 break :x f;
54 }) {54 }) {
55 expect(f);55 try expect(f);
56 }56 }
57 if (f and x: {57 if (f and x: {
58 expect(f);58 try expect(f);
59 break :x f;59 break :x f;
60 }) {60 }) {
61 expect(f);61 try expect(f);
62 } else {62 } else {
63 hit_4 = t;63 hit_4 = t;
64 }64 }
65 expect(hit_1);65 try expect(hit_1);
66 expect(hit_2);66 try expect(hit_2);
67 expect(hit_3);67 try expect(hit_3);
68 expect(hit_4);68 try expect(hit_4);
69}69}
7070
71test "truncate" {71test "truncate" {
72 expect(testTruncate(0x10fd) == 0xfd);72 try expect(testTruncate(0x10fd) == 0xfd);
73}73}
74fn testTruncate(x: u32) u8 {74fn testTruncate(x: u32) u8 {
75 return @truncate(u8, x);75 return @truncate(u8, x);
...@@ -80,16 +80,16 @@ fn first4KeysOfHomeRow() []const u8 {...@@ -80,16 +80,16 @@ fn first4KeysOfHomeRow() []const u8 {
80}80}
8181
82test "return string from function" {82test "return string from function" {
83 expect(mem.eql(u8, first4KeysOfHomeRow(), "aoeu"));83 try expect(mem.eql(u8, first4KeysOfHomeRow(), "aoeu"));
84}84}
8585
86const g1: i32 = 1233 + 1;86const g1: i32 = 1233 + 1;
87var g2: i32 = 0;87var g2: i32 = 0;
8888
89test "global variables" {89test "global variables" {
90 expect(g2 == 0);90 try expect(g2 == 0);
91 g2 = g1;91 g2 = g1;
92 expect(g2 == 1234);92 try expect(g2 == 1234);
93}93}
9494
95test "memcpy and memset intrinsics" {95test "memcpy and memset intrinsics" {
...@@ -106,7 +106,7 @@ test "builtin static eval" {...@@ -106,7 +106,7 @@ test "builtin static eval" {
106 const x: i32 = comptime x: {106 const x: i32 = comptime x: {
107 break :x 1 + 2 + 3;107 break :x 1 + 2 + 3;
108 };108 };
109 expect(x == comptime 6);109 try expect(x == comptime 6);
110}110}
111111
112test "slicing" {112test "slicing" {
...@@ -127,7 +127,7 @@ test "slicing" {...@@ -127,7 +127,7 @@ test "slicing" {
127127
128test "constant equal function pointers" {128test "constant equal function pointers" {
129 const alias = emptyFn;129 const alias = emptyFn;
130 expect(comptime x: {130 try expect(comptime x: {
131 break :x emptyFn == alias;131 break :x emptyFn == alias;
132 });132 });
133}133}
...@@ -135,25 +135,25 @@ test "constant equal function pointers" {...@@ -135,25 +135,25 @@ test "constant equal function pointers" {
135fn emptyFn() void {}135fn emptyFn() void {}
136136
137test "hex escape" {137test "hex escape" {
138 expect(mem.eql(u8, "\x68\x65\x6c\x6c\x6f", "hello"));138 try expect(mem.eql(u8, "\x68\x65\x6c\x6c\x6f", "hello"));
139}139}
140140
141test "string concatenation" {141test "string concatenation" {
142 expect(mem.eql(u8, "OK" ++ " IT " ++ "WORKED", "OK IT WORKED"));142 try expect(mem.eql(u8, "OK" ++ " IT " ++ "WORKED", "OK IT WORKED"));
143}143}
144144
145test "array mult operator" {145test "array mult operator" {
146 expect(mem.eql(u8, "ab" ** 5, "ababababab"));146 try expect(mem.eql(u8, "ab" ** 5, "ababababab"));
147}147}
148148
149test "string escapes" {149test "string escapes" {
150 expect(mem.eql(u8, "\"", "\x22"));150 try expect(mem.eql(u8, "\"", "\x22"));
151 expect(mem.eql(u8, "\'", "\x27"));151 try expect(mem.eql(u8, "\'", "\x27"));
152 expect(mem.eql(u8, "\n", "\x0a"));152 try expect(mem.eql(u8, "\n", "\x0a"));
153 expect(mem.eql(u8, "\r", "\x0d"));153 try expect(mem.eql(u8, "\r", "\x0d"));
154 expect(mem.eql(u8, "\t", "\x09"));154 try expect(mem.eql(u8, "\t", "\x09"));
155 expect(mem.eql(u8, "\\", "\x5c"));155 try expect(mem.eql(u8, "\\", "\x5c"));
156 expect(mem.eql(u8, "\u{1234}\u{069}\u{1}", "\xe1\x88\xb4\x69\x01"));156 try expect(mem.eql(u8, "\u{1234}\u{069}\u{1}", "\xe1\x88\xb4\x69\x01"));
157}157}
158158
159test "multiline string" {159test "multiline string" {
...@@ -163,7 +163,7 @@ test "multiline string" {...@@ -163,7 +163,7 @@ test "multiline string" {
163 \\three163 \\three
164 ;164 ;
165 const s2 = "one\ntwo)\nthree";165 const s2 = "one\ntwo)\nthree";
166 expect(mem.eql(u8, s1, s2));166 try expect(mem.eql(u8, s1, s2));
167}167}
168168
169test "multiline string comments at start" {169test "multiline string comments at start" {
...@@ -173,7 +173,7 @@ test "multiline string comments at start" {...@@ -173,7 +173,7 @@ test "multiline string comments at start" {
173 \\three173 \\three
174 ;174 ;
175 const s2 = "two)\nthree";175 const s2 = "two)\nthree";
176 expect(mem.eql(u8, s1, s2));176 try expect(mem.eql(u8, s1, s2));
177}177}
178178
179test "multiline string comments at end" {179test "multiline string comments at end" {
...@@ -183,7 +183,7 @@ test "multiline string comments at end" {...@@ -183,7 +183,7 @@ test "multiline string comments at end" {
183 //\\three183 //\\three
184 ;184 ;
185 const s2 = "one\ntwo)";185 const s2 = "one\ntwo)";
186 expect(mem.eql(u8, s1, s2));186 try expect(mem.eql(u8, s1, s2));
187}187}
188188
189test "multiline string comments in middle" {189test "multiline string comments in middle" {
...@@ -193,7 +193,7 @@ test "multiline string comments in middle" {...@@ -193,7 +193,7 @@ test "multiline string comments in middle" {
193 \\three193 \\three
194 ;194 ;
195 const s2 = "one\nthree";195 const s2 = "one\nthree";
196 expect(mem.eql(u8, s1, s2));196 try expect(mem.eql(u8, s1, s2));
197}197}
198198
199test "multiline string comments at multiple places" {199test "multiline string comments at multiple places" {
...@@ -205,7 +205,7 @@ test "multiline string comments at multiple places" {...@@ -205,7 +205,7 @@ test "multiline string comments at multiple places" {
205 \\five205 \\five
206 ;206 ;
207 const s2 = "one\nthree\nfive";207 const s2 = "one\nthree\nfive";
208 expect(mem.eql(u8, s1, s2));208 try expect(mem.eql(u8, s1, s2));
209}209}
210210
211test "multiline C string" {211test "multiline C string" {
...@@ -215,11 +215,11 @@ test "multiline C string" {...@@ -215,11 +215,11 @@ test "multiline C string" {
215 \\three215 \\three
216 ;216 ;
217 const s2 = "one\ntwo)\nthree";217 const s2 = "one\ntwo)\nthree";
218 expect(std.cstr.cmp(s1, s2) == 0);218 try expect(std.cstr.cmp(s1, s2) == 0);
219}219}
220220
221test "type equality" {221test "type equality" {
222 expect(*const u8 != *u8);222 try expect(*const u8 != *u8);
223}223}
224224
225const global_a: i32 = 1234;225const global_a: i32 = 1234;
...@@ -227,7 +227,7 @@ const global_b: *const i32 = &global_a;...@@ -227,7 +227,7 @@ const global_b: *const i32 = &global_a;
227const global_c: *const f32 = @ptrCast(*const f32, global_b);227const global_c: *const f32 = @ptrCast(*const f32, global_b);
228test "compile time global reinterpret" {228test "compile time global reinterpret" {
229 const d = @ptrCast(*const i32, global_c);229 const d = @ptrCast(*const i32, global_c);
230 expect(d.* == 1234);230 try expect(d.* == 1234);
231}231}
232232
233test "explicit cast maybe pointers" {233test "explicit cast maybe pointers" {
...@@ -253,8 +253,8 @@ test "cast undefined" {...@@ -253,8 +253,8 @@ test "cast undefined" {
253fn testCastUndefined(x: []const u8) void {}253fn testCastUndefined(x: []const u8) void {}
254254
255test "cast small unsigned to larger signed" {255test "cast small unsigned to larger signed" {
256 expect(castSmallUnsignedToLargerSigned1(200) == @as(i16, 200));256 try expect(castSmallUnsignedToLargerSigned1(200) == @as(i16, 200));
257 expect(castSmallUnsignedToLargerSigned2(9999) == @as(i64, 9999));257 try expect(castSmallUnsignedToLargerSigned2(9999) == @as(i64, 9999));
258}258}
259fn castSmallUnsignedToLargerSigned1(x: u8) i16 {259fn castSmallUnsignedToLargerSigned1(x: u8) i16 {
260 return x;260 return x;
...@@ -264,7 +264,7 @@ fn castSmallUnsignedToLargerSigned2(x: u16) i64 {...@@ -264,7 +264,7 @@ fn castSmallUnsignedToLargerSigned2(x: u16) i64 {
264}264}
265265
266test "implicit cast after unreachable" {266test "implicit cast after unreachable" {
267 expect(outer() == 1234);267 try expect(outer() == 1234);
268}268}
269fn inner() i32 {269fn inner() i32 {
270 return 1234;270 return 1234;
...@@ -279,13 +279,13 @@ test "pointer dereferencing" {...@@ -279,13 +279,13 @@ test "pointer dereferencing" {
279279
280 y.* += 1;280 y.* += 1;
281281
282 expect(x == 4);282 try expect(x == 4);
283 expect(y.* == 4);283 try expect(y.* == 4);
284}284}
285285
286test "call result of if else expression" {286test "call result of if else expression" {
287 expect(mem.eql(u8, f2(true), "a"));287 try expect(mem.eql(u8, f2(true), "a"));
288 expect(mem.eql(u8, f2(false), "b"));288 try expect(mem.eql(u8, f2(false), "b"));
289}289}
290fn f2(x: bool) []const u8 {290fn f2(x: bool) []const u8 {
291 return (if (x) fA else fB)();291 return (if (x) fA else fB)();
...@@ -305,8 +305,8 @@ test "const expression eval handling of variables" {...@@ -305,8 +305,8 @@ test "const expression eval handling of variables" {
305}305}
306306
307test "constant enum initialization with differing sizes" {307test "constant enum initialization with differing sizes" {
308 test3_1(test3_foo);308 try test3_1(test3_foo);
309 test3_2(test3_bar);309 try test3_2(test3_bar);
310}310}
311const Test3Foo = union(enum) {311const Test3Foo = union(enum) {
312 One: void,312 One: void,
...@@ -324,41 +324,41 @@ const test3_foo = Test3Foo{...@@ -324,41 +324,41 @@ const test3_foo = Test3Foo{
324 },324 },
325};325};
326const test3_bar = Test3Foo{ .Two = 13 };326const test3_bar = Test3Foo{ .Two = 13 };
327fn test3_1(f: Test3Foo) void {327fn test3_1(f: Test3Foo) !void {
328 switch (f) {328 switch (f) {
329 Test3Foo.Three => |pt| {329 Test3Foo.Three => |pt| {
330 expect(pt.x == 3);330 try expect(pt.x == 3);
331 expect(pt.y == 4);331 try expect(pt.y == 4);
332 },332 },
333 else => unreachable,333 else => unreachable,
334 }334 }
335}335}
336fn test3_2(f: Test3Foo) void {336fn test3_2(f: Test3Foo) !void {
337 switch (f) {337 switch (f) {
338 Test3Foo.Two => |x| {338 Test3Foo.Two => |x| {
339 expect(x == 13);339 try expect(x == 13);
340 },340 },
341 else => unreachable,341 else => unreachable,
342 }342 }
343}343}
344344
345test "character literals" {345test "character literals" {
346 expect('\'' == single_quote);346 try expect('\'' == single_quote);
347}347}
348const single_quote = '\'';348const single_quote = '\'';
349349
350test "take address of parameter" {350test "take address of parameter" {
351 testTakeAddressOfParameter(12.34);351 try testTakeAddressOfParameter(12.34);
352}352}
353fn testTakeAddressOfParameter(f: f32) void {353fn testTakeAddressOfParameter(f: f32) !void {
354 const f_ptr = &f;354 const f_ptr = &f;
355 expect(f_ptr.* == 12.34);355 try expect(f_ptr.* == 12.34);
356}356}
357357
358test "pointer comparison" {358test "pointer comparison" {
359 const a = @as([]const u8, "a");359 const a = @as([]const u8, "a");
360 const b = &a;360 const b = &a;
361 expect(ptrEql(b, b));361 try expect(ptrEql(b, b));
362}362}
363fn ptrEql(a: *const []const u8, b: *const []const u8) bool {363fn ptrEql(a: *const []const u8, b: *const []const u8) bool {
364 return a == b;364 return a == b;
...@@ -368,19 +368,19 @@ test "string concatenation" {...@@ -368,19 +368,19 @@ test "string concatenation" {
368 const a = "OK" ++ " IT " ++ "WORKED";368 const a = "OK" ++ " IT " ++ "WORKED";
369 const b = "OK IT WORKED";369 const b = "OK IT WORKED";
370370
371 comptime expect(@TypeOf(a) == *const [12:0]u8);371 comptime try expect(@TypeOf(a) == *const [12:0]u8);
372 comptime expect(@TypeOf(b) == *const [12:0]u8);372 comptime try expect(@TypeOf(b) == *const [12:0]u8);
373373
374 const len = mem.len(b);374 const len = mem.len(b);
375 const len_with_null = len + 1;375 const len_with_null = len + 1;
376 {376 {
377 var i: u32 = 0;377 var i: u32 = 0;
378 while (i < len_with_null) : (i += 1) {378 while (i < len_with_null) : (i += 1) {
379 expect(a[i] == b[i]);379 try expect(a[i] == b[i]);
380 }380 }
381 }381 }
382 expect(a[len] == 0);382 try expect(a[len] == 0);
383 expect(b[len] == 0);383 try expect(b[len] == 0);
384}384}
385385
386test "pointer to void return type" {386test "pointer to void return type" {
...@@ -397,7 +397,7 @@ fn testPointerToVoidReturnType2() *const void {...@@ -397,7 +397,7 @@ fn testPointerToVoidReturnType2() *const void {
397397
398test "non const ptr to aliased type" {398test "non const ptr to aliased type" {
399 const int = i32;399 const int = i32;
400 expect(?*int == ?*i32);400 try expect(?*int == ?*i32);
401}401}
402402
403test "array 2D const double ptr" {403test "array 2D const double ptr" {
...@@ -405,13 +405,13 @@ test "array 2D const double ptr" {...@@ -405,13 +405,13 @@ test "array 2D const double ptr" {
405 [_]f32{1.0},405 [_]f32{1.0},
406 [_]f32{2.0},406 [_]f32{2.0},
407 };407 };
408 testArray2DConstDoublePtr(&rect_2d_vertexes[0][0]);408 try testArray2DConstDoublePtr(&rect_2d_vertexes[0][0]);
409}409}
410410
411fn testArray2DConstDoublePtr(ptr: *const f32) void {411fn testArray2DConstDoublePtr(ptr: *const f32) !void {
412 const ptr2 = @ptrCast([*]const f32, ptr);412 const ptr2 = @ptrCast([*]const f32, ptr);
413 expect(ptr2[0] == 1.0);413 try expect(ptr2[0] == 1.0);
414 expect(ptr2[1] == 2.0);414 try expect(ptr2[1] == 2.0);
415}415}
416416
417const AStruct = struct {417const AStruct = struct {
...@@ -439,13 +439,13 @@ test "@typeName" {...@@ -439,13 +439,13 @@ test "@typeName" {
439 Unused,439 Unused,
440 };440 };
441 comptime {441 comptime {
442 expect(mem.eql(u8, @typeName(i64), "i64"));442 try expect(mem.eql(u8, @typeName(i64), "i64"));
443 expect(mem.eql(u8, @typeName(*usize), "*usize"));443 try expect(mem.eql(u8, @typeName(*usize), "*usize"));
444 // https://github.com/ziglang/zig/issues/675444 // https://github.com/ziglang/zig/issues/675
445 expect(mem.eql(u8, "behavior.misc.TypeFromFn(u8)", @typeName(TypeFromFn(u8))));445 try expect(mem.eql(u8, "behavior.misc.TypeFromFn(u8)", @typeName(TypeFromFn(u8))));
446 expect(mem.eql(u8, @typeName(Struct), "Struct"));446 try expect(mem.eql(u8, @typeName(Struct), "Struct"));
447 expect(mem.eql(u8, @typeName(Union), "Union"));447 try expect(mem.eql(u8, @typeName(Union), "Union"));
448 expect(mem.eql(u8, @typeName(Enum), "Enum"));448 try expect(mem.eql(u8, @typeName(Enum), "Enum"));
449 }449 }
450}450}
451451
...@@ -455,14 +455,14 @@ fn TypeFromFn(comptime T: type) type {...@@ -455,14 +455,14 @@ fn TypeFromFn(comptime T: type) type {
455455
456test "double implicit cast in same expression" {456test "double implicit cast in same expression" {
457 var x = @as(i32, @as(u16, nine()));457 var x = @as(i32, @as(u16, nine()));
458 expect(x == 9);458 try expect(x == 9);
459}459}
460fn nine() u8 {460fn nine() u8 {
461 return 9;461 return 9;
462}462}
463463
464test "global variable initialized to global variable array element" {464test "global variable initialized to global variable array element" {
465 expect(global_ptr == &gdt[0]);465 try expect(global_ptr == &gdt[0]);
466}466}
467const GDTEntry = struct {467const GDTEntry = struct {
468 field: i32,468 field: i32,
...@@ -483,9 +483,9 @@ export fn writeToVRam() void {...@@ -483,9 +483,9 @@ export fn writeToVRam() void {
483const OpaqueA = opaque {};483const OpaqueA = opaque {};
484const OpaqueB = opaque {};484const OpaqueB = opaque {};
485test "opaque types" {485test "opaque types" {
486 expect(*OpaqueA != *OpaqueB);486 try expect(*OpaqueA != *OpaqueB);
487 expect(mem.eql(u8, @typeName(OpaqueA), "OpaqueA"));487 try expect(mem.eql(u8, @typeName(OpaqueA), "OpaqueA"));
488 expect(mem.eql(u8, @typeName(OpaqueB), "OpaqueB"));488 try expect(mem.eql(u8, @typeName(OpaqueB), "OpaqueB"));
489}489}
490490
491test "variable is allowed to be a pointer to an opaque type" {491test "variable is allowed to be a pointer to an opaque type" {
...@@ -525,7 +525,7 @@ fn fnThatClosesOverLocalConst() type {...@@ -525,7 +525,7 @@ fn fnThatClosesOverLocalConst() type {
525525
526test "function closes over local const" {526test "function closes over local const" {
527 const x = fnThatClosesOverLocalConst().g();527 const x = fnThatClosesOverLocalConst().g();
528 expect(x == 1);528 try expect(x == 1);
529}529}
530530
531test "cold function" {531test "cold function" {
...@@ -562,21 +562,21 @@ export fn testPackedStuff(a: *const PackedStruct, b: *const PackedUnion, c: Pack...@@ -562,21 +562,21 @@ export fn testPackedStuff(a: *const PackedStruct, b: *const PackedUnion, c: Pack
562test "slicing zero length array" {562test "slicing zero length array" {
563 const s1 = ""[0..];563 const s1 = ""[0..];
564 const s2 = ([_]u32{})[0..];564 const s2 = ([_]u32{})[0..];
565 expect(s1.len == 0);565 try expect(s1.len == 0);
566 expect(s2.len == 0);566 try expect(s2.len == 0);
567 expect(mem.eql(u8, s1, ""));567 try expect(mem.eql(u8, s1, ""));
568 expect(mem.eql(u32, s2, &[_]u32{}));568 try expect(mem.eql(u32, s2, &[_]u32{}));
569}569}
570570
571const addr1 = @ptrCast(*const u8, emptyFn);571const addr1 = @ptrCast(*const u8, emptyFn);
572test "comptime cast fn to ptr" {572test "comptime cast fn to ptr" {
573 const addr2 = @ptrCast(*const u8, emptyFn);573 const addr2 = @ptrCast(*const u8, emptyFn);
574 comptime expect(addr1 == addr2);574 comptime try expect(addr1 == addr2);
575}575}
576576
577test "equality compare fn ptrs" {577test "equality compare fn ptrs" {
578 var a = emptyFn;578 var a = emptyFn;
579 expect(a == a);579 try expect(a == a);
580}580}
581581
582test "self reference through fn ptr field" {582test "self reference through fn ptr field" {
...@@ -591,34 +591,34 @@ test "self reference through fn ptr field" {...@@ -591,34 +591,34 @@ test "self reference through fn ptr field" {
591 };591 };
592 var a: S.A = undefined;592 var a: S.A = undefined;
593 a.f = S.foo;593 a.f = S.foo;
594 expect(a.f(a) == 12);594 try expect(a.f(a) == 12);
595}595}
596596
597test "volatile load and store" {597test "volatile load and store" {
598 var number: i32 = 1234;598 var number: i32 = 1234;
599 const ptr = @as(*volatile i32, &number);599 const ptr = @as(*volatile i32, &number);
600 ptr.* += 1;600 ptr.* += 1;
601 expect(ptr.* == 1235);601 try expect(ptr.* == 1235);
602}602}
603603
604test "slice string literal has correct type" {604test "slice string literal has correct type" {
605 comptime {605 comptime {
606 expect(@TypeOf("aoeu"[0..]) == *const [4:0]u8);606 try expect(@TypeOf("aoeu"[0..]) == *const [4:0]u8);
607 const array = [_]i32{ 1, 2, 3, 4 };607 const array = [_]i32{ 1, 2, 3, 4 };
608 expect(@TypeOf(array[0..]) == *const [4]i32);608 try expect(@TypeOf(array[0..]) == *const [4]i32);
609 }609 }
610 var runtime_zero: usize = 0;610 var runtime_zero: usize = 0;
611 comptime expect(@TypeOf("aoeu"[runtime_zero..]) == [:0]const u8);611 comptime try expect(@TypeOf("aoeu"[runtime_zero..]) == [:0]const u8);
612 const array = [_]i32{ 1, 2, 3, 4 };612 const array = [_]i32{ 1, 2, 3, 4 };
613 comptime expect(@TypeOf(array[runtime_zero..]) == []const i32);613 comptime try expect(@TypeOf(array[runtime_zero..]) == []const i32);
614}614}
615615
616test "struct inside function" {616test "struct inside function" {
617 testStructInFn();617 try testStructInFn();
618 comptime testStructInFn();618 comptime try testStructInFn();
619}619}
620620
621fn testStructInFn() void {621fn testStructInFn() !void {
622 const BlockKind = u32;622 const BlockKind = u32;
623623
624 const Block = struct {624 const Block = struct {
...@@ -629,11 +629,11 @@ fn testStructInFn() void {...@@ -629,11 +629,11 @@ fn testStructInFn() void {
629629
630 block.kind += 1;630 block.kind += 1;
631631
632 expect(block.kind == 1235);632 try expect(block.kind == 1235);
633}633}
634634
635test "fn call returning scalar optional in equality expression" {635test "fn call returning scalar optional in equality expression" {
636 expect(getNull() == null);636 try expect(getNull() == null);
637}637}
638638
639fn getNull() ?*i32 {639fn getNull() ?*i32 {
...@@ -645,16 +645,16 @@ test "thread local variable" {...@@ -645,16 +645,16 @@ test "thread local variable" {
645 threadlocal var t: i32 = 1234;645 threadlocal var t: i32 = 1234;
646 };646 };
647 S.t += 1;647 S.t += 1;
648 expect(S.t == 1235);648 try expect(S.t == 1235);
649}649}
650650
651test "unicode escape in character literal" {651test "unicode escape in character literal" {
652 var a: u24 = '\u{01f4a9}';652 var a: u24 = '\u{01f4a9}';
653 expect(a == 128169);653 try expect(a == 128169);
654}654}
655655
656test "unicode character in character literal" {656test "unicode character in character literal" {
657 expect('💩' == 128169);657 try expect('💩' == 128169);
658}658}
659659
660test "result location zero sized array inside struct field implicit cast to slice" {660test "result location zero sized array inside struct field implicit cast to slice" {
...@@ -662,7 +662,7 @@ test "result location zero sized array inside struct field implicit cast to slic...@@ -662,7 +662,7 @@ test "result location zero sized array inside struct field implicit cast to slic
662 entries: []u32,662 entries: []u32,
663 };663 };
664 var foo = E{ .entries = &[_]u32{} };664 var foo = E{ .entries = &[_]u32{} };
665 expect(foo.entries.len == 0);665 try expect(foo.entries.len == 0);
666}666}
667667
668var global_foo: *i32 = undefined;668var global_foo: *i32 = undefined;
...@@ -677,7 +677,7 @@ test "global variable assignment with optional unwrapping with var initialized t...@@ -677,7 +677,7 @@ test "global variable assignment with optional unwrapping with var initialized t
677 global_foo = S.foo() orelse {677 global_foo = S.foo() orelse {
678 @panic("bad");678 @panic("bad");
679 };679 };
680 expect(global_foo.* == 1234);680 try expect(global_foo.* == 1234);
681}681}
682682
683test "peer result location with typed parent, runtime condition, comptime prongs" {683test "peer result location with typed parent, runtime condition, comptime prongs" {
...@@ -696,8 +696,8 @@ test "peer result location with typed parent, runtime condition, comptime prongs...@@ -696,8 +696,8 @@ test "peer result location with typed parent, runtime condition, comptime prongs
696 bleh: i32,696 bleh: i32,
697 };697 };
698 };698 };
699 expect(S.doTheTest(0) == 1234);699 try expect(S.doTheTest(0) == 1234);
700 expect(S.doTheTest(1) == 1234);700 try expect(S.doTheTest(1) == 1234);
701}701}
702702
703test "nested optional field in struct" {703test "nested optional field in struct" {
...@@ -710,7 +710,7 @@ test "nested optional field in struct" {...@@ -710,7 +710,7 @@ test "nested optional field in struct" {
710 var s = S1{710 var s = S1{
711 .x = S2{ .y = 127 },711 .x = S2{ .y = 127 },
712 };712 };
713 expect(s.x.?.y == 127);713 try expect(s.x.?.y == 127);
714}714}
715715
716fn maybe(x: bool) anyerror!?u32 {716fn maybe(x: bool) anyerror!?u32 {
...@@ -722,7 +722,7 @@ fn maybe(x: bool) anyerror!?u32 {...@@ -722,7 +722,7 @@ fn maybe(x: bool) anyerror!?u32 {
722722
723test "result location is optional inside error union" {723test "result location is optional inside error union" {
724 const x = maybe(true) catch unreachable;724 const x = maybe(true) catch unreachable;
725 expect(x.? == 42);725 try expect(x.? == 42);
726}726}
727727
728threadlocal var buffer: [11]u8 = undefined;728threadlocal var buffer: [11]u8 = undefined;
...@@ -730,7 +730,7 @@ threadlocal var buffer: [11]u8 = undefined;...@@ -730,7 +730,7 @@ threadlocal var buffer: [11]u8 = undefined;
730test "pointer to thread local array" {730test "pointer to thread local array" {
731 const s = "Hello world";731 const s = "Hello world";
732 std.mem.copy(u8, buffer[0..], s);732 std.mem.copy(u8, buffer[0..], s);
733 std.testing.expectEqualSlices(u8, buffer[0..], s);733 try std.testing.expectEqualSlices(u8, buffer[0..], s);
734}734}
735735
736test "auto created variables have correct alignment" {736test "auto created variables have correct alignment" {
...@@ -742,15 +742,15 @@ test "auto created variables have correct alignment" {...@@ -742,15 +742,15 @@ test "auto created variables have correct alignment" {
742 return 0;742 return 0;
743 }743 }
744 };744 };
745 expect(S.foo("\x7a\x7a\x7a\x7a") == 0x7a7a7a7a);745 try expect(S.foo("\x7a\x7a\x7a\x7a") == 0x7a7a7a7a);
746 comptime expect(S.foo("\x7a\x7a\x7a\x7a") == 0x7a7a7a7a);746 comptime try expect(S.foo("\x7a\x7a\x7a\x7a") == 0x7a7a7a7a);
747}747}
748748
749extern var opaque_extern_var: opaque {};749extern var opaque_extern_var: opaque {};
750var var_to_export: u32 = 42;750var var_to_export: u32 = 42;
751test "extern variable with non-pointer opaque type" {751test "extern variable with non-pointer opaque type" {
752 @export(var_to_export, .{ .name = "opaque_extern_var" });752 @export(var_to_export, .{ .name = "opaque_extern_var" });
753 expect(@ptrCast(*align(1) u32, &opaque_extern_var).* == 42);753 try expect(@ptrCast(*align(1) u32, &opaque_extern_var).* == 42);
754}754}
755755
756test "lazy typeInfo value as generic parameter" {756test "lazy typeInfo value as generic parameter" {
test/stage1/behavior/muladd.zig+7-7
...@@ -1,34 +1,34 @@...@@ -1,34 +1,34 @@
1const expect = @import("std").testing.expect;1const expect = @import("std").testing.expect;
22
3test "@mulAdd" {3test "@mulAdd" {
4 comptime testMulAdd();4 comptime try testMulAdd();
5 testMulAdd();5 try testMulAdd();
6}6}
77
8fn testMulAdd() void {8fn testMulAdd() !void {
9 {9 {
10 var a: f16 = 5.5;10 var a: f16 = 5.5;
11 var b: f16 = 2.5;11 var b: f16 = 2.5;
12 var c: f16 = 6.25;12 var c: f16 = 6.25;
13 expect(@mulAdd(f16, a, b, c) == 20);13 try expect(@mulAdd(f16, a, b, c) == 20);
14 }14 }
15 {15 {
16 var a: f32 = 5.5;16 var a: f32 = 5.5;
17 var b: f32 = 2.5;17 var b: f32 = 2.5;
18 var c: f32 = 6.25;18 var c: f32 = 6.25;
19 expect(@mulAdd(f32, a, b, c) == 20);19 try expect(@mulAdd(f32, a, b, c) == 20);
20 }20 }
21 {21 {
22 var a: f64 = 5.5;22 var a: f64 = 5.5;
23 var b: f64 = 2.5;23 var b: f64 = 2.5;
24 var c: f64 = 6.25;24 var c: f64 = 6.25;
25 expect(@mulAdd(f64, a, b, c) == 20);25 try expect(@mulAdd(f64, a, b, c) == 20);
26 }26 }
27 // Awaits implementation in libm.zig27 // Awaits implementation in libm.zig
28 //{28 //{
29 // var a: f16 = 5.5;29 // var a: f16 = 5.5;
30 // var b: f128 = 2.5;30 // var b: f128 = 2.5;
31 // var c: f128 = 6.25;31 // var c: f128 = 6.25;
32 // expect(@mulAdd(f128, a, b, c) == 20);32 //try expect(@mulAdd(f128, a, b, c) == 20);
33 //}33 //}
34}34}
test/stage1/behavior/namespace_depends_on_compile_var.zig+2-2
...@@ -3,9 +3,9 @@ const expect = std.testing.expect;...@@ -3,9 +3,9 @@ const expect = std.testing.expect;
33
4test "namespace depends on compile var" {4test "namespace depends on compile var" {
5 if (some_namespace.a_bool) {5 if (some_namespace.a_bool) {
6 expect(some_namespace.a_bool);6 try expect(some_namespace.a_bool);
7 } else {7 } else {
8 expect(!some_namespace.a_bool);8 try expect(!some_namespace.a_bool);
9 }9 }
10}10}
11const some_namespace = switch (std.builtin.os.tag) {11const some_namespace = switch (std.builtin.os.tag) {
test/stage1/behavior/null.zig+24-24
...@@ -17,13 +17,13 @@ test "optional type" {...@@ -17,13 +17,13 @@ test "optional type" {
1717
18 const z = next_x orelse 1234;18 const z = next_x orelse 1234;
1919
20 expect(z == 1234);20 try expect(z == 1234);
2121
22 const final_x: ?i32 = 13;22 const final_x: ?i32 = 13;
2323
24 const num = final_x orelse unreachable;24 const num = final_x orelse unreachable;
2525
26 expect(num == 13);26 try expect(num == 13);
27}27}
2828
29test "test maybe object and get a pointer to the inner value" {29test "test maybe object and get a pointer to the inner value" {
...@@ -33,7 +33,7 @@ test "test maybe object and get a pointer to the inner value" {...@@ -33,7 +33,7 @@ test "test maybe object and get a pointer to the inner value" {
33 b.* = false;33 b.* = false;
34 }34 }
3535
36 expect(maybe_bool.? == false);36 try expect(maybe_bool.? == false);
37}37}
3838
39test "rhs maybe unwrap return" {39test "rhs maybe unwrap return" {
...@@ -42,14 +42,14 @@ test "rhs maybe unwrap return" {...@@ -42,14 +42,14 @@ test "rhs maybe unwrap return" {
42}42}
4343
44test "maybe return" {44test "maybe return" {
45 maybeReturnImpl();45 try maybeReturnImpl();
46 comptime maybeReturnImpl();46 comptime try maybeReturnImpl();
47}47}
4848
49fn maybeReturnImpl() void {49fn maybeReturnImpl() !void {
50 expect(foo(1235).?);50 try expect(foo(1235).?);
51 if (foo(null) != null) unreachable;51 if (foo(null) != null) unreachable;
52 expect(!foo(1234).?);52 try expect(!foo(1234).?);
53}53}
5454
55fn foo(x: ?i32) ?bool {55fn foo(x: ?i32) ?bool {
...@@ -58,7 +58,7 @@ fn foo(x: ?i32) ?bool {...@@ -58,7 +58,7 @@ fn foo(x: ?i32) ?bool {
58}58}
5959
60test "if var maybe pointer" {60test "if var maybe pointer" {
61 expect(shouldBeAPlus1(Particle{61 try expect(shouldBeAPlus1(Particle{
62 .a = 14,62 .a = 14,
63 .b = 1,63 .b = 1,
64 .c = 1,64 .c = 1,
...@@ -84,10 +84,10 @@ const Particle = struct {...@@ -84,10 +84,10 @@ const Particle = struct {
8484
85test "null literal outside function" {85test "null literal outside function" {
86 const is_null = here_is_a_null_literal.context == null;86 const is_null = here_is_a_null_literal.context == null;
87 expect(is_null);87 try expect(is_null);
8888
89 const is_non_null = here_is_a_null_literal.context != null;89 const is_non_null = here_is_a_null_literal.context != null;
90 expect(!is_non_null);90 try expect(!is_non_null);
91}91}
92const SillyStruct = struct {92const SillyStruct = struct {
93 context: ?i32,93 context: ?i32,
...@@ -95,21 +95,21 @@ const SillyStruct = struct {...@@ -95,21 +95,21 @@ const SillyStruct = struct {
95const here_is_a_null_literal = SillyStruct{ .context = null };95const here_is_a_null_literal = SillyStruct{ .context = null };
9696
97test "test null runtime" {97test "test null runtime" {
98 testTestNullRuntime(null);98 try testTestNullRuntime(null);
99}99}
100fn testTestNullRuntime(x: ?i32) void {100fn testTestNullRuntime(x: ?i32) !void {
101 expect(x == null);101 try expect(x == null);
102 expect(!(x != null));102 try expect(!(x != null));
103}103}
104104
105test "optional void" {105test "optional void" {
106 optionalVoidImpl();106 try optionalVoidImpl();
107 comptime optionalVoidImpl();107 comptime try optionalVoidImpl();
108}108}
109109
110fn optionalVoidImpl() void {110fn optionalVoidImpl() !void {
111 expect(bar(null) == null);111 try expect(bar(null) == null);
112 expect(bar({}) != null);112 try expect(bar({}) != null);
113}113}
114114
115fn bar(x: ?void) ?void {115fn bar(x: ?void) ?void {
...@@ -133,7 +133,7 @@ test "unwrap optional which is field of global var" {...@@ -133,7 +133,7 @@ test "unwrap optional which is field of global var" {
133 }133 }
134 struct_with_optional.field = 1234;134 struct_with_optional.field = 1234;
135 if (struct_with_optional.field) |payload| {135 if (struct_with_optional.field) |payload| {
136 expect(payload == 1234);136 try expect(payload == 1234);
137 } else {137 } else {
138 unreachable;138 unreachable;
139 }139 }
...@@ -141,13 +141,13 @@ test "unwrap optional which is field of global var" {...@@ -141,13 +141,13 @@ test "unwrap optional which is field of global var" {
141141
142test "null with default unwrap" {142test "null with default unwrap" {
143 const x: i32 = null orelse 1;143 const x: i32 = null orelse 1;
144 expect(x == 1);144 try expect(x == 1);
145}145}
146146
147test "optional types" {147test "optional types" {
148 comptime {148 comptime {
149 const opt_type_struct = StructWithOptionalType{ .t = u8 };149 const opt_type_struct = StructWithOptionalType{ .t = u8 };
150 expect(opt_type_struct.t != null and opt_type_struct.t.? == u8);150 try expect(opt_type_struct.t != null and opt_type_struct.t.? == u8);
151 }151 }
152}152}
153153
...@@ -158,5 +158,5 @@ const StructWithOptionalType = struct {...@@ -158,5 +158,5 @@ const StructWithOptionalType = struct {
158test "optional pointer to 0 bit type null value at runtime" {158test "optional pointer to 0 bit type null value at runtime" {
159 const EmptyStruct = struct {};159 const EmptyStruct = struct {};
160 var x: ?*EmptyStruct = null;160 var x: ?*EmptyStruct = null;
161 expect(x == null);161 try expect(x == null);
162}162}
test/stage1/behavior/optional.zig+51-51
...@@ -8,28 +8,28 @@ pub const EmptyStruct = struct {};...@@ -8,28 +8,28 @@ pub const EmptyStruct = struct {};
8test "optional pointer to size zero struct" {8test "optional pointer to size zero struct" {
9 var e = EmptyStruct{};9 var e = EmptyStruct{};
10 var o: ?*EmptyStruct = &e;10 var o: ?*EmptyStruct = &e;
11 expect(o != null);11 try expect(o != null);
12}12}
1313
14test "equality compare nullable pointers" {14test "equality compare nullable pointers" {
15 testNullPtrsEql();15 try testNullPtrsEql();
16 comptime testNullPtrsEql();16 comptime try testNullPtrsEql();
17}17}
1818
19fn testNullPtrsEql() void {19fn testNullPtrsEql() !void {
20 var number: i32 = 1234;20 var number: i32 = 1234;
2121
22 var x: ?*i32 = null;22 var x: ?*i32 = null;
23 var y: ?*i32 = null;23 var y: ?*i32 = null;
24 expect(x == y);24 try expect(x == y);
25 y = &number;25 y = &number;
26 expect(x != y);26 try expect(x != y);
27 expect(x != &number);27 try expect(x != &number);
28 expect(&number != x);28 try expect(&number != x);
29 x = &number;29 x = &number;
30 expect(x == y);30 try expect(x == y);
31 expect(x == &number);31 try expect(x == &number);
32 expect(&number == x);32 try expect(&number == x);
33}33}
3434
35test "address of unwrap optional" {35test "address of unwrap optional" {
...@@ -46,23 +46,23 @@ test "address of unwrap optional" {...@@ -46,23 +46,23 @@ test "address of unwrap optional" {
46 };46 };
47 S.global = S.Foo{ .a = 1234 };47 S.global = S.Foo{ .a = 1234 };
48 const foo = S.getFoo() catch unreachable;48 const foo = S.getFoo() catch unreachable;
49 expect(foo.a == 1234);49 try expect(foo.a == 1234);
50}50}
5151
52test "equality compare optional with non-optional" {52test "equality compare optional with non-optional" {
53 test_cmp_optional_non_optional();53 try test_cmp_optional_non_optional();
54 comptime test_cmp_optional_non_optional();54 comptime try test_cmp_optional_non_optional();
55}55}
5656
57fn test_cmp_optional_non_optional() void {57fn test_cmp_optional_non_optional() !void {
58 var ten: i32 = 10;58 var ten: i32 = 10;
59 var opt_ten: ?i32 = 10;59 var opt_ten: ?i32 = 10;
60 var five: i32 = 5;60 var five: i32 = 5;
61 var int_n: ?i32 = null;61 var int_n: ?i32 = null;
6262
63 expect(int_n != ten);63 try expect(int_n != ten);
64 expect(opt_ten == ten);64 try expect(opt_ten == ten);
65 expect(opt_ten != five);65 try expect(opt_ten != five);
6666
67 // test evaluation is always lexical67 // test evaluation is always lexical
68 // ensure that the optional isn't always computed before the non-optional68 // ensure that the optional isn't always computed before the non-optional
...@@ -71,14 +71,14 @@ fn test_cmp_optional_non_optional() void {...@@ -71,14 +71,14 @@ fn test_cmp_optional_non_optional() void {
71 mutable_state += 1;71 mutable_state += 1;
72 break :blk1 @as(?f64, 10.0);72 break :blk1 @as(?f64, 10.0);
73 } != blk2: {73 } != blk2: {
74 expect(mutable_state == 1);74 try expect(mutable_state == 1);
75 break :blk2 @as(f64, 5.0);75 break :blk2 @as(f64, 5.0);
76 };76 };
77 _ = blk1: {77 _ = blk1: {
78 mutable_state += 1;78 mutable_state += 1;
79 break :blk1 @as(f64, 10.0);79 break :blk1 @as(f64, 10.0);
80 } != blk2: {80 } != blk2: {
81 expect(mutable_state == 2);81 try expect(mutable_state == 2);
82 break :blk2 @as(?f64, 5.0);82 break :blk2 @as(?f64, 5.0);
83 };83 };
84}84}
...@@ -94,15 +94,15 @@ test "passing an optional integer as a parameter" {...@@ -94,15 +94,15 @@ test "passing an optional integer as a parameter" {
94 return x.? == 1234;94 return x.? == 1234;
95 }95 }
96 };96 };
97 expect(S.entry());97 try expect(S.entry());
98 comptime expect(S.entry());98 comptime try expect(S.entry());
99}99}
100100
101test "unwrap function call with optional pointer return value" {101test "unwrap function call with optional pointer return value" {
102 const S = struct {102 const S = struct {
103 fn entry() void {103 fn entry() !void {
104 expect(foo().?.* == 1234);104 try expect(foo().?.* == 1234);
105 expect(bar() == null);105 try expect(bar() == null);
106 }106 }
107 const global: i32 = 1234;107 const global: i32 = 1234;
108 fn foo() ?*const i32 {108 fn foo() ?*const i32 {
...@@ -112,14 +112,14 @@ test "unwrap function call with optional pointer return value" {...@@ -112,14 +112,14 @@ test "unwrap function call with optional pointer return value" {
112 return null;112 return null;
113 }113 }
114 };114 };
115 S.entry();115 try S.entry();
116 comptime S.entry();116 comptime try S.entry();
117}117}
118118
119test "nested orelse" {119test "nested orelse" {
120 const S = struct {120 const S = struct {
121 fn entry() void {121 fn entry() !void {
122 expect(func() == null);122 try expect(func() == null);
123 }123 }
124 fn maybe() ?Foo {124 fn maybe() ?Foo {
125 return null;125 return null;
...@@ -134,8 +134,8 @@ test "nested orelse" {...@@ -134,8 +134,8 @@ test "nested orelse" {
134 field: i32,134 field: i32,
135 };135 };
136 };136 };
137 S.entry();137 try S.entry();
138 comptime S.entry();138 comptime try S.entry();
139}139}
140140
141test "self-referential struct through a slice of optional" {141test "self-referential struct through a slice of optional" {
...@@ -154,7 +154,7 @@ test "self-referential struct through a slice of optional" {...@@ -154,7 +154,7 @@ test "self-referential struct through a slice of optional" {
154 };154 };
155155
156 var n = S.Node.new();156 var n = S.Node.new();
157 expect(n.data == null);157 try expect(n.data == null);
158}158}
159159
160test "assigning to an unwrapped optional field in an inline loop" {160test "assigning to an unwrapped optional field in an inline loop" {
...@@ -173,14 +173,14 @@ test "coerce an anon struct literal to optional struct" {...@@ -173,14 +173,14 @@ test "coerce an anon struct literal to optional struct" {
173 const Struct = struct {173 const Struct = struct {
174 field: u32,174 field: u32,
175 };175 };
176 export fn doTheTest() void {176 fn doTheTest() !void {
177 var maybe_dims: ?Struct = null;177 var maybe_dims: ?Struct = null;
178 maybe_dims = .{ .field = 1 };178 maybe_dims = .{ .field = 1 };
179 expect(maybe_dims.?.field == 1);179 try expect(maybe_dims.?.field == 1);
180 }180 }
181 };181 };
182 S.doTheTest();182 try S.doTheTest();
183 comptime S.doTheTest();183 comptime try S.doTheTest();
184}184}
185185
186test "optional with void type" {186test "optional with void type" {
...@@ -188,15 +188,15 @@ test "optional with void type" {...@@ -188,15 +188,15 @@ test "optional with void type" {
188 x: ?void,188 x: ?void,
189 };189 };
190 var x = Foo{ .x = null };190 var x = Foo{ .x = null };
191 expect(x.x == null);191 try expect(x.x == null);
192}192}
193193
194test "0-bit child type coerced to optional return ptr result location" {194test "0-bit child type coerced to optional return ptr result location" {
195 const S = struct {195 const S = struct {
196 fn doTheTest() void {196 fn doTheTest() !void {
197 var y = Foo{};197 var y = Foo{};
198 var z = y.thing();198 var z = y.thing();
199 expect(z != null);199 try expect(z != null);
200 }200 }
201201
202 const Foo = struct {202 const Foo = struct {
...@@ -209,17 +209,17 @@ test "0-bit child type coerced to optional return ptr result location" {...@@ -209,17 +209,17 @@ test "0-bit child type coerced to optional return ptr result location" {
209 }209 }
210 };210 };
211 };211 };
212 S.doTheTest();212 try S.doTheTest();
213 comptime S.doTheTest();213 comptime try S.doTheTest();
214}214}
215215
216test "0-bit child type coerced to optional" {216test "0-bit child type coerced to optional" {
217 const S = struct {217 const S = struct {
218 fn doTheTest() void {218 fn doTheTest() !void {
219 var it: Foo = .{219 var it: Foo = .{
220 .list = undefined,220 .list = undefined,
221 };221 };
222 expect(it.foo() != null);222 try expect(it.foo() != null);
223 }223 }
224224
225 const Empty = struct {};225 const Empty = struct {};
...@@ -232,8 +232,8 @@ test "0-bit child type coerced to optional" {...@@ -232,8 +232,8 @@ test "0-bit child type coerced to optional" {
232 }232 }
233 };233 };
234 };234 };
235 S.doTheTest();235 try S.doTheTest();
236 comptime S.doTheTest();236 comptime try S.doTheTest();
237}237}
238238
239test "array of optional unaligned types" {239test "array of optional unaligned types" {
...@@ -255,15 +255,15 @@ test "array of optional unaligned types" {...@@ -255,15 +255,15 @@ test "array of optional unaligned types" {
255255
256 // The index must be a runtime value256 // The index must be a runtime value
257 var i: usize = 0;257 var i: usize = 0;
258 expectEqual(Enum.one, values[i].?.Num);258 try expectEqual(Enum.one, values[i].?.Num);
259 i += 1;259 i += 1;
260 expectEqual(Enum.two, values[i].?.Num);260 try expectEqual(Enum.two, values[i].?.Num);
261 i += 1;261 i += 1;
262 expectEqual(Enum.three, values[i].?.Num);262 try expectEqual(Enum.three, values[i].?.Num);
263 i += 1;263 i += 1;
264 expectEqual(Enum.one, values[i].?.Num);264 try expectEqual(Enum.one, values[i].?.Num);
265 i += 1;265 i += 1;
266 expectEqual(Enum.two, values[i].?.Num);266 try expectEqual(Enum.two, values[i].?.Num);
267 i += 1;267 i += 1;
268 expectEqual(Enum.three, values[i].?.Num);268 try expectEqual(Enum.three, values[i].?.Num);
269}269}
test/stage1/behavior/pointers.zig+108-108
...@@ -4,15 +4,15 @@ const expect = testing.expect;...@@ -4,15 +4,15 @@ const expect = testing.expect;
4const expectError = testing.expectError;4const expectError = testing.expectError;
55
6test "dereference pointer" {6test "dereference pointer" {
7 comptime testDerefPtr();7 comptime try testDerefPtr();
8 testDerefPtr();8 try testDerefPtr();
9}9}
1010
11fn testDerefPtr() void {11fn testDerefPtr() !void {
12 var x: i32 = 1234;12 var x: i32 = 1234;
13 var y = &x;13 var y = &x;
14 y.* += 1;14 y.* += 1;
15 expect(x == 1235);15 try expect(x == 1235);
16}16}
1717
18const Foo1 = struct {18const Foo1 = struct {
...@@ -20,41 +20,41 @@ const Foo1 = struct {...@@ -20,41 +20,41 @@ const Foo1 = struct {
20};20};
2121
22test "dereference pointer again" {22test "dereference pointer again" {
23 testDerefPtrOneVal();23 try testDerefPtrOneVal();
24 comptime testDerefPtrOneVal();24 comptime try testDerefPtrOneVal();
25}25}
2626
27fn testDerefPtrOneVal() void {27fn testDerefPtrOneVal() !void {
28 // Foo1 satisfies the OnePossibleValueYes criteria28 // Foo1 satisfies the OnePossibleValueYes criteria
29 const x = &Foo1{ .x = {} };29 const x = &Foo1{ .x = {} };
30 const y = x.*;30 const y = x.*;
31 expect(@TypeOf(y.x) == void);31 try expect(@TypeOf(y.x) == void);
32}32}
3333
34test "pointer arithmetic" {34test "pointer arithmetic" {
35 var ptr: [*]const u8 = "abcd";35 var ptr: [*]const u8 = "abcd";
3636
37 expect(ptr[0] == 'a');37 try expect(ptr[0] == 'a');
38 ptr += 1;38 ptr += 1;
39 expect(ptr[0] == 'b');39 try expect(ptr[0] == 'b');
40 ptr += 1;40 ptr += 1;
41 expect(ptr[0] == 'c');41 try expect(ptr[0] == 'c');
42 ptr += 1;42 ptr += 1;
43 expect(ptr[0] == 'd');43 try expect(ptr[0] == 'd');
44 ptr += 1;44 ptr += 1;
45 expect(ptr[0] == 0);45 try expect(ptr[0] == 0);
46 ptr -= 1;46 ptr -= 1;
47 expect(ptr[0] == 'd');47 try expect(ptr[0] == 'd');
48 ptr -= 1;48 ptr -= 1;
49 expect(ptr[0] == 'c');49 try expect(ptr[0] == 'c');
50 ptr -= 1;50 ptr -= 1;
51 expect(ptr[0] == 'b');51 try expect(ptr[0] == 'b');
52 ptr -= 1;52 ptr -= 1;
53 expect(ptr[0] == 'a');53 try expect(ptr[0] == 'a');
54}54}
5555
56test "double pointer parsing" {56test "double pointer parsing" {
57 comptime expect(PtrOf(PtrOf(i32)) == **i32);57 comptime try expect(PtrOf(PtrOf(i32)) == **i32);
58}58}
5959
60fn PtrOf(comptime T: type) type {60fn PtrOf(comptime T: type) type {
...@@ -72,33 +72,33 @@ test "implicit cast single item pointer to C pointer and back" {...@@ -72,33 +72,33 @@ test "implicit cast single item pointer to C pointer and back" {
72 var x: [*c]u8 = &y;72 var x: [*c]u8 = &y;
73 var z: *u8 = x;73 var z: *u8 = x;
74 z.* += 1;74 z.* += 1;
75 expect(y == 12);75 try expect(y == 12);
76}76}
7777
78test "C pointer comparison and arithmetic" {78test "C pointer comparison and arithmetic" {
79 const S = struct {79 const S = struct {
80 fn doTheTest() void {80 fn doTheTest() !void {
81 var one: usize = 1;81 var one: usize = 1;
82 var ptr1: [*c]u32 = 0;82 var ptr1: [*c]u32 = 0;
83 var ptr2 = ptr1 + 10;83 var ptr2 = ptr1 + 10;
84 expect(ptr1 == 0);84 try expect(ptr1 == 0);
85 expect(ptr1 >= 0);85 try expect(ptr1 >= 0);
86 expect(ptr1 <= 0);86 try expect(ptr1 <= 0);
87 // expect(ptr1 < 1);87 // expect(ptr1 < 1);
88 // expect(ptr1 < one);88 // expect(ptr1 < one);
89 // expect(1 > ptr1);89 // expect(1 > ptr1);
90 // expect(one > ptr1);90 // expect(one > ptr1);
91 expect(ptr1 < ptr2);91 try expect(ptr1 < ptr2);
92 expect(ptr2 > ptr1);92 try expect(ptr2 > ptr1);
93 expect(ptr2 >= 40);93 try expect(ptr2 >= 40);
94 expect(ptr2 == 40);94 try expect(ptr2 == 40);
95 expect(ptr2 <= 40);95 try expect(ptr2 <= 40);
96 ptr2 -= 10;96 ptr2 -= 10;
97 expect(ptr1 == ptr2);97 try expect(ptr1 == ptr2);
98 }98 }
99 };99 };
100 S.doTheTest();100 try S.doTheTest();
101 comptime S.doTheTest();101 comptime try S.doTheTest();
102}102}
103103
104test "peer type resolution with C pointers" {104test "peer type resolution with C pointers" {
...@@ -110,10 +110,10 @@ test "peer type resolution with C pointers" {...@@ -110,10 +110,10 @@ test "peer type resolution with C pointers" {
110 var x2 = if (t) ptr_many else ptr_c;110 var x2 = if (t) ptr_many else ptr_c;
111 var x3 = if (t) ptr_c else ptr_one;111 var x3 = if (t) ptr_c else ptr_one;
112 var x4 = if (t) ptr_c else ptr_many;112 var x4 = if (t) ptr_c else ptr_many;
113 expect(@TypeOf(x1) == [*c]u8);113 try expect(@TypeOf(x1) == [*c]u8);
114 expect(@TypeOf(x2) == [*c]u8);114 try expect(@TypeOf(x2) == [*c]u8);
115 expect(@TypeOf(x3) == [*c]u8);115 try expect(@TypeOf(x3) == [*c]u8);
116 expect(@TypeOf(x4) == [*c]u8);116 try expect(@TypeOf(x4) == [*c]u8);
117}117}
118118
119test "implicit casting between C pointer and optional non-C pointer" {119test "implicit casting between C pointer and optional non-C pointer" {
...@@ -121,15 +121,15 @@ test "implicit casting between C pointer and optional non-C pointer" {...@@ -121,15 +121,15 @@ test "implicit casting between C pointer and optional non-C pointer" {
121 const opt_many_ptr: ?[*]const u8 = slice.ptr;121 const opt_many_ptr: ?[*]const u8 = slice.ptr;
122 var ptr_opt_many_ptr = &opt_many_ptr;122 var ptr_opt_many_ptr = &opt_many_ptr;
123 var c_ptr: [*c]const [*c]const u8 = ptr_opt_many_ptr;123 var c_ptr: [*c]const [*c]const u8 = ptr_opt_many_ptr;
124 expect(c_ptr.*.* == 'a');124 try expect(c_ptr.*.* == 'a');
125 ptr_opt_many_ptr = c_ptr;125 ptr_opt_many_ptr = c_ptr;
126 expect(ptr_opt_many_ptr.*.?[1] == 'o');126 try expect(ptr_opt_many_ptr.*.?[1] == 'o');
127}127}
128128
129test "implicit cast error unions with non-optional to optional pointer" {129test "implicit cast error unions with non-optional to optional pointer" {
130 const S = struct {130 const S = struct {
131 fn doTheTest() void {131 fn doTheTest() !void {
132 expectError(error.Fail, foo());132 try expectError(error.Fail, foo());
133 }133 }
134 fn foo() anyerror!?*u8 {134 fn foo() anyerror!?*u8 {
135 return bar() orelse error.Fail;135 return bar() orelse error.Fail;
...@@ -138,111 +138,111 @@ test "implicit cast error unions with non-optional to optional pointer" {...@@ -138,111 +138,111 @@ test "implicit cast error unions with non-optional to optional pointer" {
138 return null;138 return null;
139 }139 }
140 };140 };
141 S.doTheTest();141 try S.doTheTest();
142 comptime S.doTheTest();142 comptime try S.doTheTest();
143}143}
144144
145test "initialize const optional C pointer to null" {145test "initialize const optional C pointer to null" {
146 const a: ?[*c]i32 = null;146 const a: ?[*c]i32 = null;
147 expect(a == null);147 try expect(a == null);
148 comptime expect(a == null);148 comptime try expect(a == null);
149}149}
150150
151test "compare equality of optional and non-optional pointer" {151test "compare equality of optional and non-optional pointer" {
152 const a = @intToPtr(*const usize, 0x12345678);152 const a = @intToPtr(*const usize, 0x12345678);
153 const b = @intToPtr(?*usize, 0x12345678);153 const b = @intToPtr(?*usize, 0x12345678);
154 expect(a == b);154 try expect(a == b);
155 expect(b == a);155 try expect(b == a);
156}156}
157157
158test "allowzero pointer and slice" {158test "allowzero pointer and slice" {
159 var ptr = @intToPtr([*]allowzero i32, 0);159 var ptr = @intToPtr([*]allowzero i32, 0);
160 var opt_ptr: ?[*]allowzero i32 = ptr;160 var opt_ptr: ?[*]allowzero i32 = ptr;
161 expect(opt_ptr != null);161 try expect(opt_ptr != null);
162 expect(@ptrToInt(ptr) == 0);162 try expect(@ptrToInt(ptr) == 0);
163 var runtime_zero: usize = 0;163 var runtime_zero: usize = 0;
164 var slice = ptr[runtime_zero..10];164 var slice = ptr[runtime_zero..10];
165 comptime expect(@TypeOf(slice) == []allowzero i32);165 comptime try expect(@TypeOf(slice) == []allowzero i32);
166 expect(@ptrToInt(&slice[5]) == 20);166 try expect(@ptrToInt(&slice[5]) == 20);
167167
168 comptime expect(@typeInfo(@TypeOf(ptr)).Pointer.is_allowzero);168 comptime try expect(@typeInfo(@TypeOf(ptr)).Pointer.is_allowzero);
169 comptime expect(@typeInfo(@TypeOf(slice)).Pointer.is_allowzero);169 comptime try expect(@typeInfo(@TypeOf(slice)).Pointer.is_allowzero);
170}170}
171171
172test "assign null directly to C pointer and test null equality" {172test "assign null directly to C pointer and test null equality" {
173 var x: [*c]i32 = null;173 var x: [*c]i32 = null;
174 expect(x == null);174 try expect(x == null);
175 expect(null == x);175 try expect(null == x);
176 expect(!(x != null));176 try expect(!(x != null));
177 expect(!(null != x));177 try expect(!(null != x));
178 if (x) |same_x| {178 if (x) |same_x| {
179 @panic("fail");179 @panic("fail");
180 }180 }
181 var otherx: i32 = undefined;181 var otherx: i32 = undefined;
182 expect((x orelse &otherx) == &otherx);182 try expect((x orelse &otherx) == &otherx);
183183
184 const y: [*c]i32 = null;184 const y: [*c]i32 = null;
185 comptime expect(y == null);185 comptime try expect(y == null);
186 comptime expect(null == y);186 comptime try expect(null == y);
187 comptime expect(!(y != null));187 comptime try expect(!(y != null));
188 comptime expect(!(null != y));188 comptime try expect(!(null != y));
189 if (y) |same_y| @panic("fail");189 if (y) |same_y| @panic("fail");
190 const othery: i32 = undefined;190 const othery: i32 = undefined;
191 comptime expect((y orelse &othery) == &othery);191 comptime try expect((y orelse &othery) == &othery);
192192
193 var n: i32 = 1234;193 var n: i32 = 1234;
194 var x1: [*c]i32 = &n;194 var x1: [*c]i32 = &n;
195 expect(!(x1 == null));195 try expect(!(x1 == null));
196 expect(!(null == x1));196 try expect(!(null == x1));
197 expect(x1 != null);197 try expect(x1 != null);
198 expect(null != x1);198 try expect(null != x1);
199 expect(x1.?.* == 1234);199 try expect(x1.?.* == 1234);
200 if (x1) |same_x1| {200 if (x1) |same_x1| {
201 expect(same_x1.* == 1234);201 try expect(same_x1.* == 1234);
202 } else {202 } else {
203 @panic("fail");203 @panic("fail");
204 }204 }
205 expect((x1 orelse &otherx) == x1);205 try expect((x1 orelse &otherx) == x1);
206206
207 const nc: i32 = 1234;207 const nc: i32 = 1234;
208 const y1: [*c]const i32 = &nc;208 const y1: [*c]const i32 = &nc;
209 comptime expect(!(y1 == null));209 comptime try expect(!(y1 == null));
210 comptime expect(!(null == y1));210 comptime try expect(!(null == y1));
211 comptime expect(y1 != null);211 comptime try expect(y1 != null);
212 comptime expect(null != y1);212 comptime try expect(null != y1);
213 comptime expect(y1.?.* == 1234);213 comptime try expect(y1.?.* == 1234);
214 if (y1) |same_y1| {214 if (y1) |same_y1| {
215 expect(same_y1.* == 1234);215 try expect(same_y1.* == 1234);
216 } else {216 } else {
217 @compileError("fail");217 @compileError("fail");
218 }218 }
219 comptime expect((y1 orelse &othery) == y1);219 comptime try expect((y1 orelse &othery) == y1);
220}220}
221221
222test "null terminated pointer" {222test "null terminated pointer" {
223 const S = struct {223 const S = struct {
224 fn doTheTest() void {224 fn doTheTest() !void {
225 var array_with_zero = [_:0]u8{ 'h', 'e', 'l', 'l', 'o' };225 var array_with_zero = [_:0]u8{ 'h', 'e', 'l', 'l', 'o' };
226 var zero_ptr: [*:0]const u8 = @ptrCast([*:0]const u8, &array_with_zero);226 var zero_ptr: [*:0]const u8 = @ptrCast([*:0]const u8, &array_with_zero);
227 var no_zero_ptr: [*]const u8 = zero_ptr;227 var no_zero_ptr: [*]const u8 = zero_ptr;
228 var zero_ptr_again = @ptrCast([*:0]const u8, no_zero_ptr);228 var zero_ptr_again = @ptrCast([*:0]const u8, no_zero_ptr);
229 expect(std.mem.eql(u8, std.mem.spanZ(zero_ptr_again), "hello"));229 try expect(std.mem.eql(u8, std.mem.spanZ(zero_ptr_again), "hello"));
230 }230 }
231 };231 };
232 S.doTheTest();232 try S.doTheTest();
233 comptime S.doTheTest();233 comptime try S.doTheTest();
234}234}
235235
236test "allow any sentinel" {236test "allow any sentinel" {
237 const S = struct {237 const S = struct {
238 fn doTheTest() void {238 fn doTheTest() !void {
239 var array = [_:std.math.minInt(i32)]i32{ 1, 2, 3, 4 };239 var array = [_:std.math.minInt(i32)]i32{ 1, 2, 3, 4 };
240 var ptr: [*:std.math.minInt(i32)]i32 = &array;240 var ptr: [*:std.math.minInt(i32)]i32 = &array;
241 expect(ptr[4] == std.math.minInt(i32));241 try expect(ptr[4] == std.math.minInt(i32));
242 }242 }
243 };243 };
244 S.doTheTest();244 try S.doTheTest();
245 comptime S.doTheTest();245 comptime try S.doTheTest();
246}246}
247247
248test "pointer sentinel with enums" {248test "pointer sentinel with enums" {
...@@ -253,42 +253,42 @@ test "pointer sentinel with enums" {...@@ -253,42 +253,42 @@ test "pointer sentinel with enums" {
253 sentinel,253 sentinel,
254 };254 };
255255
256 fn doTheTest() void {256 fn doTheTest() !void {
257 var ptr: [*:.sentinel]const Number = &[_:.sentinel]Number{ .one, .two, .two, .one };257 var ptr: [*:.sentinel]const Number = &[_:.sentinel]Number{ .one, .two, .two, .one };
258 expect(ptr[4] == .sentinel); // TODO this should be comptime expect, see #3731258 try expect(ptr[4] == .sentinel); // TODO this should be comptime try expect, see #3731
259 }259 }
260 };260 };
261 S.doTheTest();261 try S.doTheTest();
262 comptime S.doTheTest();262 comptime try S.doTheTest();
263}263}
264264
265test "pointer sentinel with optional element" {265test "pointer sentinel with optional element" {
266 const S = struct {266 const S = struct {
267 fn doTheTest() void {267 fn doTheTest() !void {
268 var ptr: [*:null]const ?i32 = &[_:null]?i32{ 1, 2, 3, 4 };268 var ptr: [*:null]const ?i32 = &[_:null]?i32{ 1, 2, 3, 4 };
269 expect(ptr[4] == null); // TODO this should be comptime expect, see #3731269 try expect(ptr[4] == null); // TODO this should be comptime try expect, see #3731
270 }270 }
271 };271 };
272 S.doTheTest();272 try S.doTheTest();
273 comptime S.doTheTest();273 comptime try S.doTheTest();
274}274}
275275
276test "pointer sentinel with +inf" {276test "pointer sentinel with +inf" {
277 const S = struct {277 const S = struct {
278 fn doTheTest() void {278 fn doTheTest() !void {
279 const inf = std.math.inf_f32;279 const inf = std.math.inf_f32;
280 var ptr: [*:inf]const f32 = &[_:inf]f32{ 1.1, 2.2, 3.3, 4.4 };280 var ptr: [*:inf]const f32 = &[_:inf]f32{ 1.1, 2.2, 3.3, 4.4 };
281 expect(ptr[4] == inf); // TODO this should be comptime expect, see #3731281 try expect(ptr[4] == inf); // TODO this should be comptime try expect, see #3731
282 }282 }
283 };283 };
284 S.doTheTest();284 try S.doTheTest();
285 comptime S.doTheTest();285 comptime try S.doTheTest();
286}286}
287287
288test "pointer to array at fixed address" {288test "pointer to array at fixed address" {
289 const array = @intToPtr(*volatile [1]u32, 0x10);289 const array = @intToPtr(*volatile [1]u32, 0x10);
290 // Silly check just to reference `array`290 // Silly check just to reference `array`
291 expect(@ptrToInt(&array[0]) == 0x10);291 try expect(@ptrToInt(&array[0]) == 0x10);
292}292}
293293
294test "pointer arithmetic affects the alignment" {294test "pointer arithmetic affects the alignment" {
...@@ -296,28 +296,28 @@ test "pointer arithmetic affects the alignment" {...@@ -296,28 +296,28 @@ test "pointer arithmetic affects the alignment" {
296 var ptr: [*]align(8) u32 = undefined;296 var ptr: [*]align(8) u32 = undefined;
297 var x: usize = 1;297 var x: usize = 1;
298298
299 expect(@typeInfo(@TypeOf(ptr)).Pointer.alignment == 8);299 try expect(@typeInfo(@TypeOf(ptr)).Pointer.alignment == 8);
300 const ptr1 = ptr + 1; // 1 * 4 = 4 -> lcd(4,8) = 4300 const ptr1 = ptr + 1; // 1 * 4 = 4 -> lcd(4,8) = 4
301 expect(@typeInfo(@TypeOf(ptr1)).Pointer.alignment == 4);301 try expect(@typeInfo(@TypeOf(ptr1)).Pointer.alignment == 4);
302 const ptr2 = ptr + 4; // 4 * 4 = 16 -> lcd(16,8) = 8302 const ptr2 = ptr + 4; // 4 * 4 = 16 -> lcd(16,8) = 8
303 expect(@typeInfo(@TypeOf(ptr2)).Pointer.alignment == 8);303 try expect(@typeInfo(@TypeOf(ptr2)).Pointer.alignment == 8);
304 const ptr3 = ptr + 0; // no-op304 const ptr3 = ptr + 0; // no-op
305 expect(@typeInfo(@TypeOf(ptr3)).Pointer.alignment == 8);305 try expect(@typeInfo(@TypeOf(ptr3)).Pointer.alignment == 8);
306 const ptr4 = ptr + x; // runtime-known addend306 const ptr4 = ptr + x; // runtime-known addend
307 expect(@typeInfo(@TypeOf(ptr4)).Pointer.alignment == 4);307 try expect(@typeInfo(@TypeOf(ptr4)).Pointer.alignment == 4);
308 }308 }
309 {309 {
310 var ptr: [*]align(8) [3]u8 = undefined;310 var ptr: [*]align(8) [3]u8 = undefined;
311 var x: usize = 1;311 var x: usize = 1;
312312
313 const ptr1 = ptr + 17; // 3 * 17 = 51313 const ptr1 = ptr + 17; // 3 * 17 = 51
314 expect(@typeInfo(@TypeOf(ptr1)).Pointer.alignment == 1);314 try expect(@typeInfo(@TypeOf(ptr1)).Pointer.alignment == 1);
315 const ptr2 = ptr + x; // runtime-known addend315 const ptr2 = ptr + x; // runtime-known addend
316 expect(@typeInfo(@TypeOf(ptr2)).Pointer.alignment == 1);316 try expect(@typeInfo(@TypeOf(ptr2)).Pointer.alignment == 1);
317 const ptr3 = ptr + 8; // 3 * 8 = 24 -> lcd(8,24) = 8317 const ptr3 = ptr + 8; // 3 * 8 = 24 -> lcd(8,24) = 8
318 expect(@typeInfo(@TypeOf(ptr3)).Pointer.alignment == 8);318 try expect(@typeInfo(@TypeOf(ptr3)).Pointer.alignment == 8);
319 const ptr4 = ptr + 4; // 3 * 4 = 12 -> lcd(8,12) = 4319 const ptr4 = ptr + 4; // 3 * 4 = 12 -> lcd(8,12) = 4
320 expect(@typeInfo(@TypeOf(ptr4)).Pointer.alignment == 4);320 try expect(@typeInfo(@TypeOf(ptr4)).Pointer.alignment == 4);
321 }321 }
322}322}
323323
...@@ -325,15 +325,15 @@ test "@ptrToInt on null optional at comptime" {...@@ -325,15 +325,15 @@ test "@ptrToInt on null optional at comptime" {
325 {325 {
326 const pointer = @intToPtr(?*u8, 0x000);326 const pointer = @intToPtr(?*u8, 0x000);
327 const x = @ptrToInt(pointer);327 const x = @ptrToInt(pointer);
328 comptime expect(0 == @ptrToInt(pointer));328 comptime try expect(0 == @ptrToInt(pointer));
329 }329 }
330 {330 {
331 const pointer = @intToPtr(?*u8, 0xf00);331 const pointer = @intToPtr(?*u8, 0xf00);
332 comptime expect(0xf00 == @ptrToInt(pointer));332 comptime try expect(0xf00 == @ptrToInt(pointer));
333 }333 }
334}334}
335335
336test "indexing array with sentinel returns correct type" {336test "indexing array with sentinel returns correct type" {
337 var s: [:0]const u8 = "abc";337 var s: [:0]const u8 = "abc";
338 testing.expectEqualSlices(u8, "*const u8", @typeName(@TypeOf(&s[0])));338 try testing.expectEqualSlices(u8, "*const u8", @typeName(@TypeOf(&s[0])));
339}339}
test/stage1/behavior/popcount.zig+12-12
...@@ -1,43 +1,43 @@...@@ -1,43 +1,43 @@
1const expect = @import("std").testing.expect;1const expect = @import("std").testing.expect;
22
3test "@popCount" {3test "@popCount" {
4 comptime testPopCount();4 comptime try testPopCount();
5 testPopCount();5 try testPopCount();
6}6}
77
8fn testPopCount() void {8fn testPopCount() !void {
9 {9 {
10 var x: u32 = 0xffffffff;10 var x: u32 = 0xffffffff;
11 expect(@popCount(u32, x) == 32);11 try expect(@popCount(u32, x) == 32);
12 }12 }
13 {13 {
14 var x: u5 = 0x1f;14 var x: u5 = 0x1f;
15 expect(@popCount(u5, x) == 5);15 try expect(@popCount(u5, x) == 5);
16 }16 }
17 {17 {
18 var x: u32 = 0xaa;18 var x: u32 = 0xaa;
19 expect(@popCount(u32, x) == 4);19 try expect(@popCount(u32, x) == 4);
20 }20 }
21 {21 {
22 var x: u32 = 0xaaaaaaaa;22 var x: u32 = 0xaaaaaaaa;
23 expect(@popCount(u32, x) == 16);23 try expect(@popCount(u32, x) == 16);
24 }24 }
25 {25 {
26 var x: u32 = 0xaaaaaaaa;26 var x: u32 = 0xaaaaaaaa;
27 expect(@popCount(u32, x) == 16);27 try expect(@popCount(u32, x) == 16);
28 }28 }
29 {29 {
30 var x: i16 = -1;30 var x: i16 = -1;
31 expect(@popCount(i16, x) == 16);31 try expect(@popCount(i16, x) == 16);
32 }32 }
33 {33 {
34 var x: i8 = -120;34 var x: i8 = -120;
35 expect(@popCount(i8, x) == 2);35 try expect(@popCount(i8, x) == 2);
36 }36 }
37 comptime {37 comptime {
38 expect(@popCount(u8, @bitCast(u8, @as(i8, -120))) == 2);38 try expect(@popCount(u8, @bitCast(u8, @as(i8, -120))) == 2);
39 }39 }
40 comptime {40 comptime {
41 expect(@popCount(i128, 0b11111111000110001100010000100001000011000011100101010001) == 24);41 try expect(@popCount(i128, 0b11111111000110001100010000100001000011000011100101010001) == 24);
42 }42 }
43}43}
test/stage1/behavior/ptrcast.zig+12-12
...@@ -3,25 +3,25 @@ const builtin = std.builtin;...@@ -3,25 +3,25 @@ const builtin = std.builtin;
3const expect = std.testing.expect;3const expect = std.testing.expect;
44
5test "reinterpret bytes as integer with nonzero offset" {5test "reinterpret bytes as integer with nonzero offset" {
6 testReinterpretBytesAsInteger();6 try testReinterpretBytesAsInteger();
7 comptime testReinterpretBytesAsInteger();7 comptime try testReinterpretBytesAsInteger();
8}8}
99
10fn testReinterpretBytesAsInteger() void {10fn testReinterpretBytesAsInteger() !void {
11 const bytes = "\x12\x34\x56\x78\xab";11 const bytes = "\x12\x34\x56\x78\xab";
12 const expected = switch (builtin.endian) {12 const expected = switch (builtin.endian) {
13 builtin.Endian.Little => 0xab785634,13 builtin.Endian.Little => 0xab785634,
14 builtin.Endian.Big => 0x345678ab,14 builtin.Endian.Big => 0x345678ab,
15 };15 };
16 expect(@ptrCast(*align(1) const u32, bytes[1..5]).* == expected);16 try expect(@ptrCast(*align(1) const u32, bytes[1..5]).* == expected);
17}17}
1818
19test "reinterpret bytes of an array into an extern struct" {19test "reinterpret bytes of an array into an extern struct" {
20 testReinterpretBytesAsExternStruct();20 try testReinterpretBytesAsExternStruct();
21 comptime testReinterpretBytesAsExternStruct();21 comptime try testReinterpretBytesAsExternStruct();
22}22}
2323
24fn testReinterpretBytesAsExternStruct() void {24fn testReinterpretBytesAsExternStruct() !void {
25 var bytes align(2) = [_]u8{ 1, 2, 3, 4, 5, 6 };25 var bytes align(2) = [_]u8{ 1, 2, 3, 4, 5, 6 };
2626
27 const S = extern struct {27 const S = extern struct {
...@@ -32,15 +32,15 @@ fn testReinterpretBytesAsExternStruct() void {...@@ -32,15 +32,15 @@ fn testReinterpretBytesAsExternStruct() void {
3232
33 var ptr = @ptrCast(*const S, &bytes);33 var ptr = @ptrCast(*const S, &bytes);
34 var val = ptr.c;34 var val = ptr.c;
35 expect(val == 5);35 try expect(val == 5);
36}36}
3737
38test "reinterpret struct field at comptime" {38test "reinterpret struct field at comptime" {
39 const numNative = comptime Bytes.init(0x12345678);39 const numNative = comptime Bytes.init(0x12345678);
40 if (builtin.endian != .Little) {40 if (builtin.endian != .Little) {
41 expect(std.mem.eql(u8, &[_]u8{ 0x12, 0x34, 0x56, 0x78 }, &numNative.bytes));41 try expect(std.mem.eql(u8, &[_]u8{ 0x12, 0x34, 0x56, 0x78 }, &numNative.bytes));
42 } else {42 } else {
43 expect(std.mem.eql(u8, &[_]u8{ 0x78, 0x56, 0x34, 0x12 }, &numNative.bytes));43 try expect(std.mem.eql(u8, &[_]u8{ 0x78, 0x56, 0x34, 0x12 }, &numNative.bytes));
44 }44 }
45}45}
4646
...@@ -59,7 +59,7 @@ test "comptime ptrcast keeps larger alignment" {...@@ -59,7 +59,7 @@ test "comptime ptrcast keeps larger alignment" {
59 comptime {59 comptime {
60 const a: u32 = 1234;60 const a: u32 = 1234;
61 const p = @ptrCast([*]const u8, &a);61 const p = @ptrCast([*]const u8, &a);
62 std.debug.assert(@TypeOf(p) == [*]align(@alignOf(u32)) const u8);62 try expect(@TypeOf(p) == [*]align(@alignOf(u32)) const u8);
63 }63 }
64}64}
6565
...@@ -68,5 +68,5 @@ test "implicit optional pointer to optional c_void pointer" {...@@ -68,5 +68,5 @@ test "implicit optional pointer to optional c_void pointer" {
68 var x: ?[*]u8 = &buf;68 var x: ?[*]u8 = &buf;
69 var y: ?*c_void = x;69 var y: ?*c_void = x;
70 var z = @ptrCast(*[4]u8, y);70 var z = @ptrCast(*[4]u8, y);
71 expect(std.mem.eql(u8, z, "aoeu"));71 try expect(std.mem.eql(u8, z, "aoeu"));
72}72}
test/stage1/behavior/pub_enum.zig+4-4
...@@ -2,12 +2,12 @@ const other = @import("pub_enum/other.zig");...@@ -2,12 +2,12 @@ const other = @import("pub_enum/other.zig");
2const expect = @import("std").testing.expect;2const expect = @import("std").testing.expect;
33
4test "pub enum" {4test "pub enum" {
5 pubEnumTest(other.APubEnum.Two);5 try pubEnumTest(other.APubEnum.Two);
6}6}
7fn pubEnumTest(foo: other.APubEnum) void {7fn pubEnumTest(foo: other.APubEnum) !void {
8 expect(foo == other.APubEnum.Two);8 try expect(foo == other.APubEnum.Two);
9}9}
1010
11test "cast with imported symbol" {11test "cast with imported symbol" {
12 expect(@as(other.size_t, 42) == 42);12 try expect(@as(other.size_t, 42) == 42);
13}13}
test/stage1/behavior/ref_var_in_if_after_if_2nd_switch_prong.zig+10-10
...@@ -3,12 +3,12 @@ const mem = @import("std").mem;...@@ -3,12 +3,12 @@ const mem = @import("std").mem;
33
4var ok: bool = false;4var ok: bool = false;
5test "reference a variable in an if after an if in the 2nd switch prong" {5test "reference a variable in an if after an if in the 2nd switch prong" {
6 foo(true, Num.Two, false, "aoeu");6 try foo(true, Num.Two, false, "aoeu");
7 expect(!ok);7 try expect(!ok);
8 foo(false, Num.One, false, "aoeu");8 try foo(false, Num.One, false, "aoeu");
9 expect(!ok);9 try expect(!ok);
10 foo(true, Num.One, false, "aoeu");10 try foo(true, Num.One, false, "aoeu");
11 expect(ok);11 try expect(ok);
12}12}
1313
14const Num = enum {14const Num = enum {
...@@ -16,7 +16,7 @@ const Num = enum {...@@ -16,7 +16,7 @@ const Num = enum {
16 Two,16 Two,
17};17};
1818
19fn foo(c: bool, k: Num, c2: bool, b: []const u8) void {19fn foo(c: bool, k: Num, c2: bool, b: []const u8) !void {
20 switch (k) {20 switch (k) {
21 Num.Two => {},21 Num.Two => {},
22 Num.One => {22 Num.One => {
...@@ -25,13 +25,13 @@ fn foo(c: bool, k: Num, c2: bool, b: []const u8) void {...@@ -25,13 +25,13 @@ fn foo(c: bool, k: Num, c2: bool, b: []const u8) void {
2525
26 if (c2) {}26 if (c2) {}
2727
28 a(output_path);28 try a(output_path);
29 }29 }
30 },30 },
31 }31 }
32}32}
3333
34fn a(x: []const u8) void {34fn a(x: []const u8) !void {
35 expect(mem.eql(u8, x, "aoeu"));35 try expect(mem.eql(u8, x, "aoeu"));
36 ok = true;36 ok = true;
37}37}
test/stage1/behavior/reflection.zig+17-17
...@@ -5,12 +5,12 @@ const reflection = @This();...@@ -5,12 +5,12 @@ const reflection = @This();
5test "reflection: function return type, var args, and param types" {5test "reflection: function return type, var args, and param types" {
6 comptime {6 comptime {
7 const info = @typeInfo(@TypeOf(dummy)).Fn;7 const info = @typeInfo(@TypeOf(dummy)).Fn;
8 expect(info.return_type.? == i32);8 try expect(info.return_type.? == i32);
9 expect(!info.is_var_args);9 try expect(!info.is_var_args);
10 expect(info.args.len == 3);10 try expect(info.args.len == 3);
11 expect(info.args[0].arg_type.? == bool);11 try expect(info.args[0].arg_type.? == bool);
12 expect(info.args[1].arg_type.? == i32);12 try expect(info.args[1].arg_type.? == i32);
13 expect(info.args[2].arg_type.? == f32);13 try expect(info.args[2].arg_type.? == f32);
14 }14 }
15}15}
1616
...@@ -25,18 +25,18 @@ test "reflection: @field" {...@@ -25,18 +25,18 @@ test "reflection: @field" {
25 .three = void{},25 .three = void{},
26 };26 };
2727
28 expect(f.one == f.one);28 try expect(f.one == f.one);
29 expect(@field(f, "o" ++ "ne") == f.one);29 try expect(@field(f, "o" ++ "ne") == f.one);
30 expect(@field(f, "t" ++ "wo") == f.two);30 try expect(@field(f, "t" ++ "wo") == f.two);
31 expect(@field(f, "th" ++ "ree") == f.three);31 try expect(@field(f, "th" ++ "ree") == f.three);
32 expect(@field(Foo, "const" ++ "ant") == Foo.constant);32 try expect(@field(Foo, "const" ++ "ant") == Foo.constant);
33 expect(@field(Bar, "O" ++ "ne") == Bar.One);33 try expect(@field(Bar, "O" ++ "ne") == Bar.One);
34 expect(@field(Bar, "T" ++ "wo") == Bar.Two);34 try expect(@field(Bar, "T" ++ "wo") == Bar.Two);
35 expect(@field(Bar, "Th" ++ "ree") == Bar.Three);35 try expect(@field(Bar, "Th" ++ "ree") == Bar.Three);
36 expect(@field(Bar, "F" ++ "our") == Bar.Four);36 try expect(@field(Bar, "F" ++ "our") == Bar.Four);
37 expect(@field(reflection, "dum" ++ "my")(true, 1, 2) == dummy(true, 1, 2));37 try expect(@field(reflection, "dum" ++ "my")(true, 1, 2) == dummy(true, 1, 2));
38 @field(f, "o" ++ "ne") = 4;38 @field(f, "o" ++ "ne") = 4;
39 expect(f.one == 4);39 try expect(f.one == 4);
40}40}
4141
42const Foo = struct {42const Foo = struct {
test/stage1/behavior/shuffle.zig+10-10
...@@ -9,33 +9,33 @@ test "@shuffle" {...@@ -9,33 +9,33 @@ test "@shuffle" {
9 if (builtin.os.tag == .wasi) return error.SkipZigTest;9 if (builtin.os.tag == .wasi) return error.SkipZigTest;
1010
11 const S = struct {11 const S = struct {
12 fn doTheTest() void {12 fn doTheTest() !void {
13 var v: Vector(4, i32) = [4]i32{ 2147483647, -2, 30, 40 };13 var v: Vector(4, i32) = [4]i32{ 2147483647, -2, 30, 40 };
14 var x: Vector(4, i32) = [4]i32{ 1, 2147483647, 3, 4 };14 var x: Vector(4, i32) = [4]i32{ 1, 2147483647, 3, 4 };
15 const mask: Vector(4, i32) = [4]i32{ 0, ~@as(i32, 2), 3, ~@as(i32, 3) };15 const mask: Vector(4, i32) = [4]i32{ 0, ~@as(i32, 2), 3, ~@as(i32, 3) };
16 var res = @shuffle(i32, v, x, mask);16 var res = @shuffle(i32, v, x, mask);
17 expect(mem.eql(i32, &@as([4]i32, res), &[4]i32{ 2147483647, 3, 40, 4 }));17 try expect(mem.eql(i32, &@as([4]i32, res), &[4]i32{ 2147483647, 3, 40, 4 }));
1818
19 // Implicit cast from array (of mask)19 // Implicit cast from array (of mask)
20 res = @shuffle(i32, v, x, [4]i32{ 0, ~@as(i32, 2), 3, ~@as(i32, 3) });20 res = @shuffle(i32, v, x, [4]i32{ 0, ~@as(i32, 2), 3, ~@as(i32, 3) });
21 expect(mem.eql(i32, &@as([4]i32, res), &[4]i32{ 2147483647, 3, 40, 4 }));21 try expect(mem.eql(i32, &@as([4]i32, res), &[4]i32{ 2147483647, 3, 40, 4 }));
2222
23 // Undefined23 // Undefined
24 const mask2: Vector(4, i32) = [4]i32{ 3, 1, 2, 0 };24 const mask2: Vector(4, i32) = [4]i32{ 3, 1, 2, 0 };
25 res = @shuffle(i32, v, undefined, mask2);25 res = @shuffle(i32, v, undefined, mask2);
26 expect(mem.eql(i32, &@as([4]i32, res), &[4]i32{ 40, -2, 30, 2147483647 }));26 try expect(mem.eql(i32, &@as([4]i32, res), &[4]i32{ 40, -2, 30, 2147483647 }));
2727
28 // Upcasting of b28 // Upcasting of b
29 var v2: Vector(2, i32) = [2]i32{ 2147483647, undefined };29 var v2: Vector(2, i32) = [2]i32{ 2147483647, undefined };
30 const mask3: Vector(4, i32) = [4]i32{ ~@as(i32, 0), 2, ~@as(i32, 0), 3 };30 const mask3: Vector(4, i32) = [4]i32{ ~@as(i32, 0), 2, ~@as(i32, 0), 3 };
31 res = @shuffle(i32, x, v2, mask3);31 res = @shuffle(i32, x, v2, mask3);
32 expect(mem.eql(i32, &@as([4]i32, res), &[4]i32{ 2147483647, 3, 2147483647, 4 }));32 try expect(mem.eql(i32, &@as([4]i32, res), &[4]i32{ 2147483647, 3, 2147483647, 4 }));
3333
34 // Upcasting of a34 // Upcasting of a
35 var v3: Vector(2, i32) = [2]i32{ 2147483647, -2 };35 var v3: Vector(2, i32) = [2]i32{ 2147483647, -2 };
36 const mask4: Vector(4, i32) = [4]i32{ 0, ~@as(i32, 2), 1, ~@as(i32, 3) };36 const mask4: Vector(4, i32) = [4]i32{ 0, ~@as(i32, 2), 1, ~@as(i32, 3) };
37 res = @shuffle(i32, v3, x, mask4);37 res = @shuffle(i32, v3, x, mask4);
38 expect(mem.eql(i32, &@as([4]i32, res), &[4]i32{ 2147483647, 3, -2, 4 }));38 try expect(mem.eql(i32, &@as([4]i32, res), &[4]i32{ 2147483647, 3, -2, 4 }));
3939
40 // bool40 // bool
41 // Disabled because of #331741 // Disabled because of #3317
...@@ -44,7 +44,7 @@ test "@shuffle" {...@@ -44,7 +44,7 @@ test "@shuffle" {
44 var v4: Vector(2, bool) = [2]bool{ true, false };44 var v4: Vector(2, bool) = [2]bool{ true, false };
45 const mask5: Vector(4, i32) = [4]i32{ 0, ~@as(i32, 1), 1, 2 };45 const mask5: Vector(4, i32) = [4]i32{ 0, ~@as(i32, 1), 1, 2 };
46 var res2 = @shuffle(bool, x2, v4, mask5);46 var res2 = @shuffle(bool, x2, v4, mask5);
47 expect(mem.eql(bool, &@as([4]bool, res2), &[4]bool{ false, false, true, false }));47 try expect(mem.eql(bool, &@as([4]bool, res2), &[4]bool{ false, false, true, false }));
48 }48 }
4949
50 // TODO re-enable when LLVM codegen is fixed50 // TODO re-enable when LLVM codegen is fixed
...@@ -54,10 +54,10 @@ test "@shuffle" {...@@ -54,10 +54,10 @@ test "@shuffle" {
54 var v4: Vector(2, bool) = [2]bool{ true, false };54 var v4: Vector(2, bool) = [2]bool{ true, false };
55 const mask5: Vector(4, i32) = [4]i32{ 0, ~@as(i32, 1), 1, 2 };55 const mask5: Vector(4, i32) = [4]i32{ 0, ~@as(i32, 1), 1, 2 };
56 var res2 = @shuffle(bool, x2, v4, mask5);56 var res2 = @shuffle(bool, x2, v4, mask5);
57 expect(mem.eql(bool, &@as([4]bool, res2), &[4]bool{ false, false, true, false }));57 try expect(mem.eql(bool, &@as([4]bool, res2), &[4]bool{ false, false, true, false }));
58 }58 }
59 }59 }
60 };60 };
61 S.doTheTest();61 try S.doTheTest();
62 comptime S.doTheTest();62 comptime try S.doTheTest();
63}63}
test/stage1/behavior/sizeof_and_typeof.zig+75-75
...@@ -5,7 +5,7 @@ const expectEqual = std.testing.expectEqual;...@@ -5,7 +5,7 @@ const expectEqual = std.testing.expectEqual;
55
6test "@sizeOf and @TypeOf" {6test "@sizeOf and @TypeOf" {
7 const y: @TypeOf(x) = 120;7 const y: @TypeOf(x) = 120;
8 expect(@sizeOf(@TypeOf(y)) == 2);8 try expect(@sizeOf(@TypeOf(y)) == 2);
9}9}
10const x: u16 = 13;10const x: u16 = 13;
11const z: @TypeOf(x) = 19;11const z: @TypeOf(x) = 19;
...@@ -36,27 +36,27 @@ const P = packed struct {...@@ -36,27 +36,27 @@ const P = packed struct {
3636
37test "@byteOffsetOf" {37test "@byteOffsetOf" {
38 // Packed structs have fixed memory layout38 // Packed structs have fixed memory layout
39 expect(@byteOffsetOf(P, "a") == 0);39 try expect(@byteOffsetOf(P, "a") == 0);
40 expect(@byteOffsetOf(P, "b") == 1);40 try expect(@byteOffsetOf(P, "b") == 1);
41 expect(@byteOffsetOf(P, "c") == 5);41 try expect(@byteOffsetOf(P, "c") == 5);
42 expect(@byteOffsetOf(P, "d") == 6);42 try expect(@byteOffsetOf(P, "d") == 6);
43 expect(@byteOffsetOf(P, "e") == 6);43 try expect(@byteOffsetOf(P, "e") == 6);
44 expect(@byteOffsetOf(P, "f") == 7);44 try expect(@byteOffsetOf(P, "f") == 7);
45 expect(@byteOffsetOf(P, "g") == 9);45 try expect(@byteOffsetOf(P, "g") == 9);
46 expect(@byteOffsetOf(P, "h") == 11);46 try expect(@byteOffsetOf(P, "h") == 11);
47 expect(@byteOffsetOf(P, "i") == 12);47 try expect(@byteOffsetOf(P, "i") == 12);
4848
49 // Normal struct fields can be moved/padded49 // Normal struct fields can be moved/padded
50 var a: A = undefined;50 var a: A = undefined;
51 expect(@ptrToInt(&a.a) - @ptrToInt(&a) == @byteOffsetOf(A, "a"));51 try expect(@ptrToInt(&a.a) - @ptrToInt(&a) == @byteOffsetOf(A, "a"));
52 expect(@ptrToInt(&a.b) - @ptrToInt(&a) == @byteOffsetOf(A, "b"));52 try expect(@ptrToInt(&a.b) - @ptrToInt(&a) == @byteOffsetOf(A, "b"));
53 expect(@ptrToInt(&a.c) - @ptrToInt(&a) == @byteOffsetOf(A, "c"));53 try expect(@ptrToInt(&a.c) - @ptrToInt(&a) == @byteOffsetOf(A, "c"));
54 expect(@ptrToInt(&a.d) - @ptrToInt(&a) == @byteOffsetOf(A, "d"));54 try expect(@ptrToInt(&a.d) - @ptrToInt(&a) == @byteOffsetOf(A, "d"));
55 expect(@ptrToInt(&a.e) - @ptrToInt(&a) == @byteOffsetOf(A, "e"));55 try expect(@ptrToInt(&a.e) - @ptrToInt(&a) == @byteOffsetOf(A, "e"));
56 expect(@ptrToInt(&a.f) - @ptrToInt(&a) == @byteOffsetOf(A, "f"));56 try expect(@ptrToInt(&a.f) - @ptrToInt(&a) == @byteOffsetOf(A, "f"));
57 expect(@ptrToInt(&a.g) - @ptrToInt(&a) == @byteOffsetOf(A, "g"));57 try expect(@ptrToInt(&a.g) - @ptrToInt(&a) == @byteOffsetOf(A, "g"));
58 expect(@ptrToInt(&a.h) - @ptrToInt(&a) == @byteOffsetOf(A, "h"));58 try expect(@ptrToInt(&a.h) - @ptrToInt(&a) == @byteOffsetOf(A, "h"));
59 expect(@ptrToInt(&a.i) - @ptrToInt(&a) == @byteOffsetOf(A, "i"));59 try expect(@ptrToInt(&a.i) - @ptrToInt(&a) == @byteOffsetOf(A, "i"));
60}60}
6161
62test "@byteOffsetOf packed struct, array length not power of 2 or multiple of native pointer width in bytes" {62test "@byteOffsetOf packed struct, array length not power of 2 or multiple of native pointer width in bytes" {
...@@ -65,68 +65,68 @@ test "@byteOffsetOf packed struct, array length not power of 2 or multiple of na...@@ -65,68 +65,68 @@ test "@byteOffsetOf packed struct, array length not power of 2 or multiple of na
65 a: [p3a_len]u8,65 a: [p3a_len]u8,
66 b: usize,66 b: usize,
67 };67 };
68 std.testing.expectEqual(0, @byteOffsetOf(P3, "a"));68 try std.testing.expectEqual(0, @byteOffsetOf(P3, "a"));
69 std.testing.expectEqual(p3a_len, @byteOffsetOf(P3, "b"));69 try std.testing.expectEqual(p3a_len, @byteOffsetOf(P3, "b"));
7070
71 const p5a_len = 5;71 const p5a_len = 5;
72 const P5 = packed struct {72 const P5 = packed struct {
73 a: [p5a_len]u8,73 a: [p5a_len]u8,
74 b: usize,74 b: usize,
75 };75 };
76 std.testing.expectEqual(0, @byteOffsetOf(P5, "a"));76 try std.testing.expectEqual(0, @byteOffsetOf(P5, "a"));
77 std.testing.expectEqual(p5a_len, @byteOffsetOf(P5, "b"));77 try std.testing.expectEqual(p5a_len, @byteOffsetOf(P5, "b"));
7878
79 const p6a_len = 6;79 const p6a_len = 6;
80 const P6 = packed struct {80 const P6 = packed struct {
81 a: [p6a_len]u8,81 a: [p6a_len]u8,
82 b: usize,82 b: usize,
83 };83 };
84 std.testing.expectEqual(0, @byteOffsetOf(P6, "a"));84 try std.testing.expectEqual(0, @byteOffsetOf(P6, "a"));
85 std.testing.expectEqual(p6a_len, @byteOffsetOf(P6, "b"));85 try std.testing.expectEqual(p6a_len, @byteOffsetOf(P6, "b"));
8686
87 const p7a_len = 7;87 const p7a_len = 7;
88 const P7 = packed struct {88 const P7 = packed struct {
89 a: [p7a_len]u8,89 a: [p7a_len]u8,
90 b: usize,90 b: usize,
91 };91 };
92 std.testing.expectEqual(0, @byteOffsetOf(P7, "a"));92 try std.testing.expectEqual(0, @byteOffsetOf(P7, "a"));
93 std.testing.expectEqual(p7a_len, @byteOffsetOf(P7, "b"));93 try std.testing.expectEqual(p7a_len, @byteOffsetOf(P7, "b"));
9494
95 const p9a_len = 9;95 const p9a_len = 9;
96 const P9 = packed struct {96 const P9 = packed struct {
97 a: [p9a_len]u8,97 a: [p9a_len]u8,
98 b: usize,98 b: usize,
99 };99 };
100 std.testing.expectEqual(0, @byteOffsetOf(P9, "a"));100 try std.testing.expectEqual(0, @byteOffsetOf(P9, "a"));
101 std.testing.expectEqual(p9a_len, @byteOffsetOf(P9, "b"));101 try std.testing.expectEqual(p9a_len, @byteOffsetOf(P9, "b"));
102102
103 // 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 25 etc. are further cases103 // 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 25 etc. are further cases
104}104}
105105
106test "@bitOffsetOf" {106test "@bitOffsetOf" {
107 // Packed structs have fixed memory layout107 // Packed structs have fixed memory layout
108 expect(@bitOffsetOf(P, "a") == 0);108 try expect(@bitOffsetOf(P, "a") == 0);
109 expect(@bitOffsetOf(P, "b") == 8);109 try expect(@bitOffsetOf(P, "b") == 8);
110 expect(@bitOffsetOf(P, "c") == 40);110 try expect(@bitOffsetOf(P, "c") == 40);
111 expect(@bitOffsetOf(P, "d") == 48);111 try expect(@bitOffsetOf(P, "d") == 48);
112 expect(@bitOffsetOf(P, "e") == 51);112 try expect(@bitOffsetOf(P, "e") == 51);
113 expect(@bitOffsetOf(P, "f") == 56);113 try expect(@bitOffsetOf(P, "f") == 56);
114 expect(@bitOffsetOf(P, "g") == 72);114 try expect(@bitOffsetOf(P, "g") == 72);
115115
116 expect(@byteOffsetOf(A, "a") * 8 == @bitOffsetOf(A, "a"));116 try expect(@byteOffsetOf(A, "a") * 8 == @bitOffsetOf(A, "a"));
117 expect(@byteOffsetOf(A, "b") * 8 == @bitOffsetOf(A, "b"));117 try expect(@byteOffsetOf(A, "b") * 8 == @bitOffsetOf(A, "b"));
118 expect(@byteOffsetOf(A, "c") * 8 == @bitOffsetOf(A, "c"));118 try expect(@byteOffsetOf(A, "c") * 8 == @bitOffsetOf(A, "c"));
119 expect(@byteOffsetOf(A, "d") * 8 == @bitOffsetOf(A, "d"));119 try expect(@byteOffsetOf(A, "d") * 8 == @bitOffsetOf(A, "d"));
120 expect(@byteOffsetOf(A, "e") * 8 == @bitOffsetOf(A, "e"));120 try expect(@byteOffsetOf(A, "e") * 8 == @bitOffsetOf(A, "e"));
121 expect(@byteOffsetOf(A, "f") * 8 == @bitOffsetOf(A, "f"));121 try expect(@byteOffsetOf(A, "f") * 8 == @bitOffsetOf(A, "f"));
122 expect(@byteOffsetOf(A, "g") * 8 == @bitOffsetOf(A, "g"));122 try expect(@byteOffsetOf(A, "g") * 8 == @bitOffsetOf(A, "g"));
123}123}
124124
125test "@sizeOf on compile-time types" {125test "@sizeOf on compile-time types" {
126 expect(@sizeOf(comptime_int) == 0);126 try expect(@sizeOf(comptime_int) == 0);
127 expect(@sizeOf(comptime_float) == 0);127 try expect(@sizeOf(comptime_float) == 0);
128 expect(@sizeOf(@TypeOf(.hi)) == 0);128 try expect(@sizeOf(@TypeOf(.hi)) == 0);
129 expect(@sizeOf(@TypeOf(type)) == 0);129 try expect(@sizeOf(@TypeOf(type)) == 0);
130}130}
131131
132test "@sizeOf(T) == 0 doesn't force resolving struct size" {132test "@sizeOf(T) == 0 doesn't force resolving struct size" {
...@@ -140,8 +140,8 @@ test "@sizeOf(T) == 0 doesn't force resolving struct size" {...@@ -140,8 +140,8 @@ test "@sizeOf(T) == 0 doesn't force resolving struct size" {
140 };140 };
141 };141 };
142142
143 expect(@sizeOf(S.Foo) == 4);143 try expect(@sizeOf(S.Foo) == 4);
144 expect(@sizeOf(S.Bar) == 8);144 try expect(@sizeOf(S.Bar) == 8);
145}145}
146146
147test "@TypeOf() has no runtime side effects" {147test "@TypeOf() has no runtime side effects" {
...@@ -153,8 +153,8 @@ test "@TypeOf() has no runtime side effects" {...@@ -153,8 +153,8 @@ test "@TypeOf() has no runtime side effects" {
153 };153 };
154 var data: i32 = 0;154 var data: i32 = 0;
155 const T = @TypeOf(S.foo(i32, &data));155 const T = @TypeOf(S.foo(i32, &data));
156 comptime expect(T == i32);156 comptime try expect(T == i32);
157 expect(data == 0);157 try expect(data == 0);
158}158}
159159
160test "@TypeOf() with multiple arguments" {160test "@TypeOf() with multiple arguments" {
...@@ -162,21 +162,21 @@ test "@TypeOf() with multiple arguments" {...@@ -162,21 +162,21 @@ test "@TypeOf() with multiple arguments" {
162 var var_1: u32 = undefined;162 var var_1: u32 = undefined;
163 var var_2: u8 = undefined;163 var var_2: u8 = undefined;
164 var var_3: u64 = undefined;164 var var_3: u64 = undefined;
165 comptime expect(@TypeOf(var_1, var_2, var_3) == u64);165 comptime try expect(@TypeOf(var_1, var_2, var_3) == u64);
166 }166 }
167 {167 {
168 var var_1: f16 = undefined;168 var var_1: f16 = undefined;
169 var var_2: f32 = undefined;169 var var_2: f32 = undefined;
170 var var_3: f64 = undefined;170 var var_3: f64 = undefined;
171 comptime expect(@TypeOf(var_1, var_2, var_3) == f64);171 comptime try expect(@TypeOf(var_1, var_2, var_3) == f64);
172 }172 }
173 {173 {
174 var var_1: u16 = undefined;174 var var_1: u16 = undefined;
175 comptime expect(@TypeOf(var_1, 0xffff) == u16);175 comptime try expect(@TypeOf(var_1, 0xffff) == u16);
176 }176 }
177 {177 {
178 var var_1: f32 = undefined;178 var var_1: f32 = undefined;
179 comptime expect(@TypeOf(var_1, 3.1415) == f32);179 comptime try expect(@TypeOf(var_1, 3.1415) == f32);
180 }180 }
181}181}
182182
...@@ -189,8 +189,8 @@ test "branching logic inside @TypeOf" {...@@ -189,8 +189,8 @@ test "branching logic inside @TypeOf" {
189 }189 }
190 };190 };
191 const T = @TypeOf(S.foo() catch undefined);191 const T = @TypeOf(S.foo() catch undefined);
192 comptime expect(T == i32);192 comptime try expect(T == i32);
193 expect(S.data == 0);193 try expect(S.data == 0);
194}194}
195195
196fn fn1(alpha: bool) void {196fn fn1(alpha: bool) void {
...@@ -203,12 +203,12 @@ test "lazy @sizeOf result is checked for definedness" {...@@ -203,12 +203,12 @@ test "lazy @sizeOf result is checked for definedness" {
203}203}
204204
205test "@bitSizeOf" {205test "@bitSizeOf" {
206 expect(@bitSizeOf(u2) == 2);206 try expect(@bitSizeOf(u2) == 2);
207 expect(@bitSizeOf(u8) == @sizeOf(u8) * 8);207 try expect(@bitSizeOf(u8) == @sizeOf(u8) * 8);
208 expect(@bitSizeOf(struct {208 try expect(@bitSizeOf(struct {
209 a: u2,209 a: u2,
210 }) == 8);210 }) == 8);
211 expect(@bitSizeOf(packed struct {211 try expect(@bitSizeOf(packed struct {
212 a: u2,212 a: u2,
213 }) == 2);213 }) == 2);
214}214}
...@@ -241,24 +241,24 @@ test "@sizeOf comparison against zero" {...@@ -241,24 +241,24 @@ test "@sizeOf comparison against zero" {
241 f2: H(***@This()),241 f2: H(***@This()),
242 };242 };
243 const S = struct {243 const S = struct {
244 fn doTheTest(comptime T: type, comptime result: bool) void {244 fn doTheTest(comptime T: type, comptime result: bool) !void {
245 expectEqual(result, @sizeOf(T) > 0);245 try expectEqual(result, @sizeOf(T) > 0);
246 }246 }
247 };247 };
248 // Zero-sized type248 // Zero-sized type
249 S.doTheTest(u0, false);249 try S.doTheTest(u0, false);
250 S.doTheTest(*u0, false);250 try S.doTheTest(*u0, false);
251 // Non byte-sized type251 // Non byte-sized type
252 S.doTheTest(u1, true);252 try S.doTheTest(u1, true);
253 S.doTheTest(*u1, true);253 try S.doTheTest(*u1, true);
254 // Regular type254 // Regular type
255 S.doTheTest(u8, true);255 try S.doTheTest(u8, true);
256 S.doTheTest(*u8, true);256 try S.doTheTest(*u8, true);
257 S.doTheTest(f32, true);257 try S.doTheTest(f32, true);
258 S.doTheTest(*f32, true);258 try S.doTheTest(*f32, true);
259 // Container with ptr pointing to themselves259 // Container with ptr pointing to themselves
260 S.doTheTest(S0, true);260 try S.doTheTest(S0, true);
261 S.doTheTest(U0, true);261 try S.doTheTest(U0, true);
262 S.doTheTest(S1, true);262 try S.doTheTest(S1, true);
263 S.doTheTest(U1, true);263 try S.doTheTest(U1, true);
264}264}
test/stage1/behavior/slice.zig+119-119
...@@ -7,11 +7,11 @@ const mem = std.mem;...@@ -7,11 +7,11 @@ const mem = std.mem;
7const x = @intToPtr([*]i32, 0x1000)[0..0x500];7const x = @intToPtr([*]i32, 0x1000)[0..0x500];
8const y = x[0x100..];8const y = x[0x100..];
9test "compile time slice of pointer to hard coded address" {9test "compile time slice of pointer to hard coded address" {
10 expect(@ptrToInt(x) == 0x1000);10 try expect(@ptrToInt(x) == 0x1000);
11 expect(x.len == 0x500);11 try expect(x.len == 0x500);
1212
13 expect(@ptrToInt(y) == 0x1100);13 try expect(@ptrToInt(y) == 0x1100);
14 expect(y.len == 0x400);14 try expect(y.len == 0x400);
15}15}
1616
17test "runtime safety lets us slice from len..len" {17test "runtime safety lets us slice from len..len" {
...@@ -20,7 +20,7 @@ test "runtime safety lets us slice from len..len" {...@@ -20,7 +20,7 @@ test "runtime safety lets us slice from len..len" {
20 2,20 2,
21 3,21 3,
22 };22 };
23 expect(mem.eql(u8, sliceFromLenToLen(an_array[0..], 3, 3), ""));23 try expect(mem.eql(u8, sliceFromLenToLen(an_array[0..], 3, 3), ""));
24}24}
2525
26fn sliceFromLenToLen(a_slice: []u8, start: usize, end: usize) []u8 {26fn sliceFromLenToLen(a_slice: []u8, start: usize, end: usize) []u8 {
...@@ -29,18 +29,18 @@ fn sliceFromLenToLen(a_slice: []u8, start: usize, end: usize) []u8 {...@@ -29,18 +29,18 @@ fn sliceFromLenToLen(a_slice: []u8, start: usize, end: usize) []u8 {
2929
30test "implicitly cast array of size 0 to slice" {30test "implicitly cast array of size 0 to slice" {
31 var msg = [_]u8{};31 var msg = [_]u8{};
32 assertLenIsZero(&msg);32 try assertLenIsZero(&msg);
33}33}
3434
35fn assertLenIsZero(msg: []const u8) void {35fn assertLenIsZero(msg: []const u8) !void {
36 expect(msg.len == 0);36 try expect(msg.len == 0);
37}37}
3838
39test "C pointer" {39test "C pointer" {
40 var buf: [*c]const u8 = "kjdhfkjdhfdkjhfkfjhdfkjdhfkdjhfdkjhf";40 var buf: [*c]const u8 = "kjdhfkjdhfdkjhfkfjhdfkjdhfkdjhfdkjhf";
41 var len: u32 = 10;41 var len: u32 = 10;
42 var slice = buf[0..len];42 var slice = buf[0..len];
43 expectEqualSlices(u8, "kjdhfkjdhf", slice);43 try expectEqualSlices(u8, "kjdhfkjdhf", slice);
44}44}
4545
46test "C pointer slice access" {46test "C pointer slice access" {
...@@ -48,11 +48,11 @@ test "C pointer slice access" {...@@ -48,11 +48,11 @@ test "C pointer slice access" {
48 const c_ptr = @ptrCast([*c]const u32, &buf);48 const c_ptr = @ptrCast([*c]const u32, &buf);
4949
50 var runtime_zero: usize = 0;50 var runtime_zero: usize = 0;
51 comptime expectEqual([]const u32, @TypeOf(c_ptr[runtime_zero..1]));51 comptime try expectEqual([]const u32, @TypeOf(c_ptr[runtime_zero..1]));
52 comptime expectEqual(*const [1]u32, @TypeOf(c_ptr[0..1]));52 comptime try expectEqual(*const [1]u32, @TypeOf(c_ptr[0..1]));
5353
54 for (c_ptr[0..5]) |*cl| {54 for (c_ptr[0..5]) |*cl| {
55 expectEqual(@as(u32, 42), cl.*);55 try expectEqual(@as(u32, 42), cl.*);
56 }56 }
57}57}
5858
...@@ -65,8 +65,8 @@ fn sliceSum(comptime q: []const u8) i32 {...@@ -65,8 +65,8 @@ fn sliceSum(comptime q: []const u8) i32 {
65}65}
6666
67test "comptime slices are disambiguated" {67test "comptime slices are disambiguated" {
68 expect(sliceSum(&[_]u8{ 1, 2 }) == 3);68 try expect(sliceSum(&[_]u8{ 1, 2 }) == 3);
69 expect(sliceSum(&[_]u8{ 3, 4 }) == 7);69 try expect(sliceSum(&[_]u8{ 3, 4 }) == 7);
70}70}
7171
72test "slice type with custom alignment" {72test "slice type with custom alignment" {
...@@ -77,20 +77,20 @@ test "slice type with custom alignment" {...@@ -77,20 +77,20 @@ test "slice type with custom alignment" {
77 var array: [10]LazilyResolvedType align(32) = undefined;77 var array: [10]LazilyResolvedType align(32) = undefined;
78 slice = &array;78 slice = &array;
79 slice[1].anything = 42;79 slice[1].anything = 42;
80 expect(array[1].anything == 42);80 try expect(array[1].anything == 42);
81}81}
8282
83test "access len index of sentinel-terminated slice" {83test "access len index of sentinel-terminated slice" {
84 const S = struct {84 const S = struct {
85 fn doTheTest() void {85 fn doTheTest() !void {
86 var slice: [:0]const u8 = "hello";86 var slice: [:0]const u8 = "hello";
8787
88 expect(slice.len == 5);88 try expect(slice.len == 5);
89 expect(slice[5] == 0);89 try expect(slice[5] == 0);
90 }90 }
91 };91 };
92 S.doTheTest();92 try S.doTheTest();
93 comptime S.doTheTest();93 comptime try S.doTheTest();
94}94}
9595
96test "obtaining a null terminated slice" {96test "obtaining a null terminated slice" {
...@@ -108,230 +108,230 @@ test "obtaining a null terminated slice" {...@@ -108,230 +108,230 @@ test "obtaining a null terminated slice" {
108 var runtime_len: usize = 3;108 var runtime_len: usize = 3;
109 const ptr2 = buf[0..runtime_len :0];109 const ptr2 = buf[0..runtime_len :0];
110 // ptr2 is a null-terminated slice110 // ptr2 is a null-terminated slice
111 comptime expect(@TypeOf(ptr2) == [:0]u8);111 comptime try expect(@TypeOf(ptr2) == [:0]u8);
112 comptime expect(@TypeOf(ptr2[0..2]) == *[2]u8);112 comptime try expect(@TypeOf(ptr2[0..2]) == *[2]u8);
113 var runtime_zero: usize = 0;113 var runtime_zero: usize = 0;
114 comptime expect(@TypeOf(ptr2[runtime_zero..2]) == []u8);114 comptime try expect(@TypeOf(ptr2[runtime_zero..2]) == []u8);
115}115}
116116
117test "empty array to slice" {117test "empty array to slice" {
118 const S = struct {118 const S = struct {
119 fn doTheTest() void {119 fn doTheTest() !void {
120 const empty: []align(16) u8 = &[_]u8{};120 const empty: []align(16) u8 = &[_]u8{};
121 const align_1: []align(1) u8 = empty;121 const align_1: []align(1) u8 = empty;
122 const align_4: []align(4) u8 = empty;122 const align_4: []align(4) u8 = empty;
123 const align_16: []align(16) u8 = empty;123 const align_16: []align(16) u8 = empty;
124 expectEqual(1, @typeInfo(@TypeOf(align_1)).Pointer.alignment);124 try expectEqual(1, @typeInfo(@TypeOf(align_1)).Pointer.alignment);
125 expectEqual(4, @typeInfo(@TypeOf(align_4)).Pointer.alignment);125 try expectEqual(4, @typeInfo(@TypeOf(align_4)).Pointer.alignment);
126 expectEqual(16, @typeInfo(@TypeOf(align_16)).Pointer.alignment);126 try expectEqual(16, @typeInfo(@TypeOf(align_16)).Pointer.alignment);
127 }127 }
128 };128 };
129129
130 S.doTheTest();130 try S.doTheTest();
131 comptime S.doTheTest();131 comptime try S.doTheTest();
132}132}
133133
134test "@ptrCast slice to pointer" {134test "@ptrCast slice to pointer" {
135 const S = struct {135 const S = struct {
136 fn doTheTest() void {136 fn doTheTest() !void {
137 var array align(@alignOf(u16)) = [5]u8{ 0xff, 0xff, 0xff, 0xff, 0xff };137 var array align(@alignOf(u16)) = [5]u8{ 0xff, 0xff, 0xff, 0xff, 0xff };
138 var slice: []u8 = &array;138 var slice: []u8 = &array;
139 var ptr = @ptrCast(*u16, slice);139 var ptr = @ptrCast(*u16, slice);
140 expect(ptr.* == 65535);140 try expect(ptr.* == 65535);
141 }141 }
142 };142 };
143143
144 S.doTheTest();144 try S.doTheTest();
145 comptime S.doTheTest();145 comptime try S.doTheTest();
146}146}
147147
148test "slice syntax resulting in pointer-to-array" {148test "slice syntax resulting in pointer-to-array" {
149 const S = struct {149 const S = struct {
150 fn doTheTest() void {150 fn doTheTest() !void {
151 testArray();151 try testArray();
152 testArrayZ();152 try testArrayZ();
153 testArray0();153 try testArray0();
154 testArrayAlign();154 try testArrayAlign();
155 testPointer();155 try testPointer();
156 testPointerZ();156 try testPointerZ();
157 testPointer0();157 try testPointer0();
158 testPointerAlign();158 try testPointerAlign();
159 testSlice();159 try testSlice();
160 testSliceZ();160 try testSliceZ();
161 testSlice0();161 try testSlice0();
162 testSliceOpt();162 try testSliceOpt();
163 testSliceAlign();163 try testSliceAlign();
164 }164 }
165165
166 fn testArray() void {166 fn testArray() !void {
167 var array = [5]u8{ 1, 2, 3, 4, 5 };167 var array = [5]u8{ 1, 2, 3, 4, 5 };
168 var slice = array[1..3];168 var slice = array[1..3];
169 comptime expect(@TypeOf(slice) == *[2]u8);169 comptime try expect(@TypeOf(slice) == *[2]u8);
170 expect(slice[0] == 2);170 try expect(slice[0] == 2);
171 expect(slice[1] == 3);171 try expect(slice[1] == 3);
172 }172 }
173173
174 fn testArrayZ() void {174 fn testArrayZ() !void {
175 var array = [5:0]u8{ 1, 2, 3, 4, 5 };175 var array = [5:0]u8{ 1, 2, 3, 4, 5 };
176 comptime expect(@TypeOf(array[1..3]) == *[2]u8);176 comptime try expect(@TypeOf(array[1..3]) == *[2]u8);
177 comptime expect(@TypeOf(array[1..5]) == *[4:0]u8);177 comptime try expect(@TypeOf(array[1..5]) == *[4:0]u8);
178 comptime expect(@TypeOf(array[1..]) == *[4:0]u8);178 comptime try expect(@TypeOf(array[1..]) == *[4:0]u8);
179 comptime expect(@TypeOf(array[1..3 :4]) == *[2:4]u8);179 comptime try expect(@TypeOf(array[1..3 :4]) == *[2:4]u8);
180 }180 }
181181
182 fn testArray0() void {182 fn testArray0() !void {
183 {183 {
184 var array = [0]u8{};184 var array = [0]u8{};
185 var slice = array[0..0];185 var slice = array[0..0];
186 comptime expect(@TypeOf(slice) == *[0]u8);186 comptime try expect(@TypeOf(slice) == *[0]u8);
187 }187 }
188 {188 {
189 var array = [0:0]u8{};189 var array = [0:0]u8{};
190 var slice = array[0..0];190 var slice = array[0..0];
191 comptime expect(@TypeOf(slice) == *[0:0]u8);191 comptime try expect(@TypeOf(slice) == *[0:0]u8);
192 expect(slice[0] == 0);192 try expect(slice[0] == 0);
193 }193 }
194 }194 }
195195
196 fn testArrayAlign() void {196 fn testArrayAlign() !void {
197 var array align(4) = [5]u8{ 1, 2, 3, 4, 5 };197 var array align(4) = [5]u8{ 1, 2, 3, 4, 5 };
198 var slice = array[4..5];198 var slice = array[4..5];
199 comptime expect(@TypeOf(slice) == *align(4) [1]u8);199 comptime try expect(@TypeOf(slice) == *align(4) [1]u8);
200 expect(slice[0] == 5);200 try expect(slice[0] == 5);
201 comptime expect(@TypeOf(array[0..2]) == *align(4) [2]u8);201 comptime try expect(@TypeOf(array[0..2]) == *align(4) [2]u8);
202 }202 }
203203
204 fn testPointer() void {204 fn testPointer() !void {
205 var array = [5]u8{ 1, 2, 3, 4, 5 };205 var array = [5]u8{ 1, 2, 3, 4, 5 };
206 var pointer: [*]u8 = &array;206 var pointer: [*]u8 = &array;
207 var slice = pointer[1..3];207 var slice = pointer[1..3];
208 comptime expect(@TypeOf(slice) == *[2]u8);208 comptime try expect(@TypeOf(slice) == *[2]u8);
209 expect(slice[0] == 2);209 try expect(slice[0] == 2);
210 expect(slice[1] == 3);210 try expect(slice[1] == 3);
211 }211 }
212212
213 fn testPointerZ() void {213 fn testPointerZ() !void {
214 var array = [5:0]u8{ 1, 2, 3, 4, 5 };214 var array = [5:0]u8{ 1, 2, 3, 4, 5 };
215 var pointer: [*:0]u8 = &array;215 var pointer: [*:0]u8 = &array;
216 comptime expect(@TypeOf(pointer[1..3]) == *[2]u8);216 comptime try expect(@TypeOf(pointer[1..3]) == *[2]u8);
217 comptime expect(@TypeOf(pointer[1..3 :4]) == *[2:4]u8);217 comptime try expect(@TypeOf(pointer[1..3 :4]) == *[2:4]u8);
218 }218 }
219219
220 fn testPointer0() void {220 fn testPointer0() !void {
221 var pointer: [*]const u0 = &[1]u0{0};221 var pointer: [*]const u0 = &[1]u0{0};
222 var slice = pointer[0..1];222 var slice = pointer[0..1];
223 comptime expect(@TypeOf(slice) == *const [1]u0);223 comptime try expect(@TypeOf(slice) == *const [1]u0);
224 expect(slice[0] == 0);224 try expect(slice[0] == 0);
225 }225 }
226226
227 fn testPointerAlign() void {227 fn testPointerAlign() !void {
228 var array align(4) = [5]u8{ 1, 2, 3, 4, 5 };228 var array align(4) = [5]u8{ 1, 2, 3, 4, 5 };
229 var pointer: [*]align(4) u8 = &array;229 var pointer: [*]align(4) u8 = &array;
230 var slice = pointer[4..5];230 var slice = pointer[4..5];
231 comptime expect(@TypeOf(slice) == *align(4) [1]u8);231 comptime try expect(@TypeOf(slice) == *align(4) [1]u8);
232 expect(slice[0] == 5);232 try expect(slice[0] == 5);
233 comptime expect(@TypeOf(pointer[0..2]) == *align(4) [2]u8);233 comptime try expect(@TypeOf(pointer[0..2]) == *align(4) [2]u8);
234 }234 }
235235
236 fn testSlice() void {236 fn testSlice() !void {
237 var array = [5]u8{ 1, 2, 3, 4, 5 };237 var array = [5]u8{ 1, 2, 3, 4, 5 };
238 var src_slice: []u8 = &array;238 var src_slice: []u8 = &array;
239 var slice = src_slice[1..3];239 var slice = src_slice[1..3];
240 comptime expect(@TypeOf(slice) == *[2]u8);240 comptime try expect(@TypeOf(slice) == *[2]u8);
241 expect(slice[0] == 2);241 try expect(slice[0] == 2);
242 expect(slice[1] == 3);242 try expect(slice[1] == 3);
243 }243 }
244244
245 fn testSliceZ() void {245 fn testSliceZ() !void {
246 var array = [5:0]u8{ 1, 2, 3, 4, 5 };246 var array = [5:0]u8{ 1, 2, 3, 4, 5 };
247 var slice: [:0]u8 = &array;247 var slice: [:0]u8 = &array;
248 comptime expect(@TypeOf(slice[1..3]) == *[2]u8);248 comptime try expect(@TypeOf(slice[1..3]) == *[2]u8);
249 comptime expect(@TypeOf(slice[1..]) == [:0]u8);249 comptime try expect(@TypeOf(slice[1..]) == [:0]u8);
250 comptime expect(@TypeOf(slice[1..3 :4]) == *[2:4]u8);250 comptime try expect(@TypeOf(slice[1..3 :4]) == *[2:4]u8);
251 }251 }
252252
253 fn testSliceOpt() void {253 fn testSliceOpt() !void {
254 var array: [2]u8 = [2]u8{ 1, 2 };254 var array: [2]u8 = [2]u8{ 1, 2 };
255 var slice: ?[]u8 = &array;255 var slice: ?[]u8 = &array;
256 comptime expect(@TypeOf(&array, slice) == ?[]u8);256 comptime try expect(@TypeOf(&array, slice) == ?[]u8);
257 comptime expect(@TypeOf(slice.?[0..2]) == *[2]u8);257 comptime try expect(@TypeOf(slice.?[0..2]) == *[2]u8);
258 }258 }
259259
260 fn testSlice0() void {260 fn testSlice0() !void {
261 {261 {
262 var array = [0]u8{};262 var array = [0]u8{};
263 var src_slice: []u8 = &array;263 var src_slice: []u8 = &array;
264 var slice = src_slice[0..0];264 var slice = src_slice[0..0];
265 comptime expect(@TypeOf(slice) == *[0]u8);265 comptime try expect(@TypeOf(slice) == *[0]u8);
266 }266 }
267 {267 {
268 var array = [0:0]u8{};268 var array = [0:0]u8{};
269 var src_slice: [:0]u8 = &array;269 var src_slice: [:0]u8 = &array;
270 var slice = src_slice[0..0];270 var slice = src_slice[0..0];
271 comptime expect(@TypeOf(slice) == *[0]u8);271 comptime try expect(@TypeOf(slice) == *[0]u8);
272 }272 }
273 }273 }
274274
275 fn testSliceAlign() void {275 fn testSliceAlign() !void {
276 var array align(4) = [5]u8{ 1, 2, 3, 4, 5 };276 var array align(4) = [5]u8{ 1, 2, 3, 4, 5 };
277 var src_slice: []align(4) u8 = &array;277 var src_slice: []align(4) u8 = &array;
278 var slice = src_slice[4..5];278 var slice = src_slice[4..5];
279 comptime expect(@TypeOf(slice) == *align(4) [1]u8);279 comptime try expect(@TypeOf(slice) == *align(4) [1]u8);
280 expect(slice[0] == 5);280 try expect(slice[0] == 5);
281 comptime expect(@TypeOf(src_slice[0..2]) == *align(4) [2]u8);281 comptime try expect(@TypeOf(src_slice[0..2]) == *align(4) [2]u8);
282 }282 }
283283
284 fn testConcatStrLiterals() void {284 fn testConcatStrLiterals() !void {
285 expectEqualSlices("a"[0..] ++ "b"[0..], "ab");285 try expectEqualSlices("a"[0..] ++ "b"[0..], "ab");
286 expectEqualSlices("a"[0..:0] ++ "b"[0..:0], "ab");286 try expectEqualSlices("a"[0.. :0] ++ "b"[0.. :0], "ab");
287 }287 }
288 };288 };
289289
290 S.doTheTest();290 try S.doTheTest();
291 comptime S.doTheTest();291 comptime try S.doTheTest();
292}292}
293293
294test "slice of hardcoded address to pointer" {294test "slice of hardcoded address to pointer" {
295 const S = struct {295 const S = struct {
296 fn doTheTest() void {296 fn doTheTest() !void {
297 const pointer = @intToPtr([*]u8, 0x04)[0..2];297 const pointer = @intToPtr([*]u8, 0x04)[0..2];
298 comptime expect(@TypeOf(pointer) == *[2]u8);298 comptime try expect(@TypeOf(pointer) == *[2]u8);
299 const slice: []const u8 = pointer;299 const slice: []const u8 = pointer;
300 expect(@ptrToInt(slice.ptr) == 4);300 try expect(@ptrToInt(slice.ptr) == 4);
301 expect(slice.len == 2);301 try expect(slice.len == 2);
302 }302 }
303 };303 };
304304
305 S.doTheTest();305 try S.doTheTest();
306}306}
307307
308test "type coercion of pointer to anon struct literal to pointer to slice" {308test "type coercion of pointer to anon struct literal to pointer to slice" {
309 const S = struct {309 const S = struct {
310 const U = union{310 const U = union {
311 a: u32,311 a: u32,
312 b: bool,312 b: bool,
313 c: []const u8,313 c: []const u8,
314 };314 };
315315
316 fn doTheTest() void {316 fn doTheTest() !void {
317 var x1: u8 = 42;317 var x1: u8 = 42;
318 const t1 = &.{ x1, 56, 54 };318 const t1 = &.{ x1, 56, 54 };
319 var slice1: []const u8 = t1;319 var slice1: []const u8 = t1;
320 expect(slice1.len == 3);320 try expect(slice1.len == 3);
321 expect(slice1[0] == 42);321 try expect(slice1[0] == 42);
322 expect(slice1[1] == 56);322 try expect(slice1[1] == 56);
323 expect(slice1[2] == 54);323 try expect(slice1[2] == 54);
324 324
325 var x2: []const u8 = "hello";325 var x2: []const u8 = "hello";
326 const t2 = &.{ x2, ", ", "world!" };326 const t2 = &.{ x2, ", ", "world!" };
327 // @compileLog(@TypeOf(t2));327 // @compileLog(@TypeOf(t2));
328 var slice2: []const []const u8 = t2;328 var slice2: []const []const u8 = t2;
329 expect(slice2.len == 3);329 try expect(slice2.len == 3);
330 expect(mem.eql(u8, slice2[0], "hello"));330 try expect(mem.eql(u8, slice2[0], "hello"));
331 expect(mem.eql(u8, slice2[1], ", "));331 try expect(mem.eql(u8, slice2[1], ", "));
332 expect(mem.eql(u8, slice2[2], "world!"));332 try expect(mem.eql(u8, slice2[2], "world!"));
333 }333 }
334 };334 };
335 // S.doTheTest();335 // try S.doTheTest();
336 comptime S.doTheTest();336 comptime try S.doTheTest();
337}337}
test/stage1/behavior/src.zig+8-8
...@@ -2,16 +2,16 @@ const std = @import("std");...@@ -2,16 +2,16 @@ const std = @import("std");
2const expect = std.testing.expect;2const expect = std.testing.expect;
33
4test "@src" {4test "@src" {
5 doTheTest();5 try doTheTest();
6}6}
77
8fn doTheTest() void {8fn doTheTest() !void {
9 const src = @src();9 const src = @src();
1010
11 expect(src.line == 9);11 try expect(src.line == 9);
12 expect(src.column == 17);12 try expect(src.column == 17);
13 expect(std.mem.endsWith(u8, src.fn_name, "doTheTest"));13 try expect(std.mem.endsWith(u8, src.fn_name, "doTheTest"));
14 expect(std.mem.endsWith(u8, src.file, "src.zig"));14 try expect(std.mem.endsWith(u8, src.file, "src.zig"));
15 expect(src.fn_name[src.fn_name.len] == 0);15 try expect(src.fn_name[src.fn_name.len] == 0);
16 expect(src.file[src.file.len] == 0);16 try expect(src.file[src.file.len] == 0);
17}17}
test/stage1/behavior/struct.zig+190-190
...@@ -18,12 +18,12 @@ test "top level fields" {...@@ -18,12 +18,12 @@ test "top level fields" {
18 .top_level_field = 1234,18 .top_level_field = 1234,
19 };19 };
20 instance.top_level_field += 1;20 instance.top_level_field += 1;
21 expectEqual(@as(i32, 1235), instance.top_level_field);21 try expectEqual(@as(i32, 1235), instance.top_level_field);
22}22}
2323
24test "call struct static method" {24test "call struct static method" {
25 const result = StructWithNoFields.add(3, 4);25 const result = StructWithNoFields.add(3, 4);
26 expect(result == 7);26 try expect(result == 7);
27}27}
2828
29test "return empty struct instance" {29test "return empty struct instance" {
...@@ -36,7 +36,7 @@ fn returnEmptyStructInstance() StructWithNoFields {...@@ -36,7 +36,7 @@ fn returnEmptyStructInstance() StructWithNoFields {
36const should_be_11 = StructWithNoFields.add(5, 6);36const should_be_11 = StructWithNoFields.add(5, 6);
3737
38test "invoke static method in global scope" {38test "invoke static method in global scope" {
39 expect(should_be_11 == 11);39 try expect(should_be_11 == 11);
40}40}
4141
42test "void struct fields" {42test "void struct fields" {
...@@ -45,8 +45,8 @@ test "void struct fields" {...@@ -45,8 +45,8 @@ test "void struct fields" {
45 .b = 1,45 .b = 1,
46 .c = void{},46 .c = void{},
47 };47 };
48 expect(foo.b == 1);48 try expect(foo.b == 1);
49 expect(@sizeOf(VoidStructFieldsFoo) == 4);49 try expect(@sizeOf(VoidStructFieldsFoo) == 4);
50}50}
51const VoidStructFieldsFoo = struct {51const VoidStructFieldsFoo = struct {
52 a: void,52 a: void,
...@@ -59,17 +59,17 @@ test "structs" {...@@ -59,17 +59,17 @@ test "structs" {
59 @memset(@ptrCast([*]u8, &foo), 0, @sizeOf(StructFoo));59 @memset(@ptrCast([*]u8, &foo), 0, @sizeOf(StructFoo));
60 foo.a += 1;60 foo.a += 1;
61 foo.b = foo.a == 1;61 foo.b = foo.a == 1;
62 testFoo(foo);62 try testFoo(foo);
63 testMutation(&foo);63 testMutation(&foo);
64 expect(foo.c == 100);64 try expect(foo.c == 100);
65}65}
66const StructFoo = struct {66const StructFoo = struct {
67 a: i32,67 a: i32,
68 b: bool,68 b: bool,
69 c: f32,69 c: f32,
70};70};
71fn testFoo(foo: StructFoo) void {71fn testFoo(foo: StructFoo) !void {
72 expect(foo.b);72 try expect(foo.b);
73}73}
74fn testMutation(foo: *StructFoo) void {74fn testMutation(foo: *StructFoo) void {
75 foo.c = 100;75 foo.c = 100;
...@@ -94,7 +94,7 @@ test "struct point to self" {...@@ -94,7 +94,7 @@ test "struct point to self" {
9494
95 root.next = &node;95 root.next = &node;
9696
97 expect(node.next.next.next.val.x == 1);97 try expect(node.next.next.next.val.x == 1);
98}98}
9999
100test "struct byval assign" {100test "struct byval assign" {
...@@ -103,14 +103,14 @@ test "struct byval assign" {...@@ -103,14 +103,14 @@ test "struct byval assign" {
103103
104 foo1.a = 1234;104 foo1.a = 1234;
105 foo2.a = 0;105 foo2.a = 0;
106 expect(foo2.a == 0);106 try expect(foo2.a == 0);
107 foo2 = foo1;107 foo2 = foo1;
108 expect(foo2.a == 1234);108 try expect(foo2.a == 1234);
109}109}
110110
111fn structInitializer() void {111fn structInitializer() void {
112 const val = Val{ .x = 42 };112 const val = Val{ .x = 42 };
113 expect(val.x == 42);113 try expect(val.x == 42);
114}114}
115115
116test "fn call of struct field" {116test "fn call of struct field" {
...@@ -127,14 +127,14 @@ test "fn call of struct field" {...@@ -127,14 +127,14 @@ test "fn call of struct field" {
127 }127 }
128 };128 };
129129
130 expect(S.callStructField(Foo{ .ptr = S.aFunc }) == 13);130 try expect(S.callStructField(Foo{ .ptr = S.aFunc }) == 13);
131}131}
132132
133test "store member function in variable" {133test "store member function in variable" {
134 const instance = MemberFnTestFoo{ .x = 1234 };134 const instance = MemberFnTestFoo{ .x = 1234 };
135 const memberFn = MemberFnTestFoo.member;135 const memberFn = MemberFnTestFoo.member;
136 const result = memberFn(instance);136 const result = memberFn(instance);
137 expect(result == 1234);137 try expect(result == 1234);
138}138}
139const MemberFnTestFoo = struct {139const MemberFnTestFoo = struct {
140 x: i32,140 x: i32,
...@@ -146,12 +146,12 @@ const MemberFnTestFoo = struct {...@@ -146,12 +146,12 @@ const MemberFnTestFoo = struct {
146test "call member function directly" {146test "call member function directly" {
147 const instance = MemberFnTestFoo{ .x = 1234 };147 const instance = MemberFnTestFoo{ .x = 1234 };
148 const result = MemberFnTestFoo.member(instance);148 const result = MemberFnTestFoo.member(instance);
149 expect(result == 1234);149 try expect(result == 1234);
150}150}
151151
152test "member functions" {152test "member functions" {
153 const r = MemberFnRand{ .seed = 1234 };153 const r = MemberFnRand{ .seed = 1234 };
154 expect(r.getSeed() == 1234);154 try expect(r.getSeed() == 1234);
155}155}
156const MemberFnRand = struct {156const MemberFnRand = struct {
157 seed: u32,157 seed: u32,
...@@ -162,7 +162,7 @@ const MemberFnRand = struct {...@@ -162,7 +162,7 @@ const MemberFnRand = struct {
162162
163test "return struct byval from function" {163test "return struct byval from function" {
164 const bar = makeBar(1234, 5678);164 const bar = makeBar(1234, 5678);
165 expect(bar.y == 5678);165 try expect(bar.y == 5678);
166}166}
167const Bar = struct {167const Bar = struct {
168 x: i32,168 x: i32,
...@@ -177,7 +177,7 @@ fn makeBar(x: i32, y: i32) Bar {...@@ -177,7 +177,7 @@ fn makeBar(x: i32, y: i32) Bar {
177177
178test "empty struct method call" {178test "empty struct method call" {
179 const es = EmptyStruct{};179 const es = EmptyStruct{};
180 expect(es.method() == 1234);180 try expect(es.method() == 1234);
181}181}
182const EmptyStruct = struct {182const EmptyStruct = struct {
183 fn method(es: *const EmptyStruct) i32 {183 fn method(es: *const EmptyStruct) i32 {
...@@ -194,7 +194,7 @@ fn testReturnEmptyStructFromFn() EmptyStruct2 {...@@ -194,7 +194,7 @@ fn testReturnEmptyStructFromFn() EmptyStruct2 {
194}194}
195195
196test "pass slice of empty struct to fn" {196test "pass slice of empty struct to fn" {
197 expect(testPassSliceOfEmptyStructToFn(&[_]EmptyStruct2{EmptyStruct2{}}) == 1);197 try expect(testPassSliceOfEmptyStructToFn(&[_]EmptyStruct2{EmptyStruct2{}}) == 1);
198}198}
199fn testPassSliceOfEmptyStructToFn(slice: []const EmptyStruct2) usize {199fn testPassSliceOfEmptyStructToFn(slice: []const EmptyStruct2) usize {
200 return slice.len;200 return slice.len;
...@@ -212,7 +212,7 @@ test "packed struct" {...@@ -212,7 +212,7 @@ test "packed struct" {
212 };212 };
213 foo.y += 1;213 foo.y += 1;
214 const four = foo.x + foo.y;214 const four = foo.x + foo.y;
215 expect(four == 4);215 try expect(four == 4);
216}216}
217217
218const BitField1 = packed struct {218const BitField1 = packed struct {
...@@ -229,17 +229,17 @@ const bit_field_1 = BitField1{...@@ -229,17 +229,17 @@ const bit_field_1 = BitField1{
229229
230test "bit field access" {230test "bit field access" {
231 var data = bit_field_1;231 var data = bit_field_1;
232 expect(getA(&data) == 1);232 try expect(getA(&data) == 1);
233 expect(getB(&data) == 2);233 try expect(getB(&data) == 2);
234 expect(getC(&data) == 3);234 try expect(getC(&data) == 3);
235 comptime expect(@sizeOf(BitField1) == 1);235 comptime try expect(@sizeOf(BitField1) == 1);
236236
237 data.b += 1;237 data.b += 1;
238 expect(data.b == 3);238 try expect(data.b == 3);
239239
240 data.a += 1;240 data.a += 1;
241 expect(data.a == 2);241 try expect(data.a == 2);
242 expect(data.b == 3);242 try expect(data.b == 3);
243}243}
244244
245fn getA(data: *const BitField1) u3 {245fn getA(data: *const BitField1) u3 {
...@@ -266,11 +266,11 @@ const Foo96Bits = packed struct {...@@ -266,11 +266,11 @@ const Foo96Bits = packed struct {
266266
267test "packed struct 24bits" {267test "packed struct 24bits" {
268 comptime {268 comptime {
269 expect(@sizeOf(Foo24Bits) == 4);269 try expect(@sizeOf(Foo24Bits) == 4);
270 if (@sizeOf(usize) == 4) {270 if (@sizeOf(usize) == 4) {
271 expect(@sizeOf(Foo96Bits) == 12);271 try expect(@sizeOf(Foo96Bits) == 12);
272 } else {272 } else {
273 expect(@sizeOf(Foo96Bits) == 16);273 try expect(@sizeOf(Foo96Bits) == 16);
274 }274 }
275 }275 }
276276
...@@ -281,28 +281,28 @@ test "packed struct 24bits" {...@@ -281,28 +281,28 @@ test "packed struct 24bits" {
281 .d = 0,281 .d = 0,
282 };282 };
283 value.a += 1;283 value.a += 1;
284 expect(value.a == 1);284 try expect(value.a == 1);
285 expect(value.b == 0);285 try expect(value.b == 0);
286 expect(value.c == 0);286 try expect(value.c == 0);
287 expect(value.d == 0);287 try expect(value.d == 0);
288288
289 value.b += 1;289 value.b += 1;
290 expect(value.a == 1);290 try expect(value.a == 1);
291 expect(value.b == 1);291 try expect(value.b == 1);
292 expect(value.c == 0);292 try expect(value.c == 0);
293 expect(value.d == 0);293 try expect(value.d == 0);
294294
295 value.c += 1;295 value.c += 1;
296 expect(value.a == 1);296 try expect(value.a == 1);
297 expect(value.b == 1);297 try expect(value.b == 1);
298 expect(value.c == 1);298 try expect(value.c == 1);
299 expect(value.d == 0);299 try expect(value.d == 0);
300300
301 value.d += 1;301 value.d += 1;
302 expect(value.a == 1);302 try expect(value.a == 1);
303 expect(value.b == 1);303 try expect(value.b == 1);
304 expect(value.c == 1);304 try expect(value.c == 1);
305 expect(value.d == 1);305 try expect(value.d == 1);
306}306}
307307
308const Foo32Bits = packed struct {308const Foo32Bits = packed struct {
...@@ -319,43 +319,43 @@ const FooArray24Bits = packed struct {...@@ -319,43 +319,43 @@ const FooArray24Bits = packed struct {
319// TODO revisit this test when doing https://github.com/ziglang/zig/issues/1512319// TODO revisit this test when doing https://github.com/ziglang/zig/issues/1512
320test "packed array 24bits" {320test "packed array 24bits" {
321 comptime {321 comptime {
322 expect(@sizeOf([9]Foo32Bits) == 9 * 4);322 try expect(@sizeOf([9]Foo32Bits) == 9 * 4);
323 expect(@sizeOf(FooArray24Bits) == 2 + 2 * 4 + 2);323 try expect(@sizeOf(FooArray24Bits) == 2 + 2 * 4 + 2);
324 }324 }
325325
326 var bytes = [_]u8{0} ** (@sizeOf(FooArray24Bits) + 1);326 var bytes = [_]u8{0} ** (@sizeOf(FooArray24Bits) + 1);
327 bytes[bytes.len - 1] = 0xaa;327 bytes[bytes.len - 1] = 0xaa;
328 const ptr = &std.mem.bytesAsSlice(FooArray24Bits, bytes[0 .. bytes.len - 1])[0];328 const ptr = &std.mem.bytesAsSlice(FooArray24Bits, bytes[0 .. bytes.len - 1])[0];
329 expect(ptr.a == 0);329 try expect(ptr.a == 0);
330 expect(ptr.b[0].field == 0);330 try expect(ptr.b[0].field == 0);
331 expect(ptr.b[1].field == 0);331 try expect(ptr.b[1].field == 0);
332 expect(ptr.c == 0);332 try expect(ptr.c == 0);
333333
334 ptr.a = maxInt(u16);334 ptr.a = maxInt(u16);
335 expect(ptr.a == maxInt(u16));335 try expect(ptr.a == maxInt(u16));
336 expect(ptr.b[0].field == 0);336 try expect(ptr.b[0].field == 0);
337 expect(ptr.b[1].field == 0);337 try expect(ptr.b[1].field == 0);
338 expect(ptr.c == 0);338 try expect(ptr.c == 0);
339339
340 ptr.b[0].field = maxInt(u24);340 ptr.b[0].field = maxInt(u24);
341 expect(ptr.a == maxInt(u16));341 try expect(ptr.a == maxInt(u16));
342 expect(ptr.b[0].field == maxInt(u24));342 try expect(ptr.b[0].field == maxInt(u24));
343 expect(ptr.b[1].field == 0);343 try expect(ptr.b[1].field == 0);
344 expect(ptr.c == 0);344 try expect(ptr.c == 0);
345345
346 ptr.b[1].field = maxInt(u24);346 ptr.b[1].field = maxInt(u24);
347 expect(ptr.a == maxInt(u16));347 try expect(ptr.a == maxInt(u16));
348 expect(ptr.b[0].field == maxInt(u24));348 try expect(ptr.b[0].field == maxInt(u24));
349 expect(ptr.b[1].field == maxInt(u24));349 try expect(ptr.b[1].field == maxInt(u24));
350 expect(ptr.c == 0);350 try expect(ptr.c == 0);
351351
352 ptr.c = maxInt(u16);352 ptr.c = maxInt(u16);
353 expect(ptr.a == maxInt(u16));353 try expect(ptr.a == maxInt(u16));
354 expect(ptr.b[0].field == maxInt(u24));354 try expect(ptr.b[0].field == maxInt(u24));
355 expect(ptr.b[1].field == maxInt(u24));355 try expect(ptr.b[1].field == maxInt(u24));
356 expect(ptr.c == maxInt(u16));356 try expect(ptr.c == maxInt(u16));
357357
358 expect(bytes[bytes.len - 1] == 0xaa);358 try expect(bytes[bytes.len - 1] == 0xaa);
359}359}
360360
361const FooStructAligned = packed struct {361const FooStructAligned = packed struct {
...@@ -369,17 +369,17 @@ const FooArrayOfAligned = packed struct {...@@ -369,17 +369,17 @@ const FooArrayOfAligned = packed struct {
369369
370test "aligned array of packed struct" {370test "aligned array of packed struct" {
371 comptime {371 comptime {
372 expect(@sizeOf(FooStructAligned) == 2);372 try expect(@sizeOf(FooStructAligned) == 2);
373 expect(@sizeOf(FooArrayOfAligned) == 2 * 2);373 try expect(@sizeOf(FooArrayOfAligned) == 2 * 2);
374 }374 }
375375
376 var bytes = [_]u8{0xbb} ** @sizeOf(FooArrayOfAligned);376 var bytes = [_]u8{0xbb} ** @sizeOf(FooArrayOfAligned);
377 const ptr = &std.mem.bytesAsSlice(FooArrayOfAligned, bytes[0..])[0];377 const ptr = &std.mem.bytesAsSlice(FooArrayOfAligned, bytes[0..])[0];
378378
379 expect(ptr.a[0].a == 0xbb);379 try expect(ptr.a[0].a == 0xbb);
380 expect(ptr.a[0].b == 0xbb);380 try expect(ptr.a[0].b == 0xbb);
381 expect(ptr.a[1].a == 0xbb);381 try expect(ptr.a[1].a == 0xbb);
382 expect(ptr.a[1].b == 0xbb);382 try expect(ptr.a[1].b == 0xbb);
383}383}
384384
385test "runtime struct initialization of bitfield" {385test "runtime struct initialization of bitfield" {
...@@ -392,10 +392,10 @@ test "runtime struct initialization of bitfield" {...@@ -392,10 +392,10 @@ test "runtime struct initialization of bitfield" {
392 .y = @intCast(u4, x2),392 .y = @intCast(u4, x2),
393 };393 };
394394
395 expect(s1.x == x1);395 try expect(s1.x == x1);
396 expect(s1.y == x1);396 try expect(s1.y == x1);
397 expect(s2.x == @intCast(u4, x2));397 try expect(s2.x == @intCast(u4, x2));
398 expect(s2.y == @intCast(u4, x2));398 try expect(s2.y == @intCast(u4, x2));
399}399}
400400
401var x1 = @as(u4, 1);401var x1 = @as(u4, 1);
...@@ -425,18 +425,18 @@ test "native bit field understands endianness" {...@@ -425,18 +425,18 @@ test "native bit field understands endianness" {
425 @memcpy(&bytes, @ptrCast([*]u8, &all), 8);425 @memcpy(&bytes, @ptrCast([*]u8, &all), 8);
426 var bitfields = @ptrCast(*Bitfields, &bytes).*;426 var bitfields = @ptrCast(*Bitfields, &bytes).*;
427427
428 expect(bitfields.f1 == 0x1111);428 try expect(bitfields.f1 == 0x1111);
429 expect(bitfields.f2 == 0x2222);429 try expect(bitfields.f2 == 0x2222);
430 expect(bitfields.f3 == 0x33);430 try expect(bitfields.f3 == 0x33);
431 expect(bitfields.f4 == 0x44);431 try expect(bitfields.f4 == 0x44);
432 expect(bitfields.f5 == 0x5);432 try expect(bitfields.f5 == 0x5);
433 expect(bitfields.f6 == 0x6);433 try expect(bitfields.f6 == 0x6);
434 expect(bitfields.f7 == 0x77);434 try expect(bitfields.f7 == 0x77);
435}435}
436436
437test "align 1 field before self referential align 8 field as slice return type" {437test "align 1 field before self referential align 8 field as slice return type" {
438 const result = alloc(Expr);438 const result = alloc(Expr);
439 expect(result.len == 0);439 try expect(result.len == 0);
440}440}
441441
442const Expr = union(enum) {442const Expr = union(enum) {
...@@ -459,10 +459,10 @@ test "call method with mutable reference to struct with no fields" {...@@ -459,10 +459,10 @@ test "call method with mutable reference to struct with no fields" {
459 };459 };
460460
461 var s = S{};461 var s = S{};
462 expect(S.doC(&s));462 try expect(S.doC(&s));
463 expect(s.doC());463 try expect(s.doC());
464 expect(S.do(&s));464 try expect(S.do(&s));
465 expect(s.do());465 try expect(s.do());
466}466}
467467
468test "implicit cast packed struct field to const ptr" {468test "implicit cast packed struct field to const ptr" {
...@@ -478,7 +478,7 @@ test "implicit cast packed struct field to const ptr" {...@@ -478,7 +478,7 @@ test "implicit cast packed struct field to const ptr" {
478 var lup: LevelUpMove = undefined;478 var lup: LevelUpMove = undefined;
479 lup.level = 12;479 lup.level = 12;
480 const res = LevelUpMove.toInt(lup.level);480 const res = LevelUpMove.toInt(lup.level);
481 expect(res == 12);481 try expect(res == 12);
482}482}
483483
484test "pointer to packed struct member in a stack variable" {484test "pointer to packed struct member in a stack variable" {
...@@ -489,9 +489,9 @@ test "pointer to packed struct member in a stack variable" {...@@ -489,9 +489,9 @@ test "pointer to packed struct member in a stack variable" {
489489
490 var s = S{ .a = 2, .b = 0 };490 var s = S{ .a = 2, .b = 0 };
491 var b_ptr = &s.b;491 var b_ptr = &s.b;
492 expect(s.b == 0);492 try expect(s.b == 0);
493 b_ptr.* = 2;493 b_ptr.* = 2;
494 expect(s.b == 2);494 try expect(s.b == 2);
495}495}
496496
497test "non-byte-aligned array inside packed struct" {497test "non-byte-aligned array inside packed struct" {
...@@ -500,20 +500,20 @@ test "non-byte-aligned array inside packed struct" {...@@ -500,20 +500,20 @@ test "non-byte-aligned array inside packed struct" {
500 b: [0x16]u8,500 b: [0x16]u8,
501 };501 };
502 const S = struct {502 const S = struct {
503 fn bar(slice: []const u8) void {503 fn bar(slice: []const u8) !void {
504 expectEqualSlices(u8, slice, "abcdefghijklmnopqurstu");504 try expectEqualSlices(u8, slice, "abcdefghijklmnopqurstu");
505 }505 }
506 fn doTheTest() void {506 fn doTheTest() !void {
507 var foo = Foo{507 var foo = Foo{
508 .a = true,508 .a = true,
509 .b = "abcdefghijklmnopqurstu".*,509 .b = "abcdefghijklmnopqurstu".*,
510 };510 };
511 const value = foo.b;511 const value = foo.b;
512 bar(&value);512 try bar(&value);
513 }513 }
514 };514 };
515 S.doTheTest();515 try S.doTheTest();
516 comptime S.doTheTest();516 comptime try S.doTheTest();
517}517}
518518
519test "packed struct with u0 field access" {519test "packed struct with u0 field access" {
...@@ -521,7 +521,7 @@ test "packed struct with u0 field access" {...@@ -521,7 +521,7 @@ test "packed struct with u0 field access" {
521 f0: u0,521 f0: u0,
522 };522 };
523 var s = S{ .f0 = 0 };523 var s = S{ .f0 = 0 };
524 comptime expect(s.f0 == 0);524 comptime try expect(s.f0 == 0);
525}525}
526526
527const S0 = struct {527const S0 = struct {
...@@ -540,7 +540,7 @@ var g_foo: S0 = S0.init();...@@ -540,7 +540,7 @@ var g_foo: S0 = S0.init();
540540
541test "access to global struct fields" {541test "access to global struct fields" {
542 g_foo.bar.value = 42;542 g_foo.bar.value = 42;
543 expect(g_foo.bar.value == 42);543 try expect(g_foo.bar.value == 42);
544}544}
545545
546test "packed struct with fp fields" {546test "packed struct with fp fields" {
...@@ -559,9 +559,9 @@ test "packed struct with fp fields" {...@@ -559,9 +559,9 @@ test "packed struct with fp fields" {
559 s.data[1] = 2.0;559 s.data[1] = 2.0;
560 s.data[2] = 3.0;560 s.data[2] = 3.0;
561 s.frob();561 s.frob();
562 expectEqual(@as(f32, 6.0), s.data[0]);562 try expectEqual(@as(f32, 6.0), s.data[0]);
563 expectEqual(@as(f32, 11.0), s.data[1]);563 try expectEqual(@as(f32, 11.0), s.data[1]);
564 expectEqual(@as(f32, 20.0), s.data[2]);564 try expectEqual(@as(f32, 20.0), s.data[2]);
565}565}
566566
567test "use within struct scope" {567test "use within struct scope" {
...@@ -572,7 +572,7 @@ test "use within struct scope" {...@@ -572,7 +572,7 @@ test "use within struct scope" {
572 }572 }
573 };573 };
574 };574 };
575 expectEqual(@as(i32, 42), S.inner());575 try expectEqual(@as(i32, 42), S.inner());
576}576}
577577
578test "default struct initialization fields" {578test "default struct initialization fields" {
...@@ -590,14 +590,14 @@ test "default struct initialization fields" {...@@ -590,14 +590,14 @@ test "default struct initialization fields" {
590 const y = S{590 const y = S{
591 .b = five,591 .b = five,
592 };592 };
593 expectEqual(1239, x.a + x.b);593 try expectEqual(1239, x.a + x.b);
594}594}
595595
596test "fn with C calling convention returns struct by value" {596test "fn with C calling convention returns struct by value" {
597 const S = struct {597 const S = struct {
598 fn entry() void {598 fn entry() !void {
599 var x = makeBar(10);599 var x = makeBar(10);
600 expectEqual(@as(i32, 10), x.handle);600 try expectEqual(@as(i32, 10), x.handle);
601 }601 }
602602
603 const ExternBar = extern struct {603 const ExternBar = extern struct {
...@@ -610,8 +610,8 @@ test "fn with C calling convention returns struct by value" {...@@ -610,8 +610,8 @@ test "fn with C calling convention returns struct by value" {
610 };610 };
611 }611 }
612 };612 };
613 S.entry();613 try S.entry();
614 comptime S.entry();614 comptime try S.entry();
615}615}
616616
617test "for loop over pointers to struct, getting field from struct pointer" {617test "for loop over pointers to struct, getting field from struct pointer" {
...@@ -632,7 +632,7 @@ test "for loop over pointers to struct, getting field from struct pointer" {...@@ -632,7 +632,7 @@ test "for loop over pointers to struct, getting field from struct pointer" {
632 }632 }
633 };633 };
634634
635 fn doTheTest() void {635 fn doTheTest() !void {
636 var objects: ArrayList = undefined;636 var objects: ArrayList = undefined;
637637
638 for (objects.toSlice()) |obj| {638 for (objects.toSlice()) |obj| {
...@@ -641,10 +641,10 @@ test "for loop over pointers to struct, getting field from struct pointer" {...@@ -641,10 +641,10 @@ test "for loop over pointers to struct, getting field from struct pointer" {
641 }641 }
642 }642 }
643643
644 expect(ok);644 try expect(ok);
645 }645 }
646 };646 };
647 S.doTheTest();647 try S.doTheTest();
648}648}
649649
650test "zero-bit field in packed struct" {650test "zero-bit field in packed struct" {
...@@ -657,20 +657,20 @@ test "zero-bit field in packed struct" {...@@ -657,20 +657,20 @@ test "zero-bit field in packed struct" {
657657
658test "struct field init with catch" {658test "struct field init with catch" {
659 const S = struct {659 const S = struct {
660 fn doTheTest() void {660 fn doTheTest() !void {
661 var x: anyerror!isize = 1;661 var x: anyerror!isize = 1;
662 var req = Foo{662 var req = Foo{
663 .field = x catch undefined,663 .field = x catch undefined,
664 };664 };
665 expect(req.field == 1);665 try expect(req.field == 1);
666 }666 }
667667
668 pub const Foo = extern struct {668 pub const Foo = extern struct {
669 field: isize,669 field: isize,
670 };670 };
671 };671 };
672 S.doTheTest();672 try S.doTheTest();
673 comptime S.doTheTest();673 comptime try S.doTheTest();
674}674}
675675
676test "packed struct with non-ABI-aligned field" {676test "packed struct with non-ABI-aligned field" {
...@@ -681,8 +681,8 @@ test "packed struct with non-ABI-aligned field" {...@@ -681,8 +681,8 @@ test "packed struct with non-ABI-aligned field" {
681 var s: S = undefined;681 var s: S = undefined;
682 s.x = 1;682 s.x = 1;
683 s.y = 42;683 s.y = 42;
684 expect(s.x == 1);684 try expect(s.x == 1);
685 expect(s.y == 42);685 try expect(s.y == 42);
686}686}
687687
688test "non-packed struct with u128 entry in union" {688test "non-packed struct with u128 entry in union" {
...@@ -698,10 +698,10 @@ test "non-packed struct with u128 entry in union" {...@@ -698,10 +698,10 @@ test "non-packed struct with u128 entry in union" {
698698
699 var sx: S = undefined;699 var sx: S = undefined;
700 var s = &sx;700 var s = &sx;
701 std.testing.expect(@ptrToInt(&s.f2) - @ptrToInt(&s.f1) == @byteOffsetOf(S, "f2"));701 try std.testing.expect(@ptrToInt(&s.f2) - @ptrToInt(&s.f1) == @byteOffsetOf(S, "f2"));
702 var v2 = U{ .Num = 123 };702 var v2 = U{ .Num = 123 };
703 s.f2 = v2;703 s.f2 = v2;
704 std.testing.expect(s.f2.Num == 123);704 try std.testing.expect(s.f2.Num == 123);
705}705}
706706
707test "packed struct field passed to generic function" {707test "packed struct field passed to generic function" {
...@@ -721,7 +721,7 @@ test "packed struct field passed to generic function" {...@@ -721,7 +721,7 @@ test "packed struct field passed to generic function" {
721 var p: S.P = undefined;721 var p: S.P = undefined;
722 p.b = 29;722 p.b = 29;
723 var loaded = S.genericReadPackedField(&p.b);723 var loaded = S.genericReadPackedField(&p.b);
724 expect(loaded == 29);724 try expect(loaded == 29);
725}725}
726726
727test "anonymous struct literal syntax" {727test "anonymous struct literal syntax" {
...@@ -731,63 +731,63 @@ test "anonymous struct literal syntax" {...@@ -731,63 +731,63 @@ test "anonymous struct literal syntax" {
731 y: i32,731 y: i32,
732 };732 };
733733
734 fn doTheTest() void {734 fn doTheTest() !void {
735 var p: Point = .{735 var p: Point = .{
736 .x = 1,736 .x = 1,
737 .y = 2,737 .y = 2,
738 };738 };
739 expect(p.x == 1);739 try expect(p.x == 1);
740 expect(p.y == 2);740 try expect(p.y == 2);
741 }741 }
742 };742 };
743 S.doTheTest();743 try S.doTheTest();
744 comptime S.doTheTest();744 comptime try S.doTheTest();
745}745}
746746
747test "fully anonymous struct" {747test "fully anonymous struct" {
748 const S = struct {748 const S = struct {
749 fn doTheTest() void {749 fn doTheTest() !void {
750 dump(.{750 try dump(.{
751 .int = @as(u32, 1234),751 .int = @as(u32, 1234),
752 .float = @as(f64, 12.34),752 .float = @as(f64, 12.34),
753 .b = true,753 .b = true,
754 .s = "hi",754 .s = "hi",
755 });755 });
756 }756 }
757 fn dump(args: anytype) void {757 fn dump(args: anytype) !void {
758 expect(args.int == 1234);758 try expect(args.int == 1234);
759 expect(args.float == 12.34);759 try expect(args.float == 12.34);
760 expect(args.b);760 try expect(args.b);
761 expect(args.s[0] == 'h');761 try expect(args.s[0] == 'h');
762 expect(args.s[1] == 'i');762 try expect(args.s[1] == 'i');
763 }763 }
764 };764 };
765 S.doTheTest();765 try S.doTheTest();
766 comptime S.doTheTest();766 comptime try S.doTheTest();
767}767}
768768
769test "fully anonymous list literal" {769test "fully anonymous list literal" {
770 const S = struct {770 const S = struct {
771 fn doTheTest() void {771 fn doTheTest() !void {
772 dump(.{ @as(u32, 1234), @as(f64, 12.34), true, "hi" });772 try dump(.{ @as(u32, 1234), @as(f64, 12.34), true, "hi" });
773 }773 }
774 fn dump(args: anytype) void {774 fn dump(args: anytype) !void {
775 expect(args.@"0" == 1234);775 try expect(args.@"0" == 1234);
776 expect(args.@"1" == 12.34);776 try expect(args.@"1" == 12.34);
777 expect(args.@"2");777 try expect(args.@"2");
778 expect(args.@"3"[0] == 'h');778 try expect(args.@"3"[0] == 'h');
779 expect(args.@"3"[1] == 'i');779 try expect(args.@"3"[1] == 'i');
780 }780 }
781 };781 };
782 S.doTheTest();782 try S.doTheTest();
783 comptime S.doTheTest();783 comptime try S.doTheTest();
784}784}
785785
786test "anonymous struct literal assigned to variable" {786test "anonymous struct literal assigned to variable" {
787 var vec = .{ @as(i32, 22), @as(i32, 55), @as(i32, 99) };787 var vec = .{ @as(i32, 22), @as(i32, 55), @as(i32, 99) };
788 expect(vec.@"0" == 22);788 try expect(vec.@"0" == 22);
789 expect(vec.@"1" == 55);789 try expect(vec.@"1" == 55);
790 expect(vec.@"2" == 99);790 try expect(vec.@"2" == 99);
791}791}
792792
793test "struct with var field" {793test "struct with var field" {
...@@ -799,8 +799,8 @@ test "struct with var field" {...@@ -799,8 +799,8 @@ test "struct with var field" {
799 .x = 1,799 .x = 1,
800 .y = 2,800 .y = 2,
801 };801 };
802 expect(pt.x == 1);802 try expect(pt.x == 1);
803 expect(pt.y == 2);803 try expect(pt.y == 2);
804}804}
805805
806test "comptime struct field" {806test "comptime struct field" {
...@@ -810,21 +810,21 @@ test "comptime struct field" {...@@ -810,21 +810,21 @@ test "comptime struct field" {
810 };810 };
811811
812 var foo: T = undefined;812 var foo: T = undefined;
813 comptime expect(foo.b == 1234);813 comptime try expect(foo.b == 1234);
814}814}
815815
816test "anon struct literal field value initialized with fn call" {816test "anon struct literal field value initialized with fn call" {
817 const S = struct {817 const S = struct {
818 fn doTheTest() void {818 fn doTheTest() !void {
819 var x = .{foo()};819 var x = .{foo()};
820 expectEqualSlices(u8, x[0], "hi");820 try expectEqualSlices(u8, x[0], "hi");
821 }821 }
822 fn foo() []const u8 {822 fn foo() []const u8 {
823 return "hi";823 return "hi";
824 }824 }
825 };825 };
826 S.doTheTest();826 try S.doTheTest();
827 comptime S.doTheTest();827 comptime try S.doTheTest();
828}828}
829829
830test "self-referencing struct via array member" {830test "self-referencing struct via array member" {
...@@ -833,7 +833,7 @@ test "self-referencing struct via array member" {...@@ -833,7 +833,7 @@ test "self-referencing struct via array member" {
833 };833 };
834 var x: T = undefined;834 var x: T = undefined;
835 x = T{ .children = .{&x} };835 x = T{ .children = .{&x} };
836 expect(x.children[0] == &x);836 try expect(x.children[0] == &x);
837}837}
838838
839test "struct with union field" {839test "struct with union field" {
...@@ -848,8 +848,8 @@ test "struct with union field" {...@@ -848,8 +848,8 @@ test "struct with union field" {
848 var True = Value{848 var True = Value{
849 .kind = .{ .Bool = true },849 .kind = .{ .Bool = true },
850 };850 };
851 expectEqual(@as(u32, 2), True.ref);851 try expectEqual(@as(u32, 2), True.ref);
852 expectEqual(true, True.kind.Bool);852 try expectEqual(true, True.kind.Bool);
853}853}
854854
855test "type coercion of anon struct literal to struct" {855test "type coercion of anon struct literal to struct" {
...@@ -865,24 +865,24 @@ test "type coercion of anon struct literal to struct" {...@@ -865,24 +865,24 @@ test "type coercion of anon struct literal to struct" {
865 field: i32 = 1234,865 field: i32 = 1234,
866 };866 };
867867
868 fn doTheTest() void {868 fn doTheTest() !void {
869 var y: u32 = 42;869 var y: u32 = 42;
870 const t0 = .{ .A = 123, .B = "foo", .C = {} };870 const t0 = .{ .A = 123, .B = "foo", .C = {} };
871 const t1 = .{ .A = y, .B = "foo", .C = {} };871 const t1 = .{ .A = y, .B = "foo", .C = {} };
872 const y0: S2 = t0;872 const y0: S2 = t0;
873 var y1: S2 = t1;873 var y1: S2 = t1;
874 expect(y0.A == 123);874 try expect(y0.A == 123);
875 expect(std.mem.eql(u8, y0.B, "foo"));875 try expect(std.mem.eql(u8, y0.B, "foo"));
876 expect(y0.C == {});876 try expect(y0.C == {});
877 expect(y0.D.field == 1234);877 try expect(y0.D.field == 1234);
878 expect(y1.A == y);878 try expect(y1.A == y);
879 expect(std.mem.eql(u8, y1.B, "foo"));879 try expect(std.mem.eql(u8, y1.B, "foo"));
880 expect(y1.C == {});880 try expect(y1.C == {});
881 expect(y1.D.field == 1234);881 try expect(y1.D.field == 1234);
882 }882 }
883 };883 };
884 S.doTheTest();884 try S.doTheTest();
885 comptime S.doTheTest();885 comptime try S.doTheTest();
886}886}
887887
888test "type coercion of pointer to anon struct literal to pointer to struct" {888test "type coercion of pointer to anon struct literal to pointer to struct" {
...@@ -898,24 +898,24 @@ test "type coercion of pointer to anon struct literal to pointer to struct" {...@@ -898,24 +898,24 @@ test "type coercion of pointer to anon struct literal to pointer to struct" {
898 field: i32 = 1234,898 field: i32 = 1234,
899 };899 };
900900
901 fn doTheTest() void {901 fn doTheTest() !void {
902 var y: u32 = 42;902 var y: u32 = 42;
903 const t0 = &.{ .A = 123, .B = "foo", .C = {} };903 const t0 = &.{ .A = 123, .B = "foo", .C = {} };
904 const t1 = &.{ .A = y, .B = "foo", .C = {} };904 const t1 = &.{ .A = y, .B = "foo", .C = {} };
905 const y0: *const S2 = t0;905 const y0: *const S2 = t0;
906 var y1: *const S2 = t1;906 var y1: *const S2 = t1;
907 expect(y0.A == 123);907 try expect(y0.A == 123);
908 expect(std.mem.eql(u8, y0.B, "foo"));908 try expect(std.mem.eql(u8, y0.B, "foo"));
909 expect(y0.C == {});909 try expect(y0.C == {});
910 expect(y0.D.field == 1234);910 try expect(y0.D.field == 1234);
911 expect(y1.A == y);911 try expect(y1.A == y);
912 expect(std.mem.eql(u8, y1.B, "foo"));912 try expect(std.mem.eql(u8, y1.B, "foo"));
913 expect(y1.C == {});913 try expect(y1.C == {});
914 expect(y1.D.field == 1234);914 try expect(y1.D.field == 1234);
915 }915 }
916 };916 };
917 S.doTheTest();917 try S.doTheTest();
918 comptime S.doTheTest();918 comptime try S.doTheTest();
919}919}
920920
921test "packed struct with undefined initializers" {921test "packed struct with undefined initializers" {
...@@ -929,16 +929,16 @@ test "packed struct with undefined initializers" {...@@ -929,16 +929,16 @@ test "packed struct with undefined initializers" {
929 _c: u3 = undefined,929 _c: u3 = undefined,
930 };930 };
931931
932 fn doTheTest() void {932 fn doTheTest() !void {
933 var p: P = undefined;933 var p: P = undefined;
934 p = P{ .a = 2, .b = 4, .c = 6 };934 p = P{ .a = 2, .b = 4, .c = 6 };
935 // Make sure the compiler doesn't touch the unprefixed fields.935 // Make sure the compiler doesn't touch the unprefixed fields.
936 expectEqual(@as(u3, 2), p.a);936 try expectEqual(@as(u3, 2), p.a);
937 expectEqual(@as(u3, 4), p.b);937 try expectEqual(@as(u3, 4), p.b);
938 expectEqual(@as(u3, 6), p.c);938 try expectEqual(@as(u3, 6), p.c);
939 }939 }
940 };940 };
941941
942 S.doTheTest();942 try S.doTheTest();
943 comptime S.doTheTest();943 comptime try S.doTheTest();
944}944}
test/stage1/behavior/struct_contains_null_ptr_itself.zig+1-1
...@@ -3,7 +3,7 @@ const expect = std.testing.expect;...@@ -3,7 +3,7 @@ const expect = std.testing.expect;
33
4test "struct contains null pointer which contains original struct" {4test "struct contains null pointer which contains original struct" {
5 var x: ?*NodeLineComment = null;5 var x: ?*NodeLineComment = null;
6 expect(x == null);6 try expect(x == null);
7}7}
88
9pub const Node = struct {9pub const Node = struct {
test/stage1/behavior/struct_contains_slice_of_itself.zig+12-12
...@@ -39,12 +39,12 @@ test "struct contains slice of itself" {...@@ -39,12 +39,12 @@ test "struct contains slice of itself" {
39 .payload = 1234,39 .payload = 1234,
40 .children = nodes[0..],40 .children = nodes[0..],
41 };41 };
42 expect(root.payload == 1234);42 try expect(root.payload == 1234);
43 expect(root.children[0].payload == 1);43 try expect(root.children[0].payload == 1);
44 expect(root.children[1].payload == 2);44 try expect(root.children[1].payload == 2);
45 expect(root.children[2].payload == 3);45 try expect(root.children[2].payload == 3);
46 expect(root.children[2].children[0].payload == 31);46 try expect(root.children[2].children[0].payload == 31);
47 expect(root.children[2].children[1].payload == 32);47 try expect(root.children[2].children[1].payload == 32);
48}48}
4949
50test "struct contains aligned slice of itself" {50test "struct contains aligned slice of itself" {
...@@ -76,10 +76,10 @@ test "struct contains aligned slice of itself" {...@@ -76,10 +76,10 @@ test "struct contains aligned slice of itself" {
76 .payload = 1234,76 .payload = 1234,
77 .children = nodes[0..],77 .children = nodes[0..],
78 };78 };
79 expect(root.payload == 1234);79 try expect(root.payload == 1234);
80 expect(root.children[0].payload == 1);80 try expect(root.children[0].payload == 1);
81 expect(root.children[1].payload == 2);81 try expect(root.children[1].payload == 2);
82 expect(root.children[2].payload == 3);82 try expect(root.children[2].payload == 3);
83 expect(root.children[2].children[0].payload == 31);83 try expect(root.children[2].children[0].payload == 31);
84 expect(root.children[2].children[1].payload == 32);84 try expect(root.children[2].children[1].payload == 32);
85}85}
test/stage1/behavior/switch.zig+104-104
...@@ -4,23 +4,23 @@ const expectError = std.testing.expectError;...@@ -4,23 +4,23 @@ const expectError = std.testing.expectError;
4const expectEqual = std.testing.expectEqual;4const expectEqual = std.testing.expectEqual;
55
6test "switch with numbers" {6test "switch with numbers" {
7 testSwitchWithNumbers(13);7 try testSwitchWithNumbers(13);
8}8}
99
10fn testSwitchWithNumbers(x: u32) void {10fn testSwitchWithNumbers(x: u32) !void {
11 const result = switch (x) {11 const result = switch (x) {
12 1, 2, 3, 4...8 => false,12 1, 2, 3, 4...8 => false,
13 13 => true,13 13 => true,
14 else => false,14 else => false,
15 };15 };
16 expect(result);16 try expect(result);
17}17}
1818
19test "switch with all ranges" {19test "switch with all ranges" {
20 expect(testSwitchWithAllRanges(50, 3) == 1);20 try expect(testSwitchWithAllRanges(50, 3) == 1);
21 expect(testSwitchWithAllRanges(101, 0) == 2);21 try expect(testSwitchWithAllRanges(101, 0) == 2);
22 expect(testSwitchWithAllRanges(300, 5) == 3);22 try expect(testSwitchWithAllRanges(300, 5) == 3);
23 expect(testSwitchWithAllRanges(301, 6) == 6);23 try expect(testSwitchWithAllRanges(301, 6) == 6);
24}24}
2525
26fn testSwitchWithAllRanges(x: u32, y: u32) u32 {26fn testSwitchWithAllRanges(x: u32, y: u32) u32 {
...@@ -43,7 +43,7 @@ test "implicit comptime switch" {...@@ -43,7 +43,7 @@ test "implicit comptime switch" {
43 };43 };
4444
45 comptime {45 comptime {
46 expect(result + 1 == 14);46 try expect(result + 1 == 14);
47 }47 }
48}48}
4949
...@@ -65,16 +65,16 @@ fn nonConstSwitchOnEnum(fruit: Fruit) void {...@@ -65,16 +65,16 @@ fn nonConstSwitchOnEnum(fruit: Fruit) void {
65}65}
6666
67test "switch statement" {67test "switch statement" {
68 nonConstSwitch(SwitchStatmentFoo.C);68 try nonConstSwitch(SwitchStatmentFoo.C);
69}69}
70fn nonConstSwitch(foo: SwitchStatmentFoo) void {70fn nonConstSwitch(foo: SwitchStatmentFoo) !void {
71 const val = switch (foo) {71 const val = switch (foo) {
72 SwitchStatmentFoo.A => @as(i32, 1),72 SwitchStatmentFoo.A => @as(i32, 1),
73 SwitchStatmentFoo.B => 2,73 SwitchStatmentFoo.B => 2,
74 SwitchStatmentFoo.C => 3,74 SwitchStatmentFoo.C => 3,
75 SwitchStatmentFoo.D => 4,75 SwitchStatmentFoo.D => 4,
76 };76 };
77 expect(val == 3);77 try expect(val == 3);
78}78}
79const SwitchStatmentFoo = enum {79const SwitchStatmentFoo = enum {
80 A,80 A,
...@@ -84,22 +84,22 @@ const SwitchStatmentFoo = enum {...@@ -84,22 +84,22 @@ const SwitchStatmentFoo = enum {
84};84};
8585
86test "switch prong with variable" {86test "switch prong with variable" {
87 switchProngWithVarFn(SwitchProngWithVarEnum{ .One = 13 });87 try switchProngWithVarFn(SwitchProngWithVarEnum{ .One = 13 });
88 switchProngWithVarFn(SwitchProngWithVarEnum{ .Two = 13.0 });88 try switchProngWithVarFn(SwitchProngWithVarEnum{ .Two = 13.0 });
89 switchProngWithVarFn(SwitchProngWithVarEnum{ .Meh = {} });89 try switchProngWithVarFn(SwitchProngWithVarEnum{ .Meh = {} });
90}90}
91const SwitchProngWithVarEnum = union(enum) {91const SwitchProngWithVarEnum = union(enum) {
92 One: i32,92 One: i32,
93 Two: f32,93 Two: f32,
94 Meh: void,94 Meh: void,
95};95};
96fn switchProngWithVarFn(a: SwitchProngWithVarEnum) void {96fn switchProngWithVarFn(a: SwitchProngWithVarEnum) !void {
97 switch (a) {97 switch (a) {
98 SwitchProngWithVarEnum.One => |x| {98 SwitchProngWithVarEnum.One => |x| {
99 expect(x == 13);99 try expect(x == 13);
100 },100 },
101 SwitchProngWithVarEnum.Two => |x| {101 SwitchProngWithVarEnum.Two => |x| {
102 expect(x == 13.0);102 try expect(x == 13.0);
103 },103 },
104 SwitchProngWithVarEnum.Meh => |x| {104 SwitchProngWithVarEnum.Meh => |x| {
105 const v: void = x;105 const v: void = x;
...@@ -108,18 +108,18 @@ fn switchProngWithVarFn(a: SwitchProngWithVarEnum) void {...@@ -108,18 +108,18 @@ fn switchProngWithVarFn(a: SwitchProngWithVarEnum) void {
108}108}
109109
110test "switch on enum using pointer capture" {110test "switch on enum using pointer capture" {
111 testSwitchEnumPtrCapture();111 try testSwitchEnumPtrCapture();
112 comptime testSwitchEnumPtrCapture();112 comptime try testSwitchEnumPtrCapture();
113}113}
114114
115fn testSwitchEnumPtrCapture() void {115fn testSwitchEnumPtrCapture() !void {
116 var value = SwitchProngWithVarEnum{ .One = 1234 };116 var value = SwitchProngWithVarEnum{ .One = 1234 };
117 switch (value) {117 switch (value) {
118 SwitchProngWithVarEnum.One => |*x| x.* += 1,118 SwitchProngWithVarEnum.One => |*x| x.* += 1,
119 else => unreachable,119 else => unreachable,
120 }120 }
121 switch (value) {121 switch (value) {
122 SwitchProngWithVarEnum.One => |x| expect(x == 1235),122 SwitchProngWithVarEnum.One => |x| try expect(x == 1235),
123 else => unreachable,123 else => unreachable,
124 }124 }
125}125}
...@@ -130,7 +130,7 @@ test "switch with multiple expressions" {...@@ -130,7 +130,7 @@ test "switch with multiple expressions" {
130 4, 5, 6 => 2,130 4, 5, 6 => 2,
131 else => @as(i32, 3),131 else => @as(i32, 3),
132 };132 };
133 expect(x == 2);133 try expect(x == 2);
134}134}
135fn returnsFive() i32 {135fn returnsFive() i32 {
136 return 5;136 return 5;
...@@ -152,12 +152,12 @@ fn returnsFalse() bool {...@@ -152,12 +152,12 @@ fn returnsFalse() bool {
152 }152 }
153}153}
154test "switch on const enum with var" {154test "switch on const enum with var" {
155 expect(!returnsFalse());155 try expect(!returnsFalse());
156}156}
157157
158test "switch on type" {158test "switch on type" {
159 expect(trueIfBoolFalseOtherwise(bool));159 try expect(trueIfBoolFalseOtherwise(bool));
160 expect(!trueIfBoolFalseOtherwise(i32));160 try expect(!trueIfBoolFalseOtherwise(i32));
161}161}
162162
163fn trueIfBoolFalseOtherwise(comptime T: type) bool {163fn trueIfBoolFalseOtherwise(comptime T: type) bool {
...@@ -168,21 +168,21 @@ fn trueIfBoolFalseOtherwise(comptime T: type) bool {...@@ -168,21 +168,21 @@ fn trueIfBoolFalseOtherwise(comptime T: type) bool {
168}168}
169169
170test "switch handles all cases of number" {170test "switch handles all cases of number" {
171 testSwitchHandleAllCases();171 try testSwitchHandleAllCases();
172 comptime testSwitchHandleAllCases();172 comptime try testSwitchHandleAllCases();
173}173}
174174
175fn testSwitchHandleAllCases() void {175fn testSwitchHandleAllCases() !void {
176 expect(testSwitchHandleAllCasesExhaustive(0) == 3);176 try expect(testSwitchHandleAllCasesExhaustive(0) == 3);
177 expect(testSwitchHandleAllCasesExhaustive(1) == 2);177 try expect(testSwitchHandleAllCasesExhaustive(1) == 2);
178 expect(testSwitchHandleAllCasesExhaustive(2) == 1);178 try expect(testSwitchHandleAllCasesExhaustive(2) == 1);
179 expect(testSwitchHandleAllCasesExhaustive(3) == 0);179 try expect(testSwitchHandleAllCasesExhaustive(3) == 0);
180180
181 expect(testSwitchHandleAllCasesRange(100) == 0);181 try expect(testSwitchHandleAllCasesRange(100) == 0);
182 expect(testSwitchHandleAllCasesRange(200) == 1);182 try expect(testSwitchHandleAllCasesRange(200) == 1);
183 expect(testSwitchHandleAllCasesRange(201) == 2);183 try expect(testSwitchHandleAllCasesRange(201) == 2);
184 expect(testSwitchHandleAllCasesRange(202) == 4);184 try expect(testSwitchHandleAllCasesRange(202) == 4);
185 expect(testSwitchHandleAllCasesRange(230) == 3);185 try expect(testSwitchHandleAllCasesRange(230) == 3);
186}186}
187187
188fn testSwitchHandleAllCasesExhaustive(x: u2) u2 {188fn testSwitchHandleAllCasesExhaustive(x: u2) u2 {
...@@ -205,13 +205,13 @@ fn testSwitchHandleAllCasesRange(x: u8) u8 {...@@ -205,13 +205,13 @@ fn testSwitchHandleAllCasesRange(x: u8) u8 {
205}205}
206206
207test "switch all prongs unreachable" {207test "switch all prongs unreachable" {
208 testAllProngsUnreachable();208 try testAllProngsUnreachable();
209 comptime testAllProngsUnreachable();209 comptime try testAllProngsUnreachable();
210}210}
211211
212fn testAllProngsUnreachable() void {212fn testAllProngsUnreachable() !void {
213 expect(switchWithUnreachable(1) == 2);213 try expect(switchWithUnreachable(1) == 2);
214 expect(switchWithUnreachable(2) == 10);214 try expect(switchWithUnreachable(2) == 10);
215}215}
216216
217fn switchWithUnreachable(x: i32) i32 {217fn switchWithUnreachable(x: i32) i32 {
...@@ -233,23 +233,23 @@ test "capture value of switch with all unreachable prongs" {...@@ -233,23 +233,23 @@ test "capture value of switch with all unreachable prongs" {
233 const x = return_a_number() catch |err| switch (err) {233 const x = return_a_number() catch |err| switch (err) {
234 else => unreachable,234 else => unreachable,
235 };235 };
236 expect(x == 1);236 try expect(x == 1);
237}237}
238238
239test "switching on booleans" {239test "switching on booleans" {
240 testSwitchOnBools();240 try testSwitchOnBools();
241 comptime testSwitchOnBools();241 comptime try testSwitchOnBools();
242}242}
243243
244fn testSwitchOnBools() void {244fn testSwitchOnBools() !void {
245 expect(testSwitchOnBoolsTrueAndFalse(true) == false);245 try expect(testSwitchOnBoolsTrueAndFalse(true) == false);
246 expect(testSwitchOnBoolsTrueAndFalse(false) == true);246 try expect(testSwitchOnBoolsTrueAndFalse(false) == true);
247247
248 expect(testSwitchOnBoolsTrueWithElse(true) == false);248 try expect(testSwitchOnBoolsTrueWithElse(true) == false);
249 expect(testSwitchOnBoolsTrueWithElse(false) == true);249 try expect(testSwitchOnBoolsTrueWithElse(false) == true);
250250
251 expect(testSwitchOnBoolsFalseWithElse(true) == false);251 try expect(testSwitchOnBoolsFalseWithElse(true) == false);
252 expect(testSwitchOnBoolsFalseWithElse(false) == true);252 try expect(testSwitchOnBoolsFalseWithElse(false) == true);
253}253}
254254
255fn testSwitchOnBoolsTrueAndFalse(x: bool) bool {255fn testSwitchOnBoolsTrueAndFalse(x: bool) bool {
...@@ -276,14 +276,14 @@ fn testSwitchOnBoolsFalseWithElse(x: bool) bool {...@@ -276,14 +276,14 @@ fn testSwitchOnBoolsFalseWithElse(x: bool) bool {
276test "u0" {276test "u0" {
277 var val: u0 = 0;277 var val: u0 = 0;
278 switch (val) {278 switch (val) {
279 0 => expect(val == 0),279 0 => try expect(val == 0),
280 }280 }
281}281}
282282
283test "undefined.u0" {283test "undefined.u0" {
284 var val: u0 = undefined;284 var val: u0 = undefined;
285 switch (val) {285 switch (val) {
286 0 => expect(val == 0),286 0 => try expect(val == 0),
287 }287 }
288}288}
289289
...@@ -295,15 +295,15 @@ test "anon enum literal used in switch on union enum" {...@@ -295,15 +295,15 @@ test "anon enum literal used in switch on union enum" {
295 var foo = Foo{ .a = 1234 };295 var foo = Foo{ .a = 1234 };
296 switch (foo) {296 switch (foo) {
297 .a => |x| {297 .a => |x| {
298 expect(x == 1234);298 try expect(x == 1234);
299 },299 },
300 }300 }
301}301}
302302
303test "else prong of switch on error set excludes other cases" {303test "else prong of switch on error set excludes other cases" {
304 const S = struct {304 const S = struct {
305 fn doTheTest() void {305 fn doTheTest() !void {
306 expectError(error.C, bar());306 try expectError(error.C, bar());
307 }307 }
308 const E = error{308 const E = error{
309 A,309 A,
...@@ -326,14 +326,14 @@ test "else prong of switch on error set excludes other cases" {...@@ -326,14 +326,14 @@ test "else prong of switch on error set excludes other cases" {
326 };326 };
327 }327 }
328 };328 };
329 S.doTheTest();329 try S.doTheTest();
330 comptime S.doTheTest();330 comptime try S.doTheTest();
331}331}
332332
333test "switch prongs with error set cases make a new error set type for capture value" {333test "switch prongs with error set cases make a new error set type for capture value" {
334 const S = struct {334 const S = struct {
335 fn doTheTest() void {335 fn doTheTest() !void {
336 expectError(error.B, bar());336 try expectError(error.B, bar());
337 }337 }
338 const E = E1 || E2;338 const E = E1 || E2;
339339
...@@ -358,14 +358,14 @@ test "switch prongs with error set cases make a new error set type for capture v...@@ -358,14 +358,14 @@ test "switch prongs with error set cases make a new error set type for capture v
358 };358 };
359 }359 }
360 };360 };
361 S.doTheTest();361 try S.doTheTest();
362 comptime S.doTheTest();362 comptime try S.doTheTest();
363}363}
364364
365test "return result loc and then switch with range implicit casted to error union" {365test "return result loc and then switch with range implicit casted to error union" {
366 const S = struct {366 const S = struct {
367 fn doTheTest() void {367 fn doTheTest() !void {
368 expect((func(0xb) catch unreachable) == 0xb);368 try expect((func(0xb) catch unreachable) == 0xb);
369 }369 }
370 fn func(d: u8) anyerror!u8 {370 fn func(d: u8) anyerror!u8 {
371 return switch (d) {371 return switch (d) {
...@@ -374,13 +374,13 @@ test "return result loc and then switch with range implicit casted to error unio...@@ -374,13 +374,13 @@ test "return result loc and then switch with range implicit casted to error unio
374 };374 };
375 }375 }
376 };376 };
377 S.doTheTest();377 try S.doTheTest();
378 comptime S.doTheTest();378 comptime try S.doTheTest();
379}379}
380380
381test "switch with null and T peer types and inferred result location type" {381test "switch with null and T peer types and inferred result location type" {
382 const S = struct {382 const S = struct {
383 fn doTheTest(c: u8) void {383 fn doTheTest(c: u8) !void {
384 if (switch (c) {384 if (switch (c) {
385 0 => true,385 0 => true,
386 else => null,386 else => null,
...@@ -389,8 +389,8 @@ test "switch with null and T peer types and inferred result location type" {...@@ -389,8 +389,8 @@ test "switch with null and T peer types and inferred result location type" {
389 }389 }
390 }390 }
391 };391 };
392 S.doTheTest(1);392 try S.doTheTest(1);
393 comptime S.doTheTest(1);393 comptime try S.doTheTest(1);
394}394}
395395
396test "switch prongs with cases with identical payload types" {396test "switch prongs with cases with identical payload types" {
...@@ -400,31 +400,31 @@ test "switch prongs with cases with identical payload types" {...@@ -400,31 +400,31 @@ test "switch prongs with cases with identical payload types" {
400 C: usize,400 C: usize,
401 };401 };
402 const S = struct {402 const S = struct {
403 fn doTheTest() void {403 fn doTheTest() !void {
404 doTheSwitch1(Union{ .A = 8 });404 try doTheSwitch1(Union{ .A = 8 });
405 doTheSwitch2(Union{ .B = -8 });405 try doTheSwitch2(Union{ .B = -8 });
406 }406 }
407 fn doTheSwitch1(u: Union) void {407 fn doTheSwitch1(u: Union) !void {
408 switch (u) {408 switch (u) {
409 .A, .C => |e| {409 .A, .C => |e| {
410 expect(@TypeOf(e) == usize);410 try expect(@TypeOf(e) == usize);
411 expect(e == 8);411 try expect(e == 8);
412 },412 },
413 .B => |e| @panic("fail"),413 .B => |e| @panic("fail"),
414 }414 }
415 }415 }
416 fn doTheSwitch2(u: Union) void {416 fn doTheSwitch2(u: Union) !void {
417 switch (u) {417 switch (u) {
418 .A, .C => |e| @panic("fail"),418 .A, .C => |e| @panic("fail"),
419 .B => |e| {419 .B => |e| {
420 expect(@TypeOf(e) == isize);420 try expect(@TypeOf(e) == isize);
421 expect(e == -8);421 try expect(e == -8);
422 },422 },
423 }423 }
424 }424 }
425 };425 };
426 S.doTheTest();426 try S.doTheTest();
427 comptime S.doTheTest();427 comptime try S.doTheTest();
428}428}
429429
430test "switch with disjoint range" {430test "switch with disjoint range" {
...@@ -438,19 +438,19 @@ test "switch with disjoint range" {...@@ -438,19 +438,19 @@ test "switch with disjoint range" {
438438
439test "switch variable for range and multiple prongs" {439test "switch variable for range and multiple prongs" {
440 const S = struct {440 const S = struct {
441 fn doTheTest() void {441 fn doTheTest() !void {
442 var u: u8 = 16;442 var u: u8 = 16;
443 doTheSwitch(u);443 try doTheSwitch(u);
444 comptime doTheSwitch(u);444 comptime try doTheSwitch(u);
445 var v: u8 = 42;445 var v: u8 = 42;
446 doTheSwitch(v);446 try doTheSwitch(v);
447 comptime doTheSwitch(v);447 comptime try doTheSwitch(v);
448 }448 }
449 fn doTheSwitch(q: u8) void {449 fn doTheSwitch(q: u8) !void {
450 switch (q) {450 switch (q) {
451 0...40 => |x| expect(x == 16),451 0...40 => |x| try expect(x == 16),
452 41, 42, 43 => |x| expect(x == 42),452 41, 42, 43 => |x| try expect(x == 42),
453 else => expect(false),453 else => try expect(false),
454 }454 }
455 }455 }
456 };456 };
...@@ -493,31 +493,31 @@ test "switch on pointer type" {...@@ -493,31 +493,31 @@ test "switch on pointer type" {
493 }493 }
494 };494 };
495495
496 expect(1 == S.doTheTest(S.P1));496 try expect(1 == S.doTheTest(S.P1));
497 expect(2 == S.doTheTest(S.P2));497 try expect(2 == S.doTheTest(S.P2));
498 expect(3 == S.doTheTest(S.P3));498 try expect(3 == S.doTheTest(S.P3));
499 comptime expect(1 == S.doTheTest(S.P1));499 comptime try expect(1 == S.doTheTest(S.P1));
500 comptime expect(2 == S.doTheTest(S.P2));500 comptime try expect(2 == S.doTheTest(S.P2));
501 comptime expect(3 == S.doTheTest(S.P3));501 comptime try expect(3 == S.doTheTest(S.P3));
502}502}
503503
504test "switch on error set with single else" {504test "switch on error set with single else" {
505 const S = struct {505 const S = struct {
506 fn doTheTest() void {506 fn doTheTest() !void {
507 var some: error{Foo} = error.Foo;507 var some: error{Foo} = error.Foo;
508 expect(switch (some) {508 try expect(switch (some) {
509 else => |a| true,509 else => |a| true,
510 });510 });
511 }511 }
512 };512 };
513513
514 S.doTheTest();514 try S.doTheTest();
515 comptime S.doTheTest();515 comptime try S.doTheTest();
516}516}
517517
518test "while copies its payload" {518test "while copies its payload" {
519 const S = struct {519 const S = struct {
520 fn doTheTest() void {520 fn doTheTest() !void {
521 var tmp: union(enum) {521 var tmp: union(enum) {
522 A: u8,522 A: u8,
523 B: u32,523 B: u32,
...@@ -526,12 +526,12 @@ test "while copies its payload" {...@@ -526,12 +526,12 @@ test "while copies its payload" {
526 .A => |value| {526 .A => |value| {
527 // Modify the original union527 // Modify the original union
528 tmp = .{ .B = 0x10101010 };528 tmp = .{ .B = 0x10101010 };
529 expectEqual(@as(u8, 42), value);529 try expectEqual(@as(u8, 42), value);
530 },530 },
531 else => unreachable,531 else => unreachable,
532 }532 }
533 }533 }
534 };534 };
535 S.doTheTest();535 try S.doTheTest();
536 comptime S.doTheTest();536 comptime try S.doTheTest();
537}537}
test/stage1/behavior/switch_prong_err_enum.zig+2-2
...@@ -22,9 +22,9 @@ fn doThing(form_id: u64) anyerror!FormValue {...@@ -22,9 +22,9 @@ fn doThing(form_id: u64) anyerror!FormValue {
22test "switch prong returns error enum" {22test "switch prong returns error enum" {
23 switch (doThing(17) catch unreachable) {23 switch (doThing(17) catch unreachable) {
24 FormValue.Address => |payload| {24 FormValue.Address => |payload| {
25 expect(payload == 1);25 try expect(payload == 1);
26 },26 },
27 else => unreachable,27 else => unreachable,
28 }28 }
29 expect(read_count == 1);29 try expect(read_count == 1);
30}30}
test/stage1/behavior/switch_prong_implicit_cast.zig+1-1
...@@ -18,5 +18,5 @@ test "switch prong implicit cast" {...@@ -18,5 +18,5 @@ test "switch prong implicit cast" {
18 FormValue.One => false,18 FormValue.One => false,
19 FormValue.Two => |x| x,19 FormValue.Two => |x| x,
20 };20 };
21 expect(result);21 try expect(result);
22}22}
test/stage1/behavior/this.zig+3-3
...@@ -20,7 +20,7 @@ fn add(x: i32, y: i32) i32 {...@@ -20,7 +20,7 @@ fn add(x: i32, y: i32) i32 {
20}20}
2121
22test "this refer to module call private fn" {22test "this refer to module call private fn" {
23 expect(module.add(1, 2) == 3);23 try expect(module.add(1, 2) == 3);
24}24}
2525
26test "this refer to container" {26test "this refer to container" {
...@@ -29,6 +29,6 @@ test "this refer to container" {...@@ -29,6 +29,6 @@ test "this refer to container" {
29 .y = 34,29 .y = 34,
30 };30 };
31 pt.addOne();31 pt.addOne();
32 expect(pt.x == 13);32 try expect(pt.x == 13);
33 expect(pt.y == 35);33 try expect(pt.y == 35);
34}34}
test/stage1/behavior/translate_c_macros.zig+4-4
...@@ -4,7 +4,7 @@ const expectEqual = @import("std").testing.expectEqual;...@@ -4,7 +4,7 @@ const expectEqual = @import("std").testing.expectEqual;
4const h = @cImport(@cInclude("stage1/behavior/translate_c_macros.h"));4const h = @cImport(@cInclude("stage1/behavior/translate_c_macros.h"));
55
6test "initializer list expression" {6test "initializer list expression" {
7 expectEqual(h.Color{7 try expectEqual(h.Color{
8 .r = 200,8 .r = 200,
9 .g = 200,9 .g = 200,
10 .b = 200,10 .b = 200,
...@@ -13,10 +13,10 @@ test "initializer list expression" {...@@ -13,10 +13,10 @@ test "initializer list expression" {
13}13}
1414
15test "sizeof in macros" {15test "sizeof in macros" {
16 expectEqual(@as(c_int, @sizeOf(u32)), h.MY_SIZEOF(u32));16 try expectEqual(@as(c_int, @sizeOf(u32)), h.MY_SIZEOF(u32));
17 expectEqual(@as(c_int, @sizeOf(u32)), h.MY_SIZEOF2(u32));17 try expectEqual(@as(c_int, @sizeOf(u32)), h.MY_SIZEOF2(u32));
18}18}
1919
20test "reference to a struct type" {20test "reference to a struct type" {
21 expectEqual(@sizeOf(h.struct_Foo), h.SIZE_OF_FOO);21 try expectEqual(@sizeOf(h.struct_Foo), h.SIZE_OF_FOO);
22}22}
test/stage1/behavior/truncate.zig+6-6
...@@ -4,33 +4,33 @@ const expect = std.testing.expect;...@@ -4,33 +4,33 @@ const expect = std.testing.expect;
4test "truncate u0 to larger integer allowed and has comptime known result" {4test "truncate u0 to larger integer allowed and has comptime known result" {
5 var x: u0 = 0;5 var x: u0 = 0;
6 const y = @truncate(u8, x);6 const y = @truncate(u8, x);
7 comptime expect(y == 0);7 comptime try expect(y == 0);
8}8}
99
10test "truncate.u0.literal" {10test "truncate.u0.literal" {
11 var z = @truncate(u0, 0);11 var z = @truncate(u0, 0);
12 expect(z == 0);12 try expect(z == 0);
13}13}
1414
15test "truncate.u0.const" {15test "truncate.u0.const" {
16 const c0: usize = 0;16 const c0: usize = 0;
17 var z = @truncate(u0, c0);17 var z = @truncate(u0, c0);
18 expect(z == 0);18 try expect(z == 0);
19}19}
2020
21test "truncate.u0.var" {21test "truncate.u0.var" {
22 var d: u8 = 2;22 var d: u8 = 2;
23 var z = @truncate(u0, d);23 var z = @truncate(u0, d);
24 expect(z == 0);24 try expect(z == 0);
25}25}
2626
27test "truncate sign mismatch but comptime known so it works anyway" {27test "truncate sign mismatch but comptime known so it works anyway" {
28 const x: u32 = 10;28 const x: u32 = 10;
29 var result = @truncate(i8, x);29 var result = @truncate(i8, x);
30 expect(result == 10);30 try expect(result == 10);
31}31}
3232
33test "truncate on comptime integer" {33test "truncate on comptime integer" {
34 var x = @truncate(u16, 9999);34 var x = @truncate(u16, 9999);
35 expect(x == 9999);35 try expect(x == 9999);
36}36}
test/stage1/behavior/try.zig+7-7
...@@ -1,17 +1,17 @@...@@ -1,17 +1,17 @@
1const expect = @import("std").testing.expect;1const expect = @import("std").testing.expect;
22
3test "try on error union" {3test "try on error union" {
4 tryOnErrorUnionImpl();4 try tryOnErrorUnionImpl();
5 comptime tryOnErrorUnionImpl();5 comptime try tryOnErrorUnionImpl();
6}6}
77
8fn tryOnErrorUnionImpl() void {8fn tryOnErrorUnionImpl() !void {
9 const x = if (returnsTen()) |val| val + 1 else |err| switch (err) {9 const x = if (returnsTen()) |val| val + 1 else |err| switch (err) {
10 error.ItBroke, error.NoMem => 1,10 error.ItBroke, error.NoMem => 1,
11 error.CrappedOut => @as(i32, 2),11 error.CrappedOut => @as(i32, 2),
12 else => unreachable,12 else => unreachable,
13 };13 };
14 expect(x == 11);14 try expect(x == 11);
15}15}
1616
17fn returnsTen() anyerror!i32 {17fn returnsTen() anyerror!i32 {
...@@ -20,10 +20,10 @@ fn returnsTen() anyerror!i32 {...@@ -20,10 +20,10 @@ fn returnsTen() anyerror!i32 {
2020
21test "try without vars" {21test "try without vars" {
22 const result1 = if (failIfTrue(true)) 1 else |_| @as(i32, 2);22 const result1 = if (failIfTrue(true)) 1 else |_| @as(i32, 2);
23 expect(result1 == 2);23 try expect(result1 == 2);
2424
25 const result2 = if (failIfTrue(false)) 1 else |_| @as(i32, 2);25 const result2 = if (failIfTrue(false)) 1 else |_| @as(i32, 2);
26 expect(result2 == 1);26 try expect(result2 == 1);
27}27}
2828
29fn failIfTrue(ok: bool) anyerror!void {29fn failIfTrue(ok: bool) anyerror!void {
...@@ -38,6 +38,6 @@ test "try then not executed with assignment" {...@@ -38,6 +38,6 @@ test "try then not executed with assignment" {
38 if (failIfTrue(true)) {38 if (failIfTrue(true)) {
39 unreachable;39 unreachable;
40 } else |err| {40 } else |err| {
41 expect(err == error.ItBroke);41 try expect(err == error.ItBroke);
42 }42 }
43}43}
test/stage1/behavior/tuple.zig+44-44
...@@ -5,93 +5,93 @@ const expectEqual = testing.expectEqual;...@@ -5,93 +5,93 @@ const expectEqual = testing.expectEqual;
55
6test "tuple concatenation" {6test "tuple concatenation" {
7 const S = struct {7 const S = struct {
8 fn doTheTest() void {8 fn doTheTest() !void {
9 var a: i32 = 1;9 var a: i32 = 1;
10 var b: i32 = 2;10 var b: i32 = 2;
11 var x = .{a};11 var x = .{a};
12 var y = .{b};12 var y = .{b};
13 var c = x ++ y;13 var c = x ++ y;
14 expectEqual(@as(i32, 1), c[0]);14 try expectEqual(@as(i32, 1), c[0]);
15 expectEqual(@as(i32, 2), c[1]);15 try expectEqual(@as(i32, 2), c[1]);
16 }16 }
17 };17 };
18 S.doTheTest();18 try S.doTheTest();
19 comptime S.doTheTest();19 comptime try S.doTheTest();
20}20}
2121
22test "tuple multiplication" {22test "tuple multiplication" {
23 const S = struct {23 const S = struct {
24 fn doTheTest() void {24 fn doTheTest() !void {
25 {25 {
26 const t = .{} ** 4;26 const t = .{} ** 4;
27 expectEqual(0, @typeInfo(@TypeOf(t)).Struct.fields.len);27 try expectEqual(0, @typeInfo(@TypeOf(t)).Struct.fields.len);
28 }28 }
29 {29 {
30 const t = .{'a'} ** 4;30 const t = .{'a'} ** 4;
31 expectEqual(4, @typeInfo(@TypeOf(t)).Struct.fields.len);31 try expectEqual(4, @typeInfo(@TypeOf(t)).Struct.fields.len);
32 inline for (t) |x| expectEqual('a', x);32 inline for (t) |x| try expectEqual('a', x);
33 }33 }
34 {34 {
35 const t = .{ 1, 2, 3 } ** 4;35 const t = .{ 1, 2, 3 } ** 4;
36 expectEqual(12, @typeInfo(@TypeOf(t)).Struct.fields.len);36 try expectEqual(12, @typeInfo(@TypeOf(t)).Struct.fields.len);
37 inline for (t) |x, i| expectEqual(1 + i % 3, x);37 inline for (t) |x, i| try expectEqual(1 + i % 3, x);
38 }38 }
39 }39 }
40 };40 };
41 S.doTheTest();41 try S.doTheTest();
42 comptime S.doTheTest();42 comptime try S.doTheTest();
4343
44 const T = struct {44 const T = struct {
45 fn consume_tuple(tuple: anytype, len: usize) void {45 fn consume_tuple(tuple: anytype, len: usize) !void {
46 expect(tuple.len == len);46 try expect(tuple.len == len);
47 }47 }
4848
49 fn doTheTest() void {49 fn doTheTest() !void {
50 const t1 = .{};50 const t1 = .{};
5151
52 var rt_var: u8 = 42;52 var rt_var: u8 = 42;
53 const t2 = .{rt_var} ++ .{};53 const t2 = .{rt_var} ++ .{};
5454
55 expect(t2.len == 1);55 try expect(t2.len == 1);
56 expect(t2.@"0" == rt_var);56 try expect(t2.@"0" == rt_var);
57 expect(t2.@"0" == 42);57 try expect(t2.@"0" == 42);
58 expect(&t2.@"0" != &rt_var);58 try expect(&t2.@"0" != &rt_var);
5959
60 consume_tuple(t1 ++ t1, 0);60 try consume_tuple(t1 ++ t1, 0);
61 consume_tuple(.{} ++ .{}, 0);61 try consume_tuple(.{} ++ .{}, 0);
62 consume_tuple(.{0} ++ .{}, 1);62 try consume_tuple(.{0} ++ .{}, 1);
63 consume_tuple(.{0} ++ .{1}, 2);63 try consume_tuple(.{0} ++ .{1}, 2);
64 consume_tuple(.{ 0, 1, 2 } ++ .{ u8, 1, noreturn }, 6);64 try consume_tuple(.{ 0, 1, 2 } ++ .{ u8, 1, noreturn }, 6);
65 consume_tuple(t2 ++ t1, 1);65 try consume_tuple(t2 ++ t1, 1);
66 consume_tuple(t1 ++ t2, 1);66 try consume_tuple(t1 ++ t2, 1);
67 consume_tuple(t2 ++ t2, 2);67 try consume_tuple(t2 ++ t2, 2);
68 consume_tuple(.{rt_var} ++ .{}, 1);68 try consume_tuple(.{rt_var} ++ .{}, 1);
69 consume_tuple(.{rt_var} ++ t1, 1);69 try consume_tuple(.{rt_var} ++ t1, 1);
70 consume_tuple(.{} ++ .{rt_var}, 1);70 try consume_tuple(.{} ++ .{rt_var}, 1);
71 consume_tuple(t2 ++ .{void}, 2);71 try consume_tuple(t2 ++ .{void}, 2);
72 consume_tuple(t2 ++ .{0}, 2);72 try consume_tuple(t2 ++ .{0}, 2);
73 consume_tuple(.{0} ++ t2, 2);73 try consume_tuple(.{0} ++ t2, 2);
74 consume_tuple(.{void} ++ t2, 2);74 try consume_tuple(.{void} ++ t2, 2);
75 consume_tuple(.{u8} ++ .{rt_var} ++ .{true}, 3);75 try consume_tuple(.{u8} ++ .{rt_var} ++ .{true}, 3);
76 }76 }
77 };77 };
7878
79 T.doTheTest();79 try T.doTheTest();
80 comptime T.doTheTest();80 comptime try T.doTheTest();
81}81}
8282
83test "pass tuple to comptime var parameter" {83test "pass tuple to comptime var parameter" {
84 const S = struct {84 const S = struct {
85 fn Foo(comptime args: anytype) void {85 fn Foo(comptime args: anytype) !void {
86 expect(args[0] == 1);86 try expect(args[0] == 1);
87 }87 }
8888
89 fn doTheTest() void {89 fn doTheTest() !void {
90 Foo(.{1});90 try Foo(.{1});
91 }91 }
92 };92 };
93 S.doTheTest();93 try S.doTheTest();
94 comptime S.doTheTest();94 comptime try S.doTheTest();
95}95}
9696
97test "tuple initializer for var" {97test "tuple initializer for var" {
test/stage1/behavior/type.zig+84-84
...@@ -4,52 +4,52 @@ const TypeInfo = builtin.TypeInfo;...@@ -4,52 +4,52 @@ const TypeInfo = builtin.TypeInfo;
4const std = @import("std");4const std = @import("std");
5const testing = std.testing;5const testing = std.testing;
66
7fn testTypes(comptime types: []const type) void {7fn testTypes(comptime types: []const type) !void {
8 inline for (types) |testType| {8 inline for (types) |testType| {
9 testing.expect(testType == @Type(@typeInfo(testType)));9 try testing.expect(testType == @Type(@typeInfo(testType)));
10 }10 }
11}11}
1212
13test "Type.MetaType" {13test "Type.MetaType" {
14 testing.expect(type == @Type(TypeInfo{ .Type = undefined }));14 try testing.expect(type == @Type(TypeInfo{ .Type = undefined }));
15 testTypes(&[_]type{type});15 try testTypes(&[_]type{type});
16}16}
1717
18test "Type.Void" {18test "Type.Void" {
19 testing.expect(void == @Type(TypeInfo{ .Void = undefined }));19 try testing.expect(void == @Type(TypeInfo{ .Void = undefined }));
20 testTypes(&[_]type{void});20 try testTypes(&[_]type{void});
21}21}
2222
23test "Type.Bool" {23test "Type.Bool" {
24 testing.expect(bool == @Type(TypeInfo{ .Bool = undefined }));24 try testing.expect(bool == @Type(TypeInfo{ .Bool = undefined }));
25 testTypes(&[_]type{bool});25 try testTypes(&[_]type{bool});
26}26}
2727
28test "Type.NoReturn" {28test "Type.NoReturn" {
29 testing.expect(noreturn == @Type(TypeInfo{ .NoReturn = undefined }));29 try testing.expect(noreturn == @Type(TypeInfo{ .NoReturn = undefined }));
30 testTypes(&[_]type{noreturn});30 try testTypes(&[_]type{noreturn});
31}31}
3232
33test "Type.Int" {33test "Type.Int" {
34 testing.expect(u1 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .signedness = .unsigned, .bits = 1 } }));34 try testing.expect(u1 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .signedness = .unsigned, .bits = 1 } }));
35 testing.expect(i1 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .signedness = .signed, .bits = 1 } }));35 try testing.expect(i1 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .signedness = .signed, .bits = 1 } }));
36 testing.expect(u8 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .signedness = .unsigned, .bits = 8 } }));36 try testing.expect(u8 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .signedness = .unsigned, .bits = 8 } }));
37 testing.expect(i8 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .signedness = .signed, .bits = 8 } }));37 try testing.expect(i8 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .signedness = .signed, .bits = 8 } }));
38 testing.expect(u64 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .signedness = .unsigned, .bits = 64 } }));38 try testing.expect(u64 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .signedness = .unsigned, .bits = 64 } }));
39 testing.expect(i64 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .signedness = .signed, .bits = 64 } }));39 try testing.expect(i64 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .signedness = .signed, .bits = 64 } }));
40 testTypes(&[_]type{ u8, u32, i64 });40 try testTypes(&[_]type{ u8, u32, i64 });
41}41}
4242
43test "Type.Float" {43test "Type.Float" {
44 testing.expect(f16 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 16 } }));44 try testing.expect(f16 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 16 } }));
45 testing.expect(f32 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 32 } }));45 try testing.expect(f32 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 32 } }));
46 testing.expect(f64 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 64 } }));46 try testing.expect(f64 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 64 } }));
47 testing.expect(f128 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 128 } }));47 try testing.expect(f128 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 128 } }));
48 testTypes(&[_]type{ f16, f32, f64, f128 });48 try testTypes(&[_]type{ f16, f32, f64, f128 });
49}49}
5050
51test "Type.Pointer" {51test "Type.Pointer" {
52 testTypes(&[_]type{52 try testTypes(&[_]type{
53 // One Value Pointer Types53 // One Value Pointer Types
54 *u8, *const u8,54 *u8, *const u8,
55 *volatile u8, *const volatile u8,55 *volatile u8, *const volatile u8,
...@@ -94,41 +94,41 @@ test "Type.Pointer" {...@@ -94,41 +94,41 @@ test "Type.Pointer" {
94}94}
9595
96test "Type.Array" {96test "Type.Array" {
97 testing.expect([123]u8 == @Type(TypeInfo{97 try testing.expect([123]u8 == @Type(TypeInfo{
98 .Array = TypeInfo.Array{98 .Array = TypeInfo.Array{
99 .len = 123,99 .len = 123,
100 .child = u8,100 .child = u8,
101 .sentinel = null,101 .sentinel = null,
102 },102 },
103 }));103 }));
104 testing.expect([2]u32 == @Type(TypeInfo{104 try testing.expect([2]u32 == @Type(TypeInfo{
105 .Array = TypeInfo.Array{105 .Array = TypeInfo.Array{
106 .len = 2,106 .len = 2,
107 .child = u32,107 .child = u32,
108 .sentinel = null,108 .sentinel = null,
109 },109 },
110 }));110 }));
111 testing.expect([2:0]u32 == @Type(TypeInfo{111 try testing.expect([2:0]u32 == @Type(TypeInfo{
112 .Array = TypeInfo.Array{112 .Array = TypeInfo.Array{
113 .len = 2,113 .len = 2,
114 .child = u32,114 .child = u32,
115 .sentinel = 0,115 .sentinel = 0,
116 },116 },
117 }));117 }));
118 testTypes(&[_]type{ [1]u8, [30]usize, [7]bool });118 try testTypes(&[_]type{ [1]u8, [30]usize, [7]bool });
119}119}
120120
121test "Type.ComptimeFloat" {121test "Type.ComptimeFloat" {
122 testTypes(&[_]type{comptime_float});122 try testTypes(&[_]type{comptime_float});
123}123}
124test "Type.ComptimeInt" {124test "Type.ComptimeInt" {
125 testTypes(&[_]type{comptime_int});125 try testTypes(&[_]type{comptime_int});
126}126}
127test "Type.Undefined" {127test "Type.Undefined" {
128 testTypes(&[_]type{@TypeOf(undefined)});128 try testTypes(&[_]type{@TypeOf(undefined)});
129}129}
130test "Type.Null" {130test "Type.Null" {
131 testTypes(&[_]type{@TypeOf(null)});131 try testTypes(&[_]type{@TypeOf(null)});
132}132}
133test "@Type create slice with null sentinel" {133test "@Type create slice with null sentinel" {
134 const Slice = @Type(builtin.TypeInfo{134 const Slice = @Type(builtin.TypeInfo{
...@@ -142,10 +142,10 @@ test "@Type create slice with null sentinel" {...@@ -142,10 +142,10 @@ test "@Type create slice with null sentinel" {
142 .sentinel = null,142 .sentinel = null,
143 },143 },
144 });144 });
145 testing.expect(Slice == []align(8) const *i32);145 try testing.expect(Slice == []align(8) const *i32);
146}146}
147test "@Type picks up the sentinel value from TypeInfo" {147test "@Type picks up the sentinel value from TypeInfo" {
148 testTypes(&[_]type{148 try testTypes(&[_]type{
149 [11:0]u8, [4:10]u8,149 [11:0]u8, [4:10]u8,
150 [*:0]u8, [*:0]const u8,150 [*:0]u8, [*:0]const u8,
151 [*:0]volatile u8, [*:0]const volatile u8,151 [*:0]volatile u8, [*:0]const volatile u8,
...@@ -173,7 +173,7 @@ test "@Type picks up the sentinel value from TypeInfo" {...@@ -173,7 +173,7 @@ test "@Type picks up the sentinel value from TypeInfo" {
173}173}
174174
175test "Type.Optional" {175test "Type.Optional" {
176 testTypes(&[_]type{176 try testTypes(&[_]type{
177 ?u8,177 ?u8,
178 ?*u8,178 ?*u8,
179 ?[]u8,179 ?[]u8,
...@@ -183,7 +183,7 @@ test "Type.Optional" {...@@ -183,7 +183,7 @@ test "Type.Optional" {
183}183}
184184
185test "Type.ErrorUnion" {185test "Type.ErrorUnion" {
186 testTypes(&[_]type{186 try testTypes(&[_]type{
187 error{}!void,187 error{}!void,
188 error{Error}!void,188 error{Error}!void,
189 });189 });
...@@ -195,8 +195,8 @@ test "Type.Opaque" {...@@ -195,8 +195,8 @@ test "Type.Opaque" {
195 .decls = &[_]TypeInfo.Declaration{},195 .decls = &[_]TypeInfo.Declaration{},
196 },196 },
197 });197 });
198 testing.expect(Opaque != opaque {});198 try testing.expect(Opaque != opaque {});
199 testing.expectEqualSlices(199 try testing.expectEqualSlices(
200 TypeInfo.Declaration,200 TypeInfo.Declaration,
201 &[_]TypeInfo.Declaration{},201 &[_]TypeInfo.Declaration{},
202 @typeInfo(Opaque).Opaque.decls,202 @typeInfo(Opaque).Opaque.decls,
...@@ -204,7 +204,7 @@ test "Type.Opaque" {...@@ -204,7 +204,7 @@ test "Type.Opaque" {
204}204}
205205
206test "Type.Vector" {206test "Type.Vector" {
207 testTypes(&[_]type{207 try testTypes(&[_]type{
208 @Vector(0, u8),208 @Vector(0, u8),
209 @Vector(4, u8),209 @Vector(4, u8),
210 @Vector(8, *u8),210 @Vector(8, *u8),
...@@ -215,7 +215,7 @@ test "Type.Vector" {...@@ -215,7 +215,7 @@ test "Type.Vector" {
215}215}
216216
217test "Type.AnyFrame" {217test "Type.AnyFrame" {
218 testTypes(&[_]type{218 try testTypes(&[_]type{
219 anyframe,219 anyframe,
220 anyframe->u8,220 anyframe->u8,
221 anyframe->anyframe->u8,221 anyframe->anyframe->u8,
...@@ -223,7 +223,7 @@ test "Type.AnyFrame" {...@@ -223,7 +223,7 @@ test "Type.AnyFrame" {
223}223}
224224
225test "Type.EnumLiteral" {225test "Type.EnumLiteral" {
226 testTypes(&[_]type{226 try testTypes(&[_]type{
227 @TypeOf(.Dummy),227 @TypeOf(.Dummy),
228 });228 });
229}229}
...@@ -233,7 +233,7 @@ fn add(a: i32, b: i32) i32 {...@@ -233,7 +233,7 @@ fn add(a: i32, b: i32) i32 {
233}233}
234234
235test "Type.Frame" {235test "Type.Frame" {
236 testTypes(&[_]type{236 try testTypes(&[_]type{
237 @Frame(add),237 @Frame(add),
238 });238 });
239}239}
...@@ -248,45 +248,45 @@ test "Type.ErrorSet" {...@@ -248,45 +248,45 @@ test "Type.ErrorSet" {
248test "Type.Struct" {248test "Type.Struct" {
249 const A = @Type(@typeInfo(struct { x: u8, y: u32 }));249 const A = @Type(@typeInfo(struct { x: u8, y: u32 }));
250 const infoA = @typeInfo(A).Struct;250 const infoA = @typeInfo(A).Struct;
251 testing.expectEqual(TypeInfo.ContainerLayout.Auto, infoA.layout);251 try testing.expectEqual(TypeInfo.ContainerLayout.Auto, infoA.layout);
252 testing.expectEqualSlices(u8, "x", infoA.fields[0].name);252 try testing.expectEqualSlices(u8, "x", infoA.fields[0].name);
253 testing.expectEqual(u8, infoA.fields[0].field_type);253 try testing.expectEqual(u8, infoA.fields[0].field_type);
254 testing.expectEqual(@as(?u8, null), infoA.fields[0].default_value);254 try testing.expectEqual(@as(?u8, null), infoA.fields[0].default_value);
255 testing.expectEqualSlices(u8, "y", infoA.fields[1].name);255 try testing.expectEqualSlices(u8, "y", infoA.fields[1].name);
256 testing.expectEqual(u32, infoA.fields[1].field_type);256 try testing.expectEqual(u32, infoA.fields[1].field_type);
257 testing.expectEqual(@as(?u32, null), infoA.fields[1].default_value);257 try testing.expectEqual(@as(?u32, null), infoA.fields[1].default_value);
258 testing.expectEqualSlices(TypeInfo.Declaration, &[_]TypeInfo.Declaration{}, infoA.decls);258 try testing.expectEqualSlices(TypeInfo.Declaration, &[_]TypeInfo.Declaration{}, infoA.decls);
259 testing.expectEqual(@as(bool, false), infoA.is_tuple);259 try testing.expectEqual(@as(bool, false), infoA.is_tuple);
260260
261 var a = A{ .x = 0, .y = 1 };261 var a = A{ .x = 0, .y = 1 };
262 testing.expectEqual(@as(u8, 0), a.x);262 try testing.expectEqual(@as(u8, 0), a.x);
263 testing.expectEqual(@as(u32, 1), a.y);263 try testing.expectEqual(@as(u32, 1), a.y);
264 a.y += 1;264 a.y += 1;
265 testing.expectEqual(@as(u32, 2), a.y);265 try testing.expectEqual(@as(u32, 2), a.y);
266266
267 const B = @Type(@typeInfo(extern struct { x: u8, y: u32 = 5 }));267 const B = @Type(@typeInfo(extern struct { x: u8, y: u32 = 5 }));
268 const infoB = @typeInfo(B).Struct;268 const infoB = @typeInfo(B).Struct;
269 testing.expectEqual(TypeInfo.ContainerLayout.Extern, infoB.layout);269 try testing.expectEqual(TypeInfo.ContainerLayout.Extern, infoB.layout);
270 testing.expectEqualSlices(u8, "x", infoB.fields[0].name);270 try testing.expectEqualSlices(u8, "x", infoB.fields[0].name);
271 testing.expectEqual(u8, infoB.fields[0].field_type);271 try testing.expectEqual(u8, infoB.fields[0].field_type);
272 testing.expectEqual(@as(?u8, null), infoB.fields[0].default_value);272 try testing.expectEqual(@as(?u8, null), infoB.fields[0].default_value);
273 testing.expectEqualSlices(u8, "y", infoB.fields[1].name);273 try testing.expectEqualSlices(u8, "y", infoB.fields[1].name);
274 testing.expectEqual(u32, infoB.fields[1].field_type);274 try testing.expectEqual(u32, infoB.fields[1].field_type);
275 testing.expectEqual(@as(?u32, 5), infoB.fields[1].default_value);275 try testing.expectEqual(@as(?u32, 5), infoB.fields[1].default_value);
276 testing.expectEqual(@as(usize, 0), infoB.decls.len);276 try testing.expectEqual(@as(usize, 0), infoB.decls.len);
277 testing.expectEqual(@as(bool, false), infoB.is_tuple);277 try testing.expectEqual(@as(bool, false), infoB.is_tuple);
278278
279 const C = @Type(@typeInfo(packed struct { x: u8 = 3, y: u32 = 5 }));279 const C = @Type(@typeInfo(packed struct { x: u8 = 3, y: u32 = 5 }));
280 const infoC = @typeInfo(C).Struct;280 const infoC = @typeInfo(C).Struct;
281 testing.expectEqual(TypeInfo.ContainerLayout.Packed, infoC.layout);281 try testing.expectEqual(TypeInfo.ContainerLayout.Packed, infoC.layout);
282 testing.expectEqualSlices(u8, "x", infoC.fields[0].name);282 try testing.expectEqualSlices(u8, "x", infoC.fields[0].name);
283 testing.expectEqual(u8, infoC.fields[0].field_type);283 try testing.expectEqual(u8, infoC.fields[0].field_type);
284 testing.expectEqual(@as(?u8, 3), infoC.fields[0].default_value);284 try testing.expectEqual(@as(?u8, 3), infoC.fields[0].default_value);
285 testing.expectEqualSlices(u8, "y", infoC.fields[1].name);285 try testing.expectEqualSlices(u8, "y", infoC.fields[1].name);
286 testing.expectEqual(u32, infoC.fields[1].field_type);286 try testing.expectEqual(u32, infoC.fields[1].field_type);
287 testing.expectEqual(@as(?u32, 5), infoC.fields[1].default_value);287 try testing.expectEqual(@as(?u32, 5), infoC.fields[1].default_value);
288 testing.expectEqual(@as(usize, 0), infoC.decls.len);288 try testing.expectEqual(@as(usize, 0), infoC.decls.len);
289 testing.expectEqual(@as(bool, false), infoC.is_tuple);289 try testing.expectEqual(@as(bool, false), infoC.is_tuple);
290}290}
291291
292test "Type.Enum" {292test "Type.Enum" {
...@@ -302,9 +302,9 @@ test "Type.Enum" {...@@ -302,9 +302,9 @@ test "Type.Enum" {
302 .is_exhaustive = true,302 .is_exhaustive = true,
303 },303 },
304 });304 });
305 testing.expectEqual(true, @typeInfo(Foo).Enum.is_exhaustive);305 try testing.expectEqual(true, @typeInfo(Foo).Enum.is_exhaustive);
306 testing.expectEqual(@as(u8, 1), @enumToInt(Foo.a));306 try testing.expectEqual(@as(u8, 1), @enumToInt(Foo.a));
307 testing.expectEqual(@as(u8, 5), @enumToInt(Foo.b));307 try testing.expectEqual(@as(u8, 5), @enumToInt(Foo.b));
308 const Bar = @Type(.{308 const Bar = @Type(.{
309 .Enum = .{309 .Enum = .{
310 .layout = .Extern,310 .layout = .Extern,
...@@ -317,10 +317,10 @@ test "Type.Enum" {...@@ -317,10 +317,10 @@ test "Type.Enum" {
317 .is_exhaustive = false,317 .is_exhaustive = false,
318 },318 },
319 });319 });
320 testing.expectEqual(false, @typeInfo(Bar).Enum.is_exhaustive);320 try testing.expectEqual(false, @typeInfo(Bar).Enum.is_exhaustive);
321 testing.expectEqual(@as(u32, 1), @enumToInt(Bar.a));321 try testing.expectEqual(@as(u32, 1), @enumToInt(Bar.a));
322 testing.expectEqual(@as(u32, 5), @enumToInt(Bar.b));322 try testing.expectEqual(@as(u32, 5), @enumToInt(Bar.b));
323 testing.expectEqual(@as(u32, 6), @enumToInt(@intToEnum(Bar, 6)));323 try testing.expectEqual(@as(u32, 6), @enumToInt(@intToEnum(Bar, 6)));
324}324}
325325
326test "Type.Union" {326test "Type.Union" {
...@@ -338,7 +338,7 @@ test "Type.Union" {...@@ -338,7 +338,7 @@ test "Type.Union" {
338 var untagged = Untagged{ .int = 1 };338 var untagged = Untagged{ .int = 1 };
339 untagged.float = 2.0;339 untagged.float = 2.0;
340 untagged.int = 3;340 untagged.int = 3;
341 testing.expectEqual(@as(i32, 3), untagged.int);341 try testing.expectEqual(@as(i32, 3), untagged.int);
342342
343 const PackedUntagged = @Type(.{343 const PackedUntagged = @Type(.{
344 .Union = .{344 .Union = .{
...@@ -352,8 +352,8 @@ test "Type.Union" {...@@ -352,8 +352,8 @@ test "Type.Union" {
352 },352 },
353 });353 });
354 var packed_untagged = PackedUntagged{ .signed = -1 };354 var packed_untagged = PackedUntagged{ .signed = -1 };
355 testing.expectEqual(@as(i32, -1), packed_untagged.signed);355 try testing.expectEqual(@as(i32, -1), packed_untagged.signed);
356 testing.expectEqual(~@as(u32, 0), packed_untagged.unsigned);356 try testing.expectEqual(~@as(u32, 0), packed_untagged.unsigned);
357357
358 const Tag = @Type(.{358 const Tag = @Type(.{
359 .Enum = .{359 .Enum = .{
...@@ -379,9 +379,9 @@ test "Type.Union" {...@@ -379,9 +379,9 @@ test "Type.Union" {
379 },379 },
380 });380 });
381 var tagged = Tagged{ .signed = -1 };381 var tagged = Tagged{ .signed = -1 };
382 testing.expectEqual(Tag.signed, tagged);382 try testing.expectEqual(Tag.signed, tagged);
383 tagged = .{ .unsigned = 1 };383 tagged = .{ .unsigned = 1 };
384 testing.expectEqual(Tag.unsigned, tagged);384 try testing.expectEqual(Tag.unsigned, tagged);
385}385}
386386
387test "Type.Union from Type.Enum" {387test "Type.Union from Type.Enum" {
...@@ -447,7 +447,7 @@ test "Type.BoundFn" {...@@ -447,7 +447,7 @@ test "Type.BoundFn" {
447 pub fn foo(self: *const @This()) align(4) callconv(.Unspecified) void {}447 pub fn foo(self: *const @This()) align(4) callconv(.Unspecified) void {}
448 };448 };
449 const test_instance: TestStruct = undefined;449 const test_instance: TestStruct = undefined;
450 testing.expect(std.meta.eql(450 try testing.expect(std.meta.eql(
451 @typeName(@TypeOf(test_instance.foo)),451 @typeName(@TypeOf(test_instance.foo)),
452 @typeName(@Type(@typeInfo(@TypeOf(test_instance.foo)))),452 @typeName(@Type(@typeInfo(@TypeOf(test_instance.foo)))),
453 ));453 ));
test/stage1/behavior/type_info.zig+187-187
...@@ -9,151 +9,151 @@ const expect = std.testing.expect;...@@ -9,151 +9,151 @@ const expect = std.testing.expect;
9const expectEqualStrings = std.testing.expectEqualStrings;9const expectEqualStrings = std.testing.expectEqualStrings;
1010
11test "type info: tag type, void info" {11test "type info: tag type, void info" {
12 testBasic();12 try testBasic();
13 comptime testBasic();13 comptime try testBasic();
14}14}
1515
16fn testBasic() void {16fn testBasic() !void {
17 expect(@typeInfo(TypeInfo).Union.tag_type == TypeId);17 try expect(@typeInfo(TypeInfo).Union.tag_type == TypeId);
18 const void_info = @typeInfo(void);18 const void_info = @typeInfo(void);
19 expect(void_info == TypeId.Void);19 try expect(void_info == TypeId.Void);
20 expect(void_info.Void == {});20 try expect(void_info.Void == {});
21}21}
2222
23test "type info: integer, floating point type info" {23test "type info: integer, floating point type info" {
24 testIntFloat();24 try testIntFloat();
25 comptime testIntFloat();25 comptime try testIntFloat();
26}26}
2727
28fn testIntFloat() void {28fn testIntFloat() !void {
29 const u8_info = @typeInfo(u8);29 const u8_info = @typeInfo(u8);
30 expect(u8_info == .Int);30 try expect(u8_info == .Int);
31 expect(u8_info.Int.signedness == .unsigned);31 try expect(u8_info.Int.signedness == .unsigned);
32 expect(u8_info.Int.bits == 8);32 try expect(u8_info.Int.bits == 8);
3333
34 const f64_info = @typeInfo(f64);34 const f64_info = @typeInfo(f64);
35 expect(f64_info == .Float);35 try expect(f64_info == .Float);
36 expect(f64_info.Float.bits == 64);36 try expect(f64_info.Float.bits == 64);
37}37}
3838
39test "type info: pointer type info" {39test "type info: pointer type info" {
40 testPointer();40 try testPointer();
41 comptime testPointer();41 comptime try testPointer();
42}42}
4343
44fn testPointer() void {44fn testPointer() !void {
45 const u32_ptr_info = @typeInfo(*u32);45 const u32_ptr_info = @typeInfo(*u32);
46 expect(u32_ptr_info == .Pointer);46 try expect(u32_ptr_info == .Pointer);
47 expect(u32_ptr_info.Pointer.size == TypeInfo.Pointer.Size.One);47 try expect(u32_ptr_info.Pointer.size == TypeInfo.Pointer.Size.One);
48 expect(u32_ptr_info.Pointer.is_const == false);48 try expect(u32_ptr_info.Pointer.is_const == false);
49 expect(u32_ptr_info.Pointer.is_volatile == false);49 try expect(u32_ptr_info.Pointer.is_volatile == false);
50 expect(u32_ptr_info.Pointer.alignment == @alignOf(u32));50 try expect(u32_ptr_info.Pointer.alignment == @alignOf(u32));
51 expect(u32_ptr_info.Pointer.child == u32);51 try expect(u32_ptr_info.Pointer.child == u32);
52 expect(u32_ptr_info.Pointer.sentinel == null);52 try expect(u32_ptr_info.Pointer.sentinel == null);
53}53}
5454
55test "type info: unknown length pointer type info" {55test "type info: unknown length pointer type info" {
56 testUnknownLenPtr();56 try testUnknownLenPtr();
57 comptime testUnknownLenPtr();57 comptime try testUnknownLenPtr();
58}58}
5959
60fn testUnknownLenPtr() void {60fn testUnknownLenPtr() !void {
61 const u32_ptr_info = @typeInfo([*]const volatile f64);61 const u32_ptr_info = @typeInfo([*]const volatile f64);
62 expect(u32_ptr_info == .Pointer);62 try expect(u32_ptr_info == .Pointer);
63 expect(u32_ptr_info.Pointer.size == TypeInfo.Pointer.Size.Many);63 try expect(u32_ptr_info.Pointer.size == TypeInfo.Pointer.Size.Many);
64 expect(u32_ptr_info.Pointer.is_const == true);64 try expect(u32_ptr_info.Pointer.is_const == true);
65 expect(u32_ptr_info.Pointer.is_volatile == true);65 try expect(u32_ptr_info.Pointer.is_volatile == true);
66 expect(u32_ptr_info.Pointer.sentinel == null);66 try expect(u32_ptr_info.Pointer.sentinel == null);
67 expect(u32_ptr_info.Pointer.alignment == @alignOf(f64));67 try expect(u32_ptr_info.Pointer.alignment == @alignOf(f64));
68 expect(u32_ptr_info.Pointer.child == f64);68 try expect(u32_ptr_info.Pointer.child == f64);
69}69}
7070
71test "type info: null terminated pointer type info" {71test "type info: null terminated pointer type info" {
72 testNullTerminatedPtr();72 try testNullTerminatedPtr();
73 comptime testNullTerminatedPtr();73 comptime try testNullTerminatedPtr();
74}74}
7575
76fn testNullTerminatedPtr() void {76fn testNullTerminatedPtr() !void {
77 const ptr_info = @typeInfo([*:0]u8);77 const ptr_info = @typeInfo([*:0]u8);
78 expect(ptr_info == .Pointer);78 try expect(ptr_info == .Pointer);
79 expect(ptr_info.Pointer.size == TypeInfo.Pointer.Size.Many);79 try expect(ptr_info.Pointer.size == TypeInfo.Pointer.Size.Many);
80 expect(ptr_info.Pointer.is_const == false);80 try expect(ptr_info.Pointer.is_const == false);
81 expect(ptr_info.Pointer.is_volatile == false);81 try expect(ptr_info.Pointer.is_volatile == false);
82 expect(ptr_info.Pointer.sentinel.? == 0);82 try expect(ptr_info.Pointer.sentinel.? == 0);
8383
84 expect(@typeInfo([:0]u8).Pointer.sentinel != null);84 try expect(@typeInfo([:0]u8).Pointer.sentinel != null);
85}85}
8686
87test "type info: C pointer type info" {87test "type info: C pointer type info" {
88 testCPtr();88 try testCPtr();
89 comptime testCPtr();89 comptime try testCPtr();
90}90}
9191
92fn testCPtr() void {92fn testCPtr() !void {
93 const ptr_info = @typeInfo([*c]align(4) const i8);93 const ptr_info = @typeInfo([*c]align(4) const i8);
94 expect(ptr_info == .Pointer);94 try expect(ptr_info == .Pointer);
95 expect(ptr_info.Pointer.size == .C);95 try expect(ptr_info.Pointer.size == .C);
96 expect(ptr_info.Pointer.is_const);96 try expect(ptr_info.Pointer.is_const);
97 expect(!ptr_info.Pointer.is_volatile);97 try expect(!ptr_info.Pointer.is_volatile);
98 expect(ptr_info.Pointer.alignment == 4);98 try expect(ptr_info.Pointer.alignment == 4);
99 expect(ptr_info.Pointer.child == i8);99 try expect(ptr_info.Pointer.child == i8);
100}100}
101101
102test "type info: slice type info" {102test "type info: slice type info" {
103 testSlice();103 try testSlice();
104 comptime testSlice();104 comptime try testSlice();
105}105}
106106
107fn testSlice() void {107fn testSlice() !void {
108 const u32_slice_info = @typeInfo([]u32);108 const u32_slice_info = @typeInfo([]u32);
109 expect(u32_slice_info == .Pointer);109 try expect(u32_slice_info == .Pointer);
110 expect(u32_slice_info.Pointer.size == .Slice);110 try expect(u32_slice_info.Pointer.size == .Slice);
111 expect(u32_slice_info.Pointer.is_const == false);111 try expect(u32_slice_info.Pointer.is_const == false);
112 expect(u32_slice_info.Pointer.is_volatile == false);112 try expect(u32_slice_info.Pointer.is_volatile == false);
113 expect(u32_slice_info.Pointer.alignment == 4);113 try expect(u32_slice_info.Pointer.alignment == 4);
114 expect(u32_slice_info.Pointer.child == u32);114 try expect(u32_slice_info.Pointer.child == u32);
115}115}
116116
117test "type info: array type info" {117test "type info: array type info" {
118 testArray();118 try testArray();
119 comptime testArray();119 comptime try testArray();
120}120}
121121
122fn testArray() void {122fn testArray() !void {
123 {123 {
124 const info = @typeInfo([42]u8);124 const info = @typeInfo([42]u8);
125 expect(info == .Array);125 try expect(info == .Array);
126 expect(info.Array.len == 42);126 try expect(info.Array.len == 42);
127 expect(info.Array.child == u8);127 try expect(info.Array.child == u8);
128 expect(info.Array.sentinel == null);128 try expect(info.Array.sentinel == null);
129 }129 }
130130
131 {131 {
132 const info = @typeInfo([10:0]u8);132 const info = @typeInfo([10:0]u8);
133 expect(info.Array.len == 10);133 try expect(info.Array.len == 10);
134 expect(info.Array.child == u8);134 try expect(info.Array.child == u8);
135 expect(info.Array.sentinel.? == @as(u8, 0));135 try expect(info.Array.sentinel.? == @as(u8, 0));
136 expect(@sizeOf([10:0]u8) == info.Array.len + 1);136 try expect(@sizeOf([10:0]u8) == info.Array.len + 1);
137 }137 }
138}138}
139139
140test "type info: optional type info" {140test "type info: optional type info" {
141 testOptional();141 try testOptional();
142 comptime testOptional();142 comptime try testOptional();
143}143}
144144
145fn testOptional() void {145fn testOptional() !void {
146 const null_info = @typeInfo(?void);146 const null_info = @typeInfo(?void);
147 expect(null_info == .Optional);147 try expect(null_info == .Optional);
148 expect(null_info.Optional.child == void);148 try expect(null_info.Optional.child == void);
149}149}
150150
151test "type info: error set, error union info" {151test "type info: error set, error union info" {
152 testErrorSet();152 try testErrorSet();
153 comptime testErrorSet();153 comptime try testErrorSet();
154}154}
155155
156fn testErrorSet() void {156fn testErrorSet() !void {
157 const TestErrorSet = error{157 const TestErrorSet = error{
158 First,158 First,
159 Second,159 Second,
...@@ -161,26 +161,26 @@ fn testErrorSet() void {...@@ -161,26 +161,26 @@ fn testErrorSet() void {
161 };161 };
162162
163 const error_set_info = @typeInfo(TestErrorSet);163 const error_set_info = @typeInfo(TestErrorSet);
164 expect(error_set_info == .ErrorSet);164 try expect(error_set_info == .ErrorSet);
165 expect(error_set_info.ErrorSet.?.len == 3);165 try expect(error_set_info.ErrorSet.?.len == 3);
166 expect(mem.eql(u8, error_set_info.ErrorSet.?[0].name, "First"));166 try expect(mem.eql(u8, error_set_info.ErrorSet.?[0].name, "First"));
167167
168 const error_union_info = @typeInfo(TestErrorSet!usize);168 const error_union_info = @typeInfo(TestErrorSet!usize);
169 expect(error_union_info == .ErrorUnion);169 try expect(error_union_info == .ErrorUnion);
170 expect(error_union_info.ErrorUnion.error_set == TestErrorSet);170 try expect(error_union_info.ErrorUnion.error_set == TestErrorSet);
171 expect(error_union_info.ErrorUnion.payload == usize);171 try expect(error_union_info.ErrorUnion.payload == usize);
172172
173 const global_info = @typeInfo(anyerror);173 const global_info = @typeInfo(anyerror);
174 expect(global_info == .ErrorSet);174 try expect(global_info == .ErrorSet);
175 expect(global_info.ErrorSet == null);175 try expect(global_info.ErrorSet == null);
176}176}
177177
178test "type info: enum info" {178test "type info: enum info" {
179 testEnum();179 try testEnum();
180 comptime testEnum();180 comptime try testEnum();
181}181}
182182
183fn testEnum() void {183fn testEnum() !void {
184 const Os = enum {184 const Os = enum {
185 Windows,185 Windows,
186 Macos,186 Macos,
...@@ -189,28 +189,28 @@ fn testEnum() void {...@@ -189,28 +189,28 @@ fn testEnum() void {
189 };189 };
190190
191 const os_info = @typeInfo(Os);191 const os_info = @typeInfo(Os);
192 expect(os_info == .Enum);192 try expect(os_info == .Enum);
193 expect(os_info.Enum.layout == .Auto);193 try expect(os_info.Enum.layout == .Auto);
194 expect(os_info.Enum.fields.len == 4);194 try expect(os_info.Enum.fields.len == 4);
195 expect(mem.eql(u8, os_info.Enum.fields[1].name, "Macos"));195 try expect(mem.eql(u8, os_info.Enum.fields[1].name, "Macos"));
196 expect(os_info.Enum.fields[3].value == 3);196 try expect(os_info.Enum.fields[3].value == 3);
197 expect(os_info.Enum.tag_type == u2);197 try expect(os_info.Enum.tag_type == u2);
198 expect(os_info.Enum.decls.len == 0);198 try expect(os_info.Enum.decls.len == 0);
199}199}
200200
201test "type info: union info" {201test "type info: union info" {
202 testUnion();202 try testUnion();
203 comptime testUnion();203 comptime try testUnion();
204}204}
205205
206fn testUnion() void {206fn testUnion() !void {
207 const typeinfo_info = @typeInfo(TypeInfo);207 const typeinfo_info = @typeInfo(TypeInfo);
208 expect(typeinfo_info == .Union);208 try expect(typeinfo_info == .Union);
209 expect(typeinfo_info.Union.layout == .Auto);209 try expect(typeinfo_info.Union.layout == .Auto);
210 expect(typeinfo_info.Union.tag_type.? == TypeId);210 try expect(typeinfo_info.Union.tag_type.? == TypeId);
211 expect(typeinfo_info.Union.fields.len == 25);211 try expect(typeinfo_info.Union.fields.len == 25);
212 expect(typeinfo_info.Union.fields[4].field_type == @TypeOf(@typeInfo(u8).Int));212 try expect(typeinfo_info.Union.fields[4].field_type == @TypeOf(@typeInfo(u8).Int));
213 expect(typeinfo_info.Union.decls.len == 22);213 try expect(typeinfo_info.Union.decls.len == 22);
214214
215 const TestNoTagUnion = union {215 const TestNoTagUnion = union {
216 Foo: void,216 Foo: void,
...@@ -218,52 +218,52 @@ fn testUnion() void {...@@ -218,52 +218,52 @@ fn testUnion() void {
218 };218 };
219219
220 const notag_union_info = @typeInfo(TestNoTagUnion);220 const notag_union_info = @typeInfo(TestNoTagUnion);
221 expect(notag_union_info == .Union);221 try expect(notag_union_info == .Union);
222 expect(notag_union_info.Union.tag_type == null);222 try expect(notag_union_info.Union.tag_type == null);
223 expect(notag_union_info.Union.layout == .Auto);223 try expect(notag_union_info.Union.layout == .Auto);
224 expect(notag_union_info.Union.fields.len == 2);224 try expect(notag_union_info.Union.fields.len == 2);
225 expect(notag_union_info.Union.fields[0].alignment == @alignOf(void));225 try expect(notag_union_info.Union.fields[0].alignment == @alignOf(void));
226 expect(notag_union_info.Union.fields[1].field_type == u32);226 try expect(notag_union_info.Union.fields[1].field_type == u32);
227 expect(notag_union_info.Union.fields[1].alignment == @alignOf(u32));227 try expect(notag_union_info.Union.fields[1].alignment == @alignOf(u32));
228228
229 const TestExternUnion = extern union {229 const TestExternUnion = extern union {
230 foo: *c_void,230 foo: *c_void,
231 };231 };
232232
233 const extern_union_info = @typeInfo(TestExternUnion);233 const extern_union_info = @typeInfo(TestExternUnion);
234 expect(extern_union_info.Union.layout == .Extern);234 try expect(extern_union_info.Union.layout == .Extern);
235 expect(extern_union_info.Union.tag_type == null);235 try expect(extern_union_info.Union.tag_type == null);
236 expect(extern_union_info.Union.fields[0].field_type == *c_void);236 try expect(extern_union_info.Union.fields[0].field_type == *c_void);
237}237}
238238
239test "type info: struct info" {239test "type info: struct info" {
240 testStruct();240 try testStruct();
241 comptime testStruct();241 comptime try testStruct();
242}242}
243243
244fn testStruct() void {244fn testStruct() !void {
245 const unpacked_struct_info = @typeInfo(TestUnpackedStruct);245 const unpacked_struct_info = @typeInfo(TestUnpackedStruct);
246 expect(unpacked_struct_info.Struct.is_tuple == false);246 try expect(unpacked_struct_info.Struct.is_tuple == false);
247 expect(unpacked_struct_info.Struct.fields[0].alignment == @alignOf(u32));247 try expect(unpacked_struct_info.Struct.fields[0].alignment == @alignOf(u32));
248 expect(unpacked_struct_info.Struct.fields[0].default_value.? == 4);248 try expect(unpacked_struct_info.Struct.fields[0].default_value.? == 4);
249 expectEqualStrings("foobar", unpacked_struct_info.Struct.fields[1].default_value.?);249 try expectEqualStrings("foobar", unpacked_struct_info.Struct.fields[1].default_value.?);
250250
251 const struct_info = @typeInfo(TestStruct);251 const struct_info = @typeInfo(TestStruct);
252 expect(struct_info == .Struct);252 try expect(struct_info == .Struct);
253 expect(struct_info.Struct.is_tuple == false);253 try expect(struct_info.Struct.is_tuple == false);
254 expect(struct_info.Struct.layout == .Packed);254 try expect(struct_info.Struct.layout == .Packed);
255 expect(struct_info.Struct.fields.len == 4);255 try expect(struct_info.Struct.fields.len == 4);
256 expect(struct_info.Struct.fields[0].alignment == 2 * @alignOf(usize));256 try expect(struct_info.Struct.fields[0].alignment == 2 * @alignOf(usize));
257 expect(struct_info.Struct.fields[2].field_type == *TestStruct);257 try expect(struct_info.Struct.fields[2].field_type == *TestStruct);
258 expect(struct_info.Struct.fields[2].default_value == null);258 try expect(struct_info.Struct.fields[2].default_value == null);
259 expect(struct_info.Struct.fields[3].default_value.? == 4);259 try expect(struct_info.Struct.fields[3].default_value.? == 4);
260 expect(struct_info.Struct.fields[3].alignment == 1);260 try expect(struct_info.Struct.fields[3].alignment == 1);
261 expect(struct_info.Struct.decls.len == 2);261 try expect(struct_info.Struct.decls.len == 2);
262 expect(struct_info.Struct.decls[0].is_pub);262 try expect(struct_info.Struct.decls[0].is_pub);
263 expect(!struct_info.Struct.decls[0].data.Fn.is_extern);263 try expect(!struct_info.Struct.decls[0].data.Fn.is_extern);
264 expect(struct_info.Struct.decls[0].data.Fn.lib_name == null);264 try expect(struct_info.Struct.decls[0].data.Fn.lib_name == null);
265 expect(struct_info.Struct.decls[0].data.Fn.return_type == void);265 try expect(struct_info.Struct.decls[0].data.Fn.return_type == void);
266 expect(struct_info.Struct.decls[0].data.Fn.fn_type == fn (*const TestStruct) void);266 try expect(struct_info.Struct.decls[0].data.Fn.fn_type == fn (*const TestStruct) void);
267}267}
268268
269const TestUnpackedStruct = struct {269const TestUnpackedStruct = struct {
...@@ -282,43 +282,43 @@ const TestStruct = packed struct {...@@ -282,43 +282,43 @@ const TestStruct = packed struct {
282};282};
283283
284test "type info: opaque info" {284test "type info: opaque info" {
285 testOpaque();285 try testOpaque();
286 comptime testOpaque();286 comptime try testOpaque();
287}287}
288288
289fn testOpaque() void {289fn testOpaque() !void {
290 const Foo = opaque {290 const Foo = opaque {
291 const A = 1;291 const A = 1;
292 fn b() void {}292 fn b() void {}
293 };293 };
294294
295 const foo_info = @typeInfo(Foo);295 const foo_info = @typeInfo(Foo);
296 expect(foo_info.Opaque.decls.len == 2);296 try expect(foo_info.Opaque.decls.len == 2);
297}297}
298298
299test "type info: function type info" {299test "type info: function type info" {
300 // wasm doesn't support align attributes on functions300 // wasm doesn't support align attributes on functions
301 if (builtin.arch == .wasm32 or builtin.arch == .wasm64) return error.SkipZigTest;301 if (builtin.arch == .wasm32 or builtin.arch == .wasm64) return error.SkipZigTest;
302 testFunction();302 try testFunction();
303 comptime testFunction();303 comptime try testFunction();
304}304}
305305
306fn testFunction() void {306fn testFunction() !void {
307 const fn_info = @typeInfo(@TypeOf(foo));307 const fn_info = @typeInfo(@TypeOf(foo));
308 expect(fn_info == .Fn);308 try expect(fn_info == .Fn);
309 expect(fn_info.Fn.alignment > 0);309 try expect(fn_info.Fn.alignment > 0);
310 expect(fn_info.Fn.calling_convention == .C);310 try expect(fn_info.Fn.calling_convention == .C);
311 expect(!fn_info.Fn.is_generic);311 try expect(!fn_info.Fn.is_generic);
312 expect(fn_info.Fn.args.len == 2);312 try expect(fn_info.Fn.args.len == 2);
313 expect(fn_info.Fn.is_var_args);313 try expect(fn_info.Fn.is_var_args);
314 expect(fn_info.Fn.return_type.? == usize);314 try expect(fn_info.Fn.return_type.? == usize);
315 const fn_aligned_info = @typeInfo(@TypeOf(fooAligned));315 const fn_aligned_info = @typeInfo(@TypeOf(fooAligned));
316 expect(fn_aligned_info.Fn.alignment == 4);316 try expect(fn_aligned_info.Fn.alignment == 4);
317317
318 const test_instance: TestStruct = undefined;318 const test_instance: TestStruct = undefined;
319 const bound_fn_info = @typeInfo(@TypeOf(test_instance.foo));319 const bound_fn_info = @typeInfo(@TypeOf(test_instance.foo));
320 expect(bound_fn_info == .BoundFn);320 try expect(bound_fn_info == .BoundFn);
321 expect(bound_fn_info.BoundFn.args[0].arg_type.? == *const TestStruct);321 try expect(bound_fn_info.BoundFn.args[0].arg_type.? == *const TestStruct);
322}322}
323323
324extern fn foo(a: usize, b: bool, ...) callconv(.C) usize;324extern fn foo(a: usize, b: bool, ...) callconv(.C) usize;
...@@ -332,33 +332,33 @@ test "typeInfo with comptime parameter in struct fn def" {...@@ -332,33 +332,33 @@ test "typeInfo with comptime parameter in struct fn def" {
332}332}
333333
334test "type info: vectors" {334test "type info: vectors" {
335 testVector();335 try testVector();
336 comptime testVector();336 comptime try testVector();
337}337}
338338
339fn testVector() void {339fn testVector() !void {
340 const vec_info = @typeInfo(std.meta.Vector(4, i32));340 const vec_info = @typeInfo(std.meta.Vector(4, i32));
341 expect(vec_info == .Vector);341 try expect(vec_info == .Vector);
342 expect(vec_info.Vector.len == 4);342 try expect(vec_info.Vector.len == 4);
343 expect(vec_info.Vector.child == i32);343 try expect(vec_info.Vector.child == i32);
344}344}
345345
346test "type info: anyframe and anyframe->T" {346test "type info: anyframe and anyframe->T" {
347 testAnyFrame();347 try testAnyFrame();
348 comptime testAnyFrame();348 comptime try testAnyFrame();
349}349}
350350
351fn testAnyFrame() void {351fn testAnyFrame() !void {
352 {352 {
353 const anyframe_info = @typeInfo(anyframe->i32);353 const anyframe_info = @typeInfo(anyframe->i32);
354 expect(anyframe_info == .AnyFrame);354 try expect(anyframe_info == .AnyFrame);
355 expect(anyframe_info.AnyFrame.child.? == i32);355 try expect(anyframe_info.AnyFrame.child.? == i32);
356 }356 }
357357
358 {358 {
359 const anyframe_info = @typeInfo(anyframe);359 const anyframe_info = @typeInfo(anyframe);
360 expect(anyframe_info == .AnyFrame);360 try expect(anyframe_info == .AnyFrame);
361 expect(anyframe_info.AnyFrame.child == null);361 try expect(anyframe_info.AnyFrame.child == null);
362 }362 }
363}363}
364364
...@@ -385,9 +385,9 @@ test "type info: extern fns with and without lib names" {...@@ -385,9 +385,9 @@ test "type info: extern fns with and without lib names" {
385 comptime {385 comptime {
386 for (info.Struct.decls) |decl| {386 for (info.Struct.decls) |decl| {
387 if (std.mem.eql(u8, decl.name, "bar1")) {387 if (std.mem.eql(u8, decl.name, "bar1")) {
388 expect(decl.data.Fn.lib_name == null);388 try expect(decl.data.Fn.lib_name == null);
389 } else {389 } else {
390 expectEqualStrings("cool", decl.data.Fn.lib_name.?);390 try expectEqualStrings("cool", decl.data.Fn.lib_name.?);
391 }391 }
392 }392 }
393 }393 }
...@@ -397,12 +397,12 @@ test "data field is a compile-time value" {...@@ -397,12 +397,12 @@ test "data field is a compile-time value" {
397 const S = struct {397 const S = struct {
398 const Bar = @as(isize, -1);398 const Bar = @as(isize, -1);
399 };399 };
400 comptime expect(@typeInfo(S).Struct.decls[0].data.Var == isize);400 comptime try expect(@typeInfo(S).Struct.decls[0].data.Var == isize);
401}401}
402402
403test "sentinel of opaque pointer type" {403test "sentinel of opaque pointer type" {
404 const c_void_info = @typeInfo(*c_void);404 const c_void_info = @typeInfo(*c_void);
405 expect(c_void_info.Pointer.sentinel == null);405 try expect(c_void_info.Pointer.sentinel == null);
406}406}
407407
408test "@typeInfo does not force declarations into existence" {408test "@typeInfo does not force declarations into existence" {
...@@ -413,12 +413,12 @@ test "@typeInfo does not force declarations into existence" {...@@ -413,12 +413,12 @@ test "@typeInfo does not force declarations into existence" {
413 @compileError("test failed");413 @compileError("test failed");
414 }414 }
415 };415 };
416 comptime expect(@typeInfo(S).Struct.fields.len == 1);416 comptime try expect(@typeInfo(S).Struct.fields.len == 1);
417}417}
418418
419test "defaut value for a var-typed field" {419test "defaut value for a var-typed field" {
420 const S = struct { x: anytype };420 const S = struct { x: anytype };
421 expect(@typeInfo(S).Struct.fields[0].default_value == null);421 try expect(@typeInfo(S).Struct.fields[0].default_value == null);
422}422}
423423
424fn add(a: i32, b: i32) i32 {424fn add(a: i32, b: i32) i32 {
...@@ -428,7 +428,7 @@ fn add(a: i32, b: i32) i32 {...@@ -428,7 +428,7 @@ fn add(a: i32, b: i32) i32 {
428test "type info for async frames" {428test "type info for async frames" {
429 switch (@typeInfo(@Frame(add))) {429 switch (@typeInfo(@Frame(add))) {
430 .Frame => |frame| {430 .Frame => |frame| {
431 expect(frame.function == add);431 try expect(frame.function == add);
432 },432 },
433 else => unreachable,433 else => unreachable,
434 }434 }
...@@ -438,7 +438,7 @@ test "type info: value is correctly copied" {...@@ -438,7 +438,7 @@ test "type info: value is correctly copied" {
438 comptime {438 comptime {
439 var ptrInfo = @typeInfo([]u32);439 var ptrInfo = @typeInfo([]u32);
440 ptrInfo.Pointer.size = .One;440 ptrInfo.Pointer.size = .One;
441 expect(@typeInfo([]u32).Pointer.size == .Slice);441 try expect(@typeInfo([]u32).Pointer.size == .Slice);
442 }442 }
443}443}
444444
...@@ -451,22 +451,22 @@ test "Declarations are returned in declaration order" {...@@ -451,22 +451,22 @@ test "Declarations are returned in declaration order" {
451 const e = 5;451 const e = 5;
452 };452 };
453 const d = @typeInfo(S).Struct.decls;453 const d = @typeInfo(S).Struct.decls;
454 expect(std.mem.eql(u8, d[0].name, "a"));454 try expect(std.mem.eql(u8, d[0].name, "a"));
455 expect(std.mem.eql(u8, d[1].name, "b"));455 try expect(std.mem.eql(u8, d[1].name, "b"));
456 expect(std.mem.eql(u8, d[2].name, "c"));456 try expect(std.mem.eql(u8, d[2].name, "c"));
457 expect(std.mem.eql(u8, d[3].name, "d"));457 try expect(std.mem.eql(u8, d[3].name, "d"));
458 expect(std.mem.eql(u8, d[4].name, "e"));458 try expect(std.mem.eql(u8, d[4].name, "e"));
459}459}
460460
461test "Struct.is_tuple" {461test "Struct.is_tuple" {
462 expect(@typeInfo(@TypeOf(.{0})).Struct.is_tuple);462 try expect(@typeInfo(@TypeOf(.{0})).Struct.is_tuple);
463 expect(!@typeInfo(@TypeOf(.{ .a = 0 })).Struct.is_tuple);463 try expect(!@typeInfo(@TypeOf(.{ .a = 0 })).Struct.is_tuple);
464}464}
465465
466test "StructField.is_comptime" {466test "StructField.is_comptime" {
467 const info = @typeInfo(struct { x: u8 = 3, comptime y: u32 = 5 }).Struct;467 const info = @typeInfo(struct { x: u8 = 3, comptime y: u32 = 5 }).Struct;
468 expect(!info.fields[0].is_comptime);468 try expect(!info.fields[0].is_comptime);
469 expect(info.fields[1].is_comptime);469 try expect(info.fields[1].is_comptime);
470}470}
471471
472test "typeInfo resolves usingnamespace declarations" {472test "typeInfo resolves usingnamespace declarations" {
...@@ -479,6 +479,6 @@ test "typeInfo resolves usingnamespace declarations" {...@@ -479,6 +479,6 @@ test "typeInfo resolves usingnamespace declarations" {
479 usingnamespace A;479 usingnamespace A;
480 };480 };
481481
482 expect(@typeInfo(B).Struct.decls.len == 2);482 try expect(@typeInfo(B).Struct.decls.len == 2);
483 //a483 //a
484}484}
test/stage1/behavior/typename.zig+1-1
...@@ -3,5 +3,5 @@ const expect = std.testing.expect;...@@ -3,5 +3,5 @@ const expect = std.testing.expect;
3const expectEqualSlices = std.testing.expectEqualSlices;3const expectEqualSlices = std.testing.expectEqualSlices;
44
5test "slice" {5test "slice" {
6 expectEqualSlices(u8, "[]u8", @typeName([]u8));6 try expectEqualSlices(u8, "[]u8", @typeName([]u8));
7}7}
test/stage1/behavior/undefined.zig+13-13
...@@ -12,16 +12,16 @@ fn initStaticArray() [10]i32 {...@@ -12,16 +12,16 @@ fn initStaticArray() [10]i32 {
12}12}
13const static_array = initStaticArray();13const static_array = initStaticArray();
14test "init static array to undefined" {14test "init static array to undefined" {
15 expect(static_array[0] == 1);15 try expect(static_array[0] == 1);
16 expect(static_array[4] == 2);16 try expect(static_array[4] == 2);
17 expect(static_array[7] == 3);17 try expect(static_array[7] == 3);
18 expect(static_array[9] == 4);18 try expect(static_array[9] == 4);
1919
20 comptime {20 comptime {
21 expect(static_array[0] == 1);21 try expect(static_array[0] == 1);
22 expect(static_array[4] == 2);22 try expect(static_array[4] == 2);
23 expect(static_array[7] == 3);23 try expect(static_array[7] == 3);
24 expect(static_array[9] == 4);24 try expect(static_array[9] == 4);
25 }25 }
26}26}
2727
...@@ -41,12 +41,12 @@ test "assign undefined to struct" {...@@ -41,12 +41,12 @@ test "assign undefined to struct" {
41 comptime {41 comptime {
42 var foo: Foo = undefined;42 var foo: Foo = undefined;
43 setFooX(&foo);43 setFooX(&foo);
44 expect(foo.x == 2);44 try expect(foo.x == 2);
45 }45 }
46 {46 {
47 var foo: Foo = undefined;47 var foo: Foo = undefined;
48 setFooX(&foo);48 setFooX(&foo);
49 expect(foo.x == 2);49 try expect(foo.x == 2);
50 }50 }
51}51}
5252
...@@ -54,16 +54,16 @@ test "assign undefined to struct with method" {...@@ -54,16 +54,16 @@ test "assign undefined to struct with method" {
54 comptime {54 comptime {
55 var foo: Foo = undefined;55 var foo: Foo = undefined;
56 foo.setFooXMethod();56 foo.setFooXMethod();
57 expect(foo.x == 3);57 try expect(foo.x == 3);
58 }58 }
59 {59 {
60 var foo: Foo = undefined;60 var foo: Foo = undefined;
61 foo.setFooXMethod();61 foo.setFooXMethod();
62 expect(foo.x == 3);62 try expect(foo.x == 3);
63 }63 }
64}64}
6565
66test "type name of undefined" {66test "type name of undefined" {
67 const x = undefined;67 const x = undefined;
68 expect(mem.eql(u8, @typeName(@TypeOf(x)), "(undefined)"));68 try expect(mem.eql(u8, @typeName(@TypeOf(x)), "(undefined)"));
69}69}
test/stage1/behavior/union.zig+142-142
...@@ -30,11 +30,11 @@ const array = [_]Value{...@@ -30,11 +30,11 @@ const array = [_]Value{
3030
31test "unions embedded in aggregate types" {31test "unions embedded in aggregate types" {
32 switch (array[1]) {32 switch (array[1]) {
33 Value.Array => |arr| expect(arr[4] == 3),33 Value.Array => |arr| try expect(arr[4] == 3),
34 else => unreachable,34 else => unreachable,
35 }35 }
36 switch ((err catch unreachable).val1) {36 switch ((err catch unreachable).val1) {
37 Value.Int => |x| expect(x == 1234),37 Value.Int => |x| try expect(x == 1234),
38 else => unreachable,38 else => unreachable,
39 }39 }
40}40}
...@@ -46,18 +46,18 @@ const Foo = union {...@@ -46,18 +46,18 @@ const Foo = union {
4646
47test "basic unions" {47test "basic unions" {
48 var foo = Foo{ .int = 1 };48 var foo = Foo{ .int = 1 };
49 expect(foo.int == 1);49 try expect(foo.int == 1);
50 foo = Foo{ .float = 12.34 };50 foo = Foo{ .float = 12.34 };
51 expect(foo.float == 12.34);51 try expect(foo.float == 12.34);
52}52}
5353
54test "comptime union field access" {54test "comptime union field access" {
55 comptime {55 comptime {
56 var foo = Foo{ .int = 0 };56 var foo = Foo{ .int = 0 };
57 expect(foo.int == 0);57 try expect(foo.int == 0);
5858
59 foo = Foo{ .float = 42.42 };59 foo = Foo{ .float = 42.42 };
60 expect(foo.float == 42.42);60 try expect(foo.float == 42.42);
61 }61 }
62}62}
6363
...@@ -65,10 +65,10 @@ test "init union with runtime value" {...@@ -65,10 +65,10 @@ test "init union with runtime value" {
65 var foo: Foo = undefined;65 var foo: Foo = undefined;
6666
67 setFloat(&foo, 12.34);67 setFloat(&foo, 12.34);
68 expect(foo.float == 12.34);68 try expect(foo.float == 12.34);
6969
70 setInt(&foo, 42);70 setInt(&foo, 42);
71 expect(foo.int == 42);71 try expect(foo.int == 42);
72}72}
7373
74fn setFloat(foo: *Foo, x: f64) void {74fn setFloat(foo: *Foo, x: f64) void {
...@@ -86,9 +86,9 @@ const FooExtern = extern union {...@@ -86,9 +86,9 @@ const FooExtern = extern union {
8686
87test "basic extern unions" {87test "basic extern unions" {
88 var foo = FooExtern{ .int = 1 };88 var foo = FooExtern{ .int = 1 };
89 expect(foo.int == 1);89 try expect(foo.int == 1);
90 foo.float = 12.34;90 foo.float = 12.34;
91 expect(foo.float == 12.34);91 try expect(foo.float == 12.34);
92}92}
9393
94const Letter = enum {94const Letter = enum {
...@@ -103,16 +103,16 @@ const Payload = union(Letter) {...@@ -103,16 +103,16 @@ const Payload = union(Letter) {
103};103};
104104
105test "union with specified enum tag" {105test "union with specified enum tag" {
106 doTest();106 try doTest();
107 comptime doTest();107 comptime try doTest();
108}108}
109109
110fn doTest() void {110fn doTest() !void {
111 expect(bar(Payload{ .A = 1234 }) == -10);111 try expect((try bar(Payload{ .A = 1234 })) == -10);
112}112}
113113
114fn bar(value: Payload) i32 {114fn bar(value: Payload) !i32 {
115 expect(@as(Letter, value) == Letter.A);115 try expect(@as(Letter, value) == Letter.A);
116 return switch (value) {116 return switch (value) {
117 Payload.A => |x| return x - 1244,117 Payload.A => |x| return x - 1244,
118 Payload.B => |x| if (x == 12.34) @as(i32, 20) else 21,118 Payload.B => |x| if (x == 12.34) @as(i32, 20) else 21,
...@@ -128,8 +128,8 @@ const MultipleChoice = union(enum(u32)) {...@@ -128,8 +128,8 @@ const MultipleChoice = union(enum(u32)) {
128};128};
129test "simple union(enum(u32))" {129test "simple union(enum(u32))" {
130 var x = MultipleChoice.C;130 var x = MultipleChoice.C;
131 expect(x == MultipleChoice.C);131 try expect(x == MultipleChoice.C);
132 expect(@enumToInt(@as(Tag(MultipleChoice), x)) == 60);132 try expect(@enumToInt(@as(Tag(MultipleChoice), x)) == 60);
133}133}
134134
135const MultipleChoice2 = union(enum(u32)) {135const MultipleChoice2 = union(enum(u32)) {
...@@ -145,14 +145,14 @@ const MultipleChoice2 = union(enum(u32)) {...@@ -145,14 +145,14 @@ const MultipleChoice2 = union(enum(u32)) {
145};145};
146146
147test "union(enum(u32)) with specified and unspecified tag values" {147test "union(enum(u32)) with specified and unspecified tag values" {
148 comptime expect(Tag(Tag(MultipleChoice2)) == u32);148 comptime try expect(Tag(Tag(MultipleChoice2)) == u32);
149 testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2{ .C = 123 });149 try testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2{ .C = 123 });
150 comptime testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2{ .C = 123 });150 comptime try testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2{ .C = 123 });
151}151}
152152
153fn testEnumWithSpecifiedAndUnspecifiedTagValues(x: MultipleChoice2) void {153fn testEnumWithSpecifiedAndUnspecifiedTagValues(x: MultipleChoice2) !void {
154 expect(@enumToInt(@as(Tag(MultipleChoice2), x)) == 60);154 try expect(@enumToInt(@as(Tag(MultipleChoice2), x)) == 60);
155 expect(1123 == switch (x) {155 try expect(1123 == switch (x) {
156 MultipleChoice2.A => 1,156 MultipleChoice2.A => 1,
157 MultipleChoice2.B => 2,157 MultipleChoice2.B => 2,
158 MultipleChoice2.C => |v| @as(i32, 1000) + v,158 MultipleChoice2.C => |v| @as(i32, 1000) + v,
...@@ -170,7 +170,7 @@ const ExternPtrOrInt = extern union {...@@ -170,7 +170,7 @@ const ExternPtrOrInt = extern union {
170 int: u64,170 int: u64,
171};171};
172test "extern union size" {172test "extern union size" {
173 comptime expect(@sizeOf(ExternPtrOrInt) == 8);173 comptime try expect(@sizeOf(ExternPtrOrInt) == 8);
174}174}
175175
176const PackedPtrOrInt = packed union {176const PackedPtrOrInt = packed union {
...@@ -178,14 +178,14 @@ const PackedPtrOrInt = packed union {...@@ -178,14 +178,14 @@ const PackedPtrOrInt = packed union {
178 int: u64,178 int: u64,
179};179};
180test "extern union size" {180test "extern union size" {
181 comptime expect(@sizeOf(PackedPtrOrInt) == 8);181 comptime try expect(@sizeOf(PackedPtrOrInt) == 8);
182}182}
183183
184const ZeroBits = union {184const ZeroBits = union {
185 OnlyField: void,185 OnlyField: void,
186};186};
187test "union with only 1 field which is void should be zero bits" {187test "union with only 1 field which is void should be zero bits" {
188 comptime expect(@sizeOf(ZeroBits) == 0);188 comptime try expect(@sizeOf(ZeroBits) == 0);
189}189}
190190
191const TheTag = enum {191const TheTag = enum {
...@@ -199,23 +199,23 @@ const TheUnion = union(TheTag) {...@@ -199,23 +199,23 @@ const TheUnion = union(TheTag) {
199 C: i32,199 C: i32,
200};200};
201test "union field access gives the enum values" {201test "union field access gives the enum values" {
202 expect(TheUnion.A == TheTag.A);202 try expect(TheUnion.A == TheTag.A);
203 expect(TheUnion.B == TheTag.B);203 try expect(TheUnion.B == TheTag.B);
204 expect(TheUnion.C == TheTag.C);204 try expect(TheUnion.C == TheTag.C);
205}205}
206206
207test "cast union to tag type of union" {207test "cast union to tag type of union" {
208 testCastUnionToTag(TheUnion{ .B = 1234 });208 try testCastUnionToTag(TheUnion{ .B = 1234 });
209 comptime testCastUnionToTag(TheUnion{ .B = 1234 });209 comptime try testCastUnionToTag(TheUnion{ .B = 1234 });
210}210}
211211
212fn testCastUnionToTag(x: TheUnion) void {212fn testCastUnionToTag(x: TheUnion) !void {
213 expect(@as(TheTag, x) == TheTag.B);213 try expect(@as(TheTag, x) == TheTag.B);
214}214}
215215
216test "cast tag type of union to union" {216test "cast tag type of union to union" {
217 var x: Value2 = Letter2.B;217 var x: Value2 = Letter2.B;
218 expect(@as(Letter2, x) == Letter2.B);218 try expect(@as(Letter2, x) == Letter2.B);
219}219}
220const Letter2 = enum {220const Letter2 = enum {
221 A,221 A,
...@@ -230,11 +230,11 @@ const Value2 = union(Letter2) {...@@ -230,11 +230,11 @@ const Value2 = union(Letter2) {
230230
231test "implicit cast union to its tag type" {231test "implicit cast union to its tag type" {
232 var x: Value2 = Letter2.B;232 var x: Value2 = Letter2.B;
233 expect(x == Letter2.B);233 try expect(x == Letter2.B);
234 giveMeLetterB(x);234 try giveMeLetterB(x);
235}235}
236fn giveMeLetterB(x: Letter2) void {236fn giveMeLetterB(x: Letter2) !void {
237 expect(x == Value2.B);237 try expect(x == Value2.B);
238}238}
239239
240pub const PackThis = union(enum) {240pub const PackThis = union(enum) {
...@@ -243,11 +243,11 @@ pub const PackThis = union(enum) {...@@ -243,11 +243,11 @@ pub const PackThis = union(enum) {
243};243};
244244
245test "constant packed union" {245test "constant packed union" {
246 testConstPackedUnion(&[_]PackThis{PackThis{ .StringLiteral = 1 }});246 try testConstPackedUnion(&[_]PackThis{PackThis{ .StringLiteral = 1 }});
247}247}
248248
249fn testConstPackedUnion(expected_tokens: []const PackThis) void {249fn testConstPackedUnion(expected_tokens: []const PackThis) !void {
250 expect(expected_tokens[0].StringLiteral == 1);250 try expect(expected_tokens[0].StringLiteral == 1);
251}251}
252252
253test "switch on union with only 1 field" {253test "switch on union with only 1 field" {
...@@ -259,7 +259,7 @@ test "switch on union with only 1 field" {...@@ -259,7 +259,7 @@ test "switch on union with only 1 field" {
259 z = PartialInstWithPayload{ .Compiled = 1234 };259 z = PartialInstWithPayload{ .Compiled = 1234 };
260 switch (z) {260 switch (z) {
261 PartialInstWithPayload.Compiled => |x| {261 PartialInstWithPayload.Compiled => |x| {
262 expect(x == 1234);262 try expect(x == 1234);
263 return;263 return;
264 },264 },
265 }265 }
...@@ -285,11 +285,11 @@ test "access a member of tagged union with conflicting enum tag name" {...@@ -285,11 +285,11 @@ test "access a member of tagged union with conflicting enum tag name" {
285 const B = void;285 const B = void;
286 };286 };
287287
288 comptime expect(Bar.A == u8);288 comptime try expect(Bar.A == u8);
289}289}
290290
291test "tagged union initialization with runtime void" {291test "tagged union initialization with runtime void" {
292 expect(testTaggedUnionInit({}));292 try expect(testTaggedUnionInit({}));
293}293}
294294
295const TaggedUnionWithAVoid = union(enum) {295const TaggedUnionWithAVoid = union(enum) {
...@@ -327,9 +327,9 @@ test "union with only 1 field casted to its enum type" {...@@ -327,9 +327,9 @@ test "union with only 1 field casted to its enum type" {
327327
328 var e = Expr{ .Literal = Literal{ .Bool = true } };328 var e = Expr{ .Literal = Literal{ .Bool = true } };
329 const ExprTag = Tag(Expr);329 const ExprTag = Tag(Expr);
330 comptime expect(Tag(ExprTag) == u0);330 comptime try expect(Tag(ExprTag) == u0);
331 var t = @as(ExprTag, e);331 var t = @as(ExprTag, e);
332 expect(t == Expr.Literal);332 try expect(t == Expr.Literal);
333}333}
334334
335test "union with only 1 field casted to its enum type which has enum value specified" {335test "union with only 1 field casted to its enum type which has enum value specified" {
...@@ -347,11 +347,11 @@ test "union with only 1 field casted to its enum type which has enum value speci...@@ -347,11 +347,11 @@ test "union with only 1 field casted to its enum type which has enum value speci
347 };347 };
348348
349 var e = Expr{ .Literal = Literal{ .Bool = true } };349 var e = Expr{ .Literal = Literal{ .Bool = true } };
350 comptime expect(Tag(ExprTag) == comptime_int);350 comptime try expect(Tag(ExprTag) == comptime_int);
351 var t = @as(ExprTag, e);351 var t = @as(ExprTag, e);
352 expect(t == Expr.Literal);352 try expect(t == Expr.Literal);
353 expect(@enumToInt(t) == 33);353 try expect(@enumToInt(t) == 33);
354 comptime expect(@enumToInt(t) == 33);354 comptime try expect(@enumToInt(t) == 33);
355}355}
356356
357test "@enumToInt works on unions" {357test "@enumToInt works on unions" {
...@@ -364,9 +364,9 @@ test "@enumToInt works on unions" {...@@ -364,9 +364,9 @@ test "@enumToInt works on unions" {
364 const a = Bar{ .A = true };364 const a = Bar{ .A = true };
365 var b = Bar{ .B = undefined };365 var b = Bar{ .B = undefined };
366 var c = Bar.C;366 var c = Bar.C;
367 expect(@enumToInt(a) == 0);367 try expect(@enumToInt(a) == 0);
368 expect(@enumToInt(b) == 1);368 try expect(@enumToInt(b) == 1);
369 expect(@enumToInt(c) == 2);369 try expect(@enumToInt(c) == 2);
370}370}
371371
372const Attribute = union(enum) {372const Attribute = union(enum) {
...@@ -393,23 +393,23 @@ test "comptime union field value equality" {...@@ -393,23 +393,23 @@ test "comptime union field value equality" {
393 const b1 = Setter(Attribute{ .B = 9 });393 const b1 = Setter(Attribute{ .B = 9 });
394 const b2 = Setter(Attribute{ .B = 5 });394 const b2 = Setter(Attribute{ .B = 5 });
395395
396 expect(a0 == a0);396 try expect(a0 == a0);
397 expect(a1 == a1);397 try expect(a1 == a1);
398 expect(a0 == a2);398 try expect(a0 == a2);
399399
400 expect(b0 == b0);400 try expect(b0 == b0);
401 expect(b1 == b1);401 try expect(b1 == b1);
402 expect(b0 == b2);402 try expect(b0 == b2);
403403
404 expect(a0 != b0);404 try expect(a0 != b0);
405 expect(a0 != a1);405 try expect(a0 != a1);
406 expect(b0 != b1);406 try expect(b0 != b1);
407}407}
408408
409test "return union init with void payload" {409test "return union init with void payload" {
410 const S = struct {410 const S = struct {
411 fn entry() void {411 fn entry() !void {
412 expect(func().state == State.one);412 try expect(func().state == State.one);
413 }413 }
414 const Outer = union(enum) {414 const Outer = union(enum) {
415 state: State,415 state: State,
...@@ -422,8 +422,8 @@ test "return union init with void payload" {...@@ -422,8 +422,8 @@ test "return union init with void payload" {
422 return Outer{ .state = State{ .one = {} } };422 return Outer{ .state = State{ .one = {} } };
423 }423 }
424 };424 };
425 S.entry();425 try S.entry();
426 comptime S.entry();426 comptime try S.entry();
427}427}
428428
429test "@unionInit can modify a union type" {429test "@unionInit can modify a union type" {
...@@ -435,14 +435,14 @@ test "@unionInit can modify a union type" {...@@ -435,14 +435,14 @@ test "@unionInit can modify a union type" {
435 var value: UnionInitEnum = undefined;435 var value: UnionInitEnum = undefined;
436436
437 value = @unionInit(UnionInitEnum, "Boolean", true);437 value = @unionInit(UnionInitEnum, "Boolean", true);
438 expect(value.Boolean == true);438 try expect(value.Boolean == true);
439 value.Boolean = false;439 value.Boolean = false;
440 expect(value.Boolean == false);440 try expect(value.Boolean == false);
441441
442 value = @unionInit(UnionInitEnum, "Byte", 2);442 value = @unionInit(UnionInitEnum, "Byte", 2);
443 expect(value.Byte == 2);443 try expect(value.Byte == 2);
444 value.Byte = 3;444 value.Byte = 3;
445 expect(value.Byte == 3);445 try expect(value.Byte == 3);
446}446}
447447
448test "@unionInit can modify a pointer value" {448test "@unionInit can modify a pointer value" {
...@@ -455,10 +455,10 @@ test "@unionInit can modify a pointer value" {...@@ -455,10 +455,10 @@ test "@unionInit can modify a pointer value" {
455 var value_ptr = &value;455 var value_ptr = &value;
456456
457 value_ptr.* = @unionInit(UnionInitEnum, "Boolean", true);457 value_ptr.* = @unionInit(UnionInitEnum, "Boolean", true);
458 expect(value.Boolean == true);458 try expect(value.Boolean == true);
459459
460 value_ptr.* = @unionInit(UnionInitEnum, "Byte", 2);460 value_ptr.* = @unionInit(UnionInitEnum, "Byte", 2);
461 expect(value.Byte == 2);461 try expect(value.Byte == 2);
462}462}
463463
464test "union no tag with struct member" {464test "union no tag with struct member" {
...@@ -471,38 +471,38 @@ test "union no tag with struct member" {...@@ -471,38 +471,38 @@ test "union no tag with struct member" {
471 u.foo();471 u.foo();
472}472}
473473
474fn testComparison() void {474fn testComparison() !void {
475 var x = Payload{ .A = 42 };475 var x = Payload{ .A = 42 };
476 expect(x == .A);476 try expect(x == .A);
477 expect(x != .B);477 try expect(x != .B);
478 expect(x != .C);478 try expect(x != .C);
479 expect((x == .B) == false);479 try expect((x == .B) == false);
480 expect((x == .C) == false);480 try expect((x == .C) == false);
481 expect((x != .A) == false);481 try expect((x != .A) == false);
482}482}
483483
484test "comparison between union and enum literal" {484test "comparison between union and enum literal" {
485 testComparison();485 try testComparison();
486 comptime testComparison();486 comptime try testComparison();
487}487}
488488
489test "packed union generates correctly aligned LLVM type" {489test "packed union generates correctly aligned LLVM type" {
490 const U = packed union {490 const U = packed union {
491 f1: fn () void,491 f1: fn () error{TestUnexpectedResult}!void,
492 f2: u32,492 f2: u32,
493 };493 };
494 var foo = [_]U{494 var foo = [_]U{
495 U{ .f1 = doTest },495 U{ .f1 = doTest },
496 U{ .f2 = 0 },496 U{ .f2 = 0 },
497 };497 };
498 foo[0].f1();498 try foo[0].f1();
499}499}
500500
501test "union with one member defaults to u0 tag type" {501test "union with one member defaults to u0 tag type" {
502 const U0 = union(enum) {502 const U0 = union(enum) {
503 X: u32,503 X: u32,
504 };504 };
505 comptime expect(Tag(Tag(U0)) == u0);505 comptime try expect(Tag(Tag(U0)) == u0);
506}506}
507507
508test "union with comptime_int tag" {508test "union with comptime_int tag" {
...@@ -511,7 +511,7 @@ test "union with comptime_int tag" {...@@ -511,7 +511,7 @@ test "union with comptime_int tag" {
511 Y: u16,511 Y: u16,
512 Z: u8,512 Z: u8,
513 };513 };
514 comptime expect(Tag(Tag(Union)) == comptime_int);514 comptime try expect(Tag(Tag(Union)) == comptime_int);
515}515}
516516
517test "extern union doesn't trigger field check at comptime" {517test "extern union doesn't trigger field check at comptime" {
...@@ -521,7 +521,7 @@ test "extern union doesn't trigger field check at comptime" {...@@ -521,7 +521,7 @@ test "extern union doesn't trigger field check at comptime" {
521 };521 };
522522
523 const x = U{ .x = 0x55AAAA55 };523 const x = U{ .x = 0x55AAAA55 };
524 comptime expect(x.y == 0x55);524 comptime try expect(x.y == 0x55);
525}525}
526526
527const Foo1 = union(enum) {527const Foo1 = union(enum) {
...@@ -535,7 +535,7 @@ test "global union with single field is correctly initialized" {...@@ -535,7 +535,7 @@ test "global union with single field is correctly initialized" {
535 glbl = Foo1{535 glbl = Foo1{
536 .f = @typeInfo(Foo1).Union.fields[0].field_type{ .x = 123 },536 .f = @typeInfo(Foo1).Union.fields[0].field_type{ .x = 123 },
537 };537 };
538 expect(glbl.f.x == 123);538 try expect(glbl.f.x == 123);
539}539}
540540
541pub const FooUnion = union(enum) {541pub const FooUnion = union(enum) {
...@@ -548,8 +548,8 @@ var glbl_array: [2]FooUnion = undefined;...@@ -548,8 +548,8 @@ var glbl_array: [2]FooUnion = undefined;
548test "initialize global array of union" {548test "initialize global array of union" {
549 glbl_array[1] = FooUnion{ .U1 = 2 };549 glbl_array[1] = FooUnion{ .U1 = 2 };
550 glbl_array[0] = FooUnion{ .U0 = 1 };550 glbl_array[0] = FooUnion{ .U0 = 1 };
551 expect(glbl_array[0].U0 == 1);551 try expect(glbl_array[0].U0 == 1);
552 expect(glbl_array[1].U1 == 2);552 try expect(glbl_array[1].U1 == 2);
553}553}
554554
555test "anonymous union literal syntax" {555test "anonymous union literal syntax" {
...@@ -559,19 +559,19 @@ test "anonymous union literal syntax" {...@@ -559,19 +559,19 @@ test "anonymous union literal syntax" {
559 float: f64,559 float: f64,
560 };560 };
561561
562 fn doTheTest() void {562 fn doTheTest() !void {
563 var i: Number = .{ .int = 42 };563 var i: Number = .{ .int = 42 };
564 var f = makeNumber();564 var f = makeNumber();
565 expect(i.int == 42);565 try expect(i.int == 42);
566 expect(f.float == 12.34);566 try expect(f.float == 12.34);
567 }567 }
568568
569 fn makeNumber() Number {569 fn makeNumber() Number {
570 return .{ .float = 12.34 };570 return .{ .float = 12.34 };
571 }571 }
572 };572 };
573 S.doTheTest();573 try S.doTheTest();
574 comptime S.doTheTest();574 comptime try S.doTheTest();
575}575}
576576
577test "update the tag value for zero-sized unions" {577test "update the tag value for zero-sized unions" {
...@@ -580,9 +580,9 @@ test "update the tag value for zero-sized unions" {...@@ -580,9 +580,9 @@ test "update the tag value for zero-sized unions" {
580 U1: void,580 U1: void,
581 };581 };
582 var x = S{ .U0 = {} };582 var x = S{ .U0 = {} };
583 expect(x == .U0);583 try expect(x == .U0);
584 x = S{ .U1 = {} };584 x = S{ .U1 = {} };
585 expect(x == .U1);585 try expect(x == .U1);
586}586}
587587
588test "function call result coerces from tagged union to the tag" {588test "function call result coerces from tagged union to the tag" {
...@@ -594,12 +594,12 @@ test "function call result coerces from tagged union to the tag" {...@@ -594,12 +594,12 @@ test "function call result coerces from tagged union to the tag" {
594594
595 const ArchTag = Tag(Arch);595 const ArchTag = Tag(Arch);
596596
597 fn doTheTest() void {597 fn doTheTest() !void {
598 var x: ArchTag = getArch1();598 var x: ArchTag = getArch1();
599 expect(x == .One);599 try expect(x == .One);
600600
601 var y: ArchTag = getArch2();601 var y: ArchTag = getArch2();
602 expect(y == .Two);602 try expect(y == .Two);
603 }603 }
604604
605 pub fn getArch1() Arch {605 pub fn getArch1() Arch {
...@@ -610,8 +610,8 @@ test "function call result coerces from tagged union to the tag" {...@@ -610,8 +610,8 @@ test "function call result coerces from tagged union to the tag" {
610 return .{ .Two = 99 };610 return .{ .Two = 99 };
611 }611 }
612 };612 };
613 S.doTheTest();613 try S.doTheTest();
614 comptime S.doTheTest();614 comptime try S.doTheTest();
615}615}
616616
617test "0-sized extern union definition" {617test "0-sized extern union definition" {
...@@ -620,7 +620,7 @@ test "0-sized extern union definition" {...@@ -620,7 +620,7 @@ test "0-sized extern union definition" {
620 const f = 1;620 const f = 1;
621 };621 };
622622
623 expect(U.f == 1);623 try expect(U.f == 1);
624}624}
625625
626test "union initializer generates padding only if needed" {626test "union initializer generates padding only if needed" {
...@@ -629,7 +629,7 @@ test "union initializer generates padding only if needed" {...@@ -629,7 +629,7 @@ test "union initializer generates padding only if needed" {
629 };629 };
630630
631 var v = U{ .A = 532 };631 var v = U{ .A = 532 };
632 expect(v.A == 532);632 try expect(v.A == 532);
633}633}
634634
635test "runtime tag name with single field" {635test "runtime tag name with single field" {
...@@ -638,7 +638,7 @@ test "runtime tag name with single field" {...@@ -638,7 +638,7 @@ test "runtime tag name with single field" {
638 };638 };
639639
640 var v = U{ .A = 42 };640 var v = U{ .A = 42 };
641 expect(std.mem.eql(u8, @tagName(v), "A"));641 try expect(std.mem.eql(u8, @tagName(v), "A"));
642}642}
643643
644test "cast from anonymous struct to union" {644test "cast from anonymous struct to union" {
...@@ -648,7 +648,7 @@ test "cast from anonymous struct to union" {...@@ -648,7 +648,7 @@ test "cast from anonymous struct to union" {
648 B: []const u8,648 B: []const u8,
649 C: void,649 C: void,
650 };650 };
651 fn doTheTest() void {651 fn doTheTest() !void {
652 var y: u32 = 42;652 var y: u32 = 42;
653 const t0 = .{ .A = 123 };653 const t0 = .{ .A = 123 };
654 const t1 = .{ .B = "foo" };654 const t1 = .{ .B = "foo" };
...@@ -658,14 +658,14 @@ test "cast from anonymous struct to union" {...@@ -658,14 +658,14 @@ test "cast from anonymous struct to union" {
658 var x1: U = t1;658 var x1: U = t1;
659 const x2: U = t2;659 const x2: U = t2;
660 var x3: U = t3;660 var x3: U = t3;
661 expect(x0.A == 123);661 try expect(x0.A == 123);
662 expect(std.mem.eql(u8, x1.B, "foo"));662 try expect(std.mem.eql(u8, x1.B, "foo"));
663 expect(x2 == .C);663 try expect(x2 == .C);
664 expect(x3.A == y);664 try expect(x3.A == y);
665 }665 }
666 };666 };
667 S.doTheTest();667 try S.doTheTest();
668 comptime S.doTheTest();668 comptime try S.doTheTest();
669}669}
670670
671test "cast from pointer to anonymous struct to pointer to union" {671test "cast from pointer to anonymous struct to pointer to union" {
...@@ -675,7 +675,7 @@ test "cast from pointer to anonymous struct to pointer to union" {...@@ -675,7 +675,7 @@ test "cast from pointer to anonymous struct to pointer to union" {
675 B: []const u8,675 B: []const u8,
676 C: void,676 C: void,
677 };677 };
678 fn doTheTest() void {678 fn doTheTest() !void {
679 var y: u32 = 42;679 var y: u32 = 42;
680 const t0 = &.{ .A = 123 };680 const t0 = &.{ .A = 123 };
681 const t1 = &.{ .B = "foo" };681 const t1 = &.{ .B = "foo" };
...@@ -685,14 +685,14 @@ test "cast from pointer to anonymous struct to pointer to union" {...@@ -685,14 +685,14 @@ test "cast from pointer to anonymous struct to pointer to union" {
685 var x1: *const U = t1;685 var x1: *const U = t1;
686 const x2: *const U = t2;686 const x2: *const U = t2;
687 var x3: *const U = t3;687 var x3: *const U = t3;
688 expect(x0.A == 123);688 try expect(x0.A == 123);
689 expect(std.mem.eql(u8, x1.B, "foo"));689 try expect(std.mem.eql(u8, x1.B, "foo"));
690 expect(x2.* == .C);690 try expect(x2.* == .C);
691 expect(x3.A == y);691 try expect(x3.A == y);
692 }692 }
693 };693 };
694 S.doTheTest();694 try S.doTheTest();
695 comptime S.doTheTest();695 comptime try S.doTheTest();
696}696}
697697
698test "method call on an empty union" {698test "method call on an empty union" {
...@@ -707,13 +707,13 @@ test "method call on an empty union" {...@@ -707,13 +707,13 @@ test "method call on an empty union" {
707 }707 }
708 };708 };
709709
710 fn doTheTest() void {710 fn doTheTest() !void {
711 var u = MyUnion{ .X1 = [0]u8{} };711 var u = MyUnion{ .X1 = [0]u8{} };
712 expect(u.useIt());712 try expect(u.useIt());
713 }713 }
714 };714 };
715 S.doTheTest();715 try S.doTheTest();
716 comptime S.doTheTest();716 comptime try S.doTheTest();
717}717}
718718
719test "switching on non exhaustive union" {719test "switching on non exhaustive union" {
...@@ -727,16 +727,16 @@ test "switching on non exhaustive union" {...@@ -727,16 +727,16 @@ test "switching on non exhaustive union" {
727 a: i32,727 a: i32,
728 b: u32,728 b: u32,
729 };729 };
730 fn doTheTest() void {730 fn doTheTest() !void {
731 var a = U{ .a = 2 };731 var a = U{ .a = 2 };
732 switch (a) {732 switch (a) {
733 .a => |val| expect(val == 2),733 .a => |val| try expect(val == 2),
734 .b => unreachable,734 .b => unreachable,
735 }735 }
736 }736 }
737 };737 };
738 S.doTheTest();738 try S.doTheTest();
739 comptime S.doTheTest();739 comptime try S.doTheTest();
740}740}
741741
742test "containers with single-field enums" {742test "containers with single-field enums" {
...@@ -746,21 +746,21 @@ test "containers with single-field enums" {...@@ -746,21 +746,21 @@ test "containers with single-field enums" {
746 const C = struct { a: A };746 const C = struct { a: A };
747 const D = struct { a: B };747 const D = struct { a: B };
748748
749 fn doTheTest() void {749 fn doTheTest() !void {
750 var array1 = [1]A{A{ .f1 = {} }};750 var array1 = [1]A{A{ .f1 = {} }};
751 var array2 = [1]B{B{ .f1 = {} }};751 var array2 = [1]B{B{ .f1 = {} }};
752 expect(array1[0] == .f1);752 try expect(array1[0] == .f1);
753 expect(array2[0] == .f1);753 try expect(array2[0] == .f1);
754754
755 var struct1 = C{ .a = A{ .f1 = {} } };755 var struct1 = C{ .a = A{ .f1 = {} } };
756 var struct2 = D{ .a = B{ .f1 = {} } };756 var struct2 = D{ .a = B{ .f1 = {} } };
757 expect(struct1.a == .f1);757 try expect(struct1.a == .f1);
758 expect(struct2.a == .f1);758 try expect(struct2.a == .f1);
759 }759 }
760 };760 };
761761
762 S.doTheTest();762 try S.doTheTest();
763 comptime S.doTheTest();763 comptime try S.doTheTest();
764}764}
765765
766test "@unionInit on union w/ tag but no fields" {766test "@unionInit on union w/ tag but no fields" {
...@@ -776,18 +776,18 @@ test "@unionInit on union w/ tag but no fields" {...@@ -776,18 +776,18 @@ test "@unionInit on union w/ tag but no fields" {
776 };776 };
777777
778 comptime {778 comptime {
779 expect(@sizeOf(Data) != 0);779 try expect(@sizeOf(Data) != 0);
780 }780 }
781781
782 fn doTheTest() void {782 fn doTheTest() !void {
783 var data: Data = .{ .no_op = .{} };783 var data: Data = .{ .no_op = .{} };
784 var o = Data.decode(&[_]u8{});784 var o = Data.decode(&[_]u8{});
785 expectEqual(Type.no_op, o);785 try expectEqual(Type.no_op, o);
786 }786 }
787 };787 };
788788
789 S.doTheTest();789 try S.doTheTest();
790 comptime S.doTheTest();790 comptime try S.doTheTest();
791}791}
792792
793test "union enum type gets a separate scope" {793test "union enum type gets a separate scope" {
...@@ -797,10 +797,10 @@ test "union enum type gets a separate scope" {...@@ -797,10 +797,10 @@ test "union enum type gets a separate scope" {
797 const foo = 1;797 const foo = 1;
798 };798 };
799799
800 fn doTheTest() void {800 fn doTheTest() !void {
801 expect(!@hasDecl(Tag(U), "foo"));801 try expect(!@hasDecl(Tag(U), "foo"));
802 }802 }
803 };803 };
804804
805 S.doTheTest();805 try S.doTheTest();
806}806}
test/stage1/behavior/usingnamespace.zig+3-3
...@@ -9,8 +9,8 @@ fn Foo(comptime T: type) type {...@@ -9,8 +9,8 @@ fn Foo(comptime T: type) type {
9test "usingnamespace inside a generic struct" {9test "usingnamespace inside a generic struct" {
10 const std2 = Foo(std);10 const std2 = Foo(std);
11 const testing2 = Foo(std.testing);11 const testing2 = Foo(std.testing);
12 std2.testing.expect(true);12 try std2.testing.expect(true);
13 testing2.expect(true);13 try testing2.expect(true);
14}14}
1515
16usingnamespace struct {16usingnamespace struct {
...@@ -18,5 +18,5 @@ usingnamespace struct {...@@ -18,5 +18,5 @@ usingnamespace struct {
18};18};
1919
20test "usingnamespace does not redeclare an imported variable" {20test "usingnamespace does not redeclare an imported variable" {
21 comptime std.testing.expect(foo == 42);21 comptime try std.testing.expect(foo == 42);
22}22}
test/stage1/behavior/var_args.zig+17-17
...@@ -12,9 +12,9 @@ fn add(args: anytype) i32 {...@@ -12,9 +12,9 @@ fn add(args: anytype) i32 {
12}12}
1313
14test "add arbitrary args" {14test "add arbitrary args" {
15 expect(add(.{ @as(i32, 1), @as(i32, 2), @as(i32, 3), @as(i32, 4) }) == 10);15 try expect(add(.{ @as(i32, 1), @as(i32, 2), @as(i32, 3), @as(i32, 4) }) == 10);
16 expect(add(.{@as(i32, 1234)}) == 1234);16 try expect(add(.{@as(i32, 1234)}) == 1234);
17 expect(add(.{}) == 0);17 try expect(add(.{}) == 0);
18}18}
1919
20fn readFirstVarArg(args: anytype) void {20fn readFirstVarArg(args: anytype) void {
...@@ -26,9 +26,9 @@ test "send void arg to var args" {...@@ -26,9 +26,9 @@ test "send void arg to var args" {
26}26}
2727
28test "pass args directly" {28test "pass args directly" {
29 expect(addSomeStuff(.{ @as(i32, 1), @as(i32, 2), @as(i32, 3), @as(i32, 4) }) == 10);29 try expect(addSomeStuff(.{ @as(i32, 1), @as(i32, 2), @as(i32, 3), @as(i32, 4) }) == 10);
30 expect(addSomeStuff(.{@as(i32, 1234)}) == 1234);30 try expect(addSomeStuff(.{@as(i32, 1234)}) == 1234);
31 expect(addSomeStuff(.{}) == 0);31 try expect(addSomeStuff(.{}) == 0);
32}32}
3333
34fn addSomeStuff(args: anytype) i32 {34fn addSomeStuff(args: anytype) i32 {
...@@ -36,23 +36,23 @@ fn addSomeStuff(args: anytype) i32 {...@@ -36,23 +36,23 @@ fn addSomeStuff(args: anytype) i32 {
36}36}
3737
38test "runtime parameter before var args" {38test "runtime parameter before var args" {
39 expect(extraFn(10, .{}) == 0);39 try expect((try extraFn(10, .{})) == 0);
40 expect(extraFn(10, .{false}) == 1);40 try expect((try extraFn(10, .{false})) == 1);
41 expect(extraFn(10, .{ false, true }) == 2);41 try expect((try extraFn(10, .{ false, true })) == 2);
4242
43 comptime {43 comptime {
44 expect(extraFn(10, .{}) == 0);44 try expect((try extraFn(10, .{})) == 0);
45 expect(extraFn(10, .{false}) == 1);45 try expect((try extraFn(10, .{false})) == 1);
46 expect(extraFn(10, .{ false, true }) == 2);46 try expect((try extraFn(10, .{ false, true })) == 2);
47 }47 }
48}48}
4949
50fn extraFn(extra: u32, args: anytype) usize {50fn extraFn(extra: u32, args: anytype) !usize {
51 if (args.len >= 1) {51 if (args.len >= 1) {
52 expect(args[0] == false);52 try expect(args[0] == false);
53 }53 }
54 if (args.len >= 2) {54 if (args.len >= 2) {
55 expect(args[1] == true);55 try expect(args[1] == true);
56 }56 }
57 return args.len;57 return args.len;
58}58}
...@@ -70,8 +70,8 @@ fn foo2(args: anytype) bool {...@@ -70,8 +70,8 @@ fn foo2(args: anytype) bool {
70}70}
7171
72test "array of var args functions" {72test "array of var args functions" {
73 expect(foos[0](.{}));73 try expect(foos[0](.{}));
74 expect(!foos[1](.{}));74 try expect(!foos[1](.{}));
75}75}
7676
77test "pass zero length array to var args param" {77test "pass zero length array to var args param" {
test/stage1/behavior/vector.zig+280-280
...@@ -9,104 +9,104 @@ const Vector = std.meta.Vector;...@@ -9,104 +9,104 @@ const Vector = std.meta.Vector;
99
10test "implicit cast vector to array - bool" {10test "implicit cast vector to array - bool" {
11 const S = struct {11 const S = struct {
12 fn doTheTest() void {12 fn doTheTest() !void {
13 const a: Vector(4, bool) = [_]bool{ true, false, true, false };13 const a: Vector(4, bool) = [_]bool{ true, false, true, false };
14 const result_array: [4]bool = a;14 const result_array: [4]bool = a;
15 expect(mem.eql(bool, &result_array, &[4]bool{ true, false, true, false }));15 try expect(mem.eql(bool, &result_array, &[4]bool{ true, false, true, false }));
16 }16 }
17 };17 };
18 S.doTheTest();18 try S.doTheTest();
19 comptime S.doTheTest();19 comptime try S.doTheTest();
20}20}
2121
22test "vector wrap operators" {22test "vector wrap operators" {
23 const S = struct {23 const S = struct {
24 fn doTheTest() void {24 fn doTheTest() !void {
25 var v: Vector(4, i32) = [4]i32{ 2147483647, -2, 30, 40 };25 var v: Vector(4, i32) = [4]i32{ 2147483647, -2, 30, 40 };
26 var x: Vector(4, i32) = [4]i32{ 1, 2147483647, 3, 4 };26 var x: Vector(4, i32) = [4]i32{ 1, 2147483647, 3, 4 };
27 expect(mem.eql(i32, &@as([4]i32, v +% x), &[4]i32{ -2147483648, 2147483645, 33, 44 }));27 try expect(mem.eql(i32, &@as([4]i32, v +% x), &[4]i32{ -2147483648, 2147483645, 33, 44 }));
28 expect(mem.eql(i32, &@as([4]i32, v -% x), &[4]i32{ 2147483646, 2147483647, 27, 36 }));28 try expect(mem.eql(i32, &@as([4]i32, v -% x), &[4]i32{ 2147483646, 2147483647, 27, 36 }));
29 expect(mem.eql(i32, &@as([4]i32, v *% x), &[4]i32{ 2147483647, 2, 90, 160 }));29 try expect(mem.eql(i32, &@as([4]i32, v *% x), &[4]i32{ 2147483647, 2, 90, 160 }));
30 var z: Vector(4, i32) = [4]i32{ 1, 2, 3, -2147483648 };30 var z: Vector(4, i32) = [4]i32{ 1, 2, 3, -2147483648 };
31 expect(mem.eql(i32, &@as([4]i32, -%z), &[4]i32{ -1, -2, -3, -2147483648 }));31 try expect(mem.eql(i32, &@as([4]i32, -%z), &[4]i32{ -1, -2, -3, -2147483648 }));
32 }32 }
33 };33 };
34 S.doTheTest();34 try S.doTheTest();
35 comptime S.doTheTest();35 comptime try S.doTheTest();
36}36}
3737
38test "vector bin compares with mem.eql" {38test "vector bin compares with mem.eql" {
39 const S = struct {39 const S = struct {
40 fn doTheTest() void {40 fn doTheTest() !void {
41 var v: Vector(4, i32) = [4]i32{ 2147483647, -2, 30, 40 };41 var v: Vector(4, i32) = [4]i32{ 2147483647, -2, 30, 40 };
42 var x: Vector(4, i32) = [4]i32{ 1, 2147483647, 30, 4 };42 var x: Vector(4, i32) = [4]i32{ 1, 2147483647, 30, 4 };
43 expect(mem.eql(bool, &@as([4]bool, v == x), &[4]bool{ false, false, true, false }));43 try expect(mem.eql(bool, &@as([4]bool, v == x), &[4]bool{ false, false, true, false }));
44 expect(mem.eql(bool, &@as([4]bool, v != x), &[4]bool{ true, true, false, true }));44 try expect(mem.eql(bool, &@as([4]bool, v != x), &[4]bool{ true, true, false, true }));
45 expect(mem.eql(bool, &@as([4]bool, v < x), &[4]bool{ false, true, false, false }));45 try expect(mem.eql(bool, &@as([4]bool, v < x), &[4]bool{ false, true, false, false }));
46 expect(mem.eql(bool, &@as([4]bool, v > x), &[4]bool{ true, false, false, true }));46 try expect(mem.eql(bool, &@as([4]bool, v > x), &[4]bool{ true, false, false, true }));
47 expect(mem.eql(bool, &@as([4]bool, v <= x), &[4]bool{ false, true, true, false }));47 try expect(mem.eql(bool, &@as([4]bool, v <= x), &[4]bool{ false, true, true, false }));
48 expect(mem.eql(bool, &@as([4]bool, v >= x), &[4]bool{ true, false, true, true }));48 try expect(mem.eql(bool, &@as([4]bool, v >= x), &[4]bool{ true, false, true, true }));
49 }49 }
50 };50 };
51 S.doTheTest();51 try S.doTheTest();
52 comptime S.doTheTest();52 comptime try S.doTheTest();
53}53}
5454
55test "vector int operators" {55test "vector int operators" {
56 const S = struct {56 const S = struct {
57 fn doTheTest() void {57 fn doTheTest() !void {
58 var v: Vector(4, i32) = [4]i32{ 10, 20, 30, 40 };58 var v: Vector(4, i32) = [4]i32{ 10, 20, 30, 40 };
59 var x: Vector(4, i32) = [4]i32{ 1, 2, 3, 4 };59 var x: Vector(4, i32) = [4]i32{ 1, 2, 3, 4 };
60 expect(mem.eql(i32, &@as([4]i32, v + x), &[4]i32{ 11, 22, 33, 44 }));60 try expect(mem.eql(i32, &@as([4]i32, v + x), &[4]i32{ 11, 22, 33, 44 }));
61 expect(mem.eql(i32, &@as([4]i32, v - x), &[4]i32{ 9, 18, 27, 36 }));61 try expect(mem.eql(i32, &@as([4]i32, v - x), &[4]i32{ 9, 18, 27, 36 }));
62 expect(mem.eql(i32, &@as([4]i32, v * x), &[4]i32{ 10, 40, 90, 160 }));62 try expect(mem.eql(i32, &@as([4]i32, v * x), &[4]i32{ 10, 40, 90, 160 }));
63 expect(mem.eql(i32, &@as([4]i32, -v), &[4]i32{ -10, -20, -30, -40 }));63 try expect(mem.eql(i32, &@as([4]i32, -v), &[4]i32{ -10, -20, -30, -40 }));
64 }64 }
65 };65 };
66 S.doTheTest();66 try S.doTheTest();
67 comptime S.doTheTest();67 comptime try S.doTheTest();
68}68}
6969
70test "vector float operators" {70test "vector float operators" {
71 const S = struct {71 const S = struct {
72 fn doTheTest() void {72 fn doTheTest() !void {
73 var v: Vector(4, f32) = [4]f32{ 10, 20, 30, 40 };73 var v: Vector(4, f32) = [4]f32{ 10, 20, 30, 40 };
74 var x: Vector(4, f32) = [4]f32{ 1, 2, 3, 4 };74 var x: Vector(4, f32) = [4]f32{ 1, 2, 3, 4 };
75 expect(mem.eql(f32, &@as([4]f32, v + x), &[4]f32{ 11, 22, 33, 44 }));75 try expect(mem.eql(f32, &@as([4]f32, v + x), &[4]f32{ 11, 22, 33, 44 }));
76 expect(mem.eql(f32, &@as([4]f32, v - x), &[4]f32{ 9, 18, 27, 36 }));76 try expect(mem.eql(f32, &@as([4]f32, v - x), &[4]f32{ 9, 18, 27, 36 }));
77 expect(mem.eql(f32, &@as([4]f32, v * x), &[4]f32{ 10, 40, 90, 160 }));77 try expect(mem.eql(f32, &@as([4]f32, v * x), &[4]f32{ 10, 40, 90, 160 }));
78 expect(mem.eql(f32, &@as([4]f32, -x), &[4]f32{ -1, -2, -3, -4 }));78 try expect(mem.eql(f32, &@as([4]f32, -x), &[4]f32{ -1, -2, -3, -4 }));
79 }79 }
80 };80 };
81 S.doTheTest();81 try S.doTheTest();
82 comptime S.doTheTest();82 comptime try S.doTheTest();
83}83}
8484
85test "vector bit operators" {85test "vector bit operators" {
86 const S = struct {86 const S = struct {
87 fn doTheTest() void {87 fn doTheTest() !void {
88 var v: Vector(4, u8) = [4]u8{ 0b10101010, 0b10101010, 0b10101010, 0b10101010 };88 var v: Vector(4, u8) = [4]u8{ 0b10101010, 0b10101010, 0b10101010, 0b10101010 };
89 var x: Vector(4, u8) = [4]u8{ 0b11110000, 0b00001111, 0b10101010, 0b01010101 };89 var x: Vector(4, u8) = [4]u8{ 0b11110000, 0b00001111, 0b10101010, 0b01010101 };
90 expect(mem.eql(u8, &@as([4]u8, v ^ x), &[4]u8{ 0b01011010, 0b10100101, 0b00000000, 0b11111111 }));90 try expect(mem.eql(u8, &@as([4]u8, v ^ x), &[4]u8{ 0b01011010, 0b10100101, 0b00000000, 0b11111111 }));
91 expect(mem.eql(u8, &@as([4]u8, v | x), &[4]u8{ 0b11111010, 0b10101111, 0b10101010, 0b11111111 }));91 try expect(mem.eql(u8, &@as([4]u8, v | x), &[4]u8{ 0b11111010, 0b10101111, 0b10101010, 0b11111111 }));
92 expect(mem.eql(u8, &@as([4]u8, v & x), &[4]u8{ 0b10100000, 0b00001010, 0b10101010, 0b00000000 }));92 try expect(mem.eql(u8, &@as([4]u8, v & x), &[4]u8{ 0b10100000, 0b00001010, 0b10101010, 0b00000000 }));
93 }93 }
94 };94 };
95 S.doTheTest();95 try S.doTheTest();
96 comptime S.doTheTest();96 comptime try S.doTheTest();
97}97}
9898
99test "implicit cast vector to array" {99test "implicit cast vector to array" {
100 const S = struct {100 const S = struct {
101 fn doTheTest() void {101 fn doTheTest() !void {
102 var a: Vector(4, i32) = [_]i32{ 1, 2, 3, 4 };102 var a: Vector(4, i32) = [_]i32{ 1, 2, 3, 4 };
103 var result_array: [4]i32 = a;103 var result_array: [4]i32 = a;
104 result_array = a;104 result_array = a;
105 expect(mem.eql(i32, &result_array, &[4]i32{ 1, 2, 3, 4 }));105 try expect(mem.eql(i32, &result_array, &[4]i32{ 1, 2, 3, 4 }));
106 }106 }
107 };107 };
108 S.doTheTest();108 try S.doTheTest();
109 comptime S.doTheTest();109 comptime try S.doTheTest();
110}110}
111111
112test "array to vector" {112test "array to vector" {
...@@ -120,141 +120,141 @@ test "vector casts of sizes not divisable by 8" {...@@ -120,141 +120,141 @@ test "vector casts of sizes not divisable by 8" {
120 if (std.Target.current.os.tag == .dragonfly) return error.SkipZigTest;120 if (std.Target.current.os.tag == .dragonfly) return error.SkipZigTest;
121121
122 const S = struct {122 const S = struct {
123 fn doTheTest() void {123 fn doTheTest() !void {
124 {124 {
125 var v: Vector(4, u3) = [4]u3{ 5, 2, 3, 0 };125 var v: Vector(4, u3) = [4]u3{ 5, 2, 3, 0 };
126 var x: [4]u3 = v;126 var x: [4]u3 = v;
127 expect(mem.eql(u3, &x, &@as([4]u3, v)));127 try expect(mem.eql(u3, &x, &@as([4]u3, v)));
128 }128 }
129 {129 {
130 var v: Vector(4, u2) = [4]u2{ 1, 2, 3, 0 };130 var v: Vector(4, u2) = [4]u2{ 1, 2, 3, 0 };
131 var x: [4]u2 = v;131 var x: [4]u2 = v;
132 expect(mem.eql(u2, &x, &@as([4]u2, v)));132 try expect(mem.eql(u2, &x, &@as([4]u2, v)));
133 }133 }
134 {134 {
135 var v: Vector(4, u1) = [4]u1{ 1, 0, 1, 0 };135 var v: Vector(4, u1) = [4]u1{ 1, 0, 1, 0 };
136 var x: [4]u1 = v;136 var x: [4]u1 = v;
137 expect(mem.eql(u1, &x, &@as([4]u1, v)));137 try expect(mem.eql(u1, &x, &@as([4]u1, v)));
138 }138 }
139 {139 {
140 var v: Vector(4, bool) = [4]bool{ false, false, true, false };140 var v: Vector(4, bool) = [4]bool{ false, false, true, false };
141 var x: [4]bool = v;141 var x: [4]bool = v;
142 expect(mem.eql(bool, &x, &@as([4]bool, v)));142 try expect(mem.eql(bool, &x, &@as([4]bool, v)));
143 }143 }
144 }144 }
145 };145 };
146 S.doTheTest();146 try S.doTheTest();
147 comptime S.doTheTest();147 comptime try S.doTheTest();
148}148}
149149
150test "vector @splat" {150test "vector @splat" {
151 const S = struct {151 const S = struct {
152 fn testForT(comptime N: comptime_int, v: anytype) void {152 fn testForT(comptime N: comptime_int, v: anytype) !void {
153 const T = @TypeOf(v);153 const T = @TypeOf(v);
154 var vec = @splat(N, v);154 var vec = @splat(N, v);
155 expectEqual(Vector(N, T), @TypeOf(vec));155 try expectEqual(Vector(N, T), @TypeOf(vec));
156 var as_array = @as([N]T, vec);156 var as_array = @as([N]T, vec);
157 for (as_array) |elem| expectEqual(v, elem);157 for (as_array) |elem| try expectEqual(v, elem);
158 }158 }
159 fn doTheTest() void {159 fn doTheTest() !void {
160 // Splats with multiple-of-8 bit types that fill a 128bit vector.160 // Splats with multiple-of-8 bit types that fill a 128bit vector.
161 testForT(16, @as(u8, 0xEE));161 try testForT(16, @as(u8, 0xEE));
162 testForT(8, @as(u16, 0xBEEF));162 try testForT(8, @as(u16, 0xBEEF));
163 testForT(4, @as(u32, 0xDEADBEEF));163 try testForT(4, @as(u32, 0xDEADBEEF));
164 testForT(2, @as(u64, 0xCAFEF00DDEADBEEF));164 try testForT(2, @as(u64, 0xCAFEF00DDEADBEEF));
165165
166 testForT(8, @as(f16, 3.1415));166 try testForT(8, @as(f16, 3.1415));
167 testForT(4, @as(f32, 3.1415));167 try testForT(4, @as(f32, 3.1415));
168 testForT(2, @as(f64, 3.1415));168 try testForT(2, @as(f64, 3.1415));
169169
170 // Same but fill more than 128 bits.170 // Same but fill more than 128 bits.
171 testForT(16 * 2, @as(u8, 0xEE));171 try testForT(16 * 2, @as(u8, 0xEE));
172 testForT(8 * 2, @as(u16, 0xBEEF));172 try testForT(8 * 2, @as(u16, 0xBEEF));
173 testForT(4 * 2, @as(u32, 0xDEADBEEF));173 try testForT(4 * 2, @as(u32, 0xDEADBEEF));
174 testForT(2 * 2, @as(u64, 0xCAFEF00DDEADBEEF));174 try testForT(2 * 2, @as(u64, 0xCAFEF00DDEADBEEF));
175175
176 testForT(8 * 2, @as(f16, 3.1415));176 try testForT(8 * 2, @as(f16, 3.1415));
177 testForT(4 * 2, @as(f32, 3.1415));177 try testForT(4 * 2, @as(f32, 3.1415));
178 testForT(2 * 2, @as(f64, 3.1415));178 try testForT(2 * 2, @as(f64, 3.1415));
179 }179 }
180 };180 };
181 S.doTheTest();181 try S.doTheTest();
182 comptime S.doTheTest();182 comptime try S.doTheTest();
183}183}
184184
185test "load vector elements via comptime index" {185test "load vector elements via comptime index" {
186 const S = struct {186 const S = struct {
187 fn doTheTest() void {187 fn doTheTest() !void {
188 var v: Vector(4, i32) = [_]i32{ 1, 2, 3, undefined };188 var v: Vector(4, i32) = [_]i32{ 1, 2, 3, undefined };
189 expect(v[0] == 1);189 try expect(v[0] == 1);
190 expect(v[1] == 2);190 try expect(v[1] == 2);
191 expect(loadv(&v[2]) == 3);191 try expect(loadv(&v[2]) == 3);
192 }192 }
193 fn loadv(ptr: anytype) i32 {193 fn loadv(ptr: anytype) i32 {
194 return ptr.*;194 return ptr.*;
195 }195 }
196 };196 };
197197
198 S.doTheTest();198 try S.doTheTest();
199 comptime S.doTheTest();199 comptime try S.doTheTest();
200}200}
201201
202test "store vector elements via comptime index" {202test "store vector elements via comptime index" {
203 const S = struct {203 const S = struct {
204 fn doTheTest() void {204 fn doTheTest() !void {
205 var v: Vector(4, i32) = [_]i32{ 1, 5, 3, undefined };205 var v: Vector(4, i32) = [_]i32{ 1, 5, 3, undefined };
206206
207 v[2] = 42;207 v[2] = 42;
208 expect(v[1] == 5);208 try expect(v[1] == 5);
209 v[3] = -364;209 v[3] = -364;
210 expect(v[2] == 42);210 try expect(v[2] == 42);
211 expect(-364 == v[3]);211 try expect(-364 == v[3]);
212212
213 storev(&v[0], 100);213 storev(&v[0], 100);
214 expect(v[0] == 100);214 try expect(v[0] == 100);
215 }215 }
216 fn storev(ptr: anytype, x: i32) void {216 fn storev(ptr: anytype, x: i32) void {
217 ptr.* = x;217 ptr.* = x;
218 }218 }
219 };219 };
220220
221 S.doTheTest();221 try S.doTheTest();
222 comptime S.doTheTest();222 comptime try S.doTheTest();
223}223}
224224
225test "load vector elements via runtime index" {225test "load vector elements via runtime index" {
226 const S = struct {226 const S = struct {
227 fn doTheTest() void {227 fn doTheTest() !void {
228 var v: Vector(4, i32) = [_]i32{ 1, 2, 3, undefined };228 var v: Vector(4, i32) = [_]i32{ 1, 2, 3, undefined };
229 var i: u32 = 0;229 var i: u32 = 0;
230 expect(v[i] == 1);230 try expect(v[i] == 1);
231 i += 1;231 i += 1;
232 expect(v[i] == 2);232 try expect(v[i] == 2);
233 i += 1;233 i += 1;
234 expect(v[i] == 3);234 try expect(v[i] == 3);
235 }235 }
236 };236 };
237237
238 S.doTheTest();238 try S.doTheTest();
239 comptime S.doTheTest();239 comptime try S.doTheTest();
240}240}
241241
242test "store vector elements via runtime index" {242test "store vector elements via runtime index" {
243 const S = struct {243 const S = struct {
244 fn doTheTest() void {244 fn doTheTest() !void {
245 var v: Vector(4, i32) = [_]i32{ 1, 5, 3, undefined };245 var v: Vector(4, i32) = [_]i32{ 1, 5, 3, undefined };
246 var i: u32 = 2;246 var i: u32 = 2;
247 v[i] = 1;247 v[i] = 1;
248 expect(v[1] == 5);248 try expect(v[1] == 5);
249 expect(v[2] == 1);249 try expect(v[2] == 1);
250 i += 1;250 i += 1;
251 v[i] = -364;251 v[i] = -364;
252 expect(-364 == v[3]);252 try expect(-364 == v[3]);
253 }253 }
254 };254 };
255255
256 S.doTheTest();256 try S.doTheTest();
257 comptime S.doTheTest();257 comptime try S.doTheTest();
258}258}
259259
260test "initialize vector which is a struct field" {260test "initialize vector which is a struct field" {
...@@ -263,155 +263,155 @@ test "initialize vector which is a struct field" {...@@ -263,155 +263,155 @@ test "initialize vector which is a struct field" {
263 };263 };
264264
265 const S = struct {265 const S = struct {
266 fn doTheTest() void {266 fn doTheTest() !void {
267 var foo = Vec4Obj{267 var foo = Vec4Obj{
268 .data = [_]f32{ 1, 2, 3, 4 },268 .data = [_]f32{ 1, 2, 3, 4 },
269 };269 };
270 }270 }
271 };271 };
272 S.doTheTest();272 try S.doTheTest();
273 comptime S.doTheTest();273 comptime try S.doTheTest();
274}274}
275275
276test "vector comparison operators" {276test "vector comparison operators" {
277 const S = struct {277 const S = struct {
278 fn doTheTest() void {278 fn doTheTest() !void {
279 {279 {
280 const v1: Vector(4, bool) = [_]bool{ true, false, true, false };280 const v1: Vector(4, bool) = [_]bool{ true, false, true, false };
281 const v2: Vector(4, bool) = [_]bool{ false, true, false, true };281 const v2: Vector(4, bool) = [_]bool{ false, true, false, true };
282 expectEqual(@splat(4, true), v1 == v1);282 try expectEqual(@splat(4, true), v1 == v1);
283 expectEqual(@splat(4, false), v1 == v2);283 try expectEqual(@splat(4, false), v1 == v2);
284 expectEqual(@splat(4, true), v1 != v2);284 try expectEqual(@splat(4, true), v1 != v2);
285 expectEqual(@splat(4, false), v2 != v2);285 try expectEqual(@splat(4, false), v2 != v2);
286 }286 }
287 {287 {
288 const v1 = @splat(4, @as(u32, 0xc0ffeeee));288 const v1 = @splat(4, @as(u32, 0xc0ffeeee));
289 const v2: Vector(4, c_uint) = v1;289 const v2: Vector(4, c_uint) = v1;
290 const v3 = @splat(4, @as(u32, 0xdeadbeef));290 const v3 = @splat(4, @as(u32, 0xdeadbeef));
291 expectEqual(@splat(4, true), v1 == v2);291 try expectEqual(@splat(4, true), v1 == v2);
292 expectEqual(@splat(4, false), v1 == v3);292 try expectEqual(@splat(4, false), v1 == v3);
293 expectEqual(@splat(4, true), v1 != v3);293 try expectEqual(@splat(4, true), v1 != v3);
294 expectEqual(@splat(4, false), v1 != v2);294 try expectEqual(@splat(4, false), v1 != v2);
295 }295 }
296 {296 {
297 // Comptime-known LHS/RHS297 // Comptime-known LHS/RHS
298 var v1: @Vector(4, u32) = [_]u32{ 2, 1, 2, 1 };298 var v1: @Vector(4, u32) = [_]u32{ 2, 1, 2, 1 };
299 const v2 = @splat(4, @as(u32, 2));299 const v2 = @splat(4, @as(u32, 2));
300 const v3: @Vector(4, bool) = [_]bool{ true, false, true, false };300 const v3: @Vector(4, bool) = [_]bool{ true, false, true, false };
301 expectEqual(v3, v1 == v2);301 try expectEqual(v3, v1 == v2);
302 expectEqual(v3, v2 == v1);302 try expectEqual(v3, v2 == v1);
303 }303 }
304 }304 }
305 };305 };
306 S.doTheTest();306 try S.doTheTest();
307 comptime S.doTheTest();307 comptime try S.doTheTest();
308}308}
309309
310test "vector division operators" {310test "vector division operators" {
311 const S = struct {311 const S = struct {
312 fn doTheTestDiv(comptime T: type, x: Vector(4, T), y: Vector(4, T)) void {312 fn doTheTestDiv(comptime T: type, x: Vector(4, T), y: Vector(4, T)) !void {
313 if (!comptime std.meta.trait.isSignedInt(T)) {313 if (!comptime std.meta.trait.isSignedInt(T)) {
314 const d0 = x / y;314 const d0 = x / y;
315 for (@as([4]T, d0)) |v, i| {315 for (@as([4]T, d0)) |v, i| {
316 expectEqual(x[i] / y[i], v);316 try expectEqual(x[i] / y[i], v);
317 }317 }
318 }318 }
319 const d1 = @divExact(x, y);319 const d1 = @divExact(x, y);
320 for (@as([4]T, d1)) |v, i| {320 for (@as([4]T, d1)) |v, i| {
321 expectEqual(@divExact(x[i], y[i]), v);321 try expectEqual(@divExact(x[i], y[i]), v);
322 }322 }
323 const d2 = @divFloor(x, y);323 const d2 = @divFloor(x, y);
324 for (@as([4]T, d2)) |v, i| {324 for (@as([4]T, d2)) |v, i| {
325 expectEqual(@divFloor(x[i], y[i]), v);325 try expectEqual(@divFloor(x[i], y[i]), v);
326 }326 }
327 const d3 = @divTrunc(x, y);327 const d3 = @divTrunc(x, y);
328 for (@as([4]T, d3)) |v, i| {328 for (@as([4]T, d3)) |v, i| {
329 expectEqual(@divTrunc(x[i], y[i]), v);329 try expectEqual(@divTrunc(x[i], y[i]), v);
330 }330 }
331 }331 }
332332
333 fn doTheTestMod(comptime T: type, x: Vector(4, T), y: Vector(4, T)) void {333 fn doTheTestMod(comptime T: type, x: Vector(4, T), y: Vector(4, T)) !void {
334 if ((!comptime std.meta.trait.isSignedInt(T)) and @typeInfo(T) != .Float) {334 if ((!comptime std.meta.trait.isSignedInt(T)) and @typeInfo(T) != .Float) {
335 const r0 = x % y;335 const r0 = x % y;
336 for (@as([4]T, r0)) |v, i| {336 for (@as([4]T, r0)) |v, i| {
337 expectEqual(x[i] % y[i], v);337 try expectEqual(x[i] % y[i], v);
338 }338 }
339 }339 }
340 const r1 = @mod(x, y);340 const r1 = @mod(x, y);
341 for (@as([4]T, r1)) |v, i| {341 for (@as([4]T, r1)) |v, i| {
342 expectEqual(@mod(x[i], y[i]), v);342 try expectEqual(@mod(x[i], y[i]), v);
343 }343 }
344 const r2 = @rem(x, y);344 const r2 = @rem(x, y);
345 for (@as([4]T, r2)) |v, i| {345 for (@as([4]T, r2)) |v, i| {
346 expectEqual(@rem(x[i], y[i]), v);346 try expectEqual(@rem(x[i], y[i]), v);
347 }347 }
348 }348 }
349349
350 fn doTheTest() void {350 fn doTheTest() !void {
351 // https://github.com/ziglang/zig/issues/4952351 // https://github.com/ziglang/zig/issues/4952
352 if (std.builtin.os.tag != .windows) {352 if (std.builtin.os.tag != .windows) {
353 doTheTestDiv(f16, [4]f16{ 4.0, -4.0, 4.0, -4.0 }, [4]f16{ 1.0, 2.0, -1.0, -2.0 });353 try doTheTestDiv(f16, [4]f16{ 4.0, -4.0, 4.0, -4.0 }, [4]f16{ 1.0, 2.0, -1.0, -2.0 });
354 }354 }
355355
356 doTheTestDiv(f32, [4]f32{ 4.0, -4.0, 4.0, -4.0 }, [4]f32{ 1.0, 2.0, -1.0, -2.0 });356 try doTheTestDiv(f32, [4]f32{ 4.0, -4.0, 4.0, -4.0 }, [4]f32{ 1.0, 2.0, -1.0, -2.0 });
357 doTheTestDiv(f64, [4]f64{ 4.0, -4.0, 4.0, -4.0 }, [4]f64{ 1.0, 2.0, -1.0, -2.0 });357 try doTheTestDiv(f64, [4]f64{ 4.0, -4.0, 4.0, -4.0 }, [4]f64{ 1.0, 2.0, -1.0, -2.0 });
358358
359 // https://github.com/ziglang/zig/issues/4952359 // https://github.com/ziglang/zig/issues/4952
360 if (std.builtin.os.tag != .windows) {360 if (std.builtin.os.tag != .windows) {
361 doTheTestMod(f16, [4]f16{ 4.0, -4.0, 4.0, -4.0 }, [4]f16{ 1.0, 2.0, 0.5, 3.0 });361 try doTheTestMod(f16, [4]f16{ 4.0, -4.0, 4.0, -4.0 }, [4]f16{ 1.0, 2.0, 0.5, 3.0 });
362 }362 }
363 doTheTestMod(f32, [4]f32{ 4.0, -4.0, 4.0, -4.0 }, [4]f32{ 1.0, 2.0, 0.5, 3.0 });363 try doTheTestMod(f32, [4]f32{ 4.0, -4.0, 4.0, -4.0 }, [4]f32{ 1.0, 2.0, 0.5, 3.0 });
364 doTheTestMod(f64, [4]f64{ 4.0, -4.0, 4.0, -4.0 }, [4]f64{ 1.0, 2.0, 0.5, 3.0 });364 try doTheTestMod(f64, [4]f64{ 4.0, -4.0, 4.0, -4.0 }, [4]f64{ 1.0, 2.0, 0.5, 3.0 });
365365
366 doTheTestDiv(i8, [4]i8{ 4, -4, 4, -4 }, [4]i8{ 1, 2, -1, -2 });366 try doTheTestDiv(i8, [4]i8{ 4, -4, 4, -4 }, [4]i8{ 1, 2, -1, -2 });
367 doTheTestDiv(i16, [4]i16{ 4, -4, 4, -4 }, [4]i16{ 1, 2, -1, -2 });367 try doTheTestDiv(i16, [4]i16{ 4, -4, 4, -4 }, [4]i16{ 1, 2, -1, -2 });
368 doTheTestDiv(i32, [4]i32{ 4, -4, 4, -4 }, [4]i32{ 1, 2, -1, -2 });368 try doTheTestDiv(i32, [4]i32{ 4, -4, 4, -4 }, [4]i32{ 1, 2, -1, -2 });
369 doTheTestDiv(i64, [4]i64{ 4, -4, 4, -4 }, [4]i64{ 1, 2, -1, -2 });369 try doTheTestDiv(i64, [4]i64{ 4, -4, 4, -4 }, [4]i64{ 1, 2, -1, -2 });
370370
371 doTheTestMod(i8, [4]i8{ 4, -4, 4, -4 }, [4]i8{ 1, 2, 4, 8 });371 try doTheTestMod(i8, [4]i8{ 4, -4, 4, -4 }, [4]i8{ 1, 2, 4, 8 });
372 doTheTestMod(i16, [4]i16{ 4, -4, 4, -4 }, [4]i16{ 1, 2, 4, 8 });372 try doTheTestMod(i16, [4]i16{ 4, -4, 4, -4 }, [4]i16{ 1, 2, 4, 8 });
373 doTheTestMod(i32, [4]i32{ 4, -4, 4, -4 }, [4]i32{ 1, 2, 4, 8 });373 try doTheTestMod(i32, [4]i32{ 4, -4, 4, -4 }, [4]i32{ 1, 2, 4, 8 });
374 doTheTestMod(i64, [4]i64{ 4, -4, 4, -4 }, [4]i64{ 1, 2, 4, 8 });374 try doTheTestMod(i64, [4]i64{ 4, -4, 4, -4 }, [4]i64{ 1, 2, 4, 8 });
375375
376 doTheTestDiv(u8, [4]u8{ 1, 2, 4, 8 }, [4]u8{ 1, 1, 2, 4 });376 try doTheTestDiv(u8, [4]u8{ 1, 2, 4, 8 }, [4]u8{ 1, 1, 2, 4 });
377 doTheTestDiv(u16, [4]u16{ 1, 2, 4, 8 }, [4]u16{ 1, 1, 2, 4 });377 try doTheTestDiv(u16, [4]u16{ 1, 2, 4, 8 }, [4]u16{ 1, 1, 2, 4 });
378 doTheTestDiv(u32, [4]u32{ 1, 2, 4, 8 }, [4]u32{ 1, 1, 2, 4 });378 try doTheTestDiv(u32, [4]u32{ 1, 2, 4, 8 }, [4]u32{ 1, 1, 2, 4 });
379 doTheTestDiv(u64, [4]u64{ 1, 2, 4, 8 }, [4]u64{ 1, 1, 2, 4 });379 try doTheTestDiv(u64, [4]u64{ 1, 2, 4, 8 }, [4]u64{ 1, 1, 2, 4 });
380380
381 doTheTestMod(u8, [4]u8{ 1, 2, 4, 8 }, [4]u8{ 1, 1, 2, 4 });381 try doTheTestMod(u8, [4]u8{ 1, 2, 4, 8 }, [4]u8{ 1, 1, 2, 4 });
382 doTheTestMod(u16, [4]u16{ 1, 2, 4, 8 }, [4]u16{ 1, 1, 2, 4 });382 try doTheTestMod(u16, [4]u16{ 1, 2, 4, 8 }, [4]u16{ 1, 1, 2, 4 });
383 doTheTestMod(u32, [4]u32{ 1, 2, 4, 8 }, [4]u32{ 1, 1, 2, 4 });383 try doTheTestMod(u32, [4]u32{ 1, 2, 4, 8 }, [4]u32{ 1, 1, 2, 4 });
384 doTheTestMod(u64, [4]u64{ 1, 2, 4, 8 }, [4]u64{ 1, 1, 2, 4 });384 try doTheTestMod(u64, [4]u64{ 1, 2, 4, 8 }, [4]u64{ 1, 1, 2, 4 });
385 }385 }
386 };386 };
387387
388 S.doTheTest();388 try S.doTheTest();
389 comptime S.doTheTest();389 comptime try S.doTheTest();
390}390}
391391
392test "vector bitwise not operator" {392test "vector bitwise not operator" {
393 const S = struct {393 const S = struct {
394 fn doTheTestNot(comptime T: type, x: Vector(4, T)) void {394 fn doTheTestNot(comptime T: type, x: Vector(4, T)) !void {
395 var y = ~x;395 var y = ~x;
396 for (@as([4]T, y)) |v, i| {396 for (@as([4]T, y)) |v, i| {
397 expectEqual(~x[i], v);397 try expectEqual(~x[i], v);
398 }398 }
399 }399 }
400 fn doTheTest() void {400 fn doTheTest() !void {
401 doTheTestNot(u8, [_]u8{ 0, 2, 4, 255 });401 try doTheTestNot(u8, [_]u8{ 0, 2, 4, 255 });
402 doTheTestNot(u16, [_]u16{ 0, 2, 4, 255 });402 try doTheTestNot(u16, [_]u16{ 0, 2, 4, 255 });
403 doTheTestNot(u32, [_]u32{ 0, 2, 4, 255 });403 try doTheTestNot(u32, [_]u32{ 0, 2, 4, 255 });
404 doTheTestNot(u64, [_]u64{ 0, 2, 4, 255 });404 try doTheTestNot(u64, [_]u64{ 0, 2, 4, 255 });
405405
406 doTheTestNot(u8, [_]u8{ 0, 2, 4, 255 });406 try doTheTestNot(u8, [_]u8{ 0, 2, 4, 255 });
407 doTheTestNot(u16, [_]u16{ 0, 2, 4, 255 });407 try doTheTestNot(u16, [_]u16{ 0, 2, 4, 255 });
408 doTheTestNot(u32, [_]u32{ 0, 2, 4, 255 });408 try doTheTestNot(u32, [_]u32{ 0, 2, 4, 255 });
409 doTheTestNot(u64, [_]u64{ 0, 2, 4, 255 });409 try doTheTestNot(u64, [_]u64{ 0, 2, 4, 255 });
410 }410 }
411 };411 };
412412
413 S.doTheTest();413 try S.doTheTest();
414 comptime S.doTheTest();414 comptime try S.doTheTest();
415}415}
416416
417test "vector shift operators" {417test "vector shift operators" {
...@@ -419,7 +419,7 @@ test "vector shift operators" {...@@ -419,7 +419,7 @@ test "vector shift operators" {
419 if (builtin.os.tag == .wasi) return error.SkipZigTest;419 if (builtin.os.tag == .wasi) return error.SkipZigTest;
420420
421 const S = struct {421 const S = struct {
422 fn doTheTestShift(x: anytype, y: anytype) void {422 fn doTheTestShift(x: anytype, y: anytype) !void {
423 const N = @typeInfo(@TypeOf(x)).Array.len;423 const N = @typeInfo(@TypeOf(x)).Array.len;
424 const TX = @typeInfo(@TypeOf(x)).Array.child;424 const TX = @typeInfo(@TypeOf(x)).Array.child;
425 const TY = @typeInfo(@TypeOf(y)).Array.child;425 const TY = @typeInfo(@TypeOf(y)).Array.child;
...@@ -429,14 +429,14 @@ test "vector shift operators" {...@@ -429,14 +429,14 @@ test "vector shift operators" {
429429
430 var z0 = xv >> yv;430 var z0 = xv >> yv;
431 for (@as([N]TX, z0)) |v, i| {431 for (@as([N]TX, z0)) |v, i| {
432 expectEqual(x[i] >> y[i], v);432 try expectEqual(x[i] >> y[i], v);
433 }433 }
434 var z1 = xv << yv;434 var z1 = xv << yv;
435 for (@as([N]TX, z1)) |v, i| {435 for (@as([N]TX, z1)) |v, i| {
436 expectEqual(x[i] << y[i], v);436 try expectEqual(x[i] << y[i], v);
437 }437 }
438 }438 }
439 fn doTheTestShiftExact(x: anytype, y: anytype, dir: enum { Left, Right }) void {439 fn doTheTestShiftExact(x: anytype, y: anytype, dir: enum { Left, Right }) !void {
440 const N = @typeInfo(@TypeOf(x)).Array.len;440 const N = @typeInfo(@TypeOf(x)).Array.len;
441 const TX = @typeInfo(@TypeOf(x)).Array.child;441 const TX = @typeInfo(@TypeOf(x)).Array.child;
442 const TY = @typeInfo(@TypeOf(y)).Array.child;442 const TY = @typeInfo(@TypeOf(y)).Array.child;
...@@ -447,33 +447,33 @@ test "vector shift operators" {...@@ -447,33 +447,33 @@ test "vector shift operators" {
447 var z = if (dir == .Left) @shlExact(xv, yv) else @shrExact(xv, yv);447 var z = if (dir == .Left) @shlExact(xv, yv) else @shrExact(xv, yv);
448 for (@as([N]TX, z)) |v, i| {448 for (@as([N]TX, z)) |v, i| {
449 const check = if (dir == .Left) x[i] << y[i] else x[i] >> y[i];449 const check = if (dir == .Left) x[i] << y[i] else x[i] >> y[i];
450 expectEqual(check, v);450 try expectEqual(check, v);
451 }451 }
452 }452 }
453 fn doTheTest() void {453 fn doTheTest() !void {
454 doTheTestShift([_]u8{ 0, 2, 4, math.maxInt(u8) }, [_]u3{ 2, 0, 2, 7 });454 try doTheTestShift([_]u8{ 0, 2, 4, math.maxInt(u8) }, [_]u3{ 2, 0, 2, 7 });
455 doTheTestShift([_]u16{ 0, 2, 4, math.maxInt(u16) }, [_]u4{ 2, 0, 2, 15 });455 try doTheTestShift([_]u16{ 0, 2, 4, math.maxInt(u16) }, [_]u4{ 2, 0, 2, 15 });
456 doTheTestShift([_]u24{ 0, 2, 4, math.maxInt(u24) }, [_]u5{ 2, 0, 2, 23 });456 try doTheTestShift([_]u24{ 0, 2, 4, math.maxInt(u24) }, [_]u5{ 2, 0, 2, 23 });
457 doTheTestShift([_]u32{ 0, 2, 4, math.maxInt(u32) }, [_]u5{ 2, 0, 2, 31 });457 try doTheTestShift([_]u32{ 0, 2, 4, math.maxInt(u32) }, [_]u5{ 2, 0, 2, 31 });
458 doTheTestShift([_]u64{ 0xfe, math.maxInt(u64) }, [_]u6{ 0, 63 });458 try doTheTestShift([_]u64{ 0xfe, math.maxInt(u64) }, [_]u6{ 0, 63 });
459459
460 doTheTestShift([_]i8{ 0, 2, 4, math.maxInt(i8) }, [_]u3{ 2, 0, 2, 7 });460 try doTheTestShift([_]i8{ 0, 2, 4, math.maxInt(i8) }, [_]u3{ 2, 0, 2, 7 });
461 doTheTestShift([_]i16{ 0, 2, 4, math.maxInt(i16) }, [_]u4{ 2, 0, 2, 7 });461 try doTheTestShift([_]i16{ 0, 2, 4, math.maxInt(i16) }, [_]u4{ 2, 0, 2, 7 });
462 doTheTestShift([_]i24{ 0, 2, 4, math.maxInt(i24) }, [_]u5{ 2, 0, 2, 7 });462 try doTheTestShift([_]i24{ 0, 2, 4, math.maxInt(i24) }, [_]u5{ 2, 0, 2, 7 });
463 doTheTestShift([_]i32{ 0, 2, 4, math.maxInt(i32) }, [_]u5{ 2, 0, 2, 7 });463 try doTheTestShift([_]i32{ 0, 2, 4, math.maxInt(i32) }, [_]u5{ 2, 0, 2, 7 });
464 doTheTestShift([_]i64{ 0xfe, math.maxInt(i64) }, [_]u6{ 0, 63 });464 try doTheTestShift([_]i64{ 0xfe, math.maxInt(i64) }, [_]u6{ 0, 63 });
465465
466 doTheTestShiftExact([_]u8{ 0, 1, 1 << 7, math.maxInt(u8) ^ 1 }, [_]u3{ 4, 0, 7, 1 }, .Right);466 try doTheTestShiftExact([_]u8{ 0, 1, 1 << 7, math.maxInt(u8) ^ 1 }, [_]u3{ 4, 0, 7, 1 }, .Right);
467 doTheTestShiftExact([_]u16{ 0, 1, 1 << 15, math.maxInt(u16) ^ 1 }, [_]u4{ 4, 0, 15, 1 }, .Right);467 try doTheTestShiftExact([_]u16{ 0, 1, 1 << 15, math.maxInt(u16) ^ 1 }, [_]u4{ 4, 0, 15, 1 }, .Right);
468 doTheTestShiftExact([_]u24{ 0, 1, 1 << 23, math.maxInt(u24) ^ 1 }, [_]u5{ 4, 0, 23, 1 }, .Right);468 try doTheTestShiftExact([_]u24{ 0, 1, 1 << 23, math.maxInt(u24) ^ 1 }, [_]u5{ 4, 0, 23, 1 }, .Right);
469 doTheTestShiftExact([_]u32{ 0, 1, 1 << 31, math.maxInt(u32) ^ 1 }, [_]u5{ 4, 0, 31, 1 }, .Right);469 try doTheTestShiftExact([_]u32{ 0, 1, 1 << 31, math.maxInt(u32) ^ 1 }, [_]u5{ 4, 0, 31, 1 }, .Right);
470 doTheTestShiftExact([_]u64{ 1 << 63, 1 }, [_]u6{ 63, 0 }, .Right);470 try doTheTestShiftExact([_]u64{ 1 << 63, 1 }, [_]u6{ 63, 0 }, .Right);
471471
472 doTheTestShiftExact([_]u8{ 0, 1, 1, math.maxInt(u8) ^ (1 << 7) }, [_]u3{ 4, 0, 7, 1 }, .Left);472 try doTheTestShiftExact([_]u8{ 0, 1, 1, math.maxInt(u8) ^ (1 << 7) }, [_]u3{ 4, 0, 7, 1 }, .Left);
473 doTheTestShiftExact([_]u16{ 0, 1, 1, math.maxInt(u16) ^ (1 << 15) }, [_]u4{ 4, 0, 15, 1 }, .Left);473 try doTheTestShiftExact([_]u16{ 0, 1, 1, math.maxInt(u16) ^ (1 << 15) }, [_]u4{ 4, 0, 15, 1 }, .Left);
474 doTheTestShiftExact([_]u24{ 0, 1, 1, math.maxInt(u24) ^ (1 << 23) }, [_]u5{ 4, 0, 23, 1 }, .Left);474 try doTheTestShiftExact([_]u24{ 0, 1, 1, math.maxInt(u24) ^ (1 << 23) }, [_]u5{ 4, 0, 23, 1 }, .Left);
475 doTheTestShiftExact([_]u32{ 0, 1, 1, math.maxInt(u32) ^ (1 << 31) }, [_]u5{ 4, 0, 31, 1 }, .Left);475 try doTheTestShiftExact([_]u32{ 0, 1, 1, math.maxInt(u32) ^ (1 << 31) }, [_]u5{ 4, 0, 31, 1 }, .Left);
476 doTheTestShiftExact([_]u64{ 1 << 63, 1 }, [_]u6{ 0, 63 }, .Left);476 try doTheTestShiftExact([_]u64{ 1 << 63, 1 }, [_]u6{ 0, 63 }, .Left);
477 }477 }
478 };478 };
479479
...@@ -500,19 +500,19 @@ test "vector shift operators" {...@@ -500,19 +500,19 @@ test "vector shift operators" {
500 else => {},500 else => {},
501 }501 }
502502
503 S.doTheTest();503 try S.doTheTest();
504 comptime S.doTheTest();504 comptime try S.doTheTest();
505}505}
506506
507test "vector reduce operation" {507test "vector reduce operation" {
508 const S = struct {508 const S = struct {
509 fn doTheTestReduce(comptime op: builtin.ReduceOp, x: anytype, expected: anytype) void {509 fn doTheTestReduce(comptime op: builtin.ReduceOp, x: anytype, expected: anytype) !void {
510 const N = @typeInfo(@TypeOf(x)).Array.len;510 const N = @typeInfo(@TypeOf(x)).Array.len;
511 const TX = @typeInfo(@TypeOf(x)).Array.child;511 const TX = @typeInfo(@TypeOf(x)).Array.child;
512512
513 var r = @reduce(op, @as(Vector(N, TX), x));513 var r = @reduce(op, @as(Vector(N, TX), x));
514 switch (@typeInfo(TX)) {514 switch (@typeInfo(TX)) {
515 .Int, .Bool => expectEqual(expected, r),515 .Int, .Bool => try expectEqual(expected, r),
516 .Float => {516 .Float => {
517 const expected_nan = math.isNan(expected);517 const expected_nan = math.isNan(expected);
518 const got_nan = math.isNan(r);518 const got_nan = math.isNan(r);
...@@ -521,120 +521,120 @@ test "vector reduce operation" {...@@ -521,120 +521,120 @@ test "vector reduce operation" {
521 // Do this check explicitly as two NaN values are never521 // Do this check explicitly as two NaN values are never
522 // equal.522 // equal.
523 } else {523 } else {
524 expectApproxEqRel(expected, r, math.sqrt(math.epsilon(TX)));524 try expectApproxEqRel(expected, r, math.sqrt(math.epsilon(TX)));
525 }525 }
526 },526 },
527 else => unreachable,527 else => unreachable,
528 }528 }
529 }529 }
530 fn doTheTest() void {530 fn doTheTest() !void {
531 doTheTestReduce(.Add, [4]i16{ -9, -99, -999, -9999 }, @as(i32, -11106));531 try doTheTestReduce(.Add, [4]i16{ -9, -99, -999, -9999 }, @as(i32, -11106));
532 doTheTestReduce(.Add, [4]u16{ 9, 99, 999, 9999 }, @as(u32, 11106));532 try doTheTestReduce(.Add, [4]u16{ 9, 99, 999, 9999 }, @as(u32, 11106));
533 doTheTestReduce(.Add, [4]i32{ -9, -99, -999, -9999 }, @as(i32, -11106));533 try doTheTestReduce(.Add, [4]i32{ -9, -99, -999, -9999 }, @as(i32, -11106));
534 doTheTestReduce(.Add, [4]u32{ 9, 99, 999, 9999 }, @as(u32, 11106));534 try doTheTestReduce(.Add, [4]u32{ 9, 99, 999, 9999 }, @as(u32, 11106));
535 doTheTestReduce(.Add, [4]i64{ -9, -99, -999, -9999 }, @as(i64, -11106));535 try doTheTestReduce(.Add, [4]i64{ -9, -99, -999, -9999 }, @as(i64, -11106));
536 doTheTestReduce(.Add, [4]u64{ 9, 99, 999, 9999 }, @as(u64, 11106));536 try doTheTestReduce(.Add, [4]u64{ 9, 99, 999, 9999 }, @as(u64, 11106));
537 doTheTestReduce(.Add, [4]i128{ -9, -99, -999, -9999 }, @as(i128, -11106));537 try doTheTestReduce(.Add, [4]i128{ -9, -99, -999, -9999 }, @as(i128, -11106));
538 doTheTestReduce(.Add, [4]u128{ 9, 99, 999, 9999 }, @as(u128, 11106));538 try doTheTestReduce(.Add, [4]u128{ 9, 99, 999, 9999 }, @as(u128, 11106));
539 doTheTestReduce(.Add, [4]f16{ -1.9, 5.1, -60.3, 100.0 }, @as(f16, 42.9));539 try doTheTestReduce(.Add, [4]f16{ -1.9, 5.1, -60.3, 100.0 }, @as(f16, 42.9));
540 doTheTestReduce(.Add, [4]f32{ -1.9, 5.1, -60.3, 100.0 }, @as(f32, 42.9));540 try doTheTestReduce(.Add, [4]f32{ -1.9, 5.1, -60.3, 100.0 }, @as(f32, 42.9));
541 doTheTestReduce(.Add, [4]f64{ -1.9, 5.1, -60.3, 100.0 }, @as(f64, 42.9));541 try doTheTestReduce(.Add, [4]f64{ -1.9, 5.1, -60.3, 100.0 }, @as(f64, 42.9));
542542
543 doTheTestReduce(.And, [4]bool{ true, false, true, true }, @as(bool, false));543 try doTheTestReduce(.And, [4]bool{ true, false, true, true }, @as(bool, false));
544 doTheTestReduce(.And, [4]u1{ 1, 0, 1, 1 }, @as(u1, 0));544 try doTheTestReduce(.And, [4]u1{ 1, 0, 1, 1 }, @as(u1, 0));
545 doTheTestReduce(.And, [4]u16{ 0xffff, 0xff55, 0xaaff, 0x1010 }, @as(u16, 0x10));545 try doTheTestReduce(.And, [4]u16{ 0xffff, 0xff55, 0xaaff, 0x1010 }, @as(u16, 0x10));
546 doTheTestReduce(.And, [4]u32{ 0xffffffff, 0xffff5555, 0xaaaaffff, 0x10101010 }, @as(u32, 0x1010));546 try doTheTestReduce(.And, [4]u32{ 0xffffffff, 0xffff5555, 0xaaaaffff, 0x10101010 }, @as(u32, 0x1010));
547 doTheTestReduce(.And, [4]u64{ 0xffffffff, 0xffff5555, 0xaaaaffff, 0x10101010 }, @as(u64, 0x1010));547 try doTheTestReduce(.And, [4]u64{ 0xffffffff, 0xffff5555, 0xaaaaffff, 0x10101010 }, @as(u64, 0x1010));
548548
549 doTheTestReduce(.Min, [4]i16{ -1, 2, 3, 4 }, @as(i16, -1));549 try doTheTestReduce(.Min, [4]i16{ -1, 2, 3, 4 }, @as(i16, -1));
550 doTheTestReduce(.Min, [4]u16{ 1, 2, 3, 4 }, @as(u16, 1));550 try doTheTestReduce(.Min, [4]u16{ 1, 2, 3, 4 }, @as(u16, 1));
551 doTheTestReduce(.Min, [4]i32{ 1234567, -386, 0, 3 }, @as(i32, -386));551 try doTheTestReduce(.Min, [4]i32{ 1234567, -386, 0, 3 }, @as(i32, -386));
552 doTheTestReduce(.Min, [4]u32{ 99, 9999, 9, 99999 }, @as(u32, 9));552 try doTheTestReduce(.Min, [4]u32{ 99, 9999, 9, 99999 }, @as(u32, 9));
553553
554 // LLVM 11 ERROR: Cannot select type554 // LLVM 11 ERROR: Cannot select type
555 // https://github.com/ziglang/zig/issues/7138555 // https://github.com/ziglang/zig/issues/7138
556 if (std.builtin.arch != .aarch64) {556 if (std.builtin.arch != .aarch64) {
557 doTheTestReduce(.Min, [4]i64{ 1234567, -386, 0, 3 }, @as(i64, -386));557 try doTheTestReduce(.Min, [4]i64{ 1234567, -386, 0, 3 }, @as(i64, -386));
558 doTheTestReduce(.Min, [4]u64{ 99, 9999, 9, 99999 }, @as(u64, 9));558 try doTheTestReduce(.Min, [4]u64{ 99, 9999, 9, 99999 }, @as(u64, 9));
559 }559 }
560560
561 doTheTestReduce(.Min, [4]i128{ 1234567, -386, 0, 3 }, @as(i128, -386));561 try doTheTestReduce(.Min, [4]i128{ 1234567, -386, 0, 3 }, @as(i128, -386));
562 doTheTestReduce(.Min, [4]u128{ 99, 9999, 9, 99999 }, @as(u128, 9));562 try doTheTestReduce(.Min, [4]u128{ 99, 9999, 9, 99999 }, @as(u128, 9));
563 doTheTestReduce(.Min, [4]f16{ -10.3, 10.0e9, 13.0, -100.0 }, @as(f16, -100.0));563 try doTheTestReduce(.Min, [4]f16{ -10.3, 10.0e9, 13.0, -100.0 }, @as(f16, -100.0));
564 doTheTestReduce(.Min, [4]f32{ -10.3, 10.0e9, 13.0, -100.0 }, @as(f32, -100.0));564 try doTheTestReduce(.Min, [4]f32{ -10.3, 10.0e9, 13.0, -100.0 }, @as(f32, -100.0));
565 doTheTestReduce(.Min, [4]f64{ -10.3, 10.0e9, 13.0, -100.0 }, @as(f64, -100.0));565 try doTheTestReduce(.Min, [4]f64{ -10.3, 10.0e9, 13.0, -100.0 }, @as(f64, -100.0));
566566
567 doTheTestReduce(.Max, [4]i16{ -1, 2, 3, 4 }, @as(i16, 4));567 try doTheTestReduce(.Max, [4]i16{ -1, 2, 3, 4 }, @as(i16, 4));
568 doTheTestReduce(.Max, [4]u16{ 1, 2, 3, 4 }, @as(u16, 4));568 try doTheTestReduce(.Max, [4]u16{ 1, 2, 3, 4 }, @as(u16, 4));
569 doTheTestReduce(.Max, [4]i32{ 1234567, -386, 0, 3 }, @as(i32, 1234567));569 try doTheTestReduce(.Max, [4]i32{ 1234567, -386, 0, 3 }, @as(i32, 1234567));
570 doTheTestReduce(.Max, [4]u32{ 99, 9999, 9, 99999 }, @as(u32, 99999));570 try doTheTestReduce(.Max, [4]u32{ 99, 9999, 9, 99999 }, @as(u32, 99999));
571571
572 // LLVM 11 ERROR: Cannot select type572 // LLVM 11 ERROR: Cannot select type
573 // https://github.com/ziglang/zig/issues/7138573 // https://github.com/ziglang/zig/issues/7138
574 if (std.builtin.arch != .aarch64) {574 if (std.builtin.arch != .aarch64) {
575 doTheTestReduce(.Max, [4]i64{ 1234567, -386, 0, 3 }, @as(i64, 1234567));575 try doTheTestReduce(.Max, [4]i64{ 1234567, -386, 0, 3 }, @as(i64, 1234567));
576 doTheTestReduce(.Max, [4]u64{ 99, 9999, 9, 99999 }, @as(u64, 99999));576 try doTheTestReduce(.Max, [4]u64{ 99, 9999, 9, 99999 }, @as(u64, 99999));
577 }577 }
578578
579 doTheTestReduce(.Max, [4]i128{ 1234567, -386, 0, 3 }, @as(i128, 1234567));579 try doTheTestReduce(.Max, [4]i128{ 1234567, -386, 0, 3 }, @as(i128, 1234567));
580 doTheTestReduce(.Max, [4]u128{ 99, 9999, 9, 99999 }, @as(u128, 99999));580 try doTheTestReduce(.Max, [4]u128{ 99, 9999, 9, 99999 }, @as(u128, 99999));
581 doTheTestReduce(.Max, [4]f16{ -10.3, 10.0e9, 13.0, -100.0 }, @as(f16, 10.0e9));581 try doTheTestReduce(.Max, [4]f16{ -10.3, 10.0e9, 13.0, -100.0 }, @as(f16, 10.0e9));
582 doTheTestReduce(.Max, [4]f32{ -10.3, 10.0e9, 13.0, -100.0 }, @as(f32, 10.0e9));582 try doTheTestReduce(.Max, [4]f32{ -10.3, 10.0e9, 13.0, -100.0 }, @as(f32, 10.0e9));
583 doTheTestReduce(.Max, [4]f64{ -10.3, 10.0e9, 13.0, -100.0 }, @as(f64, 10.0e9));583 try doTheTestReduce(.Max, [4]f64{ -10.3, 10.0e9, 13.0, -100.0 }, @as(f64, 10.0e9));
584584
585 doTheTestReduce(.Mul, [4]i16{ -1, 2, 3, 4 }, @as(i16, -24));585 try doTheTestReduce(.Mul, [4]i16{ -1, 2, 3, 4 }, @as(i16, -24));
586 doTheTestReduce(.Mul, [4]u16{ 1, 2, 3, 4 }, @as(u16, 24));586 try doTheTestReduce(.Mul, [4]u16{ 1, 2, 3, 4 }, @as(u16, 24));
587 doTheTestReduce(.Mul, [4]i32{ -9, -99, -999, 999 }, @as(i32, -889218891));587 try doTheTestReduce(.Mul, [4]i32{ -9, -99, -999, 999 }, @as(i32, -889218891));
588 doTheTestReduce(.Mul, [4]u32{ 1, 2, 3, 4 }, @as(u32, 24));588 try doTheTestReduce(.Mul, [4]u32{ 1, 2, 3, 4 }, @as(u32, 24));
589 doTheTestReduce(.Mul, [4]i64{ 9, 99, 999, 9999 }, @as(i64, 8900199891));589 try doTheTestReduce(.Mul, [4]i64{ 9, 99, 999, 9999 }, @as(i64, 8900199891));
590 doTheTestReduce(.Mul, [4]u64{ 9, 99, 999, 9999 }, @as(u64, 8900199891));590 try doTheTestReduce(.Mul, [4]u64{ 9, 99, 999, 9999 }, @as(u64, 8900199891));
591 doTheTestReduce(.Mul, [4]i128{ -9, -99, -999, 9999 }, @as(i128, -8900199891));591 try doTheTestReduce(.Mul, [4]i128{ -9, -99, -999, 9999 }, @as(i128, -8900199891));
592 doTheTestReduce(.Mul, [4]u128{ 9, 99, 999, 9999 }, @as(u128, 8900199891));592 try doTheTestReduce(.Mul, [4]u128{ 9, 99, 999, 9999 }, @as(u128, 8900199891));
593 doTheTestReduce(.Mul, [4]f16{ -1.9, 5.1, -60.3, 100.0 }, @as(f16, 58430.7));593 try doTheTestReduce(.Mul, [4]f16{ -1.9, 5.1, -60.3, 100.0 }, @as(f16, 58430.7));
594 doTheTestReduce(.Mul, [4]f32{ -1.9, 5.1, -60.3, 100.0 }, @as(f32, 58430.7));594 try doTheTestReduce(.Mul, [4]f32{ -1.9, 5.1, -60.3, 100.0 }, @as(f32, 58430.7));
595 doTheTestReduce(.Mul, [4]f64{ -1.9, 5.1, -60.3, 100.0 }, @as(f64, 58430.7));595 try doTheTestReduce(.Mul, [4]f64{ -1.9, 5.1, -60.3, 100.0 }, @as(f64, 58430.7));
596596
597 doTheTestReduce(.Or, [4]bool{ false, true, false, false }, @as(bool, true));597 try doTheTestReduce(.Or, [4]bool{ false, true, false, false }, @as(bool, true));
598 doTheTestReduce(.Or, [4]u1{ 0, 1, 0, 0 }, @as(u1, 1));598 try doTheTestReduce(.Or, [4]u1{ 0, 1, 0, 0 }, @as(u1, 1));
599 doTheTestReduce(.Or, [4]u16{ 0xff00, 0xff00, 0xf0, 0xf }, ~@as(u16, 0));599 try doTheTestReduce(.Or, [4]u16{ 0xff00, 0xff00, 0xf0, 0xf }, ~@as(u16, 0));
600 doTheTestReduce(.Or, [4]u32{ 0xffff0000, 0xff00, 0xf0, 0xf }, ~@as(u32, 0));600 try doTheTestReduce(.Or, [4]u32{ 0xffff0000, 0xff00, 0xf0, 0xf }, ~@as(u32, 0));
601 doTheTestReduce(.Or, [4]u64{ 0xffff0000, 0xff00, 0xf0, 0xf }, @as(u64, 0xffffffff));601 try doTheTestReduce(.Or, [4]u64{ 0xffff0000, 0xff00, 0xf0, 0xf }, @as(u64, 0xffffffff));
602 doTheTestReduce(.Or, [4]u128{ 0xffff0000, 0xff00, 0xf0, 0xf }, @as(u128, 0xffffffff));602 try doTheTestReduce(.Or, [4]u128{ 0xffff0000, 0xff00, 0xf0, 0xf }, @as(u128, 0xffffffff));
603603
604 doTheTestReduce(.Xor, [4]bool{ true, true, true, false }, @as(bool, true));604 try doTheTestReduce(.Xor, [4]bool{ true, true, true, false }, @as(bool, true));
605 doTheTestReduce(.Xor, [4]u1{ 1, 1, 1, 0 }, @as(u1, 1));605 try doTheTestReduce(.Xor, [4]u1{ 1, 1, 1, 0 }, @as(u1, 1));
606 doTheTestReduce(.Xor, [4]u16{ 0x0000, 0x3333, 0x8888, 0x4444 }, ~@as(u16, 0));606 try doTheTestReduce(.Xor, [4]u16{ 0x0000, 0x3333, 0x8888, 0x4444 }, ~@as(u16, 0));
607 doTheTestReduce(.Xor, [4]u32{ 0x00000000, 0x33333333, 0x88888888, 0x44444444 }, ~@as(u32, 0));607 try doTheTestReduce(.Xor, [4]u32{ 0x00000000, 0x33333333, 0x88888888, 0x44444444 }, ~@as(u32, 0));
608 doTheTestReduce(.Xor, [4]u64{ 0x00000000, 0x33333333, 0x88888888, 0x44444444 }, @as(u64, 0xffffffff));608 try doTheTestReduce(.Xor, [4]u64{ 0x00000000, 0x33333333, 0x88888888, 0x44444444 }, @as(u64, 0xffffffff));
609 doTheTestReduce(.Xor, [4]u128{ 0x00000000, 0x33333333, 0x88888888, 0x44444444 }, @as(u128, 0xffffffff));609 try doTheTestReduce(.Xor, [4]u128{ 0x00000000, 0x33333333, 0x88888888, 0x44444444 }, @as(u128, 0xffffffff));
610610
611 // Test the reduction on vectors containing NaNs.611 // Test the reduction on vectors containing NaNs.
612 const f16_nan = math.nan(f16);612 const f16_nan = math.nan(f16);
613 const f32_nan = math.nan(f32);613 const f32_nan = math.nan(f32);
614 const f64_nan = math.nan(f64);614 const f64_nan = math.nan(f64);
615615
616 doTheTestReduce(.Add, [4]f16{ -1.9, 5.1, f16_nan, 100.0 }, f16_nan);616 try doTheTestReduce(.Add, [4]f16{ -1.9, 5.1, f16_nan, 100.0 }, f16_nan);
617 doTheTestReduce(.Add, [4]f32{ -1.9, 5.1, f32_nan, 100.0 }, f32_nan);617 try doTheTestReduce(.Add, [4]f32{ -1.9, 5.1, f32_nan, 100.0 }, f32_nan);
618 doTheTestReduce(.Add, [4]f64{ -1.9, 5.1, f64_nan, 100.0 }, f64_nan);618 try doTheTestReduce(.Add, [4]f64{ -1.9, 5.1, f64_nan, 100.0 }, f64_nan);
619619
620 // LLVM 11 ERROR: Cannot select type620 // LLVM 11 ERROR: Cannot select type
621 // https://github.com/ziglang/zig/issues/7138621 // https://github.com/ziglang/zig/issues/7138
622 if (false) {622 if (false) {
623 doTheTestReduce(.Min, [4]f16{ -1.9, 5.1, f16_nan, 100.0 }, f16_nan);623 try doTheTestReduce(.Min, [4]f16{ -1.9, 5.1, f16_nan, 100.0 }, f16_nan);
624 doTheTestReduce(.Min, [4]f32{ -1.9, 5.1, f32_nan, 100.0 }, f32_nan);624 try doTheTestReduce(.Min, [4]f32{ -1.9, 5.1, f32_nan, 100.0 }, f32_nan);
625 doTheTestReduce(.Min, [4]f64{ -1.9, 5.1, f64_nan, 100.0 }, f64_nan);625 try doTheTestReduce(.Min, [4]f64{ -1.9, 5.1, f64_nan, 100.0 }, f64_nan);
626626
627 doTheTestReduce(.Max, [4]f16{ -1.9, 5.1, f16_nan, 100.0 }, f16_nan);627 try doTheTestReduce(.Max, [4]f16{ -1.9, 5.1, f16_nan, 100.0 }, f16_nan);
628 doTheTestReduce(.Max, [4]f32{ -1.9, 5.1, f32_nan, 100.0 }, f32_nan);628 try doTheTestReduce(.Max, [4]f32{ -1.9, 5.1, f32_nan, 100.0 }, f32_nan);
629 doTheTestReduce(.Max, [4]f64{ -1.9, 5.1, f64_nan, 100.0 }, f64_nan);629 try doTheTestReduce(.Max, [4]f64{ -1.9, 5.1, f64_nan, 100.0 }, f64_nan);
630 }630 }
631631
632 doTheTestReduce(.Mul, [4]f16{ -1.9, 5.1, f16_nan, 100.0 }, f16_nan);632 try doTheTestReduce(.Mul, [4]f16{ -1.9, 5.1, f16_nan, 100.0 }, f16_nan);
633 doTheTestReduce(.Mul, [4]f32{ -1.9, 5.1, f32_nan, 100.0 }, f32_nan);633 try doTheTestReduce(.Mul, [4]f32{ -1.9, 5.1, f32_nan, 100.0 }, f32_nan);
634 doTheTestReduce(.Mul, [4]f64{ -1.9, 5.1, f64_nan, 100.0 }, f64_nan);634 try doTheTestReduce(.Mul, [4]f64{ -1.9, 5.1, f64_nan, 100.0 }, f64_nan);
635 }635 }
636 };636 };
637637
638 S.doTheTest();638 try S.doTheTest();
639 comptime S.doTheTest();639 comptime try S.doTheTest();
640}640}
test/stage1/behavior/void.zig+3-3
...@@ -13,14 +13,14 @@ test "compare void with void compile time known" {...@@ -13,14 +13,14 @@ test "compare void with void compile time known" {
13 .b = 1,13 .b = 1,
14 .c = {},14 .c = {},
15 };15 };
16 expect(foo.a == {});16 try expect(foo.a == {});
17 }17 }
18}18}
1919
20test "iterate over a void slice" {20test "iterate over a void slice" {
21 var j: usize = 0;21 var j: usize = 0;
22 for (times(10)) |_, i| {22 for (times(10)) |_, i| {
23 expect(i == j);23 try expect(i == j);
24 j += 1;24 j += 1;
25 }25 }
26}26}
...@@ -31,7 +31,7 @@ fn times(n: usize) []const void {...@@ -31,7 +31,7 @@ fn times(n: usize) []const void {
3131
32test "void optional" {32test "void optional" {
33 var x: ?void = {};33 var x: ?void = {};
34 expect(x != null);34 try expect(x != null);
35}35}
3636
37test "void array as a local variable initializer" {37test "void array as a local variable initializer" {
test/stage1/behavior/wasm.zig+2-2
...@@ -3,6 +3,6 @@ const expect = std.testing.expect;...@@ -3,6 +3,6 @@ const expect = std.testing.expect;
33
4test "memory size and grow" {4test "memory size and grow" {
5 var prev = @wasmMemorySize(0);5 var prev = @wasmMemorySize(0);
6 expect(prev == @wasmMemoryGrow(0, 1));6 try expect(prev == @wasmMemoryGrow(0, 1));
7 expect(prev + 1 == @wasmMemorySize(0));7 try expect(prev + 1 == @wasmMemorySize(0));
8}8}
test/stage1/behavior/while.zig+45-51
...@@ -6,8 +6,8 @@ test "while loop" {...@@ -6,8 +6,8 @@ test "while loop" {
6 while (i < 4) {6 while (i < 4) {
7 i += 1;7 i += 1;
8 }8 }
9 expect(i == 4);9 try expect(i == 4);
10 expect(whileLoop1() == 1);10 try expect(whileLoop1() == 1);
11}11}
12fn whileLoop1() i32 {12fn whileLoop1() i32 {
13 return whileLoop2();13 return whileLoop2();
...@@ -19,7 +19,7 @@ fn whileLoop2() i32 {...@@ -19,7 +19,7 @@ fn whileLoop2() i32 {
19}19}
2020
21test "static eval while" {21test "static eval while" {
22 expect(static_eval_while_number == 1);22 try expect(static_eval_while_number == 1);
23}23}
24const static_eval_while_number = staticWhileLoop1();24const static_eval_while_number = staticWhileLoop1();
25fn staticWhileLoop1() i32 {25fn staticWhileLoop1() i32 {
...@@ -32,11 +32,11 @@ fn staticWhileLoop2() i32 {...@@ -32,11 +32,11 @@ fn staticWhileLoop2() i32 {
32}32}
3333
34test "continue and break" {34test "continue and break" {
35 runContinueAndBreakTest();35 try runContinueAndBreakTest();
36 expect(continue_and_break_counter == 8);36 try expect(continue_and_break_counter == 8);
37}37}
38var continue_and_break_counter: i32 = 0;38var continue_and_break_counter: i32 = 0;
39fn runContinueAndBreakTest() void {39fn runContinueAndBreakTest() !void {
40 var i: i32 = 0;40 var i: i32 = 0;
41 while (true) {41 while (true) {
42 continue_and_break_counter += 2;42 continue_and_break_counter += 2;
...@@ -46,7 +46,7 @@ fn runContinueAndBreakTest() void {...@@ -46,7 +46,7 @@ fn runContinueAndBreakTest() void {
46 }46 }
47 break;47 break;
48 }48 }
49 expect(i == 4);49 try expect(i == 4);
50}50}
5151
52test "return with implicit cast from while loop" {52test "return with implicit cast from while loop" {
...@@ -67,7 +67,7 @@ test "while with continue expression" {...@@ -67,7 +67,7 @@ test "while with continue expression" {
67 sum += i;67 sum += i;
68 }68 }
69 }69 }
70 expect(sum == 40);70 try expect(sum == 40);
71}71}
7272
73test "while with else" {73test "while with else" {
...@@ -79,8 +79,8 @@ test "while with else" {...@@ -79,8 +79,8 @@ test "while with else" {
79 } else {79 } else {
80 got_else += 1;80 got_else += 1;
81 }81 }
82 expect(sum == 10);82 try expect(sum == 10);
83 expect(got_else == 1);83 try expect(got_else == 1);
84}84}
8585
86test "while with optional as condition" {86test "while with optional as condition" {
...@@ -89,7 +89,7 @@ test "while with optional as condition" {...@@ -89,7 +89,7 @@ test "while with optional as condition" {
89 while (getNumberOrNull()) |value| {89 while (getNumberOrNull()) |value| {
90 sum += value;90 sum += value;
91 }91 }
92 expect(sum == 45);92 try expect(sum == 45);
93}93}
9494
95test "while with optional as condition with else" {95test "while with optional as condition with else" {
...@@ -98,12 +98,12 @@ test "while with optional as condition with else" {...@@ -98,12 +98,12 @@ test "while with optional as condition with else" {
98 var got_else: i32 = 0;98 var got_else: i32 = 0;
99 while (getNumberOrNull()) |value| {99 while (getNumberOrNull()) |value| {
100 sum += value;100 sum += value;
101 expect(got_else == 0);101 try expect(got_else == 0);
102 } else {102 } else {
103 got_else += 1;103 got_else += 1;
104 }104 }
105 expect(sum == 45);105 try expect(sum == 45);
106 expect(got_else == 1);106 try expect(got_else == 1);
107}107}
108108
109test "while with error union condition" {109test "while with error union condition" {
...@@ -113,11 +113,11 @@ test "while with error union condition" {...@@ -113,11 +113,11 @@ test "while with error union condition" {
113 while (getNumberOrErr()) |value| {113 while (getNumberOrErr()) |value| {
114 sum += value;114 sum += value;
115 } else |err| {115 } else |err| {
116 expect(err == error.OutOfNumbers);116 try expect(err == error.OutOfNumbers);
117 got_else += 1;117 got_else += 1;
118 }118 }
119 expect(sum == 45);119 try expect(sum == 45);
120 expect(got_else == 1);120 try expect(got_else == 1);
121}121}
122122
123var numbers_left: i32 = undefined;123var numbers_left: i32 = undefined;
...@@ -137,49 +137,43 @@ fn getNumberOrNull() ?i32 {...@@ -137,49 +137,43 @@ fn getNumberOrNull() ?i32 {
137test "while on optional with else result follow else prong" {137test "while on optional with else result follow else prong" {
138 const result = while (returnNull()) |value| {138 const result = while (returnNull()) |value| {
139 break value;139 break value;
140 } else140 } else @as(i32, 2);
141 @as(i32, 2);141 try expect(result == 2);
142 expect(result == 2);
143}142}
144143
145test "while on optional with else result follow break prong" {144test "while on optional with else result follow break prong" {
146 const result = while (returnOptional(10)) |value| {145 const result = while (returnOptional(10)) |value| {
147 break value;146 break value;
148 } else147 } else @as(i32, 2);
149 @as(i32, 2);148 try expect(result == 10);
150 expect(result == 10);
151}149}
152150
153test "while on error union with else result follow else prong" {151test "while on error union with else result follow else prong" {
154 const result = while (returnError()) |value| {152 const result = while (returnError()) |value| {
155 break value;153 break value;
156 } else |err|154 } else |err| @as(i32, 2);
157 @as(i32, 2);155 try expect(result == 2);
158 expect(result == 2);
159}156}
160157
161test "while on error union with else result follow break prong" {158test "while on error union with else result follow break prong" {
162 const result = while (returnSuccess(10)) |value| {159 const result = while (returnSuccess(10)) |value| {
163 break value;160 break value;
164 } else |err|161 } else |err| @as(i32, 2);
165 @as(i32, 2);162 try expect(result == 10);
166 expect(result == 10);
167}163}
168164
169test "while on bool with else result follow else prong" {165test "while on bool with else result follow else prong" {
170 const result = while (returnFalse()) {166 const result = while (returnFalse()) {
171 break @as(i32, 10);167 break @as(i32, 10);
172 } else168 } else @as(i32, 2);
173 @as(i32, 2);169 try expect(result == 2);
174 expect(result == 2);
175}170}
176171
177test "while on bool with else result follow break prong" {172test "while on bool with else result follow break prong" {
178 const result = while (returnTrue()) {173 const result = while (returnTrue()) {
179 break @as(i32, 10);174 break @as(i32, 10);
180 } else175 } else @as(i32, 2);
181 @as(i32, 2);176 try expect(result == 10);
182 expect(result == 10);
183}177}
184178
185test "break from outer while loop" {179test "break from outer while loop" {
...@@ -230,60 +224,60 @@ fn returnTrue() bool {...@@ -230,60 +224,60 @@ fn returnTrue() bool {
230224
231test "while bool 2 break statements and an else" {225test "while bool 2 break statements and an else" {
232 const S = struct {226 const S = struct {
233 fn entry(t: bool, f: bool) void {227 fn entry(t: bool, f: bool) !void {
234 var ok = false;228 var ok = false;
235 ok = while (t) {229 ok = while (t) {
236 if (f) break false;230 if (f) break false;
237 if (t) break true;231 if (t) break true;
238 } else false;232 } else false;
239 expect(ok);233 try expect(ok);
240 }234 }
241 };235 };
242 S.entry(true, false);236 try S.entry(true, false);
243 comptime S.entry(true, false);237 comptime try S.entry(true, false);
244}238}
245239
246test "while optional 2 break statements and an else" {240test "while optional 2 break statements and an else" {
247 const S = struct {241 const S = struct {
248 fn entry(opt_t: ?bool, f: bool) void {242 fn entry(opt_t: ?bool, f: bool) !void {
249 var ok = false;243 var ok = false;
250 ok = while (opt_t) |t| {244 ok = while (opt_t) |t| {
251 if (f) break false;245 if (f) break false;
252 if (t) break true;246 if (t) break true;
253 } else false;247 } else false;
254 expect(ok);248 try expect(ok);
255 }249 }
256 };250 };
257 S.entry(true, false);251 try S.entry(true, false);
258 comptime S.entry(true, false);252 comptime try S.entry(true, false);
259}253}
260254
261test "while error 2 break statements and an else" {255test "while error 2 break statements and an else" {
262 const S = struct {256 const S = struct {
263 fn entry(opt_t: anyerror!bool, f: bool) void {257 fn entry(opt_t: anyerror!bool, f: bool) !void {
264 var ok = false;258 var ok = false;
265 ok = while (opt_t) |t| {259 ok = while (opt_t) |t| {
266 if (f) break false;260 if (f) break false;
267 if (t) break true;261 if (t) break true;
268 } else |_| false;262 } else |_| false;
269 expect(ok);263 try expect(ok);
270 }264 }
271 };265 };
272 S.entry(true, false);266 try S.entry(true, false);
273 comptime S.entry(true, false);267 comptime try S.entry(true, false);
274}268}
275269
276test "while copies its payload" {270test "while copies its payload" {
277 const S = struct {271 const S = struct {
278 fn doTheTest() void {272 fn doTheTest() !void {
279 var tmp: ?i32 = 10;273 var tmp: ?i32 = 10;
280 while (tmp) |value| {274 while (tmp) |value| {
281 // Modify the original variable275 // Modify the original variable
282 tmp = null;276 tmp = null;
283 expect(value == 10);277 try expect(value == 10);
284 }278 }
285 }279 }
286 };280 };
287 S.doTheTest();281 try S.doTheTest();
288 comptime S.doTheTest();282 comptime try S.doTheTest();
289}283}
test/stage1/behavior/widening.zig+6-6
...@@ -9,13 +9,13 @@ test "integer widening" {...@@ -9,13 +9,13 @@ test "integer widening" {
9 var d: u64 = c;9 var d: u64 = c;
10 var e: u64 = d;10 var e: u64 = d;
11 var f: u128 = e;11 var f: u128 = e;
12 expect(f == a);12 try expect(f == a);
13}13}
1414
15test "implicit unsigned integer to signed integer" {15test "implicit unsigned integer to signed integer" {
16 var a: u8 = 250;16 var a: u8 = 250;
17 var b: i16 = a;17 var b: i16 = a;
18 expect(b == 250);18 try expect(b == 250);
19}19}
2020
21test "float widening" {21test "float widening" {
...@@ -23,9 +23,9 @@ test "float widening" {...@@ -23,9 +23,9 @@ test "float widening" {
23 var b: f32 = a;23 var b: f32 = a;
24 var c: f64 = b;24 var c: f64 = b;
25 var d: f128 = c;25 var d: f128 = c;
26 expect(a == b);26 try expect(a == b);
27 expect(b == c);27 try expect(b == c);
28 expect(c == d);28 try expect(c == d);
29}29}
3030
31test "float widening f16 to f128" {31test "float widening f16 to f128" {
...@@ -35,5 +35,5 @@ test "float widening f16 to f128" {...@@ -35,5 +35,5 @@ test "float widening f16 to f128" {
3535
36 var x: f16 = 12.34;36 var x: f16 = 12.34;
37 var y: f128 = x;37 var y: f128 = x;
38 expect(x == y);38 try expect(x == y);
39}39}
test/stage1/c_abi/main.zig+58-58
...@@ -24,11 +24,11 @@ extern fn c_i64(i64) void;...@@ -24,11 +24,11 @@ extern fn c_i64(i64) void;
24extern fn c_five_integers(i32, i32, i32, i32, i32) void;24extern fn c_five_integers(i32, i32, i32, i32, i32) void;
2525
26export fn zig_five_integers(a: i32, b: i32, c: i32, d: i32, e: i32) void {26export fn zig_five_integers(a: i32, b: i32, c: i32, d: i32, e: i32) void {
27 expect(a == 12);27 expect(a == 12) catch @panic("test failure");
28 expect(b == 34);28 expect(b == 34) catch @panic("test failure");
29 expect(c == 56);29 expect(c == 56) catch @panic("test failure");
30 expect(d == 78);30 expect(d == 78) catch @panic("test failure");
31 expect(e == 90);31 expect(e == 90) catch @panic("test failure");
32}32}
3333
34test "C ABI integers" {34test "C ABI integers" {
...@@ -45,28 +45,28 @@ test "C ABI integers" {...@@ -45,28 +45,28 @@ test "C ABI integers" {
45}45}
4646
47export fn zig_u8(x: u8) void {47export fn zig_u8(x: u8) void {
48 expect(x == 0xff);48 expect(x == 0xff) catch @panic("test failure");
49}49}
50export fn zig_u16(x: u16) void {50export fn zig_u16(x: u16) void {
51 expect(x == 0xfffe);51 expect(x == 0xfffe) catch @panic("test failure");
52}52}
53export fn zig_u32(x: u32) void {53export fn zig_u32(x: u32) void {
54 expect(x == 0xfffffffd);54 expect(x == 0xfffffffd) catch @panic("test failure");
55}55}
56export fn zig_u64(x: u64) void {56export fn zig_u64(x: u64) void {
57 expect(x == 0xfffffffffffffffc);57 expect(x == 0xfffffffffffffffc) catch @panic("test failure");
58}58}
59export fn zig_i8(x: i8) void {59export fn zig_i8(x: i8) void {
60 expect(x == -1);60 expect(x == -1) catch @panic("test failure");
61}61}
62export fn zig_i16(x: i16) void {62export fn zig_i16(x: i16) void {
63 expect(x == -2);63 expect(x == -2) catch @panic("test failure");
64}64}
65export fn zig_i32(x: i32) void {65export fn zig_i32(x: i32) void {
66 expect(x == -3);66 expect(x == -3) catch @panic("test failure");
67}67}
68export fn zig_i64(x: i64) void {68export fn zig_i64(x: i64) void {
69 expect(x == -4);69 expect(x == -4) catch @panic("test failure");
70}70}
7171
72extern fn c_f32(f32) void;72extern fn c_f32(f32) void;
...@@ -76,11 +76,11 @@ extern fn c_f64(f64) void;...@@ -76,11 +76,11 @@ extern fn c_f64(f64) void;
76extern fn c_five_floats(f32, f32, f32, f32, f32) void;76extern fn c_five_floats(f32, f32, f32, f32, f32) void;
7777
78export fn zig_five_floats(a: f32, b: f32, c: f32, d: f32, e: f32) void {78export fn zig_five_floats(a: f32, b: f32, c: f32, d: f32, e: f32) void {
79 expect(a == 1.0);79 expect(a == 1.0) catch @panic("test failure");
80 expect(b == 2.0);80 expect(b == 2.0) catch @panic("test failure");
81 expect(c == 3.0);81 expect(c == 3.0) catch @panic("test failure");
82 expect(d == 4.0);82 expect(d == 4.0) catch @panic("test failure");
83 expect(e == 5.0);83 expect(e == 5.0) catch @panic("test failure");
84}84}
8585
86test "C ABI floats" {86test "C ABI floats" {
...@@ -90,10 +90,10 @@ test "C ABI floats" {...@@ -90,10 +90,10 @@ test "C ABI floats" {
90}90}
9191
92export fn zig_f32(x: f32) void {92export fn zig_f32(x: f32) void {
93 expect(x == 12.34);93 expect(x == 12.34) catch @panic("test failure");
94}94}
95export fn zig_f64(x: f64) void {95export fn zig_f64(x: f64) void {
96 expect(x == 56.78);96 expect(x == 56.78) catch @panic("test failure");
97}97}
9898
99extern fn c_ptr(*c_void) void;99extern fn c_ptr(*c_void) void;
...@@ -103,7 +103,7 @@ test "C ABI pointer" {...@@ -103,7 +103,7 @@ test "C ABI pointer" {
103}103}
104104
105export fn zig_ptr(x: *c_void) void {105export fn zig_ptr(x: *c_void) void {
106 expect(@ptrToInt(x) == 0xdeadbeef);106 expect(@ptrToInt(x) == 0xdeadbeef) catch @panic("test failure");
107}107}
108108
109extern fn c_bool(bool) void;109extern fn c_bool(bool) void;
...@@ -113,7 +113,7 @@ test "C ABI bool" {...@@ -113,7 +113,7 @@ test "C ABI bool" {
113}113}
114114
115export fn zig_bool(x: bool) void {115export fn zig_bool(x: bool) void {
116 expect(x);116 expect(x) catch @panic("test failure");
117}117}
118118
119const BigStruct = extern struct {119const BigStruct = extern struct {
...@@ -137,11 +137,11 @@ test "C ABI big struct" {...@@ -137,11 +137,11 @@ test "C ABI big struct" {
137}137}
138138
139export fn zig_big_struct(x: BigStruct) void {139export fn zig_big_struct(x: BigStruct) void {
140 expect(x.a == 1);140 expect(x.a == 1) catch @panic("test failure");
141 expect(x.b == 2);141 expect(x.b == 2) catch @panic("test failure");
142 expect(x.c == 3);142 expect(x.c == 3) catch @panic("test failure");
143 expect(x.d == 4);143 expect(x.d == 4) catch @panic("test failure");
144 expect(x.e == 5);144 expect(x.e == 5) catch @panic("test failure");
145}145}
146146
147const BigUnion = extern union {147const BigUnion = extern union {
...@@ -163,11 +163,11 @@ test "C ABI big union" {...@@ -163,11 +163,11 @@ test "C ABI big union" {
163}163}
164164
165export fn zig_big_union(x: BigUnion) void {165export fn zig_big_union(x: BigUnion) void {
166 expect(x.a.a == 1);166 expect(x.a.a == 1) catch @panic("test failure");
167 expect(x.a.b == 2);167 expect(x.a.b == 2) catch @panic("test failure");
168 expect(x.a.c == 3);168 expect(x.a.c == 3) catch @panic("test failure");
169 expect(x.a.d == 4);169 expect(x.a.d == 4) catch @panic("test failure");
170 expect(x.a.e == 5);170 expect(x.a.e == 5) catch @panic("test failure");
171}171}
172172
173const SmallStructInts = extern struct {173const SmallStructInts = extern struct {
...@@ -189,10 +189,10 @@ test "C ABI small struct of ints" {...@@ -189,10 +189,10 @@ test "C ABI small struct of ints" {
189}189}
190190
191export fn zig_small_struct_ints(x: SmallStructInts) void {191export fn zig_small_struct_ints(x: SmallStructInts) void {
192 expect(x.a == 1);192 expect(x.a == 1) catch @panic("test failure");
193 expect(x.b == 2);193 expect(x.b == 2) catch @panic("test failure");
194 expect(x.c == 3);194 expect(x.c == 3) catch @panic("test failure");
195 expect(x.d == 4);195 expect(x.d == 4) catch @panic("test failure");
196}196}
197197
198const SplitStructInt = extern struct {198const SplitStructInt = extern struct {
...@@ -212,9 +212,9 @@ test "C ABI split struct of ints" {...@@ -212,9 +212,9 @@ test "C ABI split struct of ints" {
212}212}
213213
214export fn zig_split_struct_ints(x: SplitStructInt) void {214export fn zig_split_struct_ints(x: SplitStructInt) void {
215 expect(x.a == 1234);215 expect(x.a == 1234) catch @panic("test failure");
216 expect(x.b == 100);216 expect(x.b == 100) catch @panic("test failure");
217 expect(x.c == 1337);217 expect(x.c == 1337) catch @panic("test failure");
218}218}
219219
220extern fn c_big_struct_both(BigStruct) BigStruct;220extern fn c_big_struct_both(BigStruct) BigStruct;
...@@ -228,19 +228,19 @@ test "C ABI sret and byval together" {...@@ -228,19 +228,19 @@ test "C ABI sret and byval together" {
228 .e = 5,228 .e = 5,
229 };229 };
230 var y = c_big_struct_both(s);230 var y = c_big_struct_both(s);
231 expect(y.a == 10);231 try expect(y.a == 10);
232 expect(y.b == 11);232 try expect(y.b == 11);
233 expect(y.c == 12);233 try expect(y.c == 12);
234 expect(y.d == 13);234 try expect(y.d == 13);
235 expect(y.e == 14);235 try expect(y.e == 14);
236}236}
237237
238export fn zig_big_struct_both(x: BigStruct) BigStruct {238export fn zig_big_struct_both(x: BigStruct) BigStruct {
239 expect(x.a == 30);239 expect(x.a == 30) catch @panic("test failure");
240 expect(x.b == 31);240 expect(x.b == 31) catch @panic("test failure");
241 expect(x.c == 32);241 expect(x.c == 32) catch @panic("test failure");
242 expect(x.d == 33);242 expect(x.d == 33) catch @panic("test failure");
243 expect(x.e == 34);243 expect(x.e == 34) catch @panic("test failure");
244 var s = BigStruct{244 var s = BigStruct{
245 .a = 20,245 .a = 20,
246 .b = 21,246 .b = 21,
...@@ -324,15 +324,15 @@ extern fn c_ret_i32() i32;...@@ -324,15 +324,15 @@ extern fn c_ret_i32() i32;
324extern fn c_ret_i64() i64;324extern fn c_ret_i64() i64;
325325
326test "C ABI integer return types" {326test "C ABI integer return types" {
327 expect(c_ret_bool() == true);327 try expect(c_ret_bool() == true);
328328
329 expect(c_ret_u8() == 0xff);329 try expect(c_ret_u8() == 0xff);
330 expect(c_ret_u16() == 0xffff);330 try expect(c_ret_u16() == 0xffff);
331 expect(c_ret_u32() == 0xffffffff);331 try expect(c_ret_u32() == 0xffffffff);
332 expect(c_ret_u64() == 0xffffffffffffffff);332 try expect(c_ret_u64() == 0xffffffffffffffff);
333333
334 expect(c_ret_i8() == -1);334 try expect(c_ret_i8() == -1);
335 expect(c_ret_i16() == -1);335 try expect(c_ret_i16() == -1);
336 expect(c_ret_i32() == -1);336 try expect(c_ret_i32() == -1);
337 expect(c_ret_i64() == -1);337 try expect(c_ret_i64() == -1);
338}338}
test/standalone/brace_expansion/main.zig+31-31
...@@ -241,52 +241,52 @@ pub fn main() !void {...@@ -241,52 +241,52 @@ pub fn main() !void {
241test "invalid inputs" {241test "invalid inputs" {
242 global_allocator = std.testing.allocator;242 global_allocator = std.testing.allocator;
243243
244 expectError("}ABC", error.InvalidInput);244 try expectError("}ABC", error.InvalidInput);
245 expectError("{ABC", error.InvalidInput);245 try expectError("{ABC", error.InvalidInput);
246 expectError("}{", error.InvalidInput);246 try expectError("}{", error.InvalidInput);
247 expectError("{}", error.InvalidInput);247 try expectError("{}", error.InvalidInput);
248 expectError("A,B,C", error.InvalidInput);248 try expectError("A,B,C", error.InvalidInput);
249 expectError("{A{B,C}", error.InvalidInput);249 try expectError("{A{B,C}", error.InvalidInput);
250 expectError("{A,}", error.InvalidInput);250 try expectError("{A,}", error.InvalidInput);
251251
252 expectError("\n", error.InvalidInput);252 try expectError("\n", error.InvalidInput);
253}253}
254254
255fn expectError(test_input: []const u8, expected_err: anyerror) void {255fn expectError(test_input: []const u8, expected_err: anyerror) !void {
256 var output_buf = ArrayList(u8).init(global_allocator);256 var output_buf = ArrayList(u8).init(global_allocator);
257 defer output_buf.deinit();257 defer output_buf.deinit();
258258
259 testing.expectError(expected_err, expandString(test_input, &output_buf));259 try testing.expectError(expected_err, expandString(test_input, &output_buf));
260}260}
261261
262test "valid inputs" {262test "valid inputs" {
263 global_allocator = std.testing.allocator;263 global_allocator = std.testing.allocator;
264264
265 expectExpansion("{x,y,z}", "x y z");265 try expectExpansion("{x,y,z}", "x y z");
266 expectExpansion("{A,B}{x,y}", "Ax Ay Bx By");266 try expectExpansion("{A,B}{x,y}", "Ax Ay Bx By");
267 expectExpansion("{A,B{x,y}}", "A Bx By");267 try expectExpansion("{A,B{x,y}}", "A Bx By");
268268
269 expectExpansion("{ABC}", "ABC");269 try expectExpansion("{ABC}", "ABC");
270 expectExpansion("{A,B,C}", "A B C");270 try expectExpansion("{A,B,C}", "A B C");
271 expectExpansion("ABC", "ABC");271 try expectExpansion("ABC", "ABC");
272272
273 expectExpansion("", "");273 try expectExpansion("", "");
274 expectExpansion("{A,B}{C,{x,y}}{g,h}", "ACg ACh Axg Axh Ayg Ayh BCg BCh Bxg Bxh Byg Byh");274 try expectExpansion("{A,B}{C,{x,y}}{g,h}", "ACg ACh Axg Axh Ayg Ayh BCg BCh Bxg Bxh Byg Byh");
275 expectExpansion("{A,B}{C,C{x,y}}{g,h}", "ACg ACh ACxg ACxh ACyg ACyh BCg BCh BCxg BCxh BCyg BCyh");275 try expectExpansion("{A,B}{C,C{x,y}}{g,h}", "ACg ACh ACxg ACxh ACyg ACyh BCg BCh BCxg BCxh BCyg BCyh");
276 expectExpansion("{A,B}a", "Aa Ba");276 try expectExpansion("{A,B}a", "Aa Ba");
277 expectExpansion("{C,{x,y}}", "C x y");277 try expectExpansion("{C,{x,y}}", "C x y");
278 expectExpansion("z{C,{x,y}}", "zC zx zy");278 try expectExpansion("z{C,{x,y}}", "zC zx zy");
279 expectExpansion("a{b,c{d,e{f,g}}}", "ab acd acef aceg");279 try expectExpansion("a{b,c{d,e{f,g}}}", "ab acd acef aceg");
280 expectExpansion("a{x,y}b", "axb ayb");280 try expectExpansion("a{x,y}b", "axb ayb");
281 expectExpansion("z{{a,b}}", "za zb");281 try expectExpansion("z{{a,b}}", "za zb");
282 expectExpansion("a{b}", "ab");282 try expectExpansion("a{b}", "ab");
283}283}
284284
285fn expectExpansion(test_input: []const u8, expected_result: []const u8) void {285fn expectExpansion(test_input: []const u8, expected_result: []const u8) !void {
286 var result = ArrayList(u8).init(global_allocator);286 var result = ArrayList(u8).init(global_allocator);
287 defer result.deinit();287 defer result.deinit();
288288
289 expandString(test_input, &result) catch unreachable;289 expandString(test_input, &result) catch unreachable;
290290
291 testing.expectEqualSlices(u8, expected_result, result.items);291 try testing.expectEqualSlices(u8, expected_result, result.items);
292}292}
test/standalone/empty_env/main.zig+2-2
...@@ -1,6 +1,6 @@...@@ -1,6 +1,6 @@
1const std = @import("std");1const std = @import("std");
22
3pub fn main() void {3pub fn main() !void {
4 const env_map = std.process.getEnvMap(std.testing.allocator) catch @panic("unable to get env map");4 const env_map = std.process.getEnvMap(std.testing.allocator) catch @panic("unable to get env map");
5 std.testing.expect(env_map.count() == 0);5 try std.testing.expect(env_map.count() == 0);
6}6}
test/standalone/global_linkage/main.zig+2-2
...@@ -4,6 +4,6 @@ extern var obj1_integer: usize;...@@ -4,6 +4,6 @@ extern var obj1_integer: usize;
4extern var obj2_integer: usize;4extern var obj2_integer: usize;
55
6test "access the external integers" {6test "access the external integers" {
7 std.testing.expect(obj1_integer == 421);7 try std.testing.expect(obj1_integer == 421);
8 std.testing.expect(obj2_integer == 422);8 try std.testing.expect(obj2_integer == 422);
9}9}
test/standalone/issue_794/main.zig+1-1
...@@ -3,5 +3,5 @@ const std = @import("std");...@@ -3,5 +3,5 @@ const std = @import("std");
3const testing = std.testing;3const testing = std.testing;
44
5test "c import" {5test "c import" {
6 comptime testing.expect(c.NUMBER == 1234);6 comptime try testing.expect(c.NUMBER == 1234);
7}7}
test/standalone/link_interdependent_static_c_libs/main.zig+1-1
...@@ -4,5 +4,5 @@ const c = @cImport(@cInclude("b.h"));...@@ -4,5 +4,5 @@ const c = @cImport(@cInclude("b.h"));
44
5test "import C sub" {5test "import C sub" {
6 const result = c.sub(2, 1);6 const result = c.sub(2, 1);
7 expect(result == 1);7 try expect(result == 1);
8}8}
test/standalone/static_c_lib/foo.zig+2-2
...@@ -4,9 +4,9 @@ const c = @cImport(@cInclude("foo.h"));...@@ -4,9 +4,9 @@ const c = @cImport(@cInclude("foo.h"));
44
5test "C add" {5test "C add" {
6 const result = c.add(1, 2);6 const result = c.add(1, 2);
7 expect(result == 3);7 try expect(result == 3);
8}8}
99
10test "C extern variable" {10test "C extern variable" {
11 expect(c.foo == 12345);11 try expect(c.foo == 12345);
12}12}
test/standalone/use_alias/main.zig+1-1
...@@ -6,5 +6,5 @@ test "symbol exists" {...@@ -6,5 +6,5 @@ test "symbol exists" {
6 .a = 1,6 .a = 1,
7 .b = 1,7 .b = 1,
8 };8 };
9 expect(foo.a + foo.b == 2);9 try expect(foo.a + foo.b == 2);
10}10}