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 {
2929
3030 const dir_path = try fs.path.join(a, &[_][]const u8{ cache_root, "clitest" });
3131 defer fs.cwd().deleteTree(dir_path) catch {};
32
32
3333 const TestFn = fn ([]const u8, []const u8) anyerror!void;
3434 const test_fns = [_]TestFn{
3535 testZigInitLib,
......@@ -94,13 +94,13 @@ fn exec(cwd: []const u8, expect_0: bool, argv: []const []const u8) !ChildProcess
9494fn testZigInitLib(zig_exe: []const u8, dir_path: []const u8) !void {
9595 _ = try exec(dir_path, true, &[_][]const u8{ zig_exe, "init-lib" });
9696 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");
9898}
9999
100100fn testZigInitExe(zig_exe: []const u8, dir_path: []const u8) !void {
101101 _ = try exec(dir_path, true, &[_][]const u8{ zig_exe, "init-exe" });
102102 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);
104104}
105105
106106fn 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 {
136136 _ = try exec(dir_path, true, args.items);
137137
138138 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);
140 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);
139 try testing.expect(std.mem.indexOf(u8, out_asm, "square:") != null);
140 try testing.expect(std.mem.indexOf(u8, out_asm, "mov\teax, edi") != null);
141 try testing.expect(std.mem.indexOf(u8, out_asm, "imul\teax, edi") != null);
142142}
143143
144144fn 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 {
149149 const result = try exec(dir_path, false, &[_][]const u8{ zig_exe, "build-exe", source_path, output_arg });
150150 const s = std.fs.path.sep_str;
151151 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);
153153}
154154
155155fn 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
163163 const run_result1 = try exec(dir_path, true, &[_][]const u8{ zig_exe, "fmt", fmt1_zig_path });
164164 // stderr should be file path + \n
165 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');
165 try testing.expect(std.mem.startsWith(u8, run_result1.stdout, fmt1_zig_path));
166 try testing.expect(run_result1.stdout.len == fmt1_zig_path.len + 1 and run_result1.stdout[run_result1.stdout.len - 1] == '\n');
167167
168168 const fmt2_zig_path = try fs.path.join(a, &[_][]const u8{ dir_path, "fmt2.zig" });
169169 try fs.cwd().writeFile(fmt2_zig_path, unformatted_code);
170170
171171 const run_result2 = try exec(dir_path, true, &[_][]const u8{ zig_exe, "fmt", dir_path });
172172 // 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));
174 testing.expect(run_result2.stdout.len == fmt2_zig_path.len + 1 and run_result2.stdout[run_result2.stdout.len - 1] == '\n');
173 try testing.expect(std.mem.startsWith(u8, run_result2.stdout, fmt2_zig_path));
174 try testing.expect(run_result2.stdout.len == fmt2_zig_path.len + 1 and run_result2.stdout[run_result2.stdout.len - 1] == '\n');
175175
176176 const run_result3 = try exec(dir_path, true, &[_][]const u8{ zig_exe, "fmt", dir_path });
177177 // 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
180180 // Check UTF-16 decoding
181181 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 {
183183 try fs.cwd().writeFile(fmt4_zig_path, unformatted_code_utf16);
184184
185185 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));
187 testing.expect(run_result4.stdout.len == fmt4_zig_path.len + 1 and run_result4.stdout[run_result4.stdout.len - 1] == '\n');
186 try testing.expect(std.mem.startsWith(u8, run_result4.stdout, fmt4_zig_path));
187 try testing.expect(run_result4.stdout.len == fmt4_zig_path.len + 1 and run_result4.stdout[run_result4.stdout.len - 1] == '\n');
188188}
test/compile_errors.zig+1-1
......@@ -230,7 +230,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
230230
231231 cases.add("array in c exported function",
232232 \\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"));
234234 \\}
235235 \\
236236 \\export fn zig_return_array() [10]u8 {
test/stack_traces.zig+17-17
......@@ -5,13 +5,13 @@ const tests = @import("tests.zig");
55pub fn addCases(cases: *tests.StackTracesContext) void {
66 cases.addCase(.{
77 .name = "return",
8 .source =
8 .source =
99 \\pub fn main() !void {
1010 \\ return error.TheSkyIsFalling;
1111 \\}
1212 ,
1313 .Debug = .{
14 .expect =
14 .expect =
1515 \\error: TheSkyIsFalling
1616 \\source.zig:2:5: [address] in main (test)
1717 \\ return error.TheSkyIsFalling;
......@@ -23,7 +23,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
2323 .exclude_os = .{
2424 .windows, // segfault
2525 },
26 .expect =
26 .expect =
2727 \\error: TheSkyIsFalling
2828 \\source.zig:2:5: [address] in [function]
2929 \\ return error.TheSkyIsFalling;
......@@ -32,13 +32,13 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
3232 ,
3333 },
3434 .ReleaseFast = .{
35 .expect =
35 .expect =
3636 \\error: TheSkyIsFalling
3737 \\
3838 ,
3939 },
4040 .ReleaseSmall = .{
41 .expect =
41 .expect =
4242 \\error: TheSkyIsFalling
4343 \\
4444 ,
......@@ -47,7 +47,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
4747
4848 cases.addCase(.{
4949 .name = "try return",
50 .source =
50 .source =
5151 \\fn foo() !void {
5252 \\ return error.TheSkyIsFalling;
5353 \\}
......@@ -57,7 +57,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
5757 \\}
5858 ,
5959 .Debug = .{
60 .expect =
60 .expect =
6161 \\error: TheSkyIsFalling
6262 \\source.zig:2:5: [address] in foo (test)
6363 \\ return error.TheSkyIsFalling;
......@@ -72,7 +72,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
7272 .exclude_os = .{
7373 .windows, // segfault
7474 },
75 .expect =
75 .expect =
7676 \\error: TheSkyIsFalling
7777 \\source.zig:2:5: [address] in [function]
7878 \\ return error.TheSkyIsFalling;
......@@ -84,13 +84,13 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
8484 ,
8585 },
8686 .ReleaseFast = .{
87 .expect =
87 .expect =
8888 \\error: TheSkyIsFalling
8989 \\
9090 ,
9191 },
9292 .ReleaseSmall = .{
93 .expect =
93 .expect =
9494 \\error: TheSkyIsFalling
9595 \\
9696 ,
......@@ -99,7 +99,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
9999
100100 cases.addCase(.{
101101 .name = "try try return return",
102 .source =
102 .source =
103103 \\fn foo() !void {
104104 \\ try bar();
105105 \\}
......@@ -117,7 +117,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
117117 \\}
118118 ,
119119 .Debug = .{
120 .expect =
120 .expect =
121121 \\error: TheSkyIsFalling
122122 \\source.zig:10:5: [address] in make_error (test)
123123 \\ return error.TheSkyIsFalling;
......@@ -138,7 +138,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
138138 .exclude_os = .{
139139 .windows, // segfault
140140 },
141 .expect =
141 .expect =
142142 \\error: TheSkyIsFalling
143143 \\source.zig:10:5: [address] in [function]
144144 \\ return error.TheSkyIsFalling;
......@@ -156,13 +156,13 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
156156 ,
157157 },
158158 .ReleaseFast = .{
159 .expect =
159 .expect =
160160 \\error: TheSkyIsFalling
161161 \\
162162 ,
163163 },
164164 .ReleaseSmall = .{
165 .expect =
165 .expect =
166166 \\error: TheSkyIsFalling
167167 \\
168168 ,
......@@ -174,7 +174,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
174174 .windows,
175175 },
176176 .name = "dumpCurrentStackTrace",
177 .source =
177 .source =
178178 \\const std = @import("std");
179179 \\
180180 \\fn bar() void {
......@@ -189,7 +189,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
189189 \\}
190190 ,
191191 .Debug = .{
192 .expect =
192 .expect =
193193 \\source.zig:7:8: [address] in foo (test)
194194 \\ bar();
195195 \\ ^
test/stage1/behavior/align.zig+66-66
......@@ -5,16 +5,16 @@ const builtin = @import("builtin");
55var foo: u8 align(4) = 100;
66
77test "global variable alignment" {
8 comptime expect(@typeInfo(@TypeOf(&foo)).Pointer.alignment == 4);
9 comptime expect(@TypeOf(&foo) == *align(4) u8);
8 comptime try expect(@typeInfo(@TypeOf(&foo)).Pointer.alignment == 4);
9 comptime try expect(@TypeOf(&foo) == *align(4) u8);
1010 {
1111 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);
1313 }
1414 {
1515 var runtime_zero: usize = 0;
1616 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);
1818 }
1919}
2020
......@@ -28,9 +28,9 @@ test "function alignment" {
2828 // function alignment is a compile error on wasm32/wasm64
2929 if (builtin.arch == .wasm32 or builtin.arch == .wasm64) return error.SkipZigTest;
3030
31 expect(derp() == 1234);
32 expect(@TypeOf(noop1) == fn () align(1) void);
33 expect(@TypeOf(noop4) == fn () align(4) void);
31 try expect(derp() == 1234);
32 try expect(@TypeOf(noop1) == fn () align(1) void);
33 try expect(@TypeOf(noop4) == fn () align(4) void);
3434 noop1();
3535 noop4();
3636}
......@@ -41,7 +41,7 @@ var baz: packed struct {
4141} = undefined;
4242
4343test "packed struct alignment" {
44 expect(@TypeOf(&baz.b) == *align(1) u32);
44 try expect(@TypeOf(&baz.b) == *align(1) u32);
4545}
4646
4747const blah: packed struct {
......@@ -51,17 +51,17 @@ const blah: packed struct {
5151} = undefined;
5252
5353test "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);
5555}
5656
5757test "default alignment allows unspecified in type syntax" {
58 expect(*u32 == *align(@alignOf(u32)) u32);
58 try expect(*u32 == *align(@alignOf(u32)) u32);
5959}
6060
6161test "implicitly decreasing pointer alignment" {
6262 const a: u32 align(4) = 3;
6363 const b: u32 align(8) = 4;
64 expect(addUnaligned(&a, &b) == 7);
64 try expect(addUnaligned(&a, &b) == 7);
6565}
6666
6767fn 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 {
7171test "implicitly decreasing slice alignment" {
7272 const a: u32 align(4) = 3;
7373 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);
7575}
7676fn addUnalignedSlice(a: []align(1) const u32, b: []align(1) const u32) u32 {
7777 return a[0] + b[0];
7878}
7979
8080test "specifying alignment allows pointer cast" {
81 testBytesAlign(0x33);
81 try testBytesAlign(0x33);
8282}
83fn testBytesAlign(b: u8) void {
83fn testBytesAlign(b: u8) !void {
8484 var bytes align(4) = [_]u8{
8585 b,
8686 b,
......@@ -88,13 +88,13 @@ fn testBytesAlign(b: u8) void {
8888 b,
8989 };
9090 const ptr = @ptrCast(*u32, &bytes[0]);
91 expect(ptr.* == 0x33333333);
91 try expect(ptr.* == 0x33333333);
9292}
9393
9494test "@alignCast pointers" {
9595 var x: u32 align(4) = 1;
9696 expectsOnly1(&x);
97 expect(x == 2);
97 try expect(x == 2);
9898}
9999fn expectsOnly1(x: *align(1) u32) void {
100100 expects4(@alignCast(4, x));
......@@ -110,7 +110,7 @@ test "@alignCast slices" {
110110 };
111111 const slice = array[0..];
112112 sliceExpectsOnly1(slice);
113 expect(slice[0] == 2);
113 try expect(slice[0] == 2);
114114}
115115fn sliceExpectsOnly1(slice: []align(1) u32) void {
116116 sliceExpects4(@alignCast(4, slice));
......@@ -123,12 +123,12 @@ test "implicitly decreasing fn alignment" {
123123 // function alignment is a compile error on wasm32/wasm64
124124 if (builtin.arch == .wasm32 or builtin.arch == .wasm64) return error.SkipZigTest;
125125
126 testImplicitlyDecreaseFnAlign(alignedSmall, 1234);
127 testImplicitlyDecreaseFnAlign(alignedBig, 5678);
126 try testImplicitlyDecreaseFnAlign(alignedSmall, 1234);
127 try testImplicitlyDecreaseFnAlign(alignedBig, 5678);
128128}
129129
130fn testImplicitlyDecreaseFnAlign(ptr: fn () align(1) i32, answer: i32) void {
131 expect(ptr() == answer);
130fn testImplicitlyDecreaseFnAlign(ptr: fn () align(1) i32, answer: i32) !void {
131 try expect(ptr() == answer);
132132}
133133
134134fn alignedSmall() align(8) i32 {
......@@ -143,7 +143,7 @@ test "@alignCast functions" {
143143 if (builtin.arch == .wasm32 or builtin.arch == .wasm64) return error.SkipZigTest;
144144 if (builtin.arch == .thumb) return error.SkipZigTest;
145145
146 expect(fnExpectsOnly1(simple4) == 0x19);
146 try expect(fnExpectsOnly1(simple4) == 0x19);
147147}
148148fn fnExpectsOnly1(ptr: fn () align(1) i32) i32 {
149149 return fnExpects4(@alignCast(4, ptr));
......@@ -160,9 +160,9 @@ test "generic function with align param" {
160160 if (builtin.arch == .wasm32 or builtin.arch == .wasm64) return error.SkipZigTest;
161161 if (builtin.arch == .thumb) return error.SkipZigTest;
162162
163 expect(whyWouldYouEverDoThis(1) == 0x1);
164 expect(whyWouldYouEverDoThis(4) == 0x1);
165 expect(whyWouldYouEverDoThis(8) == 0x1);
163 try expect(whyWouldYouEverDoThis(1) == 0x1);
164 try expect(whyWouldYouEverDoThis(4) == 0x1);
165 try expect(whyWouldYouEverDoThis(8) == 0x1);
166166}
167167
168168fn whyWouldYouEverDoThis(comptime align_bytes: u8) align(align_bytes) u8 {
......@@ -172,49 +172,49 @@ fn whyWouldYouEverDoThis(comptime align_bytes: u8) align(align_bytes) u8 {
172172test "@ptrCast preserves alignment of bigger source" {
173173 var x: u32 align(16) = 1234;
174174 const ptr = @ptrCast(*u8, &x);
175 expect(@TypeOf(ptr) == *align(16) u8);
175 try expect(@TypeOf(ptr) == *align(16) u8);
176176}
177177
178178test "runtime known array index has best alignment possible" {
179179 // take full advantage of over-alignment
180180 var array align(4) = [_]u8{ 1, 2, 3, 4 };
181 expect(@TypeOf(&array[0]) == *align(4) u8);
182 expect(@TypeOf(&array[1]) == *u8);
183 expect(@TypeOf(&array[2]) == *align(2) u8);
184 expect(@TypeOf(&array[3]) == *u8);
181 try expect(@TypeOf(&array[0]) == *align(4) u8);
182 try expect(@TypeOf(&array[1]) == *u8);
183 try expect(@TypeOf(&array[2]) == *align(2) u8);
184 try expect(@TypeOf(&array[3]) == *u8);
185185
186186 // because align is too small but we still figure out to use 2
187187 var bigger align(2) = [_]u64{ 1, 2, 3, 4 };
188 expect(@TypeOf(&bigger[0]) == *align(2) u64);
189 expect(@TypeOf(&bigger[1]) == *align(2) u64);
190 expect(@TypeOf(&bigger[2]) == *align(2) u64);
191 expect(@TypeOf(&bigger[3]) == *align(2) u64);
188 try expect(@TypeOf(&bigger[0]) == *align(2) u64);
189 try expect(@TypeOf(&bigger[1]) == *align(2) u64);
190 try expect(@TypeOf(&bigger[2]) == *align(2) u64);
191 try expect(@TypeOf(&bigger[3]) == *align(2) u64);
192192
193193 // because pointer is align 2 and u32 align % 2 == 0 we can assume align 2
194194 var smaller align(2) = [_]u32{ 1, 2, 3, 4 };
195195 var runtime_zero: usize = 0;
196 comptime expect(@TypeOf(smaller[runtime_zero..]) == []align(2) u32);
197 comptime expect(@TypeOf(smaller[runtime_zero..].ptr) == [*]align(2) u32);
198 testIndex(smaller[runtime_zero..].ptr, 0, *align(2) u32);
199 testIndex(smaller[runtime_zero..].ptr, 1, *align(2) u32);
200 testIndex(smaller[runtime_zero..].ptr, 2, *align(2) u32);
201 testIndex(smaller[runtime_zero..].ptr, 3, *align(2) u32);
196 comptime try expect(@TypeOf(smaller[runtime_zero..]) == []align(2) u32);
197 comptime try expect(@TypeOf(smaller[runtime_zero..].ptr) == [*]align(2) u32);
198 try testIndex(smaller[runtime_zero..].ptr, 0, *align(2) u32);
199 try testIndex(smaller[runtime_zero..].ptr, 1, *align(2) u32);
200 try testIndex(smaller[runtime_zero..].ptr, 2, *align(2) u32);
201 try testIndex(smaller[runtime_zero..].ptr, 3, *align(2) u32);
202202
203203 // has to use ABI alignment because index known at runtime only
204 testIndex2(array[runtime_zero..].ptr, 0, *u8);
205 testIndex2(array[runtime_zero..].ptr, 1, *u8);
206 testIndex2(array[runtime_zero..].ptr, 2, *u8);
207 testIndex2(array[runtime_zero..].ptr, 3, *u8);
204 try testIndex2(array[runtime_zero..].ptr, 0, *u8);
205 try testIndex2(array[runtime_zero..].ptr, 1, *u8);
206 try testIndex2(array[runtime_zero..].ptr, 2, *u8);
207 try testIndex2(array[runtime_zero..].ptr, 3, *u8);
208208}
209fn testIndex(smaller: [*]align(2) u32, index: usize, comptime T: type) void {
210 comptime expect(@TypeOf(&smaller[index]) == T);
209fn testIndex(smaller: [*]align(2) u32, index: usize, comptime T: type) !void {
210 comptime try expect(@TypeOf(&smaller[index]) == T);
211211}
212fn testIndex2(ptr: [*]align(4) u8, index: usize, comptime T: type) void {
213 comptime expect(@TypeOf(&ptr[index]) == T);
212fn testIndex2(ptr: [*]align(4) u8, index: usize, comptime T: type) !void {
213 comptime try expect(@TypeOf(&ptr[index]) == T);
214214}
215215
216216test "alignstack" {
217 expect(fnWithAlignedStack() == 1234);
217 try expect(fnWithAlignedStack() == 1234);
218218}
219219
220220fn fnWithAlignedStack() i32 {
......@@ -223,7 +223,7 @@ fn fnWithAlignedStack() i32 {
223223}
224224
225225test "alignment of structs" {
226 expect(@alignOf(struct {
226 try expect(@alignOf(struct {
227227 a: i32,
228228 b: *i32,
229229 }) == @alignOf(usize));
......@@ -239,37 +239,37 @@ test "alignment of function with c calling convention" {
239239fn nothing() callconv(.C) void {}
240240
241241test "return error union with 128-bit integer" {
242 expect(3 == try give());
242 try expect(3 == try give());
243243}
244244fn give() anyerror!u128 {
245245 return 3;
246246}
247247
248248test "alignment of >= 128-bit integer type" {
249 expect(@alignOf(u128) == 16);
250 expect(@alignOf(u129) == 16);
249 try expect(@alignOf(u128) == 16);
250 try expect(@alignOf(u129) == 16);
251251}
252252
253253test "alignment of struct with 128-bit field" {
254 expect(@alignOf(struct {
254 try expect(@alignOf(struct {
255255 x: u128,
256256 }) == 16);
257257
258258 comptime {
259 expect(@alignOf(struct {
259 try expect(@alignOf(struct {
260260 x: u128,
261261 }) == 16);
262262 }
263263}
264264
265265test "size of extern struct with 128-bit field" {
266 expect(@sizeOf(extern struct {
266 try expect(@sizeOf(extern struct {
267267 x: u128,
268268 y: u8,
269269 }) == 32);
270270
271271 comptime {
272 expect(@sizeOf(extern struct {
272 try expect(@sizeOf(extern struct {
273273 x: u128,
274274 y: u8,
275275 }) == 32);
......@@ -286,8 +286,8 @@ test "read 128-bit field from default aligned struct in stack memory" {
286286 .nevermind = 1,
287287 .badguy = 12,
288288 };
289 expect((@ptrToInt(&default_aligned.badguy) % 16) == 0);
290 expect(12 == default_aligned.badguy);
289 try expect((@ptrToInt(&default_aligned.badguy) % 16) == 0);
290 try expect(12 == default_aligned.badguy);
291291}
292292
293293var default_aligned_global = DefaultAligned{
......@@ -296,8 +296,8 @@ var default_aligned_global = DefaultAligned{
296296};
297297
298298test "read 128-bit field from default aligned struct in global memory" {
299 expect((@ptrToInt(&default_aligned_global.badguy) % 16) == 0);
300 expect(12 == default_aligned_global.badguy);
299 try expect((@ptrToInt(&default_aligned_global.badguy) % 16) == 0);
300 try expect(12 == default_aligned_global.badguy);
301301}
302302
303303test "struct field explicit alignment" {
......@@ -310,9 +310,9 @@ test "struct field explicit alignment" {
310310
311311 var node: S.Node = undefined;
312312 node.massive_byte = 100;
313 expect(node.massive_byte == 100);
314 comptime expect(@TypeOf(&node.massive_byte) == *align(64) u8);
315 expect(@ptrToInt(&node.massive_byte) % 64 == 0);
313 try expect(node.massive_byte == 100);
314 comptime try expect(@TypeOf(&node.massive_byte) == *align(64) u8);
315 try expect(@ptrToInt(&node.massive_byte) % 64 == 0);
316316}
317317
318318test "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" {
334334 var ok = false;
335335 };
336336 _ = async S.doTheTest();
337 expect(S.ok);
337 try expect(S.ok);
338338}
339339
340340test "align(N) on functions" {
......@@ -342,7 +342,7 @@ test "align(N) on functions" {
342342 if (builtin.arch == .wasm32 or builtin.arch == .wasm64) return error.SkipZigTest;
343343 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);
346346}
347347fn overaligned_fn() align(0x1000) i32 {
348348 return 42;
test/stage1/behavior/alignof.zig+14-14
......@@ -10,29 +10,29 @@ const Foo = struct {
1010};
1111
1212test "@alignOf(T) before referencing T" {
13 comptime expect(@alignOf(Foo) != maxInt(usize));
13 comptime try expect(@alignOf(Foo) != maxInt(usize));
1414 if (builtin.arch == builtin.Arch.x86_64) {
15 comptime expect(@alignOf(Foo) == 4);
15 comptime try expect(@alignOf(Foo) == 4);
1616 }
1717}
1818
1919test "comparison of @alignOf(T) against zero" {
2020 {
2121 const T = struct { x: u32 };
22 expect(!(@alignOf(T) == 0));
23 expect(@alignOf(T) != 0);
24 expect(!(@alignOf(T) < 0));
25 expect(!(@alignOf(T) <= 0));
26 expect(@alignOf(T) > 0);
27 expect(@alignOf(T) >= 0);
22 try expect(!(@alignOf(T) == 0));
23 try expect(@alignOf(T) != 0);
24 try expect(!(@alignOf(T) < 0));
25 try expect(!(@alignOf(T) <= 0));
26 try expect(@alignOf(T) > 0);
27 try expect(@alignOf(T) >= 0);
2828 }
2929 {
3030 const T = struct {};
31 expect(@alignOf(T) == 0);
32 expect(!(@alignOf(T) != 0));
33 expect(!(@alignOf(T) < 0));
34 expect(@alignOf(T) <= 0);
35 expect(!(@alignOf(T) > 0));
36 expect(@alignOf(T) >= 0);
31 try expect(@alignOf(T) == 0);
32 try expect(!(@alignOf(T) != 0));
33 try expect(!(@alignOf(T) < 0));
34 try expect(@alignOf(T) <= 0);
35 try expect(!(@alignOf(T) > 0));
36 try expect(@alignOf(T) >= 0);
3737 }
3838}
test/stage1/behavior/array.zig+141-141
......@@ -21,8 +21,8 @@ test "arrays" {
2121 i += 1;
2222 }
2323
24 expect(accumulator == 15);
25 expect(getArrayLen(&array) == 5);
24 try expect(accumulator == 15);
25 try expect(getArrayLen(&array) == 5);
2626}
2727fn getArrayLen(a: []const u32) usize {
2828 return a.len;
......@@ -30,37 +30,37 @@ fn getArrayLen(a: []const u32) usize {
3030
3131test "array with sentinels" {
3232 const S = struct {
33 fn doTheTest(is_ct: bool) void {
33 fn doTheTest(is_ct: bool) !void {
3434 if (is_ct) {
3535 var zero_sized: [0:0xde]u8 = [_:0xde]u8{};
3636 // Disabled at runtime because of
3737 // https://github.com/ziglang/zig/issues/4372
38 expectEqual(@as(u8, 0xde), zero_sized[0]);
38 try expectEqual(@as(u8, 0xde), zero_sized[0]);
3939 var reinterpreted = @ptrCast(*[1]u8, &zero_sized);
40 expectEqual(@as(u8, 0xde), reinterpreted[0]);
40 try expectEqual(@as(u8, 0xde), reinterpreted[0]);
4141 }
4242 var arr: [3:0x55]u8 = undefined;
4343 // Make sure the sentinel pointer is pointing after the last element
4444 if (!is_ct) {
4545 const sentinel_ptr = @ptrToInt(&arr[3]);
4646 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);
4848 }
4949 // Make sure the sentinel is writeable
5050 arr[3] = 0x55;
5151 }
5252 };
5353
54 S.doTheTest(false);
55 comptime S.doTheTest(true);
54 try S.doTheTest(false);
55 comptime try S.doTheTest(true);
5656}
5757
5858test "void arrays" {
5959 var array: [4]void = undefined;
6060 array[0] = void{};
6161 array[1] = array[2];
62 expect(@sizeOf(@TypeOf(array)) == 0);
63 expect(array.len == 4);
62 try expect(@sizeOf(@TypeOf(array)) == 0);
63 try expect(array.len == 4);
6464}
6565
6666test "array literal" {
......@@ -71,12 +71,12 @@ test "array literal" {
7171 1,
7272 };
7373
74 expect(hex_mult.len == 4);
75 expect(hex_mult[1] == 256);
74 try expect(hex_mult.len == 4);
75 try expect(hex_mult[1] == 256);
7676}
7777
7878test "array dot len const expr" {
79 expect(comptime x: {
79 try expect(comptime x: {
8080 break :x some_array.len == 4;
8181 });
8282}
......@@ -100,11 +100,11 @@ test "nested arrays" {
100100 "thing",
101101 };
102102 for (array_of_strings) |s, i| {
103 if (i == 0) expect(mem.eql(u8, s, "hello"));
104 if (i == 1) expect(mem.eql(u8, s, "this"));
105 if (i == 2) expect(mem.eql(u8, s, "is"));
106 if (i == 3) expect(mem.eql(u8, s, "my"));
107 if (i == 4) expect(mem.eql(u8, s, "thing"));
103 if (i == 0) try expect(mem.eql(u8, s, "hello"));
104 if (i == 1) try expect(mem.eql(u8, s, "this"));
105 if (i == 2) try expect(mem.eql(u8, s, "is"));
106 if (i == 3) try expect(mem.eql(u8, s, "my"));
107 if (i == 4) try expect(mem.eql(u8, s, "thing"));
108108 }
109109}
110110
......@@ -122,9 +122,9 @@ test "set global var array via slice embedded in struct" {
122122 s.a[1].b = 2;
123123 s.a[2].b = 3;
124124
125 expect(s_array[0].b == 1);
126 expect(s_array[1].b == 2);
127 expect(s_array[2].b == 3);
125 try expect(s_array[0].b == 1);
126 try expect(s_array[1].b == 2);
127 try expect(s_array[2].b == 3);
128128}
129129
130130test "array literal with specified size" {
......@@ -132,34 +132,34 @@ test "array literal with specified size" {
132132 1,
133133 2,
134134 };
135 expect(array[0] == 1);
136 expect(array[1] == 2);
135 try expect(array[0] == 1);
136 try expect(array[1] == 2);
137137}
138138
139139test "array len field" {
140140 var arr = [4]u8{ 0, 0, 0, 0 };
141141 var ptr = &arr;
142 expect(arr.len == 4);
143 comptime expect(arr.len == 4);
144 expect(ptr.len == 4);
145 comptime expect(ptr.len == 4);
142 try expect(arr.len == 4);
143 comptime try expect(arr.len == 4);
144 try expect(ptr.len == 4);
145 comptime try expect(ptr.len == 4);
146146}
147147
148148test "single-item pointer to array indexing and slicing" {
149 testSingleItemPtrArrayIndexSlice();
150 comptime testSingleItemPtrArrayIndexSlice();
149 try testSingleItemPtrArrayIndexSlice();
150 comptime try testSingleItemPtrArrayIndexSlice();
151151}
152152
153fn testSingleItemPtrArrayIndexSlice() void {
153fn testSingleItemPtrArrayIndexSlice() !void {
154154 {
155155 var array: [4]u8 = "aaaa".*;
156156 doSomeMangling(&array);
157 expect(mem.eql(u8, "azya", &array));
157 try expect(mem.eql(u8, "azya", &array));
158158 }
159159 {
160160 var array = "aaaa".*;
161161 doSomeMangling(&array);
162 expect(mem.eql(u8, "azya", &array));
162 try expect(mem.eql(u8, "azya", &array));
163163 }
164164}
165165
......@@ -169,15 +169,15 @@ fn doSomeMangling(array: *[4]u8) void {
169169}
170170
171171test "implicit cast single-item pointer" {
172 testImplicitCastSingleItemPtr();
173 comptime testImplicitCastSingleItemPtr();
172 try testImplicitCastSingleItemPtr();
173 comptime try testImplicitCastSingleItemPtr();
174174}
175175
176fn testImplicitCastSingleItemPtr() void {
176fn testImplicitCastSingleItemPtr() !void {
177177 var byte: u8 = 100;
178178 const slice = @as(*[1]u8, &byte)[0..];
179179 slice[0] += 1;
180 expect(byte == 101);
180 try expect(byte == 101);
181181}
182182
183183fn testArrayByValAtComptime(b: [2]u8) u8 {
......@@ -192,7 +192,7 @@ test "comptime evalutating function that takes array by value" {
192192
193193test "implicit comptime in array type size" {
194194 var arr: [plusOne(10)]bool = undefined;
195 expect(arr.len == 11);
195 try expect(arr.len == 11);
196196}
197197
198198fn plusOne(x: u32) u32 {
......@@ -202,52 +202,52 @@ fn plusOne(x: u32) u32 {
202202test "runtime initialize array elem and then implicit cast to slice" {
203203 var two: i32 = 2;
204204 const x: []const i32 = &[_]i32{two};
205 expect(x[0] == 2);
205 try expect(x[0] == 2);
206206}
207207
208208test "array literal as argument to function" {
209209 const S = struct {
210 fn entry(two: i32) void {
211 foo(&[_]i32{
210 fn entry(two: i32) !void {
211 try foo(&[_]i32{
212212 1,
213213 2,
214214 3,
215215 });
216 foo(&[_]i32{
216 try foo(&[_]i32{
217217 1,
218218 two,
219219 3,
220220 });
221 foo2(true, &[_]i32{
221 try foo2(true, &[_]i32{
222222 1,
223223 2,
224224 3,
225225 });
226 foo2(true, &[_]i32{
226 try foo2(true, &[_]i32{
227227 1,
228228 two,
229229 3,
230230 });
231231 }
232 fn foo(x: []const i32) void {
233 expect(x[0] == 1);
234 expect(x[1] == 2);
235 expect(x[2] == 3);
232 fn foo(x: []const i32) !void {
233 try expect(x[0] == 1);
234 try expect(x[1] == 2);
235 try expect(x[2] == 3);
236236 }
237 fn foo2(trash: bool, x: []const i32) void {
238 expect(trash);
239 expect(x[0] == 1);
240 expect(x[1] == 2);
241 expect(x[2] == 3);
237 fn foo2(trash: bool, x: []const i32) !void {
238 try expect(trash);
239 try expect(x[0] == 1);
240 try expect(x[1] == 2);
241 try expect(x[2] == 3);
242242 }
243243 };
244 S.entry(2);
245 comptime S.entry(2);
244 try S.entry(2);
245 comptime try S.entry(2);
246246}
247247
248248test "double nested array to const slice cast in array literal" {
249249 const S = struct {
250 fn entry(two: i32) void {
250 fn entry(two: i32) !void {
251251 const cases = [_][]const []const i32{
252252 &[_][]const i32{&[_]i32{1}},
253253 &[_][]const i32{&[_]i32{ 2, 3 }},
......@@ -256,18 +256,18 @@ test "double nested array to const slice cast in array literal" {
256256 &[_]i32{ 5, 6, 7 },
257257 },
258258 };
259 check(&cases);
259 try check(&cases);
260260
261261 const cases2 = [_][]const i32{
262262 &[_]i32{1},
263263 &[_]i32{ two, 3 },
264264 };
265 expect(cases2.len == 2);
266 expect(cases2[0].len == 1);
267 expect(cases2[0][0] == 1);
268 expect(cases2[1].len == 2);
269 expect(cases2[1][0] == 2);
270 expect(cases2[1][1] == 3);
265 try expect(cases2.len == 2);
266 try expect(cases2[0].len == 1);
267 try expect(cases2[0][0] == 1);
268 try expect(cases2[1].len == 2);
269 try expect(cases2[1][0] == 2);
270 try expect(cases2[1][1] == 3);
271271
272272 const cases3 = [_][]const []const i32{
273273 &[_][]const i32{&[_]i32{1}},
......@@ -277,37 +277,37 @@ test "double nested array to const slice cast in array literal" {
277277 &[_]i32{ 5, 6, 7 },
278278 },
279279 };
280 check(&cases3);
280 try check(&cases3);
281281 }
282282
283 fn check(cases: []const []const []const i32) void {
284 expect(cases.len == 3);
285 expect(cases[0].len == 1);
286 expect(cases[0][0].len == 1);
287 expect(cases[0][0][0] == 1);
288 expect(cases[1].len == 1);
289 expect(cases[1][0].len == 2);
290 expect(cases[1][0][0] == 2);
291 expect(cases[1][0][1] == 3);
292 expect(cases[2].len == 2);
293 expect(cases[2][0].len == 1);
294 expect(cases[2][0][0] == 4);
295 expect(cases[2][1].len == 3);
296 expect(cases[2][1][0] == 5);
297 expect(cases[2][1][1] == 6);
298 expect(cases[2][1][2] == 7);
283 fn check(cases: []const []const []const i32) !void {
284 try expect(cases.len == 3);
285 try expect(cases[0].len == 1);
286 try expect(cases[0][0].len == 1);
287 try expect(cases[0][0][0] == 1);
288 try expect(cases[1].len == 1);
289 try expect(cases[1][0].len == 2);
290 try expect(cases[1][0][0] == 2);
291 try expect(cases[1][0][1] == 3);
292 try expect(cases[2].len == 2);
293 try expect(cases[2][0].len == 1);
294 try expect(cases[2][0][0] == 4);
295 try expect(cases[2][1].len == 3);
296 try expect(cases[2][1][0] == 5);
297 try expect(cases[2][1][1] == 6);
298 try expect(cases[2][1][2] == 7);
299299 }
300300 };
301 S.entry(2);
302 comptime S.entry(2);
301 try S.entry(2);
302 comptime try S.entry(2);
303303}
304304
305305test "read/write through global variable array of struct fields initialized via array mult" {
306306 const S = struct {
307 fn doTheTest() void {
308 expect(storage[0].term == 1);
307 fn doTheTest() !void {
308 try expect(storage[0].term == 1);
309309 storage[0] = MyStruct{ .term = 123 };
310 expect(storage[0].term == 123);
310 try expect(storage[0].term == 123);
311311 }
312312
313313 pub const MyStruct = struct {
......@@ -316,34 +316,34 @@ test "read/write through global variable array of struct fields initialized via
316316
317317 var storage: [1]MyStruct = [_]MyStruct{MyStruct{ .term = 1 }} ** 1;
318318 };
319 S.doTheTest();
319 try S.doTheTest();
320320}
321321
322322test "implicit cast zero sized array ptr to slice" {
323323 {
324324 var b = "".*;
325325 const c: []const u8 = &b;
326 expect(c.len == 0);
326 try expect(c.len == 0);
327327 }
328328 {
329329 var b: [0]u8 = "".*;
330330 const c: []const u8 = &b;
331 expect(c.len == 0);
331 try expect(c.len == 0);
332332 }
333333}
334334
335335test "anonymous list literal syntax" {
336336 const S = struct {
337 fn doTheTest() void {
337 fn doTheTest() !void {
338338 var array: [4]u8 = .{ 1, 2, 3, 4 };
339 expect(array[0] == 1);
340 expect(array[1] == 2);
341 expect(array[2] == 3);
342 expect(array[3] == 4);
339 try expect(array[0] == 1);
340 try expect(array[1] == 2);
341 try expect(array[2] == 3);
342 try expect(array[3] == 4);
343343 }
344344 };
345 S.doTheTest();
346 comptime S.doTheTest();
345 try S.doTheTest();
346 comptime try S.doTheTest();
347347}
348348
349349test "anonymous literal in array" {
......@@ -352,51 +352,51 @@ test "anonymous literal in array" {
352352 a: usize = 2,
353353 b: usize = 4,
354354 };
355 fn doTheTest() void {
355 fn doTheTest() !void {
356356 var array: [2]Foo = .{
357357 .{ .a = 3 },
358358 .{ .b = 3 },
359359 };
360 expect(array[0].a == 3);
361 expect(array[0].b == 4);
362 expect(array[1].a == 2);
363 expect(array[1].b == 3);
360 try expect(array[0].a == 3);
361 try expect(array[0].b == 4);
362 try expect(array[1].a == 2);
363 try expect(array[1].b == 3);
364364 }
365365 };
366 S.doTheTest();
367 comptime S.doTheTest();
366 try S.doTheTest();
367 comptime try S.doTheTest();
368368}
369369
370370test "access the null element of a null terminated array" {
371371 const S = struct {
372 fn doTheTest() void {
372 fn doTheTest() !void {
373373 var array: [4:0]u8 = .{ 'a', 'o', 'e', 'u' };
374 expect(array[4] == 0);
374 try expect(array[4] == 0);
375375 var len: usize = 4;
376 expect(array[len] == 0);
376 try expect(array[len] == 0);
377377 }
378378 };
379 S.doTheTest();
380 comptime S.doTheTest();
379 try S.doTheTest();
380 comptime try S.doTheTest();
381381}
382382
383383test "type deduction for array subscript expression" {
384384 const S = struct {
385 fn doTheTest() void {
385 fn doTheTest() !void {
386386 var array = [_]u8{ 0x55, 0xAA };
387387 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]);
389389 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]);
391391 }
392392 };
393 S.doTheTest();
394 comptime S.doTheTest();
393 try S.doTheTest();
394 comptime try S.doTheTest();
395395}
396396
397397test "sentinel element count towards the ABI size calculation" {
398398 const S = struct {
399 fn doTheTest() void {
399 fn doTheTest() !void {
400400 const T = packed struct {
401401 fill_pre: u8 = 0x55,
402402 data: [0:0]u8 = undefined,
......@@ -404,14 +404,14 @@ test "sentinel element count towards the ABI size calculation" {
404404 };
405405 var x = T{};
406406 var as_slice = mem.asBytes(&x);
407 expectEqual(@as(usize, 3), as_slice.len);
408 expectEqual(@as(u8, 0x55), as_slice[0]);
409 expectEqual(@as(u8, 0xAA), as_slice[2]);
407 try expectEqual(@as(usize, 3), as_slice.len);
408 try expectEqual(@as(u8, 0x55), as_slice[0]);
409 try expectEqual(@as(u8, 0xAA), as_slice[2]);
410410 }
411411 };
412412
413 S.doTheTest();
414 comptime S.doTheTest();
413 try S.doTheTest();
414 comptime try S.doTheTest();
415415}
416416
417417test "zero-sized array with recursive type definition" {
......@@ -429,61 +429,61 @@ test "zero-sized array with recursive type definition" {
429429 };
430430
431431 var t: S = .{ .list = .{ .s = undefined } };
432 expectEqual(@as(usize, 0), t.list.x);
432 try expectEqual(@as(usize, 0), t.list.x);
433433}
434434
435435test "type coercion of anon struct literal to array" {
436436 const S = struct {
437 const U = union{
437 const U = union {
438438 a: u32,
439439 b: bool,
440440 c: []const u8,
441441 };
442442
443 fn doTheTest() void {
443 fn doTheTest() !void {
444444 var x1: u8 = 42;
445445 const t1 = .{ x1, 56, 54 };
446446 var arr1: [3]u8 = t1;
447 expect(arr1[0] == 42);
448 expect(arr1[1] == 56);
449 expect(arr1[2] == 54);
450
447 try expect(arr1[0] == 42);
448 try expect(arr1[1] == 56);
449 try expect(arr1[2] == 54);
450
451451 var x2: U = .{ .a = 42 };
452452 const t2 = .{ x2, .{ .b = true }, .{ .c = "hello" } };
453453 var arr2: [3]U = t2;
454 expect(arr2[0].a == 42);
455 expect(arr2[1].b == true);
456 expect(mem.eql(u8, arr2[2].c, "hello"));
454 try expect(arr2[0].a == 42);
455 try expect(arr2[1].b == true);
456 try expect(mem.eql(u8, arr2[2].c, "hello"));
457457 }
458458 };
459 S.doTheTest();
460 comptime S.doTheTest();
459 try S.doTheTest();
460 comptime try S.doTheTest();
461461}
462462
463463test "type coercion of pointer to anon struct literal to pointer to array" {
464464 const S = struct {
465 const U = union{
465 const U = union {
466466 a: u32,
467467 b: bool,
468468 c: []const u8,
469469 };
470470
471 fn doTheTest() void {
471 fn doTheTest() !void {
472472 var x1: u8 = 42;
473473 const t1 = &.{ x1, 56, 54 };
474 var arr1: *const[3]u8 = t1;
475 expect(arr1[0] == 42);
476 expect(arr1[1] == 56);
477 expect(arr1[2] == 54);
478
474 var arr1: *const [3]u8 = t1;
475 try expect(arr1[0] == 42);
476 try expect(arr1[1] == 56);
477 try expect(arr1[2] == 54);
478
479479 var x2: U = .{ .a = 42 };
480480 const t2 = &.{ x2, .{ .b = true }, .{ .c = "hello" } };
481481 var arr2: *const [3]U = t2;
482 expect(arr2[0].a == 42);
483 expect(arr2[1].b == true);
484 expect(mem.eql(u8, arr2[2].c, "hello"));
482 try expect(arr2[0].a == 42);
483 try expect(arr2[1].b == true);
484 try expect(mem.eql(u8, arr2[2].c, "hello"));
485485 }
486486 };
487 S.doTheTest();
488 comptime S.doTheTest();
487 try S.doTheTest();
488 comptime try S.doTheTest();
489489}
test/stage1/behavior/asm.zig+1-1
......@@ -15,7 +15,7 @@ comptime {
1515
1616test "module level assembly" {
1717 if (is_x86_64_linux) {
18 expect(this_is_my_alias() == 1234);
18 try expect(this_is_my_alias() == 1234);
1919 }
2020}
2121
test/stage1/behavior/async_fn.zig+165-165
......@@ -9,12 +9,12 @@ var global_x: i32 = 1;
99
1010test "simple coroutine suspend and resume" {
1111 var frame = async simpleAsyncFn();
12 expect(global_x == 2);
12 try expect(global_x == 2);
1313 resume frame;
14 expect(global_x == 3);
14 try expect(global_x == 3);
1515 const af: anyframe->void = &frame;
1616 resume frame;
17 expect(global_x == 4);
17 try expect(global_x == 4);
1818}
1919fn simpleAsyncFn() void {
2020 global_x += 1;
......@@ -28,9 +28,9 @@ var global_y: i32 = 1;
2828
2929test "pass parameter to coroutine" {
3030 var p = async simpleAsyncFnWithArg(2);
31 expect(global_y == 3);
31 try expect(global_y == 3);
3232 resume p;
33 expect(global_y == 5);
33 try expect(global_y == 5);
3434}
3535fn simpleAsyncFnWithArg(delta: i32) void {
3636 global_y += delta;
......@@ -42,10 +42,10 @@ test "suspend at end of function" {
4242 const S = struct {
4343 var x: i32 = 1;
4444
45 fn doTheTest() void {
46 expect(x == 1);
45 fn doTheTest() !void {
46 try expect(x == 1);
4747 const p = async suspendAtEnd();
48 expect(x == 2);
48 try expect(x == 2);
4949 }
5050
5151 fn suspendAtEnd() void {
......@@ -53,23 +53,23 @@ test "suspend at end of function" {
5353 suspend {}
5454 }
5555 };
56 S.doTheTest();
56 try S.doTheTest();
5757}
5858
5959test "local variable in async function" {
6060 const S = struct {
6161 var x: i32 = 0;
6262
63 fn doTheTest() void {
64 expect(x == 0);
63 fn doTheTest() !void {
64 try expect(x == 0);
6565 var p = async add(1, 2);
66 expect(x == 0);
66 try expect(x == 0);
6767 resume p;
68 expect(x == 0);
68 try expect(x == 0);
6969 resume p;
70 expect(x == 0);
70 try expect(x == 0);
7171 resume p;
72 expect(x == 3);
72 try expect(x == 3);
7373 }
7474
7575 fn add(a: i32, b: i32) void {
......@@ -82,7 +82,7 @@ test "local variable in async function" {
8282 x = accum;
8383 }
8484 };
85 S.doTheTest();
85 try S.doTheTest();
8686}
8787
8888test "calling an inferred async function" {
......@@ -90,11 +90,11 @@ test "calling an inferred async function" {
9090 var x: i32 = 1;
9191 var other_frame: *@Frame(other) = undefined;
9292
93 fn doTheTest() void {
93 fn doTheTest() !void {
9494 _ = async first();
95 expect(x == 1);
95 try expect(x == 1);
9696 resume other_frame.*;
97 expect(x == 2);
97 try expect(x == 2);
9898 }
9999
100100 fn first() void {
......@@ -106,7 +106,7 @@ test "calling an inferred async function" {
106106 x += 1;
107107 }
108108 };
109 S.doTheTest();
109 try S.doTheTest();
110110}
111111
112112test "@frameSize" {
......@@ -114,16 +114,16 @@ test "@frameSize" {
114114 return error.SkipZigTest;
115115
116116 const S = struct {
117 fn doTheTest() void {
117 fn doTheTest() !void {
118118 {
119119 var ptr = @ptrCast(fn (i32) callconv(.Async) void, other);
120120 const size = @frameSize(ptr);
121 expect(size == @sizeOf(@Frame(other)));
121 try expect(size == @sizeOf(@Frame(other)));
122122 }
123123 {
124124 var ptr = @ptrCast(fn () callconv(.Async) void, first);
125125 const size = @frameSize(ptr);
126 expect(size == @sizeOf(@Frame(first)));
126 try expect(size == @sizeOf(@Frame(first)));
127127 }
128128 }
129129
......@@ -135,20 +135,20 @@ test "@frameSize" {
135135 suspend {}
136136 }
137137 };
138 S.doTheTest();
138 try S.doTheTest();
139139}
140140
141141test "coroutine suspend, resume" {
142142 const S = struct {
143143 var frame: anyframe = undefined;
144144
145 fn doTheTest() void {
145 fn doTheTest() !void {
146146 _ = async amain();
147147 seq('d');
148148 resume frame;
149149 seq('h');
150150
151 expect(std.mem.eql(u8, &points, "abcdefgh"));
151 try expect(std.mem.eql(u8, &points, "abcdefgh"));
152152 }
153153
154154 fn amain() void {
......@@ -176,27 +176,27 @@ test "coroutine suspend, resume" {
176176 index += 1;
177177 }
178178 };
179 S.doTheTest();
179 try S.doTheTest();
180180}
181181
182182test "coroutine suspend with block" {
183183 const p = async testSuspendBlock();
184 expect(!global_result);
184 try expect(!global_result);
185185 resume a_promise;
186 expect(global_result);
186 try expect(global_result);
187187}
188188
189189var a_promise: anyframe = undefined;
190190var global_result = false;
191191fn testSuspendBlock() callconv(.Async) void {
192192 suspend {
193 comptime expect(@TypeOf(@frame()) == *@Frame(testSuspendBlock));
193 comptime expect(@TypeOf(@frame()) == *@Frame(testSuspendBlock)) catch unreachable;
194194 a_promise = @frame();
195195 }
196196
197197 // Test to make sure that @frame() works as advertised (issue #1296)
198198 // var our_handle: anyframe = @frame();
199 expect(a_promise == @as(anyframe, @frame()));
199 expect(a_promise == @as(anyframe, @frame())) catch @panic("test failed");
200200
201201 global_result = true;
202202}
......@@ -210,8 +210,8 @@ test "coroutine await" {
210210 await_seq('f');
211211 resume await_a_promise;
212212 await_seq('i');
213 expect(await_final_result == 1234);
214 expect(std.mem.eql(u8, &await_points, "abcdefghi"));
213 try expect(await_final_result == 1234);
214 try expect(std.mem.eql(u8, &await_points, "abcdefghi"));
215215}
216216fn await_amain() callconv(.Async) void {
217217 await_seq('b');
......@@ -244,8 +244,8 @@ test "coroutine await early return" {
244244 early_seq('a');
245245 var p = async early_amain();
246246 early_seq('f');
247 expect(early_final_result == 1234);
248 expect(std.mem.eql(u8, &early_points, "abcdef"));
247 try expect(early_final_result == 1234);
248 try expect(std.mem.eql(u8, &early_points, "abcdef"));
249249}
250250fn early_amain() callconv(.Async) void {
251251 early_seq('b');
......@@ -276,7 +276,7 @@ test "async function with dot syntax" {
276276 }
277277 };
278278 const p = async S.foo();
279 expect(S.y == 2);
279 try expect(S.y == 2);
280280}
281281
282282test "async fn pointer in a struct field" {
......@@ -287,12 +287,12 @@ test "async fn pointer in a struct field" {
287287 var foo = Foo{ .bar = simpleAsyncFn2 };
288288 var bytes: [64]u8 align(16) = undefined;
289289 const f = @asyncCall(&bytes, {}, foo.bar, .{&data});
290 comptime expect(@TypeOf(f) == anyframe->void);
291 expect(data == 2);
290 comptime try expect(@TypeOf(f) == anyframe->void);
291 try expect(data == 2);
292292 resume f;
293 expect(data == 4);
293 try expect(data == 4);
294294 _ = async doTheAwait(f);
295 expect(data == 4);
295 try expect(data == 4);
296296}
297297
298298fn doTheAwait(f: anyframe->void) void {
......@@ -323,22 +323,22 @@ test "@asyncCall with return type" {
323323 var bytes: [150]u8 align(16) = undefined;
324324 var aresult: i32 = 0;
325325 _ = @asyncCall(&bytes, &aresult, foo.bar, .{});
326 expect(aresult == 0);
326 try expect(aresult == 0);
327327 resume Foo.global_frame;
328 expect(aresult == 1234);
328 try expect(aresult == 1234);
329329}
330330
331331test "async fn with inferred error set" {
332332 const S = struct {
333333 var global_frame: anyframe = undefined;
334334
335 fn doTheTest() void {
335 fn doTheTest() !void {
336336 var frame: [1]@Frame(middle) = undefined;
337337 var fn_ptr = middle;
338338 var result: @typeInfo(@typeInfo(@TypeOf(fn_ptr)).Fn.return_type.?).ErrorUnion.error_set!void = undefined;
339339 _ = @asyncCall(std.mem.sliceAsBytes(frame[0..]), &result, fn_ptr, .{});
340340 resume global_frame;
341 std.testing.expectError(error.Fail, result);
341 try std.testing.expectError(error.Fail, result);
342342 }
343343 fn middle() callconv(.Async) !void {
344344 var f = async middle2();
......@@ -355,7 +355,7 @@ test "async fn with inferred error set" {
355355 return error.Fail;
356356 }
357357 };
358 S.doTheTest();
358 try S.doTheTest();
359359}
360360
361361test "error return trace across suspend points - early return" {
......@@ -383,9 +383,9 @@ fn suspendThenFail() callconv(.Async) anyerror!void {
383383}
384384fn printTrace(p: anyframe->(anyerror!void)) callconv(.Async) void {
385385 (await p) catch |e| {
386 std.testing.expect(e == error.Fail);
386 std.testing.expect(e == error.Fail) catch @panic("test failure");
387387 if (@errorReturnTrace()) |trace| {
388 expect(trace.index == 1);
388 expect(trace.index == 1) catch @panic("test failure");
389389 } else switch (builtin.mode) {
390390 .Debug, .ReleaseSafe => @panic("expected return trace"),
391391 .ReleaseFast, .ReleaseSmall => {},
......@@ -396,7 +396,7 @@ fn printTrace(p: anyframe->(anyerror!void)) callconv(.Async) void {
396396test "break from suspend" {
397397 var my_result: i32 = 1;
398398 const p = async testBreakFromSuspend(&my_result);
399 std.testing.expect(my_result == 2);
399 try std.testing.expect(my_result == 2);
400400}
401401fn testBreakFromSuspend(my_result: *i32) callconv(.Async) void {
402402 suspend {
......@@ -415,11 +415,11 @@ test "heap allocated async function frame" {
415415 const frame = try std.testing.allocator.create(@Frame(someFunc));
416416 defer std.testing.allocator.destroy(frame);
417417
418 expect(x == 42);
418 try expect(x == 42);
419419 frame.* = async someFunc();
420 expect(x == 43);
420 try expect(x == 43);
421421 resume frame;
422 expect(x == 44);
422 try expect(x == 44);
423423 }
424424
425425 fn someFunc() void {
......@@ -436,15 +436,15 @@ test "async function call return value" {
436436 var frame: anyframe = undefined;
437437 var pt = Point{ .x = 10, .y = 11 };
438438
439 fn doTheTest() void {
440 expectEqual(pt.x, 10);
441 expectEqual(pt.y, 11);
439 fn doTheTest() !void {
440 try expectEqual(pt.x, 10);
441 try expectEqual(pt.y, 11);
442442 _ = async first();
443 expectEqual(pt.x, 10);
444 expectEqual(pt.y, 11);
443 try expectEqual(pt.x, 10);
444 try expectEqual(pt.y, 11);
445445 resume frame;
446 expectEqual(pt.x, 1);
447 expectEqual(pt.y, 2);
446 try expectEqual(pt.x, 1);
447 try expectEqual(pt.y, 2);
448448 }
449449
450450 fn first() void {
......@@ -469,23 +469,23 @@ test "async function call return value" {
469469 y: i32,
470470 };
471471 };
472 S.doTheTest();
472 try S.doTheTest();
473473}
474474
475475test "suspension points inside branching control flow" {
476476 const S = struct {
477477 var result: i32 = 10;
478478
479 fn doTheTest() void {
480 expect(10 == result);
479 fn doTheTest() !void {
480 try expect(10 == result);
481481 var frame = async func(true);
482 expect(10 == result);
482 try expect(10 == result);
483483 resume frame;
484 expect(11 == result);
484 try expect(11 == result);
485485 resume frame;
486 expect(12 == result);
486 try expect(12 == result);
487487 resume frame;
488 expect(13 == result);
488 try expect(13 == result);
489489 }
490490
491491 fn func(b: bool) void {
......@@ -495,7 +495,7 @@ test "suspension points inside branching control flow" {
495495 }
496496 }
497497 };
498 S.doTheTest();
498 try S.doTheTest();
499499}
500500
501501test "call async function which has struct return type" {
......@@ -509,8 +509,8 @@ test "call async function which has struct return type" {
509509
510510 fn atest() void {
511511 const result = func();
512 expect(result.x == 5);
513 expect(result.y == 6);
512 expect(result.x == 5) catch @panic("test failed");
513 expect(result.y == 6) catch @panic("test failed");
514514 }
515515
516516 const Point = struct {
......@@ -536,27 +536,27 @@ test "pass string literal to async function" {
536536 var frame: anyframe = undefined;
537537 var ok: bool = false;
538538
539 fn doTheTest() void {
539 fn doTheTest() !void {
540540 _ = async hello("hello");
541541 resume frame;
542 expect(ok);
542 try expect(ok);
543543 }
544544
545545 fn hello(msg: []const u8) void {
546546 frame = @frame();
547547 suspend {}
548 expectEqualStrings("hello", msg);
548 expectEqualStrings("hello", msg) catch @panic("test failed");
549549 ok = true;
550550 }
551551 };
552 S.doTheTest();
552 try S.doTheTest();
553553}
554554
555555test "await inside an errdefer" {
556556 const S = struct {
557557 var frame: anyframe = undefined;
558558
559 fn doTheTest() void {
559 fn doTheTest() !void {
560560 _ = async amainWrap();
561561 resume frame;
562562 }
......@@ -572,7 +572,7 @@ test "await inside an errdefer" {
572572 suspend {}
573573 }
574574 };
575 S.doTheTest();
575 try S.doTheTest();
576576}
577577
578578test "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" {
580580 var frame: anyframe = undefined;
581581 var ok = false;
582582
583 fn doTheTest() void {
583 fn doTheTest() !void {
584584 _ = async amain();
585585 resume frame;
586 expect(ok);
586 try expect(ok);
587587 }
588588
589589 fn amain() void {
590 std.testing.expectError(error.Bad, theProblem());
590 std.testing.expectError(error.Bad, theProblem()) catch @panic("test failed");
591591 ok = true;
592592 }
593593
......@@ -602,7 +602,7 @@ test "try in an async function with error union and non-zero-bit payload" {
602602 return error.Bad;
603603 }
604604 };
605 S.doTheTest();
605 try S.doTheTest();
606606}
607607
608608test "returning a const error from async function" {
......@@ -610,10 +610,10 @@ test "returning a const error from async function" {
610610 var frame: anyframe = undefined;
611611 var ok = false;
612612
613 fn doTheTest() void {
613 fn doTheTest() !void {
614614 _ = async amain();
615615 resume frame;
616 expect(ok);
616 try expect(ok);
617617 }
618618
619619 fn amain() !void {
......@@ -630,7 +630,7 @@ test "returning a const error from async function" {
630630 return error.OutOfMemory;
631631 }
632632 };
633 S.doTheTest();
633 try S.doTheTest();
634634}
635635
636636test "async/await typical usage" {
......@@ -663,11 +663,11 @@ fn testAsyncAwaitTypicalUsage(
663663 }
664664 fn amainWrap() void {
665665 if (amain()) |_| {
666 expect(!simulate_fail_download);
667 expect(!simulate_fail_file);
666 expect(!simulate_fail_download) catch @panic("test failure");
667 expect(!simulate_fail_file) catch @panic("test failure");
668668 } else |e| switch (e) {
669 error.NoResponse => expect(simulate_fail_download),
670 error.FileNotFound => expect(simulate_fail_file),
669 error.NoResponse => expect(simulate_fail_download) catch @panic("test failure"),
670 error.FileNotFound => expect(simulate_fail_file) catch @panic("test failure"),
671671 else => @panic("test failure"),
672672 }
673673 }
......@@ -694,8 +694,8 @@ fn testAsyncAwaitTypicalUsage(
694694 const file_text = try await file_frame;
695695 defer allocator.free(file_text);
696696
697 expect(std.mem.eql(u8, "expected download text", download_text));
698 expect(std.mem.eql(u8, "expected file text", file_text));
697 try expect(std.mem.eql(u8, "expected download text", download_text));
698 try expect(std.mem.eql(u8, "expected file text", file_text));
699699 }
700700
701701 var global_download_frame: anyframe = undefined;
......@@ -728,13 +728,13 @@ fn testAsyncAwaitTypicalUsage(
728728
729729test "alignment of local variables in async functions" {
730730 const S = struct {
731 fn doTheTest() void {
731 fn doTheTest() !void {
732732 var y: u8 = 123;
733733 var x: u8 align(128) = 1;
734 expect(@ptrToInt(&x) % 128 == 0);
734 try expect(@ptrToInt(&x) % 128 == 0);
735735 }
736736 };
737 S.doTheTest();
737 try S.doTheTest();
738738}
739739
740740test "no reason to resolve frame still works" {
......@@ -746,10 +746,10 @@ fn simpleNothing() void {
746746
747747test "async call a generic function" {
748748 const S = struct {
749 fn doTheTest() void {
749 fn doTheTest() !void {
750750 var f = async func(i32, 2);
751751 const result = await f;
752 expect(result == 3);
752 try expect(result == 3);
753753 }
754754
755755 fn func(comptime T: type, inc: T) T {
......@@ -766,8 +766,8 @@ test "async call a generic function" {
766766
767767test "return from suspend block" {
768768 const S = struct {
769 fn doTheTest() void {
770 expect(func() == 1234);
769 fn doTheTest() !void {
770 expect(func() == 1234) catch @panic("test failure");
771771 }
772772 fn func() i32 {
773773 suspend {
......@@ -808,7 +808,7 @@ test "struct parameter to async function is copied to the frame" {
808808 var pt = Point{ .x = 1, .y = 2 };
809809 f.* = async foo(pt);
810810 var result = await f;
811 expect(result == 1);
811 expect(result == 1) catch @panic("test failure");
812812 }
813813
814814 fn foo(point: Point) i32 {
......@@ -833,7 +833,7 @@ test "cast fn to async fn when it is inferred to be async" {
833833 var result: i32 = undefined;
834834 const f = @asyncCall(&buf, &result, ptr, .{});
835835 _ = await f;
836 expect(result == 1234);
836 expect(result == 1234) catch @panic("test failure");
837837 ok = true;
838838 }
839839
......@@ -846,7 +846,7 @@ test "cast fn to async fn when it is inferred to be async" {
846846 };
847847 _ = async S.doTheTest();
848848 resume S.frame;
849 expect(S.ok);
849 try expect(S.ok);
850850}
851851
852852test "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" {
860860 var buf: [100]u8 align(16) = undefined;
861861 var result: i32 = undefined;
862862 _ = await @asyncCall(&buf, &result, ptr, .{});
863 expect(result == 1234);
863 expect(result == 1234) catch @panic("test failure");
864864 ok = true;
865865 }
866866
......@@ -873,7 +873,7 @@ test "cast fn to async fn when it is inferred to be async, awaited directly" {
873873 };
874874 _ = async S.doTheTest();
875875 resume S.frame;
876 expect(S.ok);
876 try expect(S.ok);
877877}
878878
879879test "await does not force async if callee is blocking" {
......@@ -883,12 +883,12 @@ test "await does not force async if callee is blocking" {
883883 }
884884 };
885885 var x = async S.simple();
886 expect(await x == 1234);
886 try expect(await x == 1234);
887887}
888888
889889test "recursive async function" {
890 expect(recursiveAsyncFunctionTest(false).doTheTest() == 55);
891 expect(recursiveAsyncFunctionTest(true).doTheTest() == 55);
890 try expect(recursiveAsyncFunctionTest(false).doTheTest() == 55);
891 try expect(recursiveAsyncFunctionTest(true).doTheTest() == 55);
892892}
893893
894894fn recursiveAsyncFunctionTest(comptime suspending_implementation: bool) type {
......@@ -952,12 +952,12 @@ test "@asyncCall with comptime-known function, but not awaited directly" {
952952 const S = struct {
953953 var global_frame: anyframe = undefined;
954954
955 fn doTheTest() void {
955 fn doTheTest() !void {
956956 var frame: [1]@Frame(middle) = undefined;
957957 var result: @typeInfo(@typeInfo(@TypeOf(middle)).Fn.return_type.?).ErrorUnion.error_set!void = undefined;
958958 _ = @asyncCall(std.mem.sliceAsBytes(frame[0..]), &result, middle, .{});
959959 resume global_frame;
960 std.testing.expectError(error.Fail, result);
960 try std.testing.expectError(error.Fail, result);
961961 }
962962 fn middle() callconv(.Async) !void {
963963 var f = async middle2();
......@@ -974,7 +974,7 @@ test "@asyncCall with comptime-known function, but not awaited directly" {
974974 return error.Fail;
975975 }
976976 };
977 S.doTheTest();
977 try S.doTheTest();
978978}
979979
980980test "@asyncCall with actual frame instead of byte buffer" {
......@@ -988,7 +988,7 @@ test "@asyncCall with actual frame instead of byte buffer" {
988988 var result: i32 = undefined;
989989 const ptr = @asyncCall(&frame, &result, S.func, .{});
990990 resume ptr;
991 expect(result == 1234);
991 try expect(result == 1234);
992992}
993993
994994test "@asyncCall using the result location inside the frame" {
......@@ -1010,19 +1010,19 @@ test "@asyncCall using the result location inside the frame" {
10101010 var foo = Foo{ .bar = S.simple2 };
10111011 var bytes: [64]u8 align(16) = undefined;
10121012 const f = @asyncCall(&bytes, {}, foo.bar, .{&data});
1013 comptime expect(@TypeOf(f) == anyframe->i32);
1014 expect(data == 2);
1013 comptime try expect(@TypeOf(f) == anyframe->i32);
1014 try expect(data == 2);
10151015 resume f;
1016 expect(data == 4);
1016 try expect(data == 4);
10171017 _ = async S.getAnswer(f, &data);
1018 expect(data == 1234);
1018 try expect(data == 1234);
10191019}
10201020
10211021test "@TypeOf an async function call of generic fn with error union type" {
10221022 const S = struct {
10231023 fn func(comptime x: anytype) anyerror!i32 {
10241024 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);
10261026 return undefined;
10271027 }
10281028 };
......@@ -1051,7 +1051,7 @@ test "using @TypeOf on a generic function call" {
10511051 };
10521052 _ = async S.amain(@as(u32, 1));
10531053 resume S.global_frame;
1054 expect(S.global_ok);
1054 try expect(S.global_ok);
10551055}
10561056
10571057test "recursive call of await @asyncCall with struct return type" {
......@@ -1084,17 +1084,17 @@ test "recursive call of await @asyncCall with struct return type" {
10841084 var frame: @TypeOf(async S.amain(@as(u32, 1))) = undefined;
10851085 _ = @asyncCall(&frame, &res, S.amain, .{@as(u32, 1)});
10861086 resume S.global_frame;
1087 expect(S.global_ok);
1088 expect(res.x == 1);
1089 expect(res.y == 2);
1090 expect(res.z == 3);
1087 try expect(S.global_ok);
1088 try expect(res.x == 1);
1089 try expect(res.y == 2);
1090 try expect(res.z == 3);
10911091}
10921092
10931093test "nosuspend function call" {
10941094 const S = struct {
1095 fn doTheTest() void {
1095 fn doTheTest() !void {
10961096 const result = nosuspend add(50, 100);
1097 expect(result == 150);
1097 try expect(result == 150);
10981098 }
10991099 fn add(a: i32, b: i32) i32 {
11001100 if (a > 100) {
......@@ -1103,7 +1103,7 @@ test "nosuspend function call" {
11031103 return a + b;
11041104 }
11051105 };
1106 S.doTheTest();
1106 try S.doTheTest();
11071107}
11081108
11091109test "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
11131113 var f2 = async add(3, 4);
11141114
11151115 const sum = (await f1) + (await f2);
1116 expect(sum == 10);
1116 expect(sum == 10) catch @panic("test failure");
11171117 }
11181118 fn add(a: i32, b: i32) callconv(.Async) i32 {
11191119 return a + b;
......@@ -1128,7 +1128,7 @@ test "await used in expression after a fn call" {
11281128 var f1 = async add(3, 4);
11291129 var sum: i32 = 0;
11301130 sum = foo() + await f1;
1131 expect(sum == 8);
1131 expect(sum == 8) catch @panic("test failure");
11321132 }
11331133 fn add(a: i32, b: i32) callconv(.Async) i32 {
11341134 return a + b;
......@@ -1145,7 +1145,7 @@ test "async fn call used in expression after a fn call" {
11451145 fn atest() void {
11461146 var sum: i32 = 0;
11471147 sum = foo() + add(3, 4);
1148 expect(sum == 8);
1148 expect(sum == 8) catch @panic("test failure");
11491149 }
11501150 fn add(a: i32, b: i32) callconv(.Async) i32 {
11511151 return a + b;
......@@ -1167,7 +1167,7 @@ test "suspend in for loop" {
11671167 }
11681168
11691169 fn atest() void {
1170 expect(func(&[_]u8{ 1, 2, 3 }) == 6);
1170 expect(func(&[_]u8{ 1, 2, 3 }) == 6) catch @panic("test failure");
11711171 }
11721172 fn func(stuff: []const u8) u32 {
11731173 global_frame = @frame();
......@@ -1193,8 +1193,8 @@ test "suspend in while loop" {
11931193 }
11941194
11951195 fn atest() void {
1196 expect(optional(6) == 6);
1197 expect(errunion(6) == 6);
1196 expect(optional(6) == 6) catch @panic("test failure");
1197 expect(errunion(6) == 6) catch @panic("test failure");
11981198 }
11991199 fn optional(stuff: ?u32) u32 {
12001200 global_frame = @frame();
......@@ -1223,8 +1223,8 @@ test "correctly spill when returning the error union result of another async fn"
12231223 const S = struct {
12241224 var global_frame: anyframe = undefined;
12251225
1226 fn doTheTest() void {
1227 expect((atest() catch unreachable) == 1234);
1226 fn doTheTest() !void {
1227 expect((atest() catch unreachable) == 1234) catch @panic("test failure");
12281228 }
12291229
12301230 fn atest() !i32 {
......@@ -1246,11 +1246,11 @@ test "spill target expr in a for loop" {
12461246 const S = struct {
12471247 var global_frame: anyframe = undefined;
12481248
1249 fn doTheTest() void {
1249 fn doTheTest() !void {
12501250 var foo = Foo{
12511251 .slice = &[_]i32{ 1, 2 },
12521252 };
1253 expect(atest(&foo) == 3);
1253 expect(atest(&foo) == 3) catch @panic("test failure");
12541254 }
12551255
12561256 const Foo = struct {
......@@ -1277,11 +1277,11 @@ test "spill target expr in a for loop, with a var decl in the loop body" {
12771277 const S = struct {
12781278 var global_frame: anyframe = undefined;
12791279
1280 fn doTheTest() void {
1280 fn doTheTest() !void {
12811281 var foo = Foo{
12821282 .slice = &[_]i32{ 1, 2 },
12831283 };
1284 expect(atest(&foo) == 3);
1284 expect(atest(&foo) == 3) catch @panic("test failure");
12851285 }
12861286
12871287 const Foo = struct {
......@@ -1319,7 +1319,7 @@ test "async call with @call" {
13191319 fn atest() void {
13201320 var frame = @call(.{ .modifier = .async_kw }, afoo, .{});
13211321 const res = await frame;
1322 expect(res == 42);
1322 expect(res == 42) catch @panic("test failure");
13231323 }
13241324 fn afoo() i32 {
13251325 suspend {
......@@ -1348,7 +1348,7 @@ test "async function passed 0-bit arg after non-0-bit arg" {
13481348 };
13491349 _ = async S.foo();
13501350 resume S.global_frame;
1351 expect(S.global_int == 1);
1351 try expect(S.global_int == 1);
13521352}
13531353
13541354test "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" {
13621362 }
13631363
13641364 fn bar(x: u64, args: anytype) anyerror!void {
1365 expect(x == 10);
1365 try expect(x == 10);
13661366 global_frame = @frame();
13671367 suspend {}
13681368 global_int = args[0];
......@@ -1370,7 +1370,7 @@ test "async function passed align(16) arg after align(8) arg" {
13701370 };
13711371 _ = async S.foo();
13721372 resume S.global_frame;
1373 expect(S.global_int == 99);
1373 try expect(S.global_int == 99);
13741374}
13751375
13761376test "async function call resolves target fn frame, comptime func" {
......@@ -1392,7 +1392,7 @@ test "async function call resolves target fn frame, comptime func" {
13921392 };
13931393 _ = async S.foo();
13941394 resume S.global_frame;
1395 expect(S.global_int == 10);
1395 try expect(S.global_int == 10);
13961396}
13971397
13981398test "async function call resolves target fn frame, runtime func" {
......@@ -1415,7 +1415,7 @@ test "async function call resolves target fn frame, runtime func" {
14151415 };
14161416 _ = async S.foo();
14171417 resume S.global_frame;
1418 expect(S.global_int == 10);
1418 try expect(S.global_int == 10);
14191419}
14201420
14211421test "properly spill optional payload capture value" {
......@@ -1439,7 +1439,7 @@ test "properly spill optional payload capture value" {
14391439 };
14401440 _ = async S.foo();
14411441 resume S.global_frame;
1442 expect(S.global_int == 1237);
1442 try expect(S.global_int == 1237);
14431443}
14441444
14451445test "handle defer interfering with return value spill" {
......@@ -1449,16 +1449,16 @@ test "handle defer interfering with return value spill" {
14491449 var finished = false;
14501450 var baz_happened = false;
14511451
1452 fn doTheTest() void {
1452 fn doTheTest() !void {
14531453 _ = async testFoo();
14541454 resume global_frame1;
14551455 resume global_frame2;
1456 expect(baz_happened);
1457 expect(finished);
1456 try expect(baz_happened);
1457 try expect(finished);
14581458 }
14591459
14601460 fn testFoo() void {
1461 expectError(error.Bad, foo());
1461 expectError(error.Bad, foo()) catch @panic("test failure");
14621462 finished = true;
14631463 }
14641464
......@@ -1479,7 +1479,7 @@ test "handle defer interfering with return value spill" {
14791479 baz_happened = true;
14801480 }
14811481 };
1482 S.doTheTest();
1482 try S.doTheTest();
14831483}
14841484
14851485test "take address of temporary async frame" {
......@@ -1487,14 +1487,14 @@ test "take address of temporary async frame" {
14871487 var global_frame: anyframe = undefined;
14881488 var finished = false;
14891489
1490 fn doTheTest() void {
1490 fn doTheTest() !void {
14911491 _ = async asyncDoTheTest();
14921492 resume global_frame;
1493 expect(finished);
1493 try expect(finished);
14941494 }
14951495
14961496 fn asyncDoTheTest() void {
1497 expect(finishIt(&async foo(10)) == 1245);
1497 expect(finishIt(&async foo(10)) == 1245) catch @panic("test failure");
14981498 finished = true;
14991499 }
15001500
......@@ -1508,16 +1508,16 @@ test "take address of temporary async frame" {
15081508 return (await frame) + 1;
15091509 }
15101510 };
1511 S.doTheTest();
1511 try S.doTheTest();
15121512}
15131513
15141514test "nosuspend await" {
15151515 const S = struct {
15161516 var finished = false;
15171517
1518 fn doTheTest() void {
1518 fn doTheTest() !void {
15191519 var frame = async foo(false);
1520 expect(nosuspend await frame == 42);
1520 try expect(nosuspend await frame == 42);
15211521 finished = true;
15221522 }
15231523
......@@ -1528,8 +1528,8 @@ test "nosuspend await" {
15281528 return 42;
15291529 }
15301530 };
1531 S.doTheTest();
1532 expect(S.finished);
1531 try S.doTheTest();
1532 try expect(S.finished);
15331533}
15341534
15351535test "nosuspend on function calls" {
......@@ -1544,8 +1544,8 @@ test "nosuspend on function calls" {
15441544 return S0{};
15451545 }
15461546 };
1547 expectEqual(@as(i32, 42), nosuspend S1.c().b);
1548 expectEqual(@as(i32, 42), (try nosuspend S1.d()).b);
1547 try expectEqual(@as(i32, 42), nosuspend S1.c().b);
1548 try expectEqual(@as(i32, 42), (try nosuspend S1.d()).b);
15491549}
15501550
15511551test "nosuspend on async function calls" {
......@@ -1561,9 +1561,9 @@ test "nosuspend on async function calls" {
15611561 }
15621562 };
15631563 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);
15651565 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);
15671567}
15681568
15691569// test "resume nosuspend async function calls" {
......@@ -1582,10 +1582,10 @@ test "nosuspend on async function calls" {
15821582// };
15831583// var frame_c = nosuspend async S1.c();
15841584// resume frame_c;
1585// expectEqual(@as(i32, 42), (await frame_c).b);
1585// try expectEqual(@as(i32, 42), (await frame_c).b);
15861586// var frame_d = nosuspend async S1.d();
15871587// resume frame_d;
1588// expectEqual(@as(i32, 42), (try await frame_d).b);
1588// try expectEqual(@as(i32, 42), (try await frame_d).b);
15891589// }
15901590
15911591test "nosuspend resume async function calls" {
......@@ -1604,10 +1604,10 @@ test "nosuspend resume async function calls" {
16041604 };
16051605 var frame_c = async S1.c();
16061606 nosuspend resume frame_c;
1607 expectEqual(@as(i32, 42), (await frame_c).b);
1607 try expectEqual(@as(i32, 42), (await frame_c).b);
16081608 var frame_d = async S1.d();
16091609 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);
16111611}
16121612
16131613test "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" {
16231623 };
16241624 var frame = async S.foo();
16251625 resume @ptrCast(anyframe->bool, @alignCast(@alignOf(@Frame(S.foo)), S.x));
1626 expect(nosuspend await frame);
1626 try expect(nosuspend await frame);
16271627}
16281628
16291629test "@asyncCall with pass-by-value arguments" {
......@@ -1638,9 +1638,9 @@ test "@asyncCall with pass-by-value arguments" {
16381638 pub fn f(_fill0: u64, s: ST, _fill1: u64, a: AT, _fill2: u64) callconv(.Async) void {
16391639 // Check that the array and struct arguments passed by value don't
16401640 // end up overflowing the adjacent fields in the frame structure.
1641 expectEqual(F0, _fill0);
1642 expectEqual(F1, _fill1);
1643 expectEqual(F2, _fill2);
1641 expectEqual(F0, _fill0) catch @panic("test failure");
1642 expectEqual(F1, _fill1) catch @panic("test failure");
1643 expectEqual(F2, _fill2) catch @panic("test failure");
16441644 }
16451645 };
16461646
......@@ -1664,8 +1664,8 @@ test "@asyncCall with arguments having non-standard alignment" {
16641664 pub fn f(_fill0: u32, s: struct { x: u64 align(16) }, _fill1: u64) callconv(.Async) void {
16651665 // The compiler inserts extra alignment for s, check that the
16661666 // generated code picks the right slot for fill1.
1667 expectEqual(F0, _fill0);
1668 expectEqual(F1, _fill1);
1667 expectEqual(F0, _fill0) catch @panic("test failure");
1668 expectEqual(F1, _fill1) catch @panic("test failure");
16691669 }
16701670 };
16711671
test/stage1/behavior/atomics.zig+69-69
......@@ -4,25 +4,25 @@ const expectEqual = std.testing.expectEqual;
44const builtin = @import("builtin");
55
66test "cmpxchg" {
7 testCmpxchg();
8 comptime testCmpxchg();
7 try testCmpxchg();
8 comptime try testCmpxchg();
99}
1010
11fn testCmpxchg() void {
11fn testCmpxchg() !void {
1212 var x: i32 = 1234;
1313 if (@cmpxchgWeak(i32, &x, 99, 5678, .SeqCst, .SeqCst)) |x1| {
14 expect(x1 == 1234);
14 try expect(x1 == 1234);
1515 } else {
1616 @panic("cmpxchg should have failed");
1717 }
1818
1919 while (@cmpxchgWeak(i32, &x, 1234, 5678, .SeqCst, .SeqCst)) |x1| {
20 expect(x1 == 1234);
20 try expect(x1 == 1234);
2121 }
22 expect(x == 5678);
22 try expect(x == 5678);
2323
24 expect(@cmpxchgStrong(i32, &x, 5678, 42, .SeqCst, .SeqCst) == null);
25 expect(x == 42);
24 try expect(@cmpxchgStrong(i32, &x, 5678, 42, .SeqCst, .SeqCst) == null);
25 try expect(x == 42);
2626}
2727
2828test "fence" {
......@@ -33,25 +33,25 @@ test "fence" {
3333
3434test "atomicrmw and atomicload" {
3535 var data: u8 = 200;
36 testAtomicRmw(&data);
37 expect(data == 42);
38 testAtomicLoad(&data);
36 try testAtomicRmw(&data);
37 try expect(data == 42);
38 try testAtomicLoad(&data);
3939}
4040
41fn testAtomicRmw(ptr: *u8) void {
41fn testAtomicRmw(ptr: *u8) !void {
4242 const prev_value = @atomicRmw(u8, ptr, .Xchg, 42, .SeqCst);
43 expect(prev_value == 200);
43 try expect(prev_value == 200);
4444 comptime {
4545 var x: i32 = 1234;
4646 const y: i32 = 12345;
47 expect(@atomicLoad(i32, &x, .SeqCst) == 1234);
48 expect(@atomicLoad(i32, &y, .SeqCst) == 12345);
47 try expect(@atomicLoad(i32, &x, .SeqCst) == 1234);
48 try expect(@atomicLoad(i32, &y, .SeqCst) == 12345);
4949 }
5050}
5151
52fn testAtomicLoad(ptr: *u8) void {
52fn testAtomicLoad(ptr: *u8) !void {
5353 const x = @atomicLoad(u8, ptr, .SeqCst);
54 expect(x == 42);
54 try expect(x == 42);
5555}
5656
5757test "cmpxchg with ptr" {
......@@ -60,18 +60,18 @@ test "cmpxchg with ptr" {
6060 var data3: i32 = 9101;
6161 var x: *i32 = &data1;
6262 if (@cmpxchgWeak(*i32, &x, &data2, &data3, .SeqCst, .SeqCst)) |x1| {
63 expect(x1 == &data1);
63 try expect(x1 == &data1);
6464 } else {
6565 @panic("cmpxchg should have failed");
6666 }
6767
6868 while (@cmpxchgWeak(*i32, &x, &data1, &data3, .SeqCst, .SeqCst)) |x1| {
69 expect(x1 == &data1);
69 try expect(x1 == &data1);
7070 }
71 expect(x == &data3);
71 try expect(x == &data3);
7272
73 expect(@cmpxchgStrong(*i32, &x, &data3, &data2, .SeqCst, .SeqCst) == null);
74 expect(x == &data2);
73 try expect(@cmpxchgStrong(*i32, &x, &data3, &data2, .SeqCst, .SeqCst) == null);
74 try expect(x == &data2);
7575}
7676
7777// TODO this test is disabled until this issue is resolved:
......@@ -81,18 +81,18 @@ test "cmpxchg with ptr" {
8181//test "128-bit cmpxchg" {
8282// var x: u128 align(16) = 1234; // TODO: https://github.com/ziglang/zig/issues/2987
8383// if (@cmpxchgWeak(u128, &x, 99, 5678, .SeqCst, .SeqCst)) |x1| {
84// expect(x1 == 1234);
84// try expect(x1 == 1234);
8585// } else {
8686// @panic("cmpxchg should have failed");
8787// }
8888//
8989// while (@cmpxchgWeak(u128, &x, 1234, 5678, .SeqCst, .SeqCst)) |x1| {
90// expect(x1 == 1234);
90// try expect(x1 == 1234);
9191// }
92// expect(x == 5678);
92// try expect(x == 5678);
9393//
94// expect(@cmpxchgStrong(u128, &x, 5678, 42, .SeqCst, .SeqCst) == null);
95// expect(x == 42);
94// try expect(@cmpxchgStrong(u128, &x, 5678, 42, .SeqCst, .SeqCst) == null);
95// try expect(x == 42);
9696//}
9797
9898test "cmpxchg with ignored result" {
......@@ -101,14 +101,14 @@ test "cmpxchg with ignored result" {
101101
102102 _ = @cmpxchgStrong(i32, &x, 1234, 5678, .Monotonic, .Monotonic);
103103
104 expectEqual(@as(i32, 5678), x);
104 try expectEqual(@as(i32, 5678), x);
105105}
106106
107107var a_global_variable = @as(u32, 1234);
108108
109109test "cmpxchg on a global variable" {
110110 _ = @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);
112112}
113113
114114test "atomic load and rmw with enum" {
......@@ -119,33 +119,33 @@ test "atomic load and rmw with enum" {
119119 };
120120 var x = Value.a;
121121
122 expect(@atomicLoad(Value, &x, .SeqCst) != .b);
122 try expect(@atomicLoad(Value, &x, .SeqCst) != .b);
123123
124124 _ = @atomicRmw(Value, &x, .Xchg, .c, .SeqCst);
125 expect(@atomicLoad(Value, &x, .SeqCst) == .c);
126 expect(@atomicLoad(Value, &x, .SeqCst) != .a);
127 expect(@atomicLoad(Value, &x, .SeqCst) != .b);
125 try expect(@atomicLoad(Value, &x, .SeqCst) == .c);
126 try expect(@atomicLoad(Value, &x, .SeqCst) != .a);
127 try expect(@atomicLoad(Value, &x, .SeqCst) != .b);
128128}
129129
130130test "atomic store" {
131131 var x: u32 = 0;
132132 @atomicStore(u32, &x, 1, .SeqCst);
133 expect(@atomicLoad(u32, &x, .SeqCst) == 1);
133 try expect(@atomicLoad(u32, &x, .SeqCst) == 1);
134134 @atomicStore(u32, &x, 12345678, .SeqCst);
135 expect(@atomicLoad(u32, &x, .SeqCst) == 12345678);
135 try expect(@atomicLoad(u32, &x, .SeqCst) == 12345678);
136136}
137137
138138test "atomic store comptime" {
139 comptime testAtomicStore();
140 testAtomicStore();
139 comptime try testAtomicStore();
140 try testAtomicStore();
141141}
142142
143fn testAtomicStore() void {
143fn testAtomicStore() !void {
144144 var x: u32 = 0;
145145 @atomicStore(u32, &x, 1, .SeqCst);
146 expect(@atomicLoad(u32, &x, .SeqCst) == 1);
146 try expect(@atomicLoad(u32, &x, .SeqCst) == 1);
147147 @atomicStore(u32, &x, 12345678, .SeqCst);
148 expect(@atomicLoad(u32, &x, .SeqCst) == 12345678);
148 try expect(@atomicLoad(u32, &x, .SeqCst) == 12345678);
149149}
150150
151151test "atomicrmw with floats" {
......@@ -154,66 +154,66 @@ test "atomicrmw with floats" {
154154 .aarch64, .arm, .thumb, .riscv64 => return error.SkipZigTest,
155155 else => {},
156156 }
157 testAtomicRmwFloat();
158 comptime testAtomicRmwFloat();
157 try testAtomicRmwFloat();
158 comptime try testAtomicRmwFloat();
159159}
160160
161fn testAtomicRmwFloat() void {
161fn testAtomicRmwFloat() !void {
162162 var x: f32 = 0;
163 expect(x == 0);
163 try expect(x == 0);
164164 _ = @atomicRmw(f32, &x, .Xchg, 1, .SeqCst);
165 expect(x == 1);
165 try expect(x == 1);
166166 _ = @atomicRmw(f32, &x, .Add, 5, .SeqCst);
167 expect(x == 6);
167 try expect(x == 6);
168168 _ = @atomicRmw(f32, &x, .Sub, 2, .SeqCst);
169 expect(x == 4);
169 try expect(x == 4);
170170}
171171
172172test "atomicrmw with ints" {
173 testAtomicRmwInt();
174 comptime testAtomicRmwInt();
173 try testAtomicRmwInt();
174 comptime try testAtomicRmwInt();
175175}
176176
177fn testAtomicRmwInt() void {
177fn testAtomicRmwInt() !void {
178178 var x: u8 = 1;
179179 var res = @atomicRmw(u8, &x, .Xchg, 3, .SeqCst);
180 expect(x == 3 and res == 1);
180 try expect(x == 3 and res == 1);
181181 _ = @atomicRmw(u8, &x, .Add, 3, .SeqCst);
182 expect(x == 6);
182 try expect(x == 6);
183183 _ = @atomicRmw(u8, &x, .Sub, 1, .SeqCst);
184 expect(x == 5);
184 try expect(x == 5);
185185 _ = @atomicRmw(u8, &x, .And, 4, .SeqCst);
186 expect(x == 4);
186 try expect(x == 4);
187187 _ = @atomicRmw(u8, &x, .Nand, 4, .SeqCst);
188 expect(x == 0xfb);
188 try expect(x == 0xfb);
189189 _ = @atomicRmw(u8, &x, .Or, 6, .SeqCst);
190 expect(x == 0xff);
190 try expect(x == 0xff);
191191 _ = @atomicRmw(u8, &x, .Xor, 2, .SeqCst);
192 expect(x == 0xfd);
192 try expect(x == 0xfd);
193193
194194 _ = @atomicRmw(u8, &x, .Max, 1, .SeqCst);
195 expect(x == 0xfd);
195 try expect(x == 0xfd);
196196 _ = @atomicRmw(u8, &x, .Min, 1, .SeqCst);
197 expect(x == 1);
197 try expect(x == 1);
198198}
199199
200200test "atomics with different types" {
201 testAtomicsWithType(bool, true, false);
201 try testAtomicsWithType(bool, true, false);
202202 inline for (.{ u1, i5, u15 }) |T| {
203203 var x: T = 0;
204 testAtomicsWithType(T, 0, 1);
204 try testAtomicsWithType(T, 0, 1);
205205 }
206 testAtomicsWithType(u0, 0, 0);
207 testAtomicsWithType(i0, 0, 0);
206 try testAtomicsWithType(u0, 0, 0);
207 try testAtomicsWithType(i0, 0, 0);
208208}
209209
210fn testAtomicsWithType(comptime T: type, a: T, b: T) void {
210fn testAtomicsWithType(comptime T: type, a: T, b: T) !void {
211211 var x: T = b;
212212 @atomicStore(T, &x, a, .SeqCst);
213 expect(x == a);
214 expect(@atomicLoad(T, &x, .SeqCst) == a);
215 expect(@atomicRmw(T, &x, .Xchg, b, .SeqCst) == a);
216 expect(@cmpxchgStrong(T, &x, b, a, .SeqCst, .SeqCst) == null);
213 try expect(x == a);
214 try expect(@atomicLoad(T, &x, .SeqCst) == a);
215 try expect(@atomicRmw(T, &x, .Xchg, b, .SeqCst) == a);
216 try expect(@cmpxchgStrong(T, &x, b, a, .SeqCst, .SeqCst) == null);
217217 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);
219219}
test/stage1/behavior/await_struct.zig+2-2
......@@ -15,8 +15,8 @@ test "coroutine await struct" {
1515 await_seq('f');
1616 resume await_a_promise;
1717 await_seq('i');
18 expect(await_final_result.x == 1234);
19 expect(std.mem.eql(u8, &await_points, "abcdefghi"));
18 try expect(await_final_result.x == 1234);
19 try expect(std.mem.eql(u8, &await_points, "abcdefghi"));
2020}
2121fn await_amain() callconv(.Async) void {
2222 await_seq('b');
test/stage1/behavior/bit_shifting.zig+14-14
......@@ -3,8 +3,8 @@ const expect = std.testing.expect;
33
44fn ShardedTable(comptime Key: type, comptime mask_bit_count: comptime_int, comptime V: type) type {
55 const key_bits = @typeInfo(Key).Int.bits;
6 expect(Key == std.meta.Int(.unsigned, key_bits));
7 expect(key_bits >= mask_bit_count);
6 std.debug.assert(Key == std.meta.Int(.unsigned, key_bits));
7 std.debug.assert(key_bits >= mask_bit_count);
88 const shard_key_bits = mask_bit_count;
99 const ShardKey = std.meta.Int(.unsigned, mask_bit_count);
1010 const shift_amount = key_bits - shard_key_bits;
......@@ -61,31 +61,31 @@ fn ShardedTable(comptime Key: type, comptime mask_bit_count: comptime_int, compt
6161
6262test "sharded table" {
6363 // realistic 16-way sharding
64 testShardedTable(u32, 4, 8);
64 try testShardedTable(u32, 4, 8);
6565
66 testShardedTable(u5, 0, 32); // ShardKey == u0
67 testShardedTable(u5, 2, 32);
68 testShardedTable(u5, 5, 32);
66 try testShardedTable(u5, 0, 32); // ShardKey == u0
67 try testShardedTable(u5, 2, 32);
68 try testShardedTable(u5, 5, 32);
6969
70 testShardedTable(u1, 0, 2);
71 testShardedTable(u1, 1, 2); // this does u1 >> u0
70 try testShardedTable(u1, 0, 2);
71 try testShardedTable(u1, 1, 2); // this does u1 >> u0
7272
73 testShardedTable(u0, 0, 1);
73 try testShardedTable(u0, 0, 1);
7474}
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 {
7676 const Table = ShardedTable(Key, mask_bit_count, void);
7777
7878 var table = Table.create();
7979 var node_buffer: [node_count]Table.Node = undefined;
8080 for (node_buffer) |*node, i| {
8181 const key = @intCast(Key, i);
82 expect(table.get(key) == null);
82 try expect(table.get(key) == null);
8383 node.init(key, {});
8484 table.put(node);
8585 }
8686
8787 for (node_buffer) |*node, i| {
88 expect(table.get(@intCast(Key, i)) == node);
88 try expect(table.get(@intCast(Key, i)) == node);
8989 }
9090}
9191
......@@ -93,9 +93,9 @@ fn testShardedTable(comptime Key: type, comptime mask_bit_count: comptime_int, c
9393test "comptime shr of BigInt" {
9494 comptime {
9595 var n0 = 0xdeadbeef0000000000000000;
96 std.debug.assert(n0 >> 64 == 0xdeadbeef);
96 try expect(n0 >> 64 == 0xdeadbeef);
9797 var n1 = 17908056155735594659;
98 std.debug.assert(n1 >> 64 == 0);
98 try expect(n1 >> 64 == 0);
9999 }
100100}
101101
test/stage1/behavior/bitcast.zig+52-52
......@@ -5,13 +5,13 @@ const expectEqual = std.testing.expectEqual;
55const maxInt = std.math.maxInt;
66
77test "@bitCast i32 -> u32" {
8 testBitCast_i32_u32();
9 comptime testBitCast_i32_u32();
8 try testBitCast_i32_u32();
9 comptime try testBitCast_i32_u32();
1010}
1111
12fn testBitCast_i32_u32() void {
13 expect(conv(-1) == maxInt(u32));
14 expect(conv2(maxInt(u32)) == -1);
12fn testBitCast_i32_u32() !void {
13 try expect(conv(-1) == maxInt(u32));
14 try expect(conv2(maxInt(u32)) == -1);
1515}
1616
1717fn conv(x: i32) u32 {
......@@ -26,15 +26,15 @@ test "@bitCast extern enum to its integer type" {
2626 A,
2727 B,
2828
29 fn testBitCastExternEnum() void {
29 fn testBitCastExternEnum() !void {
3030 var SOCK_DGRAM = @This().B;
3131 var sock_dgram = @bitCast(c_int, SOCK_DGRAM);
32 expect(sock_dgram == 1);
32 try expect(sock_dgram == 1);
3333 }
3434 };
3535
36 SOCK.testBitCastExternEnum();
37 comptime SOCK.testBitCastExternEnum();
36 try SOCK.testBitCastExternEnum();
37 comptime try SOCK.testBitCastExternEnum();
3838}
3939
4040test "@bitCast packed structs at runtime and comptime" {
......@@ -47,25 +47,25 @@ test "@bitCast packed structs at runtime and comptime" {
4747 quarter4: u4,
4848 };
4949 const S = struct {
50 fn doTheTest() void {
50 fn doTheTest() !void {
5151 var full = Full{ .number = 0x1234 };
5252 var two_halves = @bitCast(Divided, full);
5353 switch (builtin.endian) {
5454 builtin.Endian.Big => {
55 expect(two_halves.half1 == 0x12);
56 expect(two_halves.quarter3 == 0x3);
57 expect(two_halves.quarter4 == 0x4);
55 try expect(two_halves.half1 == 0x12);
56 try expect(two_halves.quarter3 == 0x3);
57 try expect(two_halves.quarter4 == 0x4);
5858 },
5959 builtin.Endian.Little => {
60 expect(two_halves.half1 == 0x34);
61 expect(two_halves.quarter3 == 0x2);
62 expect(two_halves.quarter4 == 0x1);
60 try expect(two_halves.half1 == 0x34);
61 try expect(two_halves.quarter3 == 0x2);
62 try expect(two_halves.quarter4 == 0x1);
6363 },
6464 }
6565 }
6666 };
67 S.doTheTest();
68 comptime S.doTheTest();
67 try S.doTheTest();
68 comptime try S.doTheTest();
6969}
7070
7171test "@bitCast extern structs at runtime and comptime" {
......@@ -77,23 +77,23 @@ test "@bitCast extern structs at runtime and comptime" {
7777 half2: u8,
7878 };
7979 const S = struct {
80 fn doTheTest() void {
80 fn doTheTest() !void {
8181 var full = Full{ .number = 0x1234 };
8282 var two_halves = @bitCast(TwoHalves, full);
8383 switch (builtin.endian) {
8484 builtin.Endian.Big => {
85 expect(two_halves.half1 == 0x12);
86 expect(two_halves.half2 == 0x34);
85 try expect(two_halves.half1 == 0x12);
86 try expect(two_halves.half2 == 0x34);
8787 },
8888 builtin.Endian.Little => {
89 expect(two_halves.half1 == 0x34);
90 expect(two_halves.half2 == 0x12);
89 try expect(two_halves.half1 == 0x34);
90 try expect(two_halves.half2 == 0x12);
9191 },
9292 }
9393 }
9494 };
95 S.doTheTest();
96 comptime S.doTheTest();
95 try S.doTheTest();
96 comptime try S.doTheTest();
9797}
9898
9999test "bitcast packed struct to integer and back" {
......@@ -102,35 +102,35 @@ test "bitcast packed struct to integer and back" {
102102 level: u7,
103103 };
104104 const S = struct {
105 fn doTheTest() void {
105 fn doTheTest() !void {
106106 var move = LevelUpMove{ .move_id = 1, .level = 2 };
107107 var v = @bitCast(u16, move);
108108 var back_to_a_move = @bitCast(LevelUpMove, v);
109 expect(back_to_a_move.move_id == 1);
110 expect(back_to_a_move.level == 2);
109 try expect(back_to_a_move.move_id == 1);
110 try expect(back_to_a_move.level == 2);
111111 }
112112 };
113 S.doTheTest();
114 comptime S.doTheTest();
113 try S.doTheTest();
114 comptime try S.doTheTest();
115115}
116116
117117test "implicit cast to error union by returning" {
118118 const S = struct {
119 fn entry() void {
120 expect((func(-1) catch unreachable) == maxInt(u64));
119 fn entry() !void {
120 try expect((func(-1) catch unreachable) == maxInt(u64));
121121 }
122122 pub fn func(sz: i64) anyerror!u64 {
123123 return @bitCast(u64, sz);
124124 }
125125 };
126 S.entry();
127 comptime S.entry();
126 try S.entry();
127 comptime try S.entry();
128128}
129129
130130// issue #3010: compiler segfault
131131test "bitcast literal [4]u8 param to u32" {
132132 const ip = @bitCast(u32, [_]u8{ 255, 255, 255, 255 });
133 expect(ip == maxInt(u32));
133 try expect(ip == maxInt(u32));
134134}
135135
136136test "bitcast packed struct literal to byte" {
......@@ -138,14 +138,14 @@ test "bitcast packed struct literal to byte" {
138138 value: u8,
139139 };
140140 const casted = @bitCast(u8, Foo{ .value = 0xF });
141 expect(casted == 0xf);
141 try expect(casted == 0xf);
142142}
143143
144144test "comptime bitcast used in expression has the correct type" {
145145 const Foo = packed struct {
146146 value: u8,
147147 };
148 expect(@bitCast(u8, Foo{ .value = 0xF }) == 0xf);
148 try expect(@bitCast(u8, Foo{ .value = 0xF }) == 0xf);
149149}
150150
151151test "bitcast result to _" {
......@@ -154,43 +154,43 @@ test "bitcast result to _" {
154154
155155test "nested bitcast" {
156156 const S = struct {
157 fn moo(x: isize) void {
158 @import("std").testing.expectEqual(@intCast(isize, 42), x);
157 fn moo(x: isize) !void {
158 try @import("std").testing.expectEqual(@intCast(isize, 42), x);
159159 }
160160
161 fn foo(x: isize) void {
162 @This().moo(
161 fn foo(x: isize) !void {
162 try @This().moo(
163163 @bitCast(isize, if (x != 0) @bitCast(usize, x) else @bitCast(usize, x)),
164164 );
165165 }
166166 };
167167
168 S.foo(42);
169 comptime S.foo(42);
168 try S.foo(42);
169 comptime try S.foo(42);
170170}
171171
172172test "bitcast passed as tuple element" {
173173 const S = struct {
174 fn foo(args: anytype) void {
175 comptime expect(@TypeOf(args[0]) == f32);
176 expect(args[0] == 12.34);
174 fn foo(args: anytype) !void {
175 comptime try expect(@TypeOf(args[0]) == f32);
176 try expect(args[0] == 12.34);
177177 }
178178 };
179 S.foo(.{@bitCast(f32, @as(u32, 0x414570A4))});
179 try S.foo(.{@bitCast(f32, @as(u32, 0x414570A4))});
180180}
181181
182182test "triple level result location with bitcast sandwich passed as tuple element" {
183183 const S = struct {
184 fn foo(args: anytype) void {
185 comptime expect(@TypeOf(args[0]) == f64);
186 expect(args[0] > 12.33 and args[0] < 12.35);
184 fn foo(args: anytype) !void {
185 comptime try expect(@TypeOf(args[0]) == f64);
186 try expect(args[0] > 12.33 and args[0] < 12.35);
187187 }
188188 };
189 S.foo(.{@as(f64, @bitCast(f32, @as(u32, 0x414570A4)))});
189 try S.foo(.{@as(f64, @bitCast(f32, @as(u32, 0x414570A4)))});
190190}
191191
192192test "bitcast generates a temporary value" {
193193 var y = @as(u16, 0x55AA);
194194 const x = @bitCast(u16, @bitCast([2]u8, y));
195 expectEqual(y, x);
195 try expectEqual(y, x);
196196}
test/stage1/behavior/bitreverse.zig+39-39
......@@ -3,67 +3,67 @@ const expect = std.testing.expect;
33const minInt = std.math.minInt;
44
55test "@bitReverse" {
6 comptime testBitReverse();
7 testBitReverse();
6 comptime try testBitReverse();
7 try testBitReverse();
88}
99
10fn testBitReverse() void {
10fn testBitReverse() !void {
1111 // using comptime_ints, unsigned
12 expect(@bitReverse(u0, 0) == 0);
13 expect(@bitReverse(u5, 0x12) == 0x9);
14 expect(@bitReverse(u8, 0x12) == 0x48);
15 expect(@bitReverse(u16, 0x1234) == 0x2c48);
16 expect(@bitReverse(u24, 0x123456) == 0x6a2c48);
17 expect(@bitReverse(u32, 0x12345678) == 0x1e6a2c48);
18 expect(@bitReverse(u40, 0x123456789a) == 0x591e6a2c48);
19 expect(@bitReverse(u48, 0x123456789abc) == 0x3d591e6a2c48);
20 expect(@bitReverse(u56, 0x123456789abcde) == 0x7b3d591e6a2c48);
21 expect(@bitReverse(u64, 0x123456789abcdef1) == 0x8f7b3d591e6a2c48);
22 expect(@bitReverse(u128, 0x123456789abcdef11121314151617181) == 0x818e868a828c84888f7b3d591e6a2c48);
12 try expect(@bitReverse(u0, 0) == 0);
13 try expect(@bitReverse(u5, 0x12) == 0x9);
14 try expect(@bitReverse(u8, 0x12) == 0x48);
15 try expect(@bitReverse(u16, 0x1234) == 0x2c48);
16 try expect(@bitReverse(u24, 0x123456) == 0x6a2c48);
17 try expect(@bitReverse(u32, 0x12345678) == 0x1e6a2c48);
18 try expect(@bitReverse(u40, 0x123456789a) == 0x591e6a2c48);
19 try expect(@bitReverse(u48, 0x123456789abc) == 0x3d591e6a2c48);
20 try expect(@bitReverse(u56, 0x123456789abcde) == 0x7b3d591e6a2c48);
21 try expect(@bitReverse(u64, 0x123456789abcdef1) == 0x8f7b3d591e6a2c48);
22 try expect(@bitReverse(u128, 0x123456789abcdef11121314151617181) == 0x818e868a828c84888f7b3d591e6a2c48);
2323
2424 // using runtime uints, unsigned
2525 var num0: u0 = 0;
26 expect(@bitReverse(u0, num0) == 0);
26 try expect(@bitReverse(u0, num0) == 0);
2727 var num5: u5 = 0x12;
28 expect(@bitReverse(u5, num5) == 0x9);
28 try expect(@bitReverse(u5, num5) == 0x9);
2929 var num8: u8 = 0x12;
30 expect(@bitReverse(u8, num8) == 0x48);
30 try expect(@bitReverse(u8, num8) == 0x48);
3131 var num16: u16 = 0x1234;
32 expect(@bitReverse(u16, num16) == 0x2c48);
32 try expect(@bitReverse(u16, num16) == 0x2c48);
3333 var num24: u24 = 0x123456;
34 expect(@bitReverse(u24, num24) == 0x6a2c48);
34 try expect(@bitReverse(u24, num24) == 0x6a2c48);
3535 var num32: u32 = 0x12345678;
36 expect(@bitReverse(u32, num32) == 0x1e6a2c48);
36 try expect(@bitReverse(u32, num32) == 0x1e6a2c48);
3737 var num40: u40 = 0x123456789a;
38 expect(@bitReverse(u40, num40) == 0x591e6a2c48);
38 try expect(@bitReverse(u40, num40) == 0x591e6a2c48);
3939 var num48: u48 = 0x123456789abc;
40 expect(@bitReverse(u48, num48) == 0x3d591e6a2c48);
40 try expect(@bitReverse(u48, num48) == 0x3d591e6a2c48);
4141 var num56: u56 = 0x123456789abcde;
42 expect(@bitReverse(u56, num56) == 0x7b3d591e6a2c48);
42 try expect(@bitReverse(u56, num56) == 0x7b3d591e6a2c48);
4343 var num64: u64 = 0x123456789abcdef1;
44 expect(@bitReverse(u64, num64) == 0x8f7b3d591e6a2c48);
44 try expect(@bitReverse(u64, num64) == 0x8f7b3d591e6a2c48);
4545 var num128: u128 = 0x123456789abcdef11121314151617181;
46 expect(@bitReverse(u128, num128) == 0x818e868a828c84888f7b3d591e6a2c48);
46 try expect(@bitReverse(u128, num128) == 0x818e868a828c84888f7b3d591e6a2c48);
4747
4848 // using comptime_ints, signed, positive
49 expect(@bitReverse(u8, @as(u8, 0)) == 0);
50 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)));
52 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)));
54 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)));
56 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)));
58 expect(@bitReverse(i128, @bitCast(i128, @as(u128, 0x123456789abcdef11121314151617181))) == @bitCast(i128, @as(u128, 0x818e868a828c84888f7b3d591e6a2c48)));
49 try expect(@bitReverse(u8, @as(u8, 0)) == 0);
50 try expect(@bitReverse(i8, @bitCast(i8, @as(u8, 0x92))) == @bitCast(i8, @as(u8, 0x49)));
51 try expect(@bitReverse(i16, @bitCast(i16, @as(u16, 0x1234))) == @bitCast(i16, @as(u16, 0x2c48)));
52 try expect(@bitReverse(i24, @bitCast(i24, @as(u24, 0x123456))) == @bitCast(i24, @as(u24, 0x6a2c48)));
53 try expect(@bitReverse(i32, @bitCast(i32, @as(u32, 0x12345678))) == @bitCast(i32, @as(u32, 0x1e6a2c48)));
54 try expect(@bitReverse(i40, @bitCast(i40, @as(u40, 0x123456789a))) == @bitCast(i40, @as(u40, 0x591e6a2c48)));
55 try expect(@bitReverse(i48, @bitCast(i48, @as(u48, 0x123456789abc))) == @bitCast(i48, @as(u48, 0x3d591e6a2c48)));
56 try expect(@bitReverse(i56, @bitCast(i56, @as(u56, 0x123456789abcde))) == @bitCast(i56, @as(u56, 0x7b3d591e6a2c48)));
57 try expect(@bitReverse(i64, @bitCast(i64, @as(u64, 0x123456789abcdef1))) == @bitCast(i64, @as(u64, 0x8f7b3d591e6a2c48)));
58 try expect(@bitReverse(i128, @bitCast(i128, @as(u128, 0x123456789abcdef11121314151617181))) == @bitCast(i128, @as(u128, 0x818e868a828c84888f7b3d591e6a2c48)));
5959
6060 // using signed, negative. Compare to runtime ints returned from llvm.
6161 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));
6363 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));
6565 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));
6767 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));
6969}
test/stage1/behavior/bool.zig+11-11
......@@ -1,25 +1,25 @@
11const expect = @import("std").testing.expect;
22
33test "bool literals" {
4 expect(true);
5 expect(!false);
4 try expect(true);
5 try expect(!false);
66}
77
88test "cast bool to int" {
99 const t = true;
1010 const f = false;
11 expect(@boolToInt(t) == @as(u32, 1));
12 expect(@boolToInt(f) == @as(u32, 0));
13 nonConstCastBoolToInt(t, f);
11 try expect(@boolToInt(t) == @as(u32, 1));
12 try expect(@boolToInt(f) == @as(u32, 0));
13 try nonConstCastBoolToInt(t, f);
1414}
1515
16fn nonConstCastBoolToInt(t: bool, f: bool) void {
17 expect(@boolToInt(t) == @as(u32, 1));
18 expect(@boolToInt(f) == @as(u32, 0));
16fn nonConstCastBoolToInt(t: bool, f: bool) !void {
17 try expect(@boolToInt(t) == @as(u32, 1));
18 try expect(@boolToInt(f) == @as(u32, 0));
1919}
2020
2121test "bool cmp" {
22 expect(testBoolCmp(true, false) == false);
22 try expect(testBoolCmp(true, false) == false);
2323}
2424fn testBoolCmp(a: bool, b: bool) bool {
2525 return a == b;
......@@ -30,6 +30,6 @@ const global_t = true;
3030const not_global_f = !global_f;
3131const not_global_t = !global_t;
3232test "compile time bool not" {
33 expect(not_global_f);
34 expect(!not_global_t);
33 try expect(not_global_f);
34 try expect(!not_global_t);
3535}
test/stage1/behavior/bugs/1025.zig+1-1
......@@ -8,5 +8,5 @@ fn getA() A {
88
99test "bug 1025" {
1010 const a = getA();
11 @import("std").testing.expect(a.B == u8);
11 try @import("std").testing.expect(a.B == u8);
1212}
test/stage1/behavior/bugs/1076.zig+5-5
......@@ -3,21 +3,21 @@ const mem = std.mem;
33const expect = std.testing.expect;
44
55test "comptime code should not modify constant data" {
6 testCastPtrOfArrayToSliceAndPtr();
7 comptime testCastPtrOfArrayToSliceAndPtr();
6 try testCastPtrOfArrayToSliceAndPtr();
7 comptime try testCastPtrOfArrayToSliceAndPtr();
88}
99
10fn testCastPtrOfArrayToSliceAndPtr() void {
10fn testCastPtrOfArrayToSliceAndPtr() !void {
1111 {
1212 var array = "aoeu".*;
1313 const x: [*]u8 = &array;
1414 x[0] += 1;
15 expect(mem.eql(u8, array[0..], "boeu"));
15 try expect(mem.eql(u8, array[0..], "boeu"));
1616 }
1717 {
1818 var array: [4]u8 = "aoeu".*;
1919 const x: [*]u8 = &array;
2020 x[0] += 1;
21 expect(mem.eql(u8, array[0..], "boeu"));
21 try expect(mem.eql(u8, array[0..], "boeu"));
2222 }
2323}
test/stage1/behavior/bugs/1120.zig+1-1
......@@ -19,5 +19,5 @@ test "bug 1120" {
1919 1 => &b.a,
2020 else => unreachable,
2121 };
22 expect(ptr.* == 2);
22 try expect(ptr.* == 2);
2323}
test/stage1/behavior/bugs/1277.zig+1-1
......@@ -11,5 +11,5 @@ fn f() i32 {
1111}
1212
1313test "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);
1515}
test/stage1/behavior/bugs/1310.zig+1-1
......@@ -20,5 +20,5 @@ fn agent_callback(_vm: [*]VM, options: [*]u8) callconv(.C) i32 {
2020}
2121
2222test "fixed" {
23 expect(agent_callback(undefined, undefined) == 11);
23 try expect(agent_callback(undefined, undefined) == 11);
2424}
test/stage1/behavior/bugs/1322.zig+2-2
......@@ -13,7 +13,7 @@ const C = struct {};
1313
1414test "tagged union with all void fields but a meaningful tag" {
1515 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);
1717 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);
1919}
test/stage1/behavior/bugs/1381.zig+1-1
......@@ -17,5 +17,5 @@ test "union that needs padding bytes inside an array" {
1717 };
1818
1919 const a = as[0].B;
20 std.testing.expect(a.D == 1);
20 try std.testing.expect(a.D == 1);
2121}
test/stage1/behavior/bugs/1421.zig+1-1
......@@ -10,5 +10,5 @@ const S = struct {
1010
1111test "functions with return type required to be comptime are generic" {
1212 const ti = S.method();
13 expect(@as(builtin.TypeId, ti) == builtin.TypeId.Struct);
13 try expect(@as(builtin.TypeId, ti) == builtin.TypeId.Struct);
1414}
test/stage1/behavior/bugs/1442.zig+1-1
......@@ -7,5 +7,5 @@ const Union = union(enum) {
77
88test "const error union field alignment" {
99 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);
1111}
test/stage1/behavior/bugs/1486.zig+2-2
......@@ -5,6 +5,6 @@ var global: u64 = 123;
55
66test "constant pointer to global variable causes runtime load" {
77 global = 1234;
8 expect(&global == ptr);
9 expect(ptr.* == 1234);
8 try expect(&global == ptr);
9 try expect(ptr.* == 1234);
1010}
test/stage1/behavior/bugs/1607.zig+4-4
......@@ -3,13 +3,13 @@ const testing = std.testing;
33
44const a = [_]u8{ 1, 2, 3 };
55
6fn checkAddress(s: []const u8) void {
6fn checkAddress(s: []const u8) !void {
77 for (s) |*i, j| {
8 testing.expect(i == &a[j]);
8 try testing.expect(i == &a[j]);
99 }
1010}
1111
1212test "slices pointing at the same address as global array." {
13 checkAddress(&a);
14 comptime checkAddress(&a);
13 try checkAddress(&a);
14 comptime try checkAddress(&a);
1515}
test/stage1/behavior/bugs/1735.zig+1-1
......@@ -42,5 +42,5 @@ const a = struct {
4242
4343test "intialization" {
4444 var t = a.init();
45 std.testing.expect(t.foo.len == 0);
45 try std.testing.expect(t.foo.len == 0);
4646}
test/stage1/behavior/bugs/1741.zig+1-1
......@@ -2,5 +2,5 @@ const std = @import("std");
22
33test "fixed" {
44 const x: f32 align(128) = 12.34;
5 std.testing.expect(@ptrToInt(&x) % 128 == 0);
5 try std.testing.expect(@ptrToInt(&x) % 128 == 0);
66}
test/stage1/behavior/bugs/1851.zig+9-9
......@@ -2,25 +2,25 @@ const std = @import("std");
22const expect = std.testing.expect;
33
44test "allocation and looping over 3-byte integer" {
5 expect(@sizeOf(u24) == 4);
6 expect(@sizeOf([1]u24) == 4);
7 expect(@alignOf(u24) == 4);
8 expect(@alignOf([1]u24) == 4);
5 try expect(@sizeOf(u24) == 4);
6 try expect(@sizeOf([1]u24) == 4);
7 try expect(@alignOf(u24) == 4);
8 try expect(@alignOf([1]u24) == 4);
99
1010 var x = try std.testing.allocator.alloc(u24, 2);
1111 defer std.testing.allocator.free(x);
12 expect(x.len == 2);
12 try expect(x.len == 2);
1313 x[0] = 0xFFFFFF;
1414 x[1] = 0xFFFFFF;
1515
1616 const bytes = std.mem.sliceAsBytes(x);
17 expect(@TypeOf(bytes) == []align(4) u8);
18 expect(bytes.len == 8);
17 try expect(@TypeOf(bytes) == []align(4) u8);
18 try expect(bytes.len == 8);
1919
2020 for (bytes) |*b| {
2121 b.* = 0x00;
2222 }
2323
24 expect(x[0] == 0x00);
25 expect(x[1] == 0x00);
24 try expect(x[0] == 0x00);
25 try expect(x[1] == 0x00);
2626}
test/stage1/behavior/bugs/2006.zig+2-2
......@@ -7,6 +7,6 @@ const S = struct {
77test "bug 2006" {
88 var a: S = undefined;
99 a = S{ .p = undefined };
10 expect(@sizeOf(S) != 0);
11 expect(@sizeOf(*void) == 0);
10 try expect(@sizeOf(S) != 0);
11 try expect(@sizeOf(*void) == 0);
1212}
test/stage1/behavior/bugs/2114.zig+7-7
......@@ -7,13 +7,13 @@ fn ctz(x: anytype) usize {
77}
88
99test "fixed" {
10 testClz();
11 comptime testClz();
10 try testClz();
11 comptime try testClz();
1212}
1313
14fn testClz() void {
15 expect(ctz(@as(u128, 0x40000000000000000000000000000000)) == 126);
16 expect(math.rotl(u128, @as(u128, 0x40000000000000000000000000000000), @as(u8, 1)) == @as(u128, 0x80000000000000000000000000000000));
17 expect(ctz(@as(u128, 0x80000000000000000000000000000000)) == 127);
18 expect(ctz(math.rotl(u128, @as(u128, 0x40000000000000000000000000000000), @as(u8, 1))) == 127);
14fn testClz() !void {
15 try expect(ctz(@as(u128, 0x40000000000000000000000000000000)) == 126);
16 try expect(math.rotl(u128, @as(u128, 0x40000000000000000000000000000000), @as(u8, 1)) == @as(u128, 0x80000000000000000000000000000000));
17 try expect(ctz(@as(u128, 0x80000000000000000000000000000000)) == 127);
18 try expect(ctz(math.rotl(u128, @as(u128, 0x40000000000000000000000000000000), @as(u8, 1))) == 127);
1919}
test/stage1/behavior/bugs/2889.zig+1-1
......@@ -27,5 +27,5 @@ fn parseNote() ?i32 {
2727
2828test "fixed" {
2929 const result = parseNote();
30 std.testing.expect(result.? == 9);
30 try std.testing.expect(result.? == 9);
3131}
test/stage1/behavior/bugs/3007.zig+1-1
......@@ -19,5 +19,5 @@ fn get_foo() Foo.FooError!*Foo {
1919
2020test "fixed" {
2121 default_foo = get_foo() catch null; // This Line
22 std.testing.expect(!default_foo.?.free);
22 try std.testing.expect(!default_foo.?.free);
2323}
test/stage1/behavior/bugs/3046.zig+1-1
......@@ -15,5 +15,5 @@ test "fixed" {
1515 some_struct = SomeStruct{
1616 .field = couldFail() catch |_| @as(i32, 0),
1717 };
18 expect(some_struct.field == 1);
18 try expect(some_struct.field == 1);
1919}
test/stage1/behavior/bugs/3112.zig+1-1
......@@ -7,7 +7,7 @@ const State = struct {
77};
88
99fn prev(p: ?State) void {
10 expect(p == null);
10 expect(p == null) catch @panic("test failure");
1111}
1212
1313test "zig test crash" {
test/stage1/behavior/bugs/3384.zig+6-6
......@@ -2,10 +2,10 @@ const std = @import("std");
22const expect = std.testing.expect;
33
44test "resolve array slice using builtin" {
5 expect(@hasDecl(@This(), "std") == true);
6 expect(@hasDecl(@This(), "std"[0..0]) == false);
7 expect(@hasDecl(@This(), "std"[0..1]) == false);
8 expect(@hasDecl(@This(), "std"[0..2]) == false);
9 expect(@hasDecl(@This(), "std"[0..3]) == true);
10 expect(@hasDecl(@This(), "std"[0..]) == true);
5 try expect(@hasDecl(@This(), "std") == true);
6 try expect(@hasDecl(@This(), "std"[0..0]) == false);
7 try expect(@hasDecl(@This(), "std"[0..1]) == false);
8 try expect(@hasDecl(@This(), "std"[0..2]) == false);
9 try expect(@hasDecl(@This(), "std"[0..3]) == true);
10 try expect(@hasDecl(@This(), "std"[0..]) == true);
1111}
test/stage1/behavior/bugs/394.zig+1-1
......@@ -14,5 +14,5 @@ test "bug 394 fixed" {
1414 .x = 3,
1515 .y = E{ .B = 1 },
1616 };
17 expect(x.x == 3);
17 try expect(x.x == 3);
1818}
test/stage1/behavior/bugs/421.zig+4-4
......@@ -1,12 +1,12 @@
11const expect = @import("std").testing.expect;
22
33test "bitCast to array" {
4 comptime testBitCastArray();
5 testBitCastArray();
4 comptime try testBitCastArray();
5 try testBitCastArray();
66}
77
8fn testBitCastArray() void {
9 expect(extractOne64(0x0123456789abcdef0123456789abcdef) == 0x0123456789abcdef);
8fn testBitCastArray() !void {
9 try expect(extractOne64(0x0123456789abcdef0123456789abcdef) == 0x0123456789abcdef);
1010}
1111
1212fn extractOne64(a: u128) u64 {
test/stage1/behavior/bugs/4328.zig+14-14
......@@ -25,14 +25,14 @@ test "Extern function calls in @TypeOf" {
2525 return 1;
2626 }
2727
28 fn doTheTest() void {
29 expectEqual(c_int, @TypeOf(test_fn_1(0, 42)));
30 expectEqual(c_short, @TypeOf(test_fn_2(0)));
28 fn doTheTest() !void {
29 try expectEqual(c_int, @TypeOf(test_fn_1(0, 42)));
30 try expectEqual(c_short, @TypeOf(test_fn_2(0)));
3131 }
3232 };
3333
34 Test.doTheTest();
35 comptime Test.doTheTest();
34 try Test.doTheTest();
35 comptime try Test.doTheTest();
3636}
3737
3838test "Peer resolution of extern function calls in @TypeOf" {
......@@ -41,13 +41,13 @@ test "Peer resolution of extern function calls in @TypeOf" {
4141 return 0;
4242 }
4343
44 fn doTheTest() void {
45 expectEqual(c_long, @TypeOf(test_fn()));
44 fn doTheTest() !void {
45 try expectEqual(c_long, @TypeOf(test_fn()));
4646 }
4747 };
4848
49 Test.doTheTest();
50 comptime Test.doTheTest();
49 try Test.doTheTest();
50 comptime try Test.doTheTest();
5151}
5252
5353test "Extern function calls, dereferences and field access in @TypeOf" {
......@@ -60,12 +60,12 @@ test "Extern function calls, dereferences and field access in @TypeOf" {
6060 return 255;
6161 }
6262
63 fn doTheTest() void {
64 expectEqual(FILE, @TypeOf(test_fn_1(0)));
65 expectEqual(u8, @TypeOf(test_fn_2(0)));
63 fn doTheTest() !void {
64 try expectEqual(FILE, @TypeOf(test_fn_1(0)));
65 try expectEqual(u8, @TypeOf(test_fn_2(0)));
6666 }
6767 };
6868
69 Test.doTheTest();
70 comptime Test.doTheTest();
69 try Test.doTheTest();
70 comptime try Test.doTheTest();
7171}
test/stage1/behavior/bugs/4560.zig+3-3
......@@ -8,9 +8,9 @@ test "fixed" {
88 .max_distance_from_start_index = 456,
99 },
1010 };
11 std.testing.expect(s.a == 1);
12 std.testing.expect(s.b.size == 123);
13 std.testing.expect(s.b.max_distance_from_start_index == 456);
11 try std.testing.expect(s.a == 1);
12 try std.testing.expect(s.b.size == 123);
13 try std.testing.expect(s.b.max_distance_from_start_index == 456);
1414}
1515
1616const S = struct {
test/stage1/behavior/bugs/4769_a.zig+1-1
......@@ -1 +1 @@
1//
\ No newline at end of file
1//
test/stage1/behavior/bugs/4769_b.zig+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" {
2525 .emits_shadows = false,
2626 },
2727 };
28 testing.expectEqual(false, renderable.material.transparent);
29 testing.expectEqual(false, renderable.material.emits_shadows);
30 testing.expectEqual(true, renderable.material.render_color);
28 try testing.expectEqual(false, renderable.material.transparent);
29 try testing.expectEqual(false, renderable.material.emits_shadows);
30 try testing.expectEqual(true, renderable.material.render_color);
3131}
test/stage1/behavior/bugs/5413.zig+2-2
......@@ -1,6 +1,6 @@
11const expect = @import("std").testing.expect;
22
33test "Peer type resolution with string literals and unknown length u8 pointers" {
4 expect(@TypeOf("", "a", @as([*:0]const u8, "")) == [*:0]const u8);
5 expect(@TypeOf(@as([*:0]const u8, "baz"), "foo", "bar") == [*:0]const u8);
4 try expect(@TypeOf("", "a", @as([*:0]const u8, "")) == [*:0]const u8);
5 try expect(@TypeOf(@as([*:0]const u8, "baz"), "foo", "bar") == [*:0]const u8);
66}
test/stage1/behavior/bugs/5474.zig+9-9
......@@ -25,33 +25,33 @@ const Box2 = struct {
2525 };
2626};
2727
28fn doTest() void {
28fn doTest() !void {
2929 // var
3030 {
3131 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
3434 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
3737 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);
3939 }
4040
4141 // const
4242 {
4343 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
4646 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
4949 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);
5151 }
5252}
5353
5454test "pointer-to-array constness for zero-size elements" {
55 doTest();
56 comptime doTest();
55 try doTest();
56 comptime try doTest();
5757}
test/stage1/behavior/bugs/624.zig+1-1
......@@ -19,5 +19,5 @@ fn MemoryPool(comptime T: type) type {
1919
2020test "foo" {
2121 var allocator = ContextAllocator{ .n = 10 };
22 expect(allocator.n == 10);
22 try expect(allocator.n == 10);
2323}
test/stage1/behavior/bugs/6456.zig+4-4
......@@ -35,9 +35,9 @@ test "issue 6456" {
3535 });
3636
3737 const gen_fields = @typeInfo(T).Struct.fields;
38 testing.expectEqual(3, gen_fields.len);
39 testing.expectEqualStrings("f1", gen_fields[0].name);
40 testing.expectEqualStrings("f2", gen_fields[1].name);
41 testing.expectEqualStrings("f3", gen_fields[2].name);
38 try testing.expectEqual(3, gen_fields.len);
39 try testing.expectEqualStrings("f1", gen_fields[0].name);
40 try testing.expectEqualStrings("f2", gen_fields[1].name);
41 try testing.expectEqualStrings("f3", gen_fields[2].name);
4242 }
4343}
test/stage1/behavior/bugs/655.zig+4-4
......@@ -3,10 +3,10 @@ const other_file = @import("655_other_file.zig");
33
44test "function with *const parameter with type dereferenced by namespace" {
55 const x: other_file.Integer = 1234;
6 comptime std.testing.expect(@TypeOf(&x) == *const other_file.Integer);
7 foo(&x);
6 comptime try std.testing.expect(@TypeOf(&x) == *const other_file.Integer);
7 try foo(&x);
88}
99
10fn foo(x: *const other_file.Integer) void {
11 std.testing.expect(x.* == 1234);
10fn foo(x: *const other_file.Integer) !void {
11 try std.testing.expect(x.* == 1234);
1212}
test/stage1/behavior/bugs/656.zig+3-3
......@@ -10,10 +10,10 @@ const Value = struct {
1010};
1111
1212test "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);
1414}
1515
16fn foo(a: bool, b: bool) void {
16fn foo(a: bool, b: bool) !void {
1717 var prefix_op = PrefixOp{
1818 .AddrOf = Value{ .align_expr = 1234 },
1919 };
......@@ -22,7 +22,7 @@ fn foo(a: bool, b: bool) void {
2222 PrefixOp.AddrOf => |addr_of_info| {
2323 if (b) {}
2424 if (addr_of_info.align_expr) |align_expr| {
25 expect(align_expr == 1234);
25 try expect(align_expr == 1234);
2626 }
2727 },
2828 PrefixOp.Return => {},
test/stage1/behavior/bugs/679.zig+1-1
......@@ -13,5 +13,5 @@ const Element = struct {
1313test "false dependency loop in struct definition" {
1414 const listType = ElementList;
1515 var x: listType = 42;
16 expect(x == 42);
16 try expect(x == 42);
1717}
test/stage1/behavior/bugs/6850.zig+1-1
......@@ -4,7 +4,7 @@ test "lazy sizeof comparison with zero" {
44 const Empty = struct {};
55 const T = *Empty;
66
7 std.testing.expect(hasNoBits(T));
7 try std.testing.expect(hasNoBits(T));
88}
99
1010fn hasNoBits(comptime T: type) bool {
test/stage1/behavior/bugs/7047.zig+2-2
......@@ -15,8 +15,8 @@ fn S(comptime query: U) type {
1515
1616test "compiler doesn't consider equal unions with different 'type' payload" {
1717 const s1 = S(U{ .T = u32 }).tag();
18 std.testing.expectEqual(u32, s1);
18 try std.testing.expectEqual(u32, s1);
1919
2020 const s2 = S(U{ .T = u64 }).tag();
21 std.testing.expectEqual(u64, s2);
21 try std.testing.expectEqual(u64, s2);
2222}
test/stage1/behavior/bugs/718.zig+4-4
......@@ -10,8 +10,8 @@ const Keys = struct {
1010var keys: Keys = undefined;
1111test "zero keys with @memset" {
1212 @memset(@ptrCast([*]u8, &keys), 0, @sizeOf(@TypeOf(keys)));
13 expect(!keys.up);
14 expect(!keys.down);
15 expect(!keys.left);
16 expect(!keys.right);
13 try expect(!keys.up);
14 try expect(!keys.down);
15 try expect(!keys.left);
16 try expect(!keys.right);
1717}
test/stage1/behavior/bugs/726.zig+2-2
......@@ -3,7 +3,7 @@ const expect = @import("std").testing.expect;
33test "@ptrCast from const to nullable" {
44 const c: u8 = 4;
55 var x: ?*const u8 = @ptrCast(?*const u8, &c);
6 expect(x.?.* == 4);
6 try expect(x.?.* == 4);
77}
88
99test "@ptrCast from var in empty struct to nullable" {
......@@ -11,5 +11,5 @@ test "@ptrCast from var in empty struct to nullable" {
1111 var c: u8 = 4;
1212 };
1313 var x: ?*const u8 = @ptrCast(?*const u8, &container.c);
14 expect(x.?.* == 4);
14 try expect(x.?.* == 4);
1515}
test/stage1/behavior/bugs/920.zig+1-1
......@@ -60,6 +60,6 @@ test "bug 920 fixed" {
6060 };
6161
6262 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]);
6464 }
6565}
test/stage1/behavior/byteswap.zig+33-33
......@@ -3,39 +3,39 @@ const expect = std.testing.expect;
33
44test "@byteSwap integers" {
55 const ByteSwapIntTest = struct {
6 fn run() void {
7 t(u0, 0, 0);
8 t(u8, 0x12, 0x12);
9 t(u16, 0x1234, 0x3412);
10 t(u24, 0x123456, 0x563412);
11 t(u32, 0x12345678, 0x78563412);
12 t(u40, 0x123456789a, 0x9a78563412);
13 t(i48, 0x123456789abc, @bitCast(i48, @as(u48, 0xbc9a78563412)));
14 t(u56, 0x123456789abcde, 0xdebc9a78563412);
15 t(u64, 0x123456789abcdef1, 0xf1debc9a78563412);
16 t(u128, 0x123456789abcdef11121314151617181, 0x8171615141312111f1debc9a78563412);
6 fn run() !void {
7 try t(u0, 0, 0);
8 try t(u8, 0x12, 0x12);
9 try t(u16, 0x1234, 0x3412);
10 try t(u24, 0x123456, 0x563412);
11 try t(u32, 0x12345678, 0x78563412);
12 try t(u40, 0x123456789a, 0x9a78563412);
13 try t(i48, 0x123456789abc, @bitCast(i48, @as(u48, 0xbc9a78563412)));
14 try t(u56, 0x123456789abcde, 0xdebc9a78563412);
15 try t(u64, 0x123456789abcdef1, 0xf1debc9a78563412);
16 try t(u128, 0x123456789abcdef11121314151617181, 0x8171615141312111f1debc9a78563412);
1717
18 t(u0, @as(u0, 0), 0);
19 t(i8, @as(i8, -50), -50);
20 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)));
22 t(i32, @bitCast(i32, @as(u32, 0x12345678)), @bitCast(i32, @as(u32, 0x78563412)));
23 t(u40, @bitCast(i40, @as(u40, 0x123456789a)), @as(u40, 0x9a78563412));
24 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)));
26 t(i64, @bitCast(i64, @as(u64, 0x123456789abcdef1)), @bitCast(i64, @as(u64, 0xf1debc9a78563412)));
27 t(
18 try t(u0, @as(u0, 0), 0);
19 try t(i8, @as(i8, -50), -50);
20 try t(i16, @bitCast(i16, @as(u16, 0x1234)), @bitCast(i16, @as(u16, 0x3412)));
21 try t(i24, @bitCast(i24, @as(u24, 0x123456)), @bitCast(i24, @as(u24, 0x563412)));
22 try t(i32, @bitCast(i32, @as(u32, 0x12345678)), @bitCast(i32, @as(u32, 0x78563412)));
23 try t(u40, @bitCast(i40, @as(u40, 0x123456789a)), @as(u40, 0x9a78563412));
24 try t(i48, @bitCast(i48, @as(u48, 0x123456789abc)), @bitCast(i48, @as(u48, 0xbc9a78563412)));
25 try t(i56, @bitCast(i56, @as(u56, 0x123456789abcde)), @bitCast(i56, @as(u56, 0xdebc9a78563412)));
26 try t(i64, @bitCast(i64, @as(u64, 0x123456789abcdef1)), @bitCast(i64, @as(u64, 0xf1debc9a78563412)));
27 try t(
2828 i128,
2929 @bitCast(i128, @as(u128, 0x123456789abcdef11121314151617181)),
3030 @bitCast(i128, @as(u128, 0x8171615141312111f1debc9a78563412)),
3131 );
3232 }
33 fn t(comptime I: type, input: I, expected_output: I) void {
34 std.testing.expectEqual(expected_output, @byteSwap(I, input));
33 fn t(comptime I: type, input: I, expected_output: I) !void {
34 try std.testing.expectEqual(expected_output, @byteSwap(I, input));
3535 }
3636 };
37 comptime ByteSwapIntTest.run();
38 ByteSwapIntTest.run();
37 comptime try ByteSwapIntTest.run();
38 try ByteSwapIntTest.run();
3939}
4040
4141test "@byteSwap vectors" {
......@@ -46,10 +46,10 @@ test "@byteSwap vectors" {
4646 if (std.Target.current.cpu.arch == .mipsel or std.Target.current.cpu.arch == .mips) return error.SkipZigTest;
4747
4848 const ByteSwapVectorTest = struct {
49 fn run() void {
50 t(u8, 2, [_]u8{ 0x12, 0x13 }, [_]u8{ 0x12, 0x13 });
51 t(u16, 2, [_]u16{ 0x1234, 0x2345 }, [_]u16{ 0x3412, 0x4523 });
52 t(u24, 2, [_]u24{ 0x123456, 0x234567 }, [_]u24{ 0x563412, 0x674523 });
49 fn run() !void {
50 try t(u8, 2, [_]u8{ 0x12, 0x13 }, [_]u8{ 0x12, 0x13 });
51 try t(u16, 2, [_]u16{ 0x1234, 0x2345 }, [_]u16{ 0x3412, 0x4523 });
52 try t(u24, 2, [_]u24{ 0x123456, 0x234567 }, [_]u24{ 0x563412, 0x674523 });
5353 }
5454
5555 fn t(
......@@ -57,12 +57,12 @@ test "@byteSwap vectors" {
5757 comptime n: comptime_int,
5858 input: std.meta.Vector(n, I),
5959 expected_vector: std.meta.Vector(n, I),
60 ) void {
60 ) !void {
6161 const actual_output: [n]I = @byteSwap(I, input);
6262 const expected_output: [n]I = expected_vector;
63 std.testing.expectEqual(expected_output, actual_output);
63 try std.testing.expectEqual(expected_output, actual_output);
6464 }
6565 };
66 comptime ByteSwapVectorTest.run();
67 ByteSwapVectorTest.run();
66 comptime try ByteSwapVectorTest.run();
67 try ByteSwapVectorTest.run();
6868}
test/stage1/behavior/byval_arg_var.zig+1-1
......@@ -6,7 +6,7 @@ test "pass string literal byvalue to a generic var param" {
66 start();
77 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"));
1010}
1111
1212fn start() void {
test/stage1/behavior/call.zig+19-19
......@@ -8,25 +8,25 @@ test "basic invocations" {
88 return 1234;
99 }
1010 }.foo;
11 expect(@call(.{}, foo, .{}) == 1234);
11 try expect(@call(.{}, foo, .{}) == 1234);
1212 comptime {
1313 // modifiers that allow comptime calls
14 expect(@call(.{}, foo, .{}) == 1234);
15 expect(@call(.{ .modifier = .no_async }, foo, .{}) == 1234);
16 expect(@call(.{ .modifier = .always_tail }, foo, .{}) == 1234);
17 expect(@call(.{ .modifier = .always_inline }, foo, .{}) == 1234);
14 try expect(@call(.{}, foo, .{}) == 1234);
15 try expect(@call(.{ .modifier = .no_async }, foo, .{}) == 1234);
16 try expect(@call(.{ .modifier = .always_tail }, foo, .{}) == 1234);
17 try expect(@call(.{ .modifier = .always_inline }, foo, .{}) == 1234);
1818 }
1919 {
2020 // comptime call without comptime keyword
2121 const result = @call(.{ .modifier = .compile_time }, foo, .{}) == 1234;
22 comptime expect(result);
22 comptime try expect(result);
2323 }
2424 {
2525 // call of non comptime-known function
2626 var alias_foo = foo;
27 expect(@call(.{ .modifier = .no_async }, alias_foo, .{}) == 1234);
28 expect(@call(.{ .modifier = .never_tail }, alias_foo, .{}) == 1234);
29 expect(@call(.{ .modifier = .never_inline }, alias_foo, .{}) == 1234);
27 try expect(@call(.{ .modifier = .no_async }, alias_foo, .{}) == 1234);
28 try expect(@call(.{ .modifier = .never_tail }, alias_foo, .{}) == 1234);
29 try expect(@call(.{ .modifier = .never_inline }, alias_foo, .{}) == 1234);
3030 }
3131}
3232
......@@ -38,20 +38,20 @@ test "tuple parameters" {
3838 }.add;
3939 var a: i32 = 12;
4040 var b: i32 = 34;
41 expect(@call(.{}, add, .{ a, 34 }) == 46);
42 expect(@call(.{}, add, .{ 12, b }) == 46);
43 expect(@call(.{}, add, .{ a, b }) == 46);
44 expect(@call(.{}, add, .{ 12, 34 }) == 46);
45 comptime expect(@call(.{}, add, .{ 12, 34 }) == 46);
41 try expect(@call(.{}, add, .{ a, 34 }) == 46);
42 try expect(@call(.{}, add, .{ 12, b }) == 46);
43 try expect(@call(.{}, add, .{ a, b }) == 46);
44 try expect(@call(.{}, add, .{ 12, 34 }) == 46);
45 comptime try expect(@call(.{}, add, .{ 12, 34 }) == 46);
4646 {
4747 const separate_args0 = .{ a, b };
4848 const separate_args1 = .{ a, 34 };
4949 const separate_args2 = .{ 12, 34 };
5050 const separate_args3 = .{ 12, b };
51 expect(@call(.{ .modifier = .always_inline }, add, separate_args0) == 46);
52 expect(@call(.{ .modifier = .always_inline }, add, separate_args1) == 46);
53 expect(@call(.{ .modifier = .always_inline }, add, separate_args2) == 46);
54 expect(@call(.{ .modifier = .always_inline }, add, separate_args3) == 46);
51 try expect(@call(.{ .modifier = .always_inline }, add, separate_args0) == 46);
52 try expect(@call(.{ .modifier = .always_inline }, add, separate_args1) == 46);
53 try expect(@call(.{ .modifier = .always_inline }, add, separate_args2) == 46);
54 try expect(@call(.{ .modifier = .always_inline }, add, separate_args3) == 46);
5555 }
5656}
5757
......@@ -70,5 +70,5 @@ test "comptime call with bound function as parameter" {
7070 };
7171
7272 var inst: S = undefined;
73 expectEqual(?i32, S.ReturnType(inst.call_me_maybe));
73 try expectEqual(?i32, S.ReturnType(inst.call_me_maybe));
7474}
test/stage1/behavior/cast.zig+226-226
......@@ -8,12 +8,12 @@ test "int to ptr cast" {
88 const x = @as(usize, 13);
99 const y = @intToPtr(*u8, x);
1010 const z = @ptrToInt(y);
11 expect(z == 13);
11 try expect(z == 13);
1212}
1313
1414test "integer literal to pointer cast" {
1515 const vga_mem = @intToPtr(*u16, 0xB8000);
16 expect(@ptrToInt(vga_mem) == 0xB8000);
16 try expect(@ptrToInt(vga_mem) == 0xB8000);
1717}
1818
1919test "pointer reinterpret const float to int" {
......@@ -23,9 +23,9 @@ test "pointer reinterpret const float to int" {
2323 const int_ptr = @ptrCast(*const i32, float_ptr);
2424 const int_val = int_ptr.*;
2525 if (std.builtin.endian == .Little)
26 expect(int_val == 0x33333303)
26 try expect(int_val == 0x33333303)
2727 else
28 expect(int_val == 0x3fe33333);
28 try expect(int_val == 0x3fe33333);
2929}
3030
3131test "implicitly cast indirect pointer to maybe-indirect pointer" {
......@@ -49,62 +49,62 @@ test "implicitly cast indirect pointer to maybe-indirect pointer" {
4949 const p = &s;
5050 const q = &p;
5151 const r = &q;
52 expect(42 == S.constConst(q));
53 expect(42 == S.maybeConstConst(q));
54 expect(42 == S.constConstConst(r));
55 expect(42 == S.maybeConstConstConst(r));
52 try expect(42 == S.constConst(q));
53 try expect(42 == S.maybeConstConst(q));
54 try expect(42 == S.constConstConst(r));
55 try expect(42 == S.maybeConstConstConst(r));
5656}
5757
5858test "explicit cast from integer to error type" {
59 testCastIntToErr(error.ItBroke);
60 comptime testCastIntToErr(error.ItBroke);
59 try testCastIntToErr(error.ItBroke);
60 comptime try testCastIntToErr(error.ItBroke);
6161}
62fn testCastIntToErr(err: anyerror) void {
62fn testCastIntToErr(err: anyerror) !void {
6363 const x = @errorToInt(err);
6464 const y = @intToError(x);
65 expect(error.ItBroke == y);
65 try expect(error.ItBroke == y);
6666}
6767
6868test "peer resolve arrays of different size to const slice" {
69 expect(mem.eql(u8, boolToStr(true), "true"));
70 expect(mem.eql(u8, boolToStr(false), "false"));
71 comptime expect(mem.eql(u8, boolToStr(true), "true"));
72 comptime expect(mem.eql(u8, boolToStr(false), "false"));
69 try expect(mem.eql(u8, boolToStr(true), "true"));
70 try expect(mem.eql(u8, boolToStr(false), "false"));
71 comptime try expect(mem.eql(u8, boolToStr(true), "true"));
72 comptime try expect(mem.eql(u8, boolToStr(false), "false"));
7373}
7474fn boolToStr(b: bool) []const u8 {
7575 return if (b) "true" else "false";
7676}
7777
7878test "peer resolve array and const slice" {
79 testPeerResolveArrayConstSlice(true);
80 comptime testPeerResolveArrayConstSlice(true);
79 try testPeerResolveArrayConstSlice(true);
80 comptime try testPeerResolveArrayConstSlice(true);
8181}
82fn testPeerResolveArrayConstSlice(b: bool) void {
82fn testPeerResolveArrayConstSlice(b: bool) !void {
8383 const value1 = if (b) "aoeu" else @as([]const u8, "zz");
8484 const value2 = if (b) @as([]const u8, "zz") else "aoeu";
85 expect(mem.eql(u8, value1, "aoeu"));
86 expect(mem.eql(u8, value2, "zz"));
85 try expect(mem.eql(u8, value1, "aoeu"));
86 try expect(mem.eql(u8, value2, "zz"));
8787}
8888
8989test "implicitly cast from T to anyerror!?T" {
90 castToOptionalTypeError(1);
91 comptime castToOptionalTypeError(1);
90 try castToOptionalTypeError(1);
91 comptime try castToOptionalTypeError(1);
9292}
9393
9494const A = struct {
9595 a: i32,
9696};
97fn castToOptionalTypeError(z: i32) void {
97fn castToOptionalTypeError(z: i32) !void {
9898 const x = @as(i32, 1);
9999 const y: anyerror!?i32 = x;
100 expect((try y).? == 1);
100 try expect((try y).? == 1);
101101
102102 const f = z;
103103 const g: anyerror!?i32 = f;
104104
105105 const a = A{ .a = z };
106106 const b: anyerror!?A = a;
107 expect((b catch unreachable).?.a == 1);
107 try expect((b catch unreachable).?.a == 1);
108108}
109109
110110test "implicitly cast from int to anyerror!?T" {
......@@ -119,7 +119,7 @@ fn implicitIntLitToOptional() void {
119119test "return null from fn() anyerror!?&T" {
120120 const a = returnNullFromOptionalTypeErrorRef();
121121 const b = returnNullLitFromOptionalTypeErrorRef();
122 expect((try a) == null and (try b) == null);
122 try expect((try a) == null and (try b) == null);
123123}
124124fn returnNullFromOptionalTypeErrorRef() anyerror!?*A {
125125 const a: ?*A = null;
......@@ -130,11 +130,11 @@ fn returnNullLitFromOptionalTypeErrorRef() anyerror!?*A {
130130}
131131
132132test "peer type resolution: ?T and T" {
133 expect(peerTypeTAndOptionalT(true, false).? == 0);
134 expect(peerTypeTAndOptionalT(false, false).? == 3);
133 try expect(peerTypeTAndOptionalT(true, false).? == 0);
134 try expect(peerTypeTAndOptionalT(false, false).? == 3);
135135 comptime {
136 expect(peerTypeTAndOptionalT(true, false).? == 0);
137 expect(peerTypeTAndOptionalT(false, false).? == 3);
136 try expect(peerTypeTAndOptionalT(true, false).? == 0);
137 try expect(peerTypeTAndOptionalT(false, false).? == 3);
138138 }
139139}
140140fn peerTypeTAndOptionalT(c: bool, b: bool) ?usize {
......@@ -146,11 +146,11 @@ fn peerTypeTAndOptionalT(c: bool, b: bool) ?usize {
146146}
147147
148148test "peer type resolution: [0]u8 and []const u8" {
149 expect(peerTypeEmptyArrayAndSlice(true, "hi").len == 0);
150 expect(peerTypeEmptyArrayAndSlice(false, "hi").len == 1);
149 try expect(peerTypeEmptyArrayAndSlice(true, "hi").len == 0);
150 try expect(peerTypeEmptyArrayAndSlice(false, "hi").len == 1);
151151 comptime {
152 expect(peerTypeEmptyArrayAndSlice(true, "hi").len == 0);
153 expect(peerTypeEmptyArrayAndSlice(false, "hi").len == 1);
152 try expect(peerTypeEmptyArrayAndSlice(true, "hi").len == 0);
153 try expect(peerTypeEmptyArrayAndSlice(false, "hi").len == 1);
154154 }
155155}
156156fn peerTypeEmptyArrayAndSlice(a: bool, slice: []const u8) []const u8 {
......@@ -162,8 +162,8 @@ fn peerTypeEmptyArrayAndSlice(a: bool, slice: []const u8) []const u8 {
162162}
163163
164164test "implicitly cast from [N]T to ?[]const T" {
165 expect(mem.eql(u8, castToOptionalSlice().?, "hi"));
166 comptime expect(mem.eql(u8, castToOptionalSlice().?, "hi"));
165 try expect(mem.eql(u8, castToOptionalSlice().?, "hi"));
166 comptime try expect(mem.eql(u8, castToOptionalSlice().?, "hi"));
167167}
168168
169169fn castToOptionalSlice() ?[]const u8 {
......@@ -171,12 +171,12 @@ fn castToOptionalSlice() ?[]const u8 {
171171}
172172
173173test "implicitly cast from [0]T to anyerror![]T" {
174 testCastZeroArrayToErrSliceMut();
175 comptime testCastZeroArrayToErrSliceMut();
174 try testCastZeroArrayToErrSliceMut();
175 comptime try testCastZeroArrayToErrSliceMut();
176176}
177177
178fn testCastZeroArrayToErrSliceMut() void {
179 expect((gimmeErrOrSlice() catch unreachable).len == 0);
178fn testCastZeroArrayToErrSliceMut() !void {
179 try expect((gimmeErrOrSlice() catch unreachable).len == 0);
180180}
181181
182182fn gimmeErrOrSlice() anyerror![]u8 {
......@@ -189,19 +189,19 @@ test "peer type resolution: [0]u8, []const u8, and anyerror![]u8" {
189189 {
190190 var data = "hi".*;
191191 const slice = data[0..];
192 expect((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0);
193 expect((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1);
192 try expect((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0);
193 try expect((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1);
194194 }
195195 {
196196 var data: [2]u8 = "hi".*;
197197 const slice = data[0..];
198 expect((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0);
199 expect((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1);
198 try expect((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0);
199 try expect((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1);
200200 }
201201 }
202202 };
203203 try S.doTheTest();
204 try comptime S.doTheTest();
204 comptime try S.doTheTest();
205205}
206206fn peerTypeEmptyArrayAndSliceAndError(a: bool, slice: []u8) anyerror![]u8 {
207207 if (a) {
......@@ -212,43 +212,43 @@ fn peerTypeEmptyArrayAndSliceAndError(a: bool, slice: []u8) anyerror![]u8 {
212212}
213213
214214test "resolve undefined with integer" {
215 testResolveUndefWithInt(true, 1234);
216 comptime testResolveUndefWithInt(true, 1234);
215 try testResolveUndefWithInt(true, 1234);
216 comptime try testResolveUndefWithInt(true, 1234);
217217}
218fn testResolveUndefWithInt(b: bool, x: i32) void {
218fn testResolveUndefWithInt(b: bool, x: i32) !void {
219219 const value = if (b) x else undefined;
220220 if (b) {
221 expect(value == x);
221 try expect(value == x);
222222 }
223223}
224224
225225test "implicit cast from &const [N]T to []const T" {
226 testCastConstArrayRefToConstSlice();
227 comptime testCastConstArrayRefToConstSlice();
226 try testCastConstArrayRefToConstSlice();
227 comptime try testCastConstArrayRefToConstSlice();
228228}
229229
230fn testCastConstArrayRefToConstSlice() void {
230fn testCastConstArrayRefToConstSlice() !void {
231231 {
232232 const blah = "aoeu".*;
233233 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);
235235 const slice: []const u8 = const_array_ref;
236 expect(mem.eql(u8, slice, "aoeu"));
236 try expect(mem.eql(u8, slice, "aoeu"));
237237 }
238238 {
239239 const blah: [4]u8 = "aoeu".*;
240240 const const_array_ref = &blah;
241 expect(@TypeOf(const_array_ref) == *const [4]u8);
241 try expect(@TypeOf(const_array_ref) == *const [4]u8);
242242 const slice: []const u8 = const_array_ref;
243 expect(mem.eql(u8, slice, "aoeu"));
243 try expect(mem.eql(u8, slice, "aoeu"));
244244 }
245245}
246246
247247test "peer type resolution: error and [N]T" {
248 expect(mem.eql(u8, try testPeerErrorAndArray(0), "OK"));
249 comptime expect(mem.eql(u8, try testPeerErrorAndArray(0), "OK"));
250 expect(mem.eql(u8, try testPeerErrorAndArray2(1), "OKK"));
251 comptime expect(mem.eql(u8, try testPeerErrorAndArray2(1), "OKK"));
248 try expect(mem.eql(u8, try testPeerErrorAndArray(0), "OK"));
249 comptime try expect(mem.eql(u8, try testPeerErrorAndArray(0), "OK"));
250 try expect(mem.eql(u8, try testPeerErrorAndArray2(1), "OKK"));
251 comptime try expect(mem.eql(u8, try testPeerErrorAndArray2(1), "OKK"));
252252}
253253
254254fn testPeerErrorAndArray(x: u8) anyerror![]const u8 {
......@@ -266,35 +266,35 @@ fn testPeerErrorAndArray2(x: u8) anyerror![]const u8 {
266266}
267267
268268test "@floatToInt" {
269 testFloatToInts();
270 comptime testFloatToInts();
269 try testFloatToInts();
270 comptime try testFloatToInts();
271271}
272272
273fn testFloatToInts() void {
273fn testFloatToInts() !void {
274274 const x = @as(i32, 1e4);
275 expect(x == 10000);
275 try expect(x == 10000);
276276 const y = @floatToInt(i32, @as(f32, 1e4));
277 expect(y == 10000);
278 expectFloatToInt(f16, 255.1, u8, 255);
279 expectFloatToInt(f16, 127.2, i8, 127);
280 expectFloatToInt(f16, -128.2, i8, -128);
281 expectFloatToInt(f32, 255.1, u8, 255);
282 expectFloatToInt(f32, 127.2, i8, 127);
283 expectFloatToInt(f32, -128.2, i8, -128);
284 expectFloatToInt(comptime_int, 1234, i16, 1234);
277 try expect(y == 10000);
278 try expectFloatToInt(f16, 255.1, u8, 255);
279 try expectFloatToInt(f16, 127.2, i8, 127);
280 try expectFloatToInt(f16, -128.2, i8, -128);
281 try expectFloatToInt(f32, 255.1, u8, 255);
282 try expectFloatToInt(f32, 127.2, i8, 127);
283 try expectFloatToInt(f32, -128.2, i8, -128);
284 try expectFloatToInt(comptime_int, 1234, i16, 1234);
285285}
286286
287fn expectFloatToInt(comptime F: type, f: F, comptime I: type, i: I) void {
288 expect(@floatToInt(I, f) == i);
287fn expectFloatToInt(comptime F: type, f: F, comptime I: type, i: I) !void {
288 try expect(@floatToInt(I, f) == i);
289289}
290290
291291test "cast u128 to f128 and back" {
292 comptime testCast128();
293 testCast128();
292 comptime try testCast128();
293 try testCast128();
294294}
295295
296fn testCast128() void {
297 expect(cast128Int(cast128Float(0x7fff0000000000000000000000000000)) == 0x7fff0000000000000000000000000000);
296fn testCast128() !void {
297 try expect(cast128Int(cast128Float(0x7fff0000000000000000000000000000)) == 0x7fff0000000000000000000000000000);
298298}
299299
300300fn cast128Int(x: f128) u128 {
......@@ -306,69 +306,69 @@ fn cast128Float(x: u128) f128 {
306306}
307307
308308test "single-item pointer of array to slice and to unknown length pointer" {
309 testCastPtrOfArrayToSliceAndPtr();
310 comptime testCastPtrOfArrayToSliceAndPtr();
309 try testCastPtrOfArrayToSliceAndPtr();
310 comptime try testCastPtrOfArrayToSliceAndPtr();
311311}
312312
313fn testCastPtrOfArrayToSliceAndPtr() void {
313fn testCastPtrOfArrayToSliceAndPtr() !void {
314314 {
315315 var array = "aoeu".*;
316316 const x: [*]u8 = &array;
317317 x[0] += 1;
318 expect(mem.eql(u8, array[0..], "boeu"));
318 try expect(mem.eql(u8, array[0..], "boeu"));
319319 const y: []u8 = &array;
320320 y[0] += 1;
321 expect(mem.eql(u8, array[0..], "coeu"));
321 try expect(mem.eql(u8, array[0..], "coeu"));
322322 }
323323 {
324324 var array: [4]u8 = "aoeu".*;
325325 const x: [*]u8 = &array;
326326 x[0] += 1;
327 expect(mem.eql(u8, array[0..], "boeu"));
327 try expect(mem.eql(u8, array[0..], "boeu"));
328328 const y: []u8 = &array;
329329 y[0] += 1;
330 expect(mem.eql(u8, array[0..], "coeu"));
330 try expect(mem.eql(u8, array[0..], "coeu"));
331331 }
332332}
333333
334334test "cast *[1][*]const u8 to [*]const ?[*]const u8" {
335335 const window_name = [1][*]const u8{"window name"};
336336 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"));
338338}
339339
340340test "@intCast comptime_int" {
341341 const result = @intCast(i32, 1234);
342 expect(@TypeOf(result) == i32);
343 expect(result == 1234);
342 try expect(@TypeOf(result) == i32);
343 try expect(result == 1234);
344344}
345345
346346test "@floatCast comptime_int and comptime_float" {
347347 {
348348 const result = @floatCast(f16, 1234);
349 expect(@TypeOf(result) == f16);
350 expect(result == 1234.0);
349 try expect(@TypeOf(result) == f16);
350 try expect(result == 1234.0);
351351 }
352352 {
353353 const result = @floatCast(f16, 1234.0);
354 expect(@TypeOf(result) == f16);
355 expect(result == 1234.0);
354 try expect(@TypeOf(result) == f16);
355 try expect(result == 1234.0);
356356 }
357357 {
358358 const result = @floatCast(f32, 1234);
359 expect(@TypeOf(result) == f32);
360 expect(result == 1234.0);
359 try expect(@TypeOf(result) == f32);
360 try expect(result == 1234.0);
361361 }
362362 {
363363 const result = @floatCast(f32, 1234.0);
364 expect(@TypeOf(result) == f32);
365 expect(result == 1234.0);
364 try expect(@TypeOf(result) == f32);
365 try expect(result == 1234.0);
366366 }
367367}
368368
369369test "vector casts" {
370370 const S = struct {
371 fn doTheTest() void {
371 fn doTheTest() !void {
372372 // Upcast (implicit, equivalent to @intCast)
373373 var up0: Vector(2, u8) = [_]u8{ 0x55, 0xaa };
374374 var up1 = @as(Vector(2, u16), up0);
......@@ -380,55 +380,55 @@ test "vector casts" {
380380 var down2 = @intCast(Vector(2, u16), down0);
381381 var down3 = @intCast(Vector(2, u8), down0);
382382
383 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 }));
385 expect(mem.eql(u64, &@as([2]u64, up3), &[2]u64{ 0x55, 0xaa }));
383 try expect(mem.eql(u16, &@as([2]u16, up1), &[2]u16{ 0x55, 0xaa }));
384 try expect(mem.eql(u32, &@as([2]u32, up2), &[2]u32{ 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 }));
388 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 }));
387 try expect(mem.eql(u32, &@as([2]u32, down1), &[2]u32{ 0x55, 0xaa }));
388 try expect(mem.eql(u16, &@as([2]u16, down2), &[2]u16{ 0x55, 0xaa }));
389 try expect(mem.eql(u8, &@as([2]u8, down3), &[2]u8{ 0x55, 0xaa }));
390390 }
391391
392 fn doTheTestFloat() void {
392 fn doTheTestFloat() !void {
393393 var vec = @splat(2, @as(f32, 1234.0));
394394 var wider: Vector(2, f64) = vec;
395 expect(wider[0] == 1234.0);
396 expect(wider[1] == 1234.0);
395 try expect(wider[0] == 1234.0);
396 try expect(wider[1] == 1234.0);
397397 }
398398 };
399399
400 S.doTheTest();
401 comptime S.doTheTest();
402 S.doTheTestFloat();
403 comptime S.doTheTestFloat();
400 try S.doTheTest();
401 comptime try S.doTheTest();
402 try S.doTheTestFloat();
403 comptime try S.doTheTestFloat();
404404}
405405
406406test "comptime_int @intToFloat" {
407407 {
408408 const result = @intToFloat(f16, 1234);
409 expect(@TypeOf(result) == f16);
410 expect(result == 1234.0);
409 try expect(@TypeOf(result) == f16);
410 try expect(result == 1234.0);
411411 }
412412 {
413413 const result = @intToFloat(f32, 1234);
414 expect(@TypeOf(result) == f32);
415 expect(result == 1234.0);
414 try expect(@TypeOf(result) == f32);
415 try expect(result == 1234.0);
416416 }
417417 {
418418 const result = @intToFloat(f64, 1234);
419 expect(@TypeOf(result) == f64);
420 expect(result == 1234.0);
419 try expect(@TypeOf(result) == f64);
420 try expect(result == 1234.0);
421421 }
422422 {
423423 const result = @intToFloat(f128, 1234);
424 expect(@TypeOf(result) == f128);
425 expect(result == 1234.0);
424 try expect(@TypeOf(result) == f128);
425 try expect(result == 1234.0);
426426 }
427427 // big comptime_int (> 64 bits) to f128 conversion
428428 {
429429 const result = @intToFloat(f128, 0x1_0000_0000_0000_0000);
430 expect(@TypeOf(result) == f128);
431 expect(result == 0x1_0000_0000_0000_0000.0);
430 try expect(@TypeOf(result) == f128);
431 try expect(result == 0x1_0000_0000_0000_0000.0);
432432 }
433433}
434434
......@@ -436,25 +436,25 @@ test "@intCast i32 to u7" {
436436 var x: u128 = maxInt(u128);
437437 var y: i32 = 120;
438438 var z = x >> @intCast(u7, y);
439 expect(z == 0xff);
439 try expect(z == 0xff);
440440}
441441
442442test "@floatCast cast down" {
443443 {
444444 var double: f64 = 0.001534;
445445 var single = @floatCast(f32, double);
446 expect(single == 0.001534);
446 try expect(single == 0.001534);
447447 }
448448 {
449449 const double: f64 = 0.001534;
450450 const single = @floatCast(f32, double);
451 expect(single == 0.001534);
451 try expect(single == 0.001534);
452452 }
453453}
454454
455455test "implicit cast undefined to optional" {
456 expect(MakeType(void).getNull() == null);
457 expect(MakeType(void).getNonNull() != null);
456 try expect(MakeType(void).getNull() == null);
457 try expect(MakeType(void).getNonNull() != null);
458458}
459459
460460fn MakeType(comptime T: type) type {
......@@ -474,26 +474,26 @@ test "implicit cast from *[N]T to ?[*]T" {
474474 var y: [4]u16 = [4]u16{ 0, 1, 2, 3 };
475475
476476 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]));
478478 x.?[0] = 8;
479479 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]));
481481}
482482
483483test "implicit cast from *[N]T to [*c]T" {
484484 var x: [4]u16 = [4]u16{ 0, 1, 2, 3 };
485485 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]));
488488 x[0] = 8;
489489 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]));
491491}
492492
493493test "implicit cast from *T to ?*c_void" {
494494 var a: u8 = 1;
495495 incrementVoidPtrValue(&a);
496 std.testing.expect(a == 2);
496 try std.testing.expect(a == 2);
497497}
498498
499499fn incrementVoidPtrValue(value: ?*c_void) void {
......@@ -504,7 +504,7 @@ test "implicit cast from [*]T to ?*c_void" {
504504 var a = [_]u8{ 3, 2, 1 };
505505 var runtime_zero: usize = 0;
506506 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 }));
508508}
509509
510510fn incrementVoidPtrArray(array: ?*c_void, len: usize) void {
......@@ -521,34 +521,34 @@ test "*usize to *void" {
521521}
522522
523523test "compile time int to ptr of function" {
524 foobar(FUNCTION_CONSTANT);
524 try foobar(FUNCTION_CONSTANT);
525525}
526526
527527pub const FUNCTION_CONSTANT = @intToPtr(PFN_void, maxInt(usize));
528528pub const PFN_void = fn (*c_void) callconv(.C) void;
529529
530fn foobar(func: PFN_void) void {
531 std.testing.expect(@ptrToInt(func) == maxInt(usize));
530fn foobar(func: PFN_void) !void {
531 try std.testing.expect(@ptrToInt(func) == maxInt(usize));
532532}
533533
534534test "implicit ptr to *c_void" {
535535 var a: u32 = 1;
536536 var ptr: *align(@alignOf(u32)) c_void = &a;
537537 var b: *u32 = @ptrCast(*u32, ptr);
538 expect(b.* == 1);
538 try expect(b.* == 1);
539539 var ptr2: ?*align(@alignOf(u32)) c_void = &a;
540540 var c: *u32 = @ptrCast(*u32, ptr2.?);
541 expect(c.* == 1);
541 try expect(c.* == 1);
542542}
543543
544544test "@intCast to comptime_int" {
545 expect(@intCast(comptime_int, 0) == 0);
545 try expect(@intCast(comptime_int, 0) == 0);
546546}
547547
548548test "implicit cast comptime numbers to any type when the value fits" {
549549 const a: u64 = 255;
550550 var b: u8 = a;
551 expect(b == 255);
551 try expect(b == 255);
552552}
553553
554554test "@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" {
556556 A,
557557 };
558558 const x = @intToEnum(E, 0);
559 expect(x == E.A);
559 try expect(x == E.A);
560560}
561561
562562test "@intToEnum runtime to an extern enum with duplicate values" {
......@@ -566,33 +566,33 @@ test "@intToEnum runtime to an extern enum with duplicate values" {
566566 };
567567 var a: u8 = 1;
568568 var x = @intToEnum(E, a);
569 expect(x == E.A);
570 expect(x == E.B);
569 try expect(x == E.A);
570 try expect(x == E.B);
571571}
572572
573573test "@intCast to u0 and use the result" {
574574 const S = struct {
575 fn doTheTest(zero: u1, one: u1, bigzero: i32) void {
576 expect((one << @intCast(u0, bigzero)) == 1);
577 expect((zero << @intCast(u0, bigzero)) == 0);
575 fn doTheTest(zero: u1, one: u1, bigzero: i32) !void {
576 try expect((one << @intCast(u0, bigzero)) == 1);
577 try expect((zero << @intCast(u0, bigzero)) == 0);
578578 }
579579 };
580 S.doTheTest(0, 1, 0);
581 comptime S.doTheTest(0, 1, 0);
580 try S.doTheTest(0, 1, 0);
581 comptime try S.doTheTest(0, 1, 0);
582582}
583583
584584test "peer type resolution: unreachable, null, slice" {
585585 const S = struct {
586 fn doTheTest(num: usize, word: []const u8) void {
586 fn doTheTest(num: usize, word: []const u8) !void {
587587 const result = switch (num) {
588588 0 => null,
589589 1 => word,
590590 else => unreachable,
591591 };
592 expect(mem.eql(u8, result.?, "hi"));
592 try expect(mem.eql(u8, result.?, "hi"));
593593 }
594594 };
595 S.doTheTest(1, "hi");
595 try S.doTheTest(1, "hi");
596596}
597597
598598test "peer type resolution: unreachable, error set, unreachable" {
......@@ -615,17 +615,17 @@ test "peer type resolution: unreachable, error set, unreachable" {
615615 error.FileDescriptorIncompatibleWithEpoll => unreachable,
616616 error.Unexpected => unreachable,
617617 };
618 expect(transformed_err == error.SystemResources);
618 try expect(transformed_err == error.SystemResources);
619619}
620620
621621test "implicit cast comptime_int to comptime_float" {
622 comptime expect(@as(comptime_float, 10) == @as(f32, 10));
623 expect(2 == 2.0);
622 comptime try expect(@as(comptime_float, 10) == @as(f32, 10));
623 try expect(2 == 2.0);
624624}
625625
626626test "implicit cast *[0]T to E![]const u8" {
627627 var x = @as(anyerror![]const u8, &[0]u8{});
628 expect((x catch unreachable).len == 0);
628 try expect((x catch unreachable).len == 0);
629629}
630630
631631test "peer cast *[0]T to E![]const T" {
......@@ -633,7 +633,7 @@ test "peer cast *[0]T to E![]const T" {
633633 var buf: anyerror![]const u8 = buffer[0..];
634634 var b = false;
635635 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));
637637}
638638
639639test "peer cast *[0]T to []const T" {
......@@ -641,25 +641,25 @@ test "peer cast *[0]T to []const T" {
641641 var buf: []const u8 = buffer[0..];
642642 var b = false;
643643 var y = if (b) &[0]u8{} else buf;
644 expect(mem.eql(u8, "abcde", y));
644 try expect(mem.eql(u8, "abcde", y));
645645}
646646
647647var global_array: [4]u8 = undefined;
648648test "cast from array reference to fn" {
649649 const f = @ptrCast(fn () callconv(.C) void, &global_array);
650 expect(@ptrToInt(f) == @ptrToInt(&global_array));
650 try expect(@ptrToInt(f) == @ptrToInt(&global_array));
651651}
652652
653653test "*const [N]null u8 to ?[]const u8" {
654654 const S = struct {
655 fn doTheTest() void {
655 fn doTheTest() !void {
656656 var a = "Hello";
657657 var b: ?[]const u8 = a;
658 expect(mem.eql(u8, b.?, "Hello"));
658 try expect(mem.eql(u8, b.?, "Hello"));
659659 }
660660 };
661 S.doTheTest();
662 comptime S.doTheTest();
661 try S.doTheTest();
662 comptime try S.doTheTest();
663663}
664664
665665test "peer resolution of string literals" {
......@@ -671,54 +671,54 @@ test "peer resolution of string literals" {
671671 d,
672672 };
673673
674 fn doTheTest(e: E) void {
674 fn doTheTest(e: E) !void {
675675 const cmd = switch (e) {
676676 .a => "one",
677677 .b => "two",
678678 .c => "three",
679679 .d => "four",
680680 };
681 expect(mem.eql(u8, cmd, "two"));
681 try expect(mem.eql(u8, cmd, "two"));
682682 }
683683 };
684 S.doTheTest(.b);
685 comptime S.doTheTest(.b);
684 try S.doTheTest(.b);
685 comptime try S.doTheTest(.b);
686686}
687687
688688test "type coercion related to sentinel-termination" {
689689 const S = struct {
690 fn doTheTest() void {
690 fn doTheTest() !void {
691691 // [:x]T to []T
692692 {
693693 var array = [4:0]i32{ 1, 2, 3, 4 };
694694 var slice: [:0]i32 = &array;
695695 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 }));
697697 }
698698
699699 // [*:x]T to [*]T
700700 {
701701 var array = [4:99]i32{ 1, 2, 3, 4 };
702702 var dest: [*]i32 = &array;
703 expect(dest[0] == 1);
704 expect(dest[1] == 2);
705 expect(dest[2] == 3);
706 expect(dest[3] == 4);
707 expect(dest[4] == 99);
703 try expect(dest[0] == 1);
704 try expect(dest[1] == 2);
705 try expect(dest[2] == 3);
706 try expect(dest[3] == 4);
707 try expect(dest[4] == 99);
708708 }
709709
710710 // [N:x]T to [N]T
711711 {
712712 var array = [4:0]i32{ 1, 2, 3, 4 };
713713 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 }));
715715 }
716716
717717 // *[N:x]T to *[N]T
718718 {
719719 var array = [4:0]i32{ 1, 2, 3, 4 };
720720 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 }));
722722 }
723723
724724 // [:x]T to [*:x]T
......@@ -726,24 +726,24 @@ test "type coercion related to sentinel-termination" {
726726 var array = [4:0]i32{ 1, 2, 3, 4 };
727727 var slice: [:0]i32 = &array;
728728 var dest: [*:0]i32 = slice;
729 expect(dest[0] == 1);
730 expect(dest[1] == 2);
731 expect(dest[2] == 3);
732 expect(dest[3] == 4);
733 expect(dest[4] == 0);
729 try expect(dest[0] == 1);
730 try expect(dest[1] == 2);
731 try expect(dest[2] == 3);
732 try expect(dest[3] == 4);
733 try expect(dest[4] == 0);
734734 }
735735 }
736736 };
737 S.doTheTest();
738 comptime S.doTheTest();
737 try S.doTheTest();
738 comptime try S.doTheTest();
739739}
740740
741741test "cast i8 fn call peers to i32 result" {
742742 const S = struct {
743 fn doTheTest() void {
743 fn doTheTest() !void {
744744 var cond = true;
745745 const value: i32 = if (cond) smallBoi() else bigBoi();
746 expect(value == 123);
746 try expect(value == 123);
747747 }
748748 fn smallBoi() i8 {
749749 return 123;
......@@ -752,21 +752,21 @@ test "cast i8 fn call peers to i32 result" {
752752 return 1234;
753753 }
754754 };
755 S.doTheTest();
756 comptime S.doTheTest();
755 try S.doTheTest();
756 comptime try S.doTheTest();
757757}
758758
759759test "return u8 coercing into ?u32 return type" {
760760 const S = struct {
761 fn doTheTest() void {
762 expect(foo(123).? == 123);
761 fn doTheTest() !void {
762 try expect(foo(123).? == 123);
763763 }
764764 fn foo(arg: u8) ?u32 {
765765 return arg;
766766 }
767767 };
768 S.doTheTest();
769 comptime S.doTheTest();
768 try S.doTheTest();
769 comptime try S.doTheTest();
770770}
771771
772772test "peer result null and comptime_int" {
......@@ -782,17 +782,17 @@ test "peer result null and comptime_int" {
782782 }
783783 };
784784
785 expect(S.blah(0) == null);
786 comptime expect(S.blah(0) == null);
787 expect(S.blah(10).? == 1);
788 comptime expect(S.blah(10).? == 1);
789 expect(S.blah(-10).? == -1);
790 comptime expect(S.blah(-10).? == -1);
785 try expect(S.blah(0) == null);
786 comptime try expect(S.blah(0) == null);
787 try expect(S.blah(10).? == 1);
788 comptime try expect(S.blah(10).? == 1);
789 try expect(S.blah(-10).? == -1);
790 comptime try expect(S.blah(-10).? == -1);
791791}
792792
793793test "peer type resolution implicit cast to return type" {
794794 const S = struct {
795 fn doTheTest() void {
795 fn doTheTest() !void {
796796 for ("hello") |c| _ = f(c);
797797 }
798798 fn f(c: u8) []const u8 {
......@@ -803,13 +803,13 @@ test "peer type resolution implicit cast to return type" {
803803 };
804804 }
805805 };
806 S.doTheTest();
807 comptime S.doTheTest();
806 try S.doTheTest();
807 comptime try S.doTheTest();
808808}
809809
810810test "peer type resolution implicit cast to variable type" {
811811 const S = struct {
812 fn doTheTest() void {
812 fn doTheTest() !void {
813813 var x: []const u8 = undefined;
814814 for ("hello") |c| x = switch (c) {
815815 'h', 'e' => &[_]u8{c}, // should cast to slice
......@@ -818,14 +818,14 @@ test "peer type resolution implicit cast to variable type" {
818818 };
819819 }
820820 };
821 S.doTheTest();
822 comptime S.doTheTest();
821 try S.doTheTest();
822 comptime try S.doTheTest();
823823}
824824
825825test "variable initialization uses result locations properly with regards to the type" {
826826 var b = true;
827827 const x: i32 = if (b) 1 else 2;
828 expect(x == 1);
828 try expect(x == 1);
829829}
830830
831831test "cast between [*c]T and ?[*:0]T on fn parameter" {
......@@ -847,27 +847,27 @@ test "cast between C pointer with different but compatible types" {
847847 fn foo(arg: [*]c_ushort) u16 {
848848 return arg[0];
849849 }
850 fn doTheTest() void {
850 fn doTheTest() !void {
851851 var x = [_]u16{ 4, 2, 1, 3 };
852 expect(foo(@ptrCast([*]u16, &x)) == 4);
852 try expect(foo(@ptrCast([*]u16, &x)) == 4);
853853 }
854854 };
855 S.doTheTest();
855 try S.doTheTest();
856856}
857857
858858var global_struct: struct { f0: usize } = undefined;
859859
860860test "assignment to optional pointer result loc" {
861861 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));
863863}
864864
865865test "peer type resolve string lit with sentinel-terminated mutable slice" {
866866 var array: [4:0]u8 = undefined;
867867 array[4] = 0; // TODO remove this when #4372 is solved
868868 var slice: [:0]u8 = array[0..4 :0];
869 comptime expect(@TypeOf(slice, "hi") == [:0]const u8);
870 comptime expect(@TypeOf("hi", slice) == [:0]const u8);
869 comptime try expect(@TypeOf(slice, "hi") == [:0]const u8);
870 comptime try expect(@TypeOf("hi", slice) == [:0]const u8);
871871}
872872
873873test "peer type unsigned int to signed" {
......@@ -875,15 +875,15 @@ test "peer type unsigned int to signed" {
875875 var x: u8 = 7;
876876 var y: i32 = -5;
877877 var a = w + y + x;
878 comptime expect(@TypeOf(a) == i32);
879 expect(a == 7);
878 comptime try expect(@TypeOf(a) == i32);
879 try expect(a == 7);
880880}
881881
882882test "peer type resolve array pointers, one of them const" {
883883 var array1: [4]u8 = undefined;
884884 const array2: [5]u8 = undefined;
885 comptime expect(@TypeOf(&array1, &array2) == []const u8);
886 comptime expect(@TypeOf(&array2, &array1) == []const u8);
885 comptime try expect(@TypeOf(&array1, &array2) == []const u8);
886 comptime try expect(@TypeOf(&array2, &array1) == []const u8);
887887}
888888
889889test "peer type resolve array pointer and unknown pointer" {
......@@ -892,35 +892,35 @@ test "peer type resolve array pointer and unknown pointer" {
892892 var const_ptr: [*]const u8 = undefined;
893893 var ptr: [*]u8 = undefined;
894894
895 comptime expect(@TypeOf(&array, ptr) == [*]u8);
896 comptime expect(@TypeOf(ptr, &array) == [*]u8);
895 comptime try expect(@TypeOf(&array, ptr) == [*]u8);
896 comptime try expect(@TypeOf(ptr, &array) == [*]u8);
897897
898 comptime expect(@TypeOf(&const_array, ptr) == [*]const u8);
899 comptime expect(@TypeOf(ptr, &const_array) == [*]const u8);
898 comptime try expect(@TypeOf(&const_array, ptr) == [*]const u8);
899 comptime try expect(@TypeOf(ptr, &const_array) == [*]const u8);
900900
901 comptime expect(@TypeOf(&array, const_ptr) == [*]const u8);
902 comptime expect(@TypeOf(const_ptr, &array) == [*]const u8);
901 comptime try expect(@TypeOf(&array, const_ptr) == [*]const u8);
902 comptime try expect(@TypeOf(const_ptr, &array) == [*]const u8);
903903
904 comptime expect(@TypeOf(&const_array, const_ptr) == [*]const u8);
905 comptime expect(@TypeOf(const_ptr, &const_array) == [*]const u8);
904 comptime try expect(@TypeOf(&const_array, const_ptr) == [*]const u8);
905 comptime try expect(@TypeOf(const_ptr, &const_array) == [*]const u8);
906906}
907907
908908test "comptime float casts" {
909909 const a = @intToFloat(comptime_float, 1);
910 expect(a == 1);
911 expect(@TypeOf(a) == comptime_float);
910 try expect(a == 1);
911 try expect(@TypeOf(a) == comptime_float);
912912 const b = @floatToInt(comptime_int, 2);
913 expect(b == 2);
914 expect(@TypeOf(b) == comptime_int);
913 try expect(b == 2);
914 try expect(@TypeOf(b) == comptime_int);
915915}
916916
917917test "cast from ?[*]T to ??[*]T" {
918918 const a: ??[*]u8 = @as(?[*]u8, null);
919 expect(a != null and a.? == null);
919 try expect(a != null and a.? == null);
920920}
921921
922922test "cast between *[N]void and []void" {
923923 var a: [4]void = undefined;
924924 var b: []void = &a;
925 expect(b.len == 4);
925 try expect(b.len == 4);
926926}
test/stage1/behavior/const_slice_child.zig+8-8
......@@ -12,24 +12,24 @@ test "const slice child" {
1212 "three",
1313 };
1414 argv = &strs;
15 bar(strs.len);
15 try bar(strs.len);
1616}
1717
18fn foo(args: [][]const u8) void {
19 expect(args.len == 3);
20 expect(streql(args[0], "one"));
21 expect(streql(args[1], "two"));
22 expect(streql(args[2], "three"));
18fn foo(args: [][]const u8) !void {
19 try expect(args.len == 3);
20 try expect(streql(args[0], "one"));
21 try expect(streql(args[1], "two"));
22 try expect(streql(args[2], "three"));
2323}
2424
25fn bar(argc: usize) void {
25fn bar(argc: usize) !void {
2626 const args = testing.allocator.alloc([]const u8, argc) catch unreachable;
2727 defer testing.allocator.free(args);
2828 for (args) |_, i| {
2929 const ptr = argv[i];
3030 args[i] = ptr[0..strlen(ptr)];
3131 }
32 foo(args);
32 try foo(args);
3333}
3434
3535fn strlen(ptr: [*]const u8) usize {
test/stage1/behavior/defer.zig+20-20
......@@ -24,18 +24,18 @@ fn runSomeErrorDefers(x: bool) !bool {
2424}
2525
2626test "mixing normal and error defers" {
27 expect(runSomeErrorDefers(true) catch unreachable);
28 expect(result[0] == 'c');
29 expect(result[1] == 'a');
27 try expect(runSomeErrorDefers(true) catch unreachable);
28 try expect(result[0] == 'c');
29 try expect(result[1] == 'a');
3030
3131 const ok = runSomeErrorDefers(false) catch |err| x: {
32 expect(err == error.FalseNotAllowed);
32 try expect(err == error.FalseNotAllowed);
3333 break :x true;
3434 };
35 expect(ok);
36 expect(result[0] == 'c');
37 expect(result[1] == 'b');
38 expect(result[2] == 'a');
35 try expect(ok);
36 try expect(result[0] == 'c');
37 try expect(result[1] == 'b');
38 try expect(result[2] == 'a');
3939}
4040
4141test "break and continue inside loop inside defer expression" {
......@@ -50,7 +50,7 @@ fn testBreakContInDefer(x: usize) void {
5050 if (i < 5) continue;
5151 if (i == 5) break;
5252 }
53 expect(i == 5);
53 expect(i == 5) catch @panic("test failure");
5454 }
5555}
5656
......@@ -62,11 +62,11 @@ test "defer and labeled break" {
6262 break :blk;
6363 }
6464
65 expect(i == 1);
65 try expect(i == 1);
6666}
6767
6868test "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);
7070}
7171
7272fn testNestedFnErrDefer() anyerror!void {
......@@ -82,8 +82,8 @@ fn testNestedFnErrDefer() anyerror!void {
8282
8383test "return variable while defer expression in scope to modify it" {
8484 const S = struct {
85 fn doTheTest() void {
86 expect(notNull().? == 1);
85 fn doTheTest() !void {
86 try expect(notNull().? == 1);
8787 }
8888
8989 fn notNull() ?u8 {
......@@ -93,22 +93,22 @@ test "return variable while defer expression in scope to modify it" {
9393 }
9494 };
9595
96 S.doTheTest();
97 comptime S.doTheTest();
96 try S.doTheTest();
97 comptime try S.doTheTest();
9898}
9999
100100test "errdefer with payload" {
101101 const S = struct {
102102 fn foo() !i32 {
103103 errdefer |a| {
104 expectEqual(error.One, a);
104 expectEqual(error.One, a) catch @panic("test failure");
105105 }
106106 return error.One;
107107 }
108 fn doTheTest() void {
109 expectError(error.One, foo());
108 fn doTheTest() !void {
109 try expectError(error.One, foo());
110110 }
111111 };
112 S.doTheTest();
113 comptime S.doTheTest();
112 try S.doTheTest();
113 comptime try S.doTheTest();
114114}
test/stage1/behavior/enum.zig+112-112
......@@ -29,41 +29,41 @@ test "non-exhaustive enum" {
2929 b,
3030 _,
3131 };
32 fn doTheTest(y: u8) void {
32 fn doTheTest(y: u8) !void {
3333 var e: E = .b;
34 expect(switch (e) {
34 try expect(switch (e) {
3535 .a => false,
3636 .b => true,
3737 _ => false,
3838 });
3939 e = @intToEnum(E, 12);
40 expect(switch (e) {
40 try expect(switch (e) {
4141 .a => false,
4242 .b => false,
4343 _ => true,
4444 });
4545
46 expect(switch (e) {
46 try expect(switch (e) {
4747 .a => false,
4848 .b => false,
4949 else => true,
5050 });
5151 e = .b;
52 expect(switch (e) {
52 try expect(switch (e) {
5353 .a => false,
5454 else => true,
5555 });
5656
57 expect(@typeInfo(E).Enum.fields.len == 2);
57 try expect(@typeInfo(E).Enum.fields.len == 2);
5858 e = @intToEnum(E, 12);
59 expect(@enumToInt(e) == 12);
59 try expect(@enumToInt(e) == 12);
6060 e = @intToEnum(E, y);
61 expect(@enumToInt(e) == 52);
62 expect(@typeInfo(E).Enum.is_exhaustive == false);
61 try expect(@enumToInt(e) == 52);
62 try expect(@typeInfo(E).Enum.is_exhaustive == false);
6363 }
6464 };
65 S.doTheTest(52);
66 comptime S.doTheTest(52);
65 try S.doTheTest(52);
66 comptime try S.doTheTest(52);
6767}
6868
6969test "empty non-exhaustive enum" {
......@@ -71,19 +71,19 @@ test "empty non-exhaustive enum" {
7171 const E = enum(u8) {
7272 _,
7373 };
74 fn doTheTest(y: u8) void {
74 fn doTheTest(y: u8) !void {
7575 var e = @intToEnum(E, y);
76 expect(switch (e) {
76 try expect(switch (e) {
7777 _ => true,
7878 });
79 expect(@enumToInt(e) == y);
79 try expect(@enumToInt(e) == y);
8080
81 expect(@typeInfo(E).Enum.fields.len == 0);
82 expect(@typeInfo(E).Enum.is_exhaustive == false);
81 try expect(@typeInfo(E).Enum.fields.len == 0);
82 try expect(@typeInfo(E).Enum.is_exhaustive == false);
8383 }
8484 };
85 S.doTheTest(42);
86 comptime S.doTheTest(42);
85 try S.doTheTest(42);
86 comptime try S.doTheTest(42);
8787}
8888
8989test "single field non-exhaustive enum" {
......@@ -92,35 +92,35 @@ test "single field non-exhaustive enum" {
9292 a,
9393 _,
9494 };
95 fn doTheTest(y: u8) void {
95 fn doTheTest(y: u8) !void {
9696 var e: E = .a;
97 expect(switch (e) {
97 try expect(switch (e) {
9898 .a => true,
9999 _ => false,
100100 });
101101 e = @intToEnum(E, 12);
102 expect(switch (e) {
102 try expect(switch (e) {
103103 .a => false,
104104 _ => true,
105105 });
106106
107 expect(switch (e) {
107 try expect(switch (e) {
108108 .a => false,
109109 else => true,
110110 });
111111 e = .a;
112 expect(switch (e) {
112 try expect(switch (e) {
113113 .a => true,
114114 else => false,
115115 });
116116
117 expect(@enumToInt(@intToEnum(E, y)) == y);
118 expect(@typeInfo(E).Enum.fields.len == 1);
119 expect(@typeInfo(E).Enum.is_exhaustive == false);
117 try expect(@enumToInt(@intToEnum(E, y)) == y);
118 try expect(@typeInfo(E).Enum.fields.len == 1);
119 try expect(@typeInfo(E).Enum.is_exhaustive == false);
120120 }
121121 };
122 S.doTheTest(23);
123 comptime S.doTheTest(23);
122 try S.doTheTest(23);
123 comptime try S.doTheTest(23);
124124}
125125
126126test "enum type" {
......@@ -133,16 +133,16 @@ test "enum type" {
133133 };
134134 const bar = Bar.B;
135135
136 expect(bar == Bar.B);
137 expect(@typeInfo(Foo).Union.fields.len == 3);
138 expect(@typeInfo(Bar).Enum.fields.len == 4);
139 expect(@sizeOf(Foo) == @sizeOf(FooNoVoid));
140 expect(@sizeOf(Bar) == 1);
136 try expect(bar == Bar.B);
137 try expect(@typeInfo(Foo).Union.fields.len == 3);
138 try expect(@typeInfo(Bar).Enum.fields.len == 4);
139 try expect(@sizeOf(Foo) == @sizeOf(FooNoVoid));
140 try expect(@sizeOf(Bar) == 1);
141141}
142142
143143test "enum as return value" {
144144 switch (returnAnInt(13)) {
145 Foo.One => |value| expect(value == 13),
145 Foo.One => |value| try expect(value == 13),
146146 else => unreachable,
147147 }
148148}
......@@ -206,22 +206,22 @@ const Number = enum {
206206};
207207
208208test "enum to int" {
209 shouldEqual(Number.Zero, 0);
210 shouldEqual(Number.One, 1);
211 shouldEqual(Number.Two, 2);
212 shouldEqual(Number.Three, 3);
213 shouldEqual(Number.Four, 4);
209 try shouldEqual(Number.Zero, 0);
210 try shouldEqual(Number.One, 1);
211 try shouldEqual(Number.Two, 2);
212 try shouldEqual(Number.Three, 3);
213 try shouldEqual(Number.Four, 4);
214214}
215215
216fn shouldEqual(n: Number, expected: u3) void {
217 expect(@enumToInt(n) == expected);
216fn shouldEqual(n: Number, expected: u3) !void {
217 try expect(@enumToInt(n) == expected);
218218}
219219
220220test "int to enum" {
221 testIntToEnumEval(3);
221 try testIntToEnumEval(3);
222222}
223fn testIntToEnumEval(x: i32) void {
224 expect(@intToEnum(IntToEnumNumber, @intCast(u3, x)) == IntToEnumNumber.Three);
223fn testIntToEnumEval(x: i32) !void {
224 try expect(@intToEnum(IntToEnumNumber, @intCast(u3, x)) == IntToEnumNumber.Three);
225225}
226226const IntToEnumNumber = enum {
227227 Zero,
......@@ -232,18 +232,18 @@ const IntToEnumNumber = enum {
232232};
233233
234234test "@tagName" {
235 expect(mem.eql(u8, testEnumTagNameBare(BareNumber.Three), "Three"));
236 comptime expect(mem.eql(u8, testEnumTagNameBare(BareNumber.Three), "Three"));
235 try expect(mem.eql(u8, testEnumTagNameBare(BareNumber.Three), "Three"));
236 comptime try expect(mem.eql(u8, testEnumTagNameBare(BareNumber.Three), "Three"));
237237}
238238
239239test "@tagName extern enum with duplicates" {
240 expect(mem.eql(u8, testEnumTagNameBare(ExternDuplicates.B), "A"));
241 comptime expect(mem.eql(u8, testEnumTagNameBare(ExternDuplicates.B), "A"));
240 try expect(mem.eql(u8, testEnumTagNameBare(ExternDuplicates.B), "A"));
241 comptime try expect(mem.eql(u8, testEnumTagNameBare(ExternDuplicates.B), "A"));
242242}
243243
244244test "@tagName non-exhaustive enum" {
245 expect(mem.eql(u8, testEnumTagNameBare(NonExhaustive.B), "B"));
246 comptime expect(mem.eql(u8, testEnumTagNameBare(NonExhaustive.B), "B"));
245 try expect(mem.eql(u8, testEnumTagNameBare(NonExhaustive.B), "B"));
246 comptime try expect(mem.eql(u8, testEnumTagNameBare(NonExhaustive.B), "B"));
247247}
248248
249249fn testEnumTagNameBare(n: anytype) []const u8 {
......@@ -269,8 +269,8 @@ const NonExhaustive = enum(u8) {
269269
270270test "enum alignment" {
271271 comptime {
272 expect(@alignOf(AlignTestEnum) >= @alignOf([9]u8));
273 expect(@alignOf(AlignTestEnum) >= @alignOf(u64));
272 try expect(@alignOf(AlignTestEnum) >= @alignOf([9]u8));
273 try expect(@alignOf(AlignTestEnum) >= @alignOf(u64));
274274 }
275275}
276276
......@@ -806,10 +806,10 @@ const ValueCount257 = enum {
806806
807807test "enum sizes" {
808808 comptime {
809 expect(@sizeOf(ValueCount1) == 0);
810 expect(@sizeOf(ValueCount2) == 1);
811 expect(@sizeOf(ValueCount256) == 1);
812 expect(@sizeOf(ValueCount257) == 2);
809 try expect(@sizeOf(ValueCount1) == 0);
810 try expect(@sizeOf(ValueCount2) == 1);
811 try expect(@sizeOf(ValueCount256) == 1);
812 try expect(@sizeOf(ValueCount257) == 2);
813813 }
814814}
815815
......@@ -828,12 +828,12 @@ test "set enum tag type" {
828828 {
829829 var x = Small.One;
830830 x = Small.Two;
831 comptime expect(Tag(Small) == u2);
831 comptime try expect(Tag(Small) == u2);
832832 }
833833 {
834834 var x = Small2.One;
835835 x = Small2.Two;
836 comptime expect(Tag(Small2) == u2);
836 comptime try expect(Tag(Small2) == u2);
837837 }
838838}
839839
......@@ -880,17 +880,17 @@ const bit_field_1 = BitFieldOfEnums{
880880
881881test "bit field access with enum fields" {
882882 var data = bit_field_1;
883 expect(getA(&data) == A.Two);
884 expect(getB(&data) == B.Three3);
885 expect(getC(&data) == C.Four4);
886 comptime expect(@sizeOf(BitFieldOfEnums) == 1);
883 try expect(getA(&data) == A.Two);
884 try expect(getB(&data) == B.Three3);
885 try expect(getC(&data) == C.Four4);
886 comptime try expect(@sizeOf(BitFieldOfEnums) == 1);
887887
888888 data.b = B.Four3;
889 expect(data.b == B.Four3);
889 try expect(data.b == B.Four3);
890890
891891 data.a = A.Three;
892 expect(data.a == A.Three);
893 expect(data.b == B.Four3);
892 try expect(data.a == A.Three);
893 try expect(data.b == B.Four3);
894894}
895895
896896fn getA(data: *const BitFieldOfEnums) A {
......@@ -906,12 +906,12 @@ fn getC(data: *const BitFieldOfEnums) C {
906906}
907907
908908test "casting enum to its tag type" {
909 testCastEnumTag(Small2.Two);
910 comptime testCastEnumTag(Small2.Two);
909 try testCastEnumTag(Small2.Two);
910 comptime try testCastEnumTag(Small2.Two);
911911}
912912
913fn testCastEnumTag(value: Small2) void {
914 expect(@enumToInt(value) == 1);
913fn testCastEnumTag(value: Small2) !void {
914 try expect(@enumToInt(value) == 1);
915915}
916916
917917const MultipleChoice = enum(u32) {
......@@ -922,13 +922,13 @@ const MultipleChoice = enum(u32) {
922922};
923923
924924test "enum with specified tag values" {
925 testEnumWithSpecifiedTagValues(MultipleChoice.C);
926 comptime testEnumWithSpecifiedTagValues(MultipleChoice.C);
925 try testEnumWithSpecifiedTagValues(MultipleChoice.C);
926 comptime try testEnumWithSpecifiedTagValues(MultipleChoice.C);
927927}
928928
929fn testEnumWithSpecifiedTagValues(x: MultipleChoice) void {
930 expect(@enumToInt(x) == 60);
931 expect(1234 == switch (x) {
929fn testEnumWithSpecifiedTagValues(x: MultipleChoice) !void {
930 try expect(@enumToInt(x) == 60);
931 try expect(1234 == switch (x) {
932932 MultipleChoice.A => 1,
933933 MultipleChoice.B => 2,
934934 MultipleChoice.C => @as(u32, 1234),
......@@ -949,13 +949,13 @@ const MultipleChoice2 = enum(u32) {
949949};
950950
951951test "enum with specified and unspecified tag values" {
952 testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2.D);
953 comptime testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2.D);
952 try testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2.D);
953 comptime try testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2.D);
954954}
955955
956fn testEnumWithSpecifiedAndUnspecifiedTagValues(x: MultipleChoice2) void {
957 expect(@enumToInt(x) == 1000);
958 expect(1234 == switch (x) {
956fn testEnumWithSpecifiedAndUnspecifiedTagValues(x: MultipleChoice2) !void {
957 try expect(@enumToInt(x) == 1000);
958 try expect(1234 == switch (x) {
959959 MultipleChoice2.A => 1,
960960 MultipleChoice2.B => 2,
961961 MultipleChoice2.C => 3,
......@@ -969,8 +969,8 @@ fn testEnumWithSpecifiedAndUnspecifiedTagValues(x: MultipleChoice2) void {
969969}
970970
971971test "cast integer literal to enum" {
972 expect(@intToEnum(MultipleChoice2, 0) == MultipleChoice2.Unspecified1);
973 expect(@intToEnum(MultipleChoice2, 40) == MultipleChoice2.B);
972 try expect(@intToEnum(MultipleChoice2, 0) == MultipleChoice2.Unspecified1);
973 try expect(@intToEnum(MultipleChoice2, 40) == MultipleChoice2.B);
974974}
975975
976976const EnumWithOneMember = enum {
......@@ -1008,14 +1008,14 @@ const EnumWithTagValues = enum(u4) {
10081008 D = 1 << 3,
10091009};
10101010test "enum with tag values don't require parens" {
1011 expect(@enumToInt(EnumWithTagValues.C) == 0b0100);
1011 try expect(@enumToInt(EnumWithTagValues.C) == 0b0100);
10121012}
10131013
10141014test "enum with 1 field but explicit tag type should still have the tag type" {
10151015 const Enum = enum(u8) {
10161016 B = 2,
10171017 };
1018 comptime @import("std").testing.expect(@sizeOf(Enum) == @sizeOf(u8));
1018 comptime try expect(@sizeOf(Enum) == @sizeOf(u8));
10191019}
10201020
10211021test "empty extern enum with members" {
......@@ -1024,7 +1024,7 @@ test "empty extern enum with members" {
10241024 B,
10251025 C,
10261026 };
1027 expect(@sizeOf(E) == @sizeOf(c_int));
1027 try expect(@sizeOf(E) == @sizeOf(c_int));
10281028}
10291029
10301030test "tag name with assigned enum values" {
......@@ -1033,7 +1033,7 @@ test "tag name with assigned enum values" {
10331033 B = 0,
10341034 };
10351035 var b = LocalFoo.B;
1036 expect(mem.eql(u8, @tagName(b), "B"));
1036 try expect(mem.eql(u8, @tagName(b), "B"));
10371037}
10381038
10391039test "enum literal equality" {
......@@ -1041,8 +1041,8 @@ test "enum literal equality" {
10411041 const y = .ok;
10421042 const z = .hi;
10431043
1044 expect(x != y);
1045 expect(x == z);
1044 try expect(x != y);
1045 try expect(x == z);
10461046}
10471047
10481048test "enum literal cast to enum" {
......@@ -1054,7 +1054,7 @@ test "enum literal cast to enum" {
10541054
10551055 var color1: Color = .Auto;
10561056 var color2 = Color.Auto;
1057 expect(color1 == color2);
1057 try expect(color1 == color2);
10581058}
10591059
10601060test "peer type resolution with enum literal" {
......@@ -1063,8 +1063,8 @@ test "peer type resolution with enum literal" {
10631063 two,
10641064 };
10651065
1066 expect(Items.two == .two);
1067 expect(.two == Items.two);
1066 try expect(Items.two == .two);
1067 try expect(.two == Items.two);
10681068}
10691069
10701070test "enum literal in array literal" {
......@@ -1078,8 +1078,8 @@ test "enum literal in array literal" {
10781078 .two,
10791079 };
10801080
1081 expect(array[0] == .one);
1082 expect(array[1] == .two);
1081 try expect(array[0] == .one);
1082 try expect(array[1] == .two);
10831083}
10841084
10851085test "signed integer as enum tag" {
......@@ -1089,9 +1089,9 @@ test "signed integer as enum tag" {
10891089 A2 = 1,
10901090 };
10911091
1092 expect(@enumToInt(SignedEnum.A0) == -1);
1093 expect(@enumToInt(SignedEnum.A1) == 0);
1094 expect(@enumToInt(SignedEnum.A2) == 1);
1092 try expect(@enumToInt(SignedEnum.A0) == -1);
1093 try expect(@enumToInt(SignedEnum.A1) == 0);
1094 try expect(@enumToInt(SignedEnum.A2) == 1);
10951095}
10961096
10971097test "enum value allocation" {
......@@ -1101,9 +1101,9 @@ test "enum value allocation" {
11011101 A2,
11021102 };
11031103
1104 expect(@enumToInt(LargeEnum.A0) == 0x80000000);
1105 expect(@enumToInt(LargeEnum.A1) == 0x80000001);
1106 expect(@enumToInt(LargeEnum.A2) == 0x80000002);
1104 try expect(@enumToInt(LargeEnum.A0) == 0x80000000);
1105 try expect(@enumToInt(LargeEnum.A1) == 0x80000001);
1106 try expect(@enumToInt(LargeEnum.A2) == 0x80000002);
11071107}
11081108
11091109test "enum literal casting to tagged union" {
......@@ -1130,32 +1130,32 @@ test "enum with one member and custom tag type" {
11301130 const E = enum(u2) {
11311131 One,
11321132 };
1133 expect(@enumToInt(E.One) == 0);
1133 try expect(@enumToInt(E.One) == 0);
11341134 const E2 = enum(u2) {
11351135 One = 2,
11361136 };
1137 expect(@enumToInt(E2.One) == 2);
1137 try expect(@enumToInt(E2.One) == 2);
11381138}
11391139
11401140test "enum literal casting to optional" {
11411141 var bar: ?Bar = undefined;
11421142 bar = .B;
11431143
1144 expect(bar.? == Bar.B);
1144 try expect(bar.? == Bar.B);
11451145}
11461146
11471147test "enum literal casting to error union with payload enum" {
11481148 var bar: error{B}!Bar = undefined;
11491149 bar = .B; // should never cast to the error set
11501150
1151 expect((try bar) == Bar.B);
1151 try expect((try bar) == Bar.B);
11521152}
11531153
11541154test "enum with one member and u1 tag type @enumToInt" {
11551155 const Enum = enum(u1) {
11561156 Test,
11571157 };
1158 expect(@enumToInt(Enum.Test) == 0);
1158 try expect(@enumToInt(Enum.Test) == 0);
11591159}
11601160
11611161test "enum with comptime_int tag type" {
......@@ -1164,19 +1164,19 @@ test "enum with comptime_int tag type" {
11641164 Two = 2,
11651165 Three = 1,
11661166 };
1167 comptime expect(Tag(Enum) == comptime_int);
1167 comptime try expect(Tag(Enum) == comptime_int);
11681168}
11691169
11701170test "enum with one member default to u0 tag type" {
11711171 const E0 = enum {
11721172 X,
11731173 };
1174 comptime expect(Tag(E0) == u0);
1174 comptime try expect(Tag(E0) == u0);
11751175}
11761176
11771177test "tagName on enum literals" {
1178 expect(mem.eql(u8, @tagName(.FooBar), "FooBar"));
1179 comptime expect(mem.eql(u8, @tagName(.FooBar), "FooBar"));
1178 try expect(mem.eql(u8, @tagName(.FooBar), "FooBar"));
1179 comptime try expect(mem.eql(u8, @tagName(.FooBar), "FooBar"));
11801180}
11811181
11821182test "method call on an enum" {
......@@ -1193,12 +1193,12 @@ test "method call on an enum" {
11931193 return self.* == .two and foo == bool;
11941194 }
11951195 };
1196 fn doTheTest() void {
1196 fn doTheTest() !void {
11971197 var e = E.two;
1198 expect(e.method());
1199 expect(e.generic_method(bool));
1198 try expect(e.method());
1199 try expect(e.generic_method(bool));
12001200 }
12011201 };
1202 S.doTheTest();
1203 comptime S.doTheTest();
1202 try S.doTheTest();
1203 comptime try S.doTheTest();
12041204}
test/stage1/behavior/enum_with_members.zig+4-4
......@@ -19,9 +19,9 @@ test "enum with members" {
1919 const b = ET{ .UINT = 42 };
2020 var buf: [20]u8 = undefined;
2121
22 expect((a.print(buf[0..]) catch unreachable) == 3);
23 expect(mem.eql(u8, buf[0..3], "-42"));
22 try expect((a.print(buf[0..]) catch unreachable) == 3);
23 try expect(mem.eql(u8, buf[0..3], "-42"));
2424
25 expect((b.print(buf[0..]) catch unreachable) == 2);
26 expect(mem.eql(u8, buf[0..2], "42"));
25 try expect((b.print(buf[0..]) catch unreachable) == 2);
26 try expect(mem.eql(u8, buf[0..2], "42"));
2727}
test/stage1/behavior/error.zig+60-60
......@@ -19,7 +19,7 @@ pub fn baz() anyerror!i32 {
1919}
2020
2121test "error wrapping" {
22 expect((baz() catch unreachable) == 15);
22 try expect((baz() catch unreachable) == 15);
2323}
2424
2525fn gimmeItBroke() []const u8 {
......@@ -27,14 +27,14 @@ fn gimmeItBroke() []const u8 {
2727}
2828
2929test "@errorName" {
30 expect(mem.eql(u8, @errorName(error.AnError), "AnError"));
31 expect(mem.eql(u8, @errorName(error.ALongerErrorName), "ALongerErrorName"));
30 try expect(mem.eql(u8, @errorName(error.AnError), "AnError"));
31 try expect(mem.eql(u8, @errorName(error.ALongerErrorName), "ALongerErrorName"));
3232}
3333
3434test "error values" {
3535 const a = @errorToInt(error.err1);
3636 const b = @errorToInt(error.err2);
37 expect(a != b);
37 try expect(a != b);
3838}
3939
4040test "redefinition of error values allowed" {
......@@ -47,8 +47,8 @@ fn shouldBeNotEqual(a: anyerror, b: anyerror) void {
4747test "error binary operator" {
4848 const a = errBinaryOperatorG(true) catch 3;
4949 const b = errBinaryOperatorG(false) catch 3;
50 expect(a == 3);
51 expect(b == 10);
50 try expect(a == 3);
51 try expect(b == 10);
5252}
5353fn errBinaryOperatorG(x: bool) anyerror!isize {
5454 return if (x) error.ItBroke else @as(isize, 10);
......@@ -56,7 +56,7 @@ fn errBinaryOperatorG(x: bool) anyerror!isize {
5656
5757test "unwrap simple value from error" {
5858 const i = unwrapSimpleValueFromErrorDo() catch unreachable;
59 expect(i == 13);
59 try expect(i == 13);
6060}
6161fn unwrapSimpleValueFromErrorDo() anyerror!isize {
6262 return 13;
......@@ -76,21 +76,21 @@ fn makeANonErr() anyerror!i32 {
7676}
7777
7878test "error union type " {
79 testErrorUnionType();
80 comptime testErrorUnionType();
79 try testErrorUnionType();
80 comptime try testErrorUnionType();
8181}
8282
83fn testErrorUnionType() void {
83fn testErrorUnionType() !void {
8484 const x: anyerror!i32 = 1234;
85 if (x) |value| expect(value == 1234) else |_| unreachable;
86 expect(@typeInfo(@TypeOf(x)) == .ErrorUnion);
87 expect(@typeInfo(@typeInfo(@TypeOf(x)).ErrorUnion.error_set) == .ErrorSet);
88 expect(@typeInfo(@TypeOf(x)).ErrorUnion.error_set == anyerror);
85 if (x) |value| try expect(value == 1234) else |_| unreachable;
86 try expect(@typeInfo(@TypeOf(x)) == .ErrorUnion);
87 try expect(@typeInfo(@typeInfo(@TypeOf(x)).ErrorUnion.error_set) == .ErrorSet);
88 try expect(@typeInfo(@TypeOf(x)).ErrorUnion.error_set == anyerror);
8989}
9090
9191test "error set type" {
92 testErrorSetType();
93 comptime testErrorSetType();
92 try testErrorSetType();
93 comptime try testErrorSetType();
9494}
9595
9696const MyErrSet = error{
......@@ -98,21 +98,21 @@ const MyErrSet = error{
9898 FileNotFound,
9999};
100100
101fn testErrorSetType() void {
102 expect(@typeInfo(MyErrSet).ErrorSet.?.len == 2);
101fn testErrorSetType() !void {
102 try expect(@typeInfo(MyErrSet).ErrorSet.?.len == 2);
103103
104104 const a: MyErrSet!i32 = 5678;
105105 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) {
108108 error.OutOfMemory => unreachable,
109109 error.FileNotFound => unreachable,
110110 }
111111}
112112
113113test "explicit error set cast" {
114 testExplicitErrorSetCast(Set1.A);
115 comptime testExplicitErrorSetCast(Set1.A);
114 try testExplicitErrorSetCast(Set1.A);
115 comptime try testExplicitErrorSetCast(Set1.A);
116116}
117117
118118const Set1 = error{
......@@ -124,26 +124,26 @@ const Set2 = error{
124124 C,
125125};
126126
127fn testExplicitErrorSetCast(set1: Set1) void {
127fn testExplicitErrorSetCast(set1: Set1) !void {
128128 var x = @errSetCast(Set2, set1);
129129 var y = @errSetCast(Set1, x);
130 expect(y == error.A);
130 try expect(y == error.A);
131131}
132132
133133test "comptime test error for empty error set" {
134 testComptimeTestErrorEmptySet(1234);
135 comptime testComptimeTestErrorEmptySet(1234);
134 try testComptimeTestErrorEmptySet(1234);
135 comptime try testComptimeTestErrorEmptySet(1234);
136136}
137137
138138const EmptyErrorSet = error{};
139139
140fn testComptimeTestErrorEmptySet(x: EmptyErrorSet!i32) void {
141 if (x) |v| expect(v == 1234) else |err| @compileError("bad");
140fn testComptimeTestErrorEmptySet(x: EmptyErrorSet!i32) !void {
141 if (x) |v| try expect(v == 1234) else |err| @compileError("bad");
142142}
143143
144144test "syntax: optional operator in front of error union operator" {
145145 comptime {
146 expect(?(anyerror!i32) == ?(anyerror!i32));
146 try expect(?(anyerror!i32) == ?(anyerror!i32));
147147 }
148148}
149149
......@@ -165,10 +165,10 @@ test "empty error union" {
165165}
166166
167167test "error union peer type resolution" {
168 testErrorUnionPeerTypeResolution(1);
168 try testErrorUnionPeerTypeResolution(1);
169169}
170170
171fn testErrorUnionPeerTypeResolution(x: i32) void {
171fn testErrorUnionPeerTypeResolution(x: i32) !void {
172172 const y = switch (x) {
173173 1 => bar_1(),
174174 2 => baz_1(),
......@@ -177,7 +177,7 @@ fn testErrorUnionPeerTypeResolution(x: i32) void {
177177 if (y) |_| {
178178 @panic("expected error");
179179 } else |e| {
180 expect(e == error.A);
180 try expect(e == error.A);
181181 }
182182}
183183
......@@ -286,13 +286,13 @@ test "nested error union function call in optional unwrap" {
286286 return null;
287287 }
288288 };
289 expect((try S.errorable()) == 1234);
290 expectError(error.Failure, S.errorable2());
291 expectError(error.Other, S.errorable3());
289 try expect((try S.errorable()) == 1234);
290 try expectError(error.Failure, S.errorable2());
291 try expectError(error.Other, S.errorable3());
292292 comptime {
293 expect((try S.errorable()) == 1234);
294 expectError(error.Failure, S.errorable2());
295 expectError(error.Other, S.errorable3());
293 try expect((try S.errorable()) == 1234);
294 try expectError(error.Failure, S.errorable2());
295 try expectError(error.Other, S.errorable3());
296296 }
297297}
298298
......@@ -307,7 +307,7 @@ test "widen cast integer payload of error union function call" {
307307 return 1234;
308308 }
309309 };
310 expect((try S.errorable()) == 1234);
310 try expect((try S.errorable()) == 1234);
311311}
312312
313313test "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" {
320320 return error.Failure;
321321 }
322322 };
323 expectError(error.Failure, S.errorable());
324 comptime expectError(error.Failure, S.errorable());
323 try expectError(error.Failure, S.errorable());
324 comptime try expectError(error.Failure, S.errorable());
325325}
326326
327327test "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));
329329 const S = struct {
330330 fn returnsOptErrSet() ?anyerror {
331331 return null;
332332 }
333333 };
334 expect(S.returnsOptErrSet() == null);
335 comptime expect(S.returnsOptErrSet() == null);
334 try expect(S.returnsOptErrSet() == null);
335 comptime try expect(S.returnsOptErrSet() == null);
336336}
337337
338338test "debug info for optional error set" {
......@@ -342,8 +342,8 @@ test "debug info for optional error set" {
342342
343343test "nested catch" {
344344 const S = struct {
345 fn entry() void {
346 expectError(error.Bad, func());
345 fn entry() !void {
346 try expectError(error.Bad, func());
347347 }
348348 fn fail() anyerror!Foo {
349349 return error.Wrong;
......@@ -358,16 +358,16 @@ test "nested catch" {
358358 field: i32,
359359 };
360360 };
361 S.entry();
362 comptime S.entry();
361 try S.entry();
362 comptime try S.entry();
363363}
364364
365365test "implicit cast to optional to error union to return result loc" {
366366 const S = struct {
367 fn entry() void {
367 fn entry() !void {
368368 var x: Foo = undefined;
369369 if (func(&x)) |opt| {
370 expect(opt != null);
370 try expect(opt != null);
371371 } else |_| @panic("expected non error");
372372 }
373373 fn func(f: *Foo) anyerror!?*Foo {
......@@ -377,7 +377,7 @@ test "implicit cast to optional to error union to return result loc" {
377377 field: i32,
378378 };
379379 };
380 S.entry();
380 try S.entry();
381381 //comptime S.entry(); TODO
382382}
383383
......@@ -393,23 +393,23 @@ test "function pointer with return type that is error union with payload which i
393393 return Err.UnspecifiedErr;
394394 }
395395
396 fn doTheTest() void {
396 fn doTheTest() !void {
397397 var x = Foo{ .fun = bar };
398 expectError(error.UnspecifiedErr, x.fun(1));
398 try expectError(error.UnspecifiedErr, x.fun(1));
399399 }
400400 };
401 S.doTheTest();
401 try S.doTheTest();
402402}
403403
404404test "return result loc as peer result loc in inferred error set function" {
405405 const S = struct {
406 fn doTheTest() void {
406 fn doTheTest() !void {
407407 if (foo(2)) |x| {
408 expect(x.Two);
408 try expect(x.Two);
409409 } else |e| switch (e) {
410410 error.Whatever => @panic("fail"),
411411 }
412 expectError(error.Whatever, foo(99));
412 try expectError(error.Whatever, foo(99));
413413 }
414414 const FormValue = union(enum) {
415415 One: void,
......@@ -424,8 +424,8 @@ test "return result loc as peer result loc in inferred error set function" {
424424 };
425425 }
426426 };
427 S.doTheTest();
428 comptime S.doTheTest();
427 try S.doTheTest();
428 comptime try S.doTheTest();
429429}
430430
431431test "error payload type is correctly resolved" {
......@@ -439,7 +439,7 @@ test "error payload type is correctly resolved" {
439439 }
440440 };
441441
442 expectEqual(MyIntWrapper{ .x = 42 }, try MyIntWrapper.create());
442 try expectEqual(MyIntWrapper{ .x = 42 }, try MyIntWrapper.create());
443443}
444444
445445test "error union comptime caching" {
......@@ -449,4 +449,4 @@ test "error union comptime caching" {
449449
450450 S.foo(@as(anyerror!void, {}));
451451 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;
44const builtin = @import("builtin");
55
66test "compile time recursion" {
7 expect(some_data.len == 21);
7 try expect(some_data.len == 21);
88}
99var some_data: [@intCast(usize, fibonacci(7))]u8 = undefined;
1010fn fibonacci(x: i32) i32 {
......@@ -17,7 +17,7 @@ fn unwrapAndAddOne(blah: ?i32) i32 {
1717}
1818const should_be_1235 = unwrapAndAddOne(1234);
1919test "static add one" {
20 expect(should_be_1235 == 1235);
20 try expect(should_be_1235 == 1235);
2121}
2222
2323test "inlined loop" {
......@@ -25,7 +25,7 @@ test "inlined loop" {
2525 comptime var sum = 0;
2626 inline while (i <= 5) : (i += 1)
2727 sum += i;
28 expect(sum == 15);
28 try expect(sum == 15);
2929}
3030
3131fn gimme1or2(comptime a: bool) i32 {
......@@ -35,12 +35,12 @@ fn gimme1or2(comptime a: bool) i32 {
3535 return z;
3636}
3737test "inline variable gets result of const if" {
38 expect(gimme1or2(true) == 1);
39 expect(gimme1or2(false) == 2);
38 try expect(gimme1or2(true) == 1);
39 try expect(gimme1or2(false) == 2);
4040}
4141
4242test "static function evaluation" {
43 expect(statically_added_number == 3);
43 try expect(statically_added_number == 3);
4444}
4545const statically_added_number = staticAdd(1, 2);
4646fn staticAdd(a: i32, b: i32) i32 {
......@@ -48,8 +48,8 @@ fn staticAdd(a: i32, b: i32) i32 {
4848}
4949
5050test "const expr eval on single expr blocks" {
51 expect(constExprEvalOnSingleExprBlocksFn(1, true) == 3);
52 comptime expect(constExprEvalOnSingleExprBlocksFn(1, true) == 3);
51 try expect(constExprEvalOnSingleExprBlocksFn(1, true) == 3);
52 comptime try expect(constExprEvalOnSingleExprBlocksFn(1, true) == 3);
5353}
5454
5555fn constExprEvalOnSingleExprBlocksFn(x: i32, b: bool) i32 {
......@@ -65,10 +65,10 @@ fn constExprEvalOnSingleExprBlocksFn(x: i32, b: bool) i32 {
6565}
6666
6767test "statically initialized list" {
68 expect(static_point_list[0].x == 1);
69 expect(static_point_list[0].y == 2);
70 expect(static_point_list[1].x == 3);
71 expect(static_point_list[1].y == 4);
68 try expect(static_point_list[0].x == 1);
69 try expect(static_point_list[0].y == 2);
70 try expect(static_point_list[1].x == 3);
71 try expect(static_point_list[1].y == 4);
7272}
7373const Point = struct {
7474 x: i32,
......@@ -86,8 +86,8 @@ fn makePoint(x: i32, y: i32) Point {
8686}
8787
8888test "static eval list init" {
89 expect(static_vec3.data[2] == 1.0);
90 expect(vec3(0.0, 0.0, 3.0).data[2] == 3.0);
89 try expect(static_vec3.data[2] == 1.0);
90 try expect(vec3(0.0, 0.0, 3.0).data[2] == 3.0);
9191}
9292const static_vec3 = vec3(0.0, 0.0, 1.0);
9393pub const Vec3 = struct {
......@@ -105,12 +105,12 @@ pub fn vec3(x: f32, y: f32, z: f32) Vec3 {
105105
106106test "constant expressions" {
107107 var array: [array_size]u8 = undefined;
108 expect(@sizeOf(@TypeOf(array)) == 20);
108 try expect(@sizeOf(@TypeOf(array)) == 20);
109109}
110110const array_size: u8 = 20;
111111
112112test "constant struct with negation" {
113 expect(vertices[0].x == -0.6);
113 try expect(vertices[0].x == -0.6);
114114}
115115const Vertex = struct {
116116 x: f32,
......@@ -145,7 +145,7 @@ const vertices = [_]Vertex{
145145
146146test "statically initialized struct" {
147147 st_init_str_foo.x += 1;
148 expect(st_init_str_foo.x == 14);
148 try expect(st_init_str_foo.x == 14);
149149}
150150const StInitStrFoo = struct {
151151 x: i32,
......@@ -158,7 +158,7 @@ var st_init_str_foo = StInitStrFoo{
158158
159159test "statically initalized array literal" {
160160 const y: [4]u8 = st_init_arr_lit_x;
161 expect(y[3] == 4);
161 try expect(y[3] == 4);
162162}
163163const st_init_arr_lit_x = [_]u8{
164164 1,
......@@ -170,15 +170,15 @@ const st_init_arr_lit_x = [_]u8{
170170test "const slice" {
171171 comptime {
172172 const a = "1234567890";
173 expect(a.len == 10);
173 try expect(a.len == 10);
174174 const b = a[1..2];
175 expect(b.len == 1);
176 expect(b[0] == '2');
175 try expect(b.len == 1);
176 try expect(b[0] == '2');
177177 }
178178}
179179
180180test "try to trick eval with runtime if" {
181 expect(testTryToTrickEvalWithRuntimeIf(true) == 10);
181 try expect(testTryToTrickEvalWithRuntimeIf(true) == 10);
182182}
183183
184184fn testTryToTrickEvalWithRuntimeIf(b: bool) usize {
......@@ -198,7 +198,7 @@ test "inlined loop has array literal with elided runtime scope on first iteratio
198198 const result = if (i == 0) [1]i32{2} else runtime;
199199 }
200200 comptime {
201 expect(i == 2);
201 try expect(i == 2);
202202 }
203203}
204204
......@@ -215,16 +215,16 @@ fn letsTryToCompareBools(a: bool, b: bool) bool {
215215 return max(bool, a, b);
216216}
217217test "inlined block and runtime block phi" {
218 expect(letsTryToCompareBools(true, true));
219 expect(letsTryToCompareBools(true, false));
220 expect(letsTryToCompareBools(false, true));
221 expect(!letsTryToCompareBools(false, false));
218 try expect(letsTryToCompareBools(true, true));
219 try expect(letsTryToCompareBools(true, false));
220 try expect(letsTryToCompareBools(false, true));
221 try expect(!letsTryToCompareBools(false, false));
222222
223223 comptime {
224 expect(letsTryToCompareBools(true, true));
225 expect(letsTryToCompareBools(true, false));
226 expect(letsTryToCompareBools(false, true));
227 expect(!letsTryToCompareBools(false, false));
224 try expect(letsTryToCompareBools(true, true));
225 try expect(letsTryToCompareBools(true, false));
226 try expect(letsTryToCompareBools(false, true));
227 try expect(!letsTryToCompareBools(false, false));
228228 }
229229}
230230
......@@ -269,14 +269,14 @@ fn performFn(comptime prefix_char: u8, start_value: i32) i32 {
269269}
270270
271271test "comptime iterate over fn ptr list" {
272 expect(performFn('t', 1) == 6);
273 expect(performFn('o', 0) == 1);
274 expect(performFn('w', 99) == 99);
272 try expect(performFn('t', 1) == 6);
273 try expect(performFn('o', 0) == 1);
274 try expect(performFn('w', 99) == 99);
275275}
276276
277277test "eval @setRuntimeSafety at compile-time" {
278278 const result = comptime fnWithSetRuntimeSafety();
279 expect(result == 1234);
279 try expect(result == 1234);
280280}
281281
282282fn fnWithSetRuntimeSafety() i32 {
......@@ -286,7 +286,7 @@ fn fnWithSetRuntimeSafety() i32 {
286286
287287test "eval @setFloatMode at compile-time" {
288288 const result = comptime fnWithFloatMode();
289 expect(result == 1234.0);
289 try expect(result == 1234.0);
290290}
291291
292292fn fnWithFloatMode() f32 {
......@@ -307,15 +307,15 @@ var simple_struct = SimpleStruct{ .field = 1234 };
307307const bound_fn = simple_struct.method;
308308
309309test "call method on bound fn referring to var instance" {
310 expect(bound_fn() == 1237);
310 try expect(bound_fn() == 1237);
311311}
312312
313313test "ptr to local array argument at comptime" {
314314 comptime {
315315 var bytes: [10]u8 = undefined;
316316 modifySomeBytes(bytes[0..]);
317 expect(bytes[0] == 'a');
318 expect(bytes[9] == 'b');
317 try expect(bytes[0] == 'a');
318 try expect(bytes[9] == 'b');
319319 }
320320}
321321
......@@ -343,9 +343,9 @@ fn testCompTimeUIntComparisons(x: u32) void {
343343}
344344
345345test "const ptr to variable data changes at runtime" {
346 expect(foo_ref.name[0] == 'a');
346 try expect(foo_ref.name[0] == 'a');
347347 foo_ref.name = "b";
348 expect(foo_ref.name[0] == 'b');
348 try expect(foo_ref.name[0] == 'b');
349349}
350350
351351const Foo = struct {
......@@ -356,8 +356,8 @@ var foo_contents = Foo{ .name = "a" };
356356const foo_ref = &foo_contents;
357357
358358test "create global array with for loop" {
359 expect(global_array[5] == 5 * 5);
360 expect(global_array[9] == 9 * 9);
359 try expect(global_array[5] == 5 * 5);
360 try expect(global_array[9] == 9 * 9);
361361}
362362
363363const global_array = x: {
......@@ -372,18 +372,18 @@ test "compile-time downcast when the bits fit" {
372372 comptime {
373373 const spartan_count: u16 = 255;
374374 const byte = @intCast(u8, spartan_count);
375 expect(byte == 255);
375 try expect(byte == 255);
376376 }
377377}
378378
379379const hi1 = "hi";
380380const hi2 = hi1;
381381test "const global shares pointer with other same one" {
382 assertEqualPtrs(&hi1[0], &hi2[0]);
383 comptime expect(&hi1[0] == &hi2[0]);
382 try assertEqualPtrs(&hi1[0], &hi2[0]);
383 comptime try expect(&hi1[0] == &hi2[0]);
384384}
385fn assertEqualPtrs(ptr1: *const u8, ptr2: *const u8) void {
386 expect(ptr1 == ptr2);
385fn assertEqualPtrs(ptr1: *const u8, ptr2: *const u8) !void {
386 try expect(ptr1 == ptr2);
387387}
388388
389389test "@setEvalBranchQuota" {
......@@ -395,29 +395,29 @@ test "@setEvalBranchQuota" {
395395 while (i < 1001) : (i += 1) {
396396 sum += i;
397397 }
398 expect(sum == 500500);
398 try expect(sum == 500500);
399399 }
400400}
401401
402402test "float literal at compile time not lossy" {
403 expect(16777216.0 + 1.0 == 16777217.0);
404 expect(9007199254740992.0 + 1.0 == 9007199254740993.0);
403 try expect(16777216.0 + 1.0 == 16777217.0);
404 try expect(9007199254740992.0 + 1.0 == 9007199254740993.0);
405405}
406406
407407test "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);
409409}
410410
411411test "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);
413413}
414414
415415test "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);
417417}
418418
419419comptime {
420 expect(@as(f128, 1 << 113) == 10384593717069655257060992658440192);
420 try expect(@as(f128, 1 << 113) == 10384593717069655257060992658440192);
421421}
422422
423423pub fn TypeWithCompTimeSlice(comptime field_name: []const u8) type {
......@@ -429,15 +429,15 @@ pub fn TypeWithCompTimeSlice(comptime field_name: []const u8) type {
429429test "string literal used as comptime slice is memoized" {
430430 const a = "link";
431431 const b = "link";
432 comptime expect(TypeWithCompTimeSlice(a).Node == TypeWithCompTimeSlice(b).Node);
433 comptime expect(TypeWithCompTimeSlice("link").Node == TypeWithCompTimeSlice("link").Node);
432 comptime try expect(TypeWithCompTimeSlice(a).Node == TypeWithCompTimeSlice(b).Node);
433 comptime try expect(TypeWithCompTimeSlice("link").Node == TypeWithCompTimeSlice("link").Node);
434434}
435435
436436test "comptime slice of undefined pointer of length 0" {
437437 const slice1 = @as([*]i32, undefined)[0..0];
438 expect(slice1.len == 0);
438 try expect(slice1.len == 0);
439439 const slice2 = @as([*]i32, undefined)[100..100];
440 expect(slice2.len == 0);
440 try expect(slice2.len == 0);
441441}
442442
443443fn copyWithPartialInline(s: []u32, b: []u8) void {
......@@ -459,16 +459,16 @@ test "binary math operator in partially inlined function" {
459459 r.* = @intCast(u8, i + 1);
460460
461461 copyWithPartialInline(s[0..], b[0..]);
462 expect(s[0] == 0x1020304);
463 expect(s[1] == 0x5060708);
464 expect(s[2] == 0x90a0b0c);
465 expect(s[3] == 0xd0e0f10);
462 try expect(s[0] == 0x1020304);
463 try expect(s[1] == 0x5060708);
464 try expect(s[2] == 0x90a0b0c);
465 try expect(s[3] == 0xd0e0f10);
466466}
467467
468468test "comptime function with the same args is memoized" {
469469 comptime {
470 expect(MakeType(i32) == MakeType(i32));
471 expect(MakeType(i32) != MakeType(f64));
470 try expect(MakeType(i32) == MakeType(i32));
471 try expect(MakeType(i32) != MakeType(f64));
472472 }
473473}
474474
......@@ -484,7 +484,7 @@ test "comptime function with mutable pointer is not memoized" {
484484 const ptr = &x;
485485 increment(ptr);
486486 increment(ptr);
487 expect(x == 3);
487 try expect(x == 3);
488488 }
489489}
490490
......@@ -510,14 +510,14 @@ fn doesAlotT(comptime T: type, value: usize) T {
510510}
511511
512512test "@setEvalBranchQuota at same scope as generic function call" {
513 expect(doesAlotT(u32, 2) == 2);
513 try expect(doesAlotT(u32, 2) == 2);
514514}
515515
516516test "comptime slice of slice preserves comptime var" {
517517 comptime {
518518 var buff: [10]u8 = undefined;
519519 buff[0..][0..][0] = 1;
520 expect(buff[0..][0..][0] == 1);
520 try expect(buff[0..][0..][0] == 1);
521521 }
522522}
523523
......@@ -526,7 +526,7 @@ test "comptime slice of pointer preserves comptime var" {
526526 var buff: [10]u8 = undefined;
527527 var a = @ptrCast([*]u8, &buff);
528528 a[0..1][0] = 1;
529 expect(buff[0..][0..][0] == 1);
529 try expect(buff[0..][0..][0] == 1);
530530 }
531531}
532532
......@@ -540,9 +540,9 @@ const SingleFieldStruct = struct {
540540test "const ptr to comptime mutable data is not memoized" {
541541 comptime {
542542 var foo = SingleFieldStruct{ .x = 1 };
543 expect(foo.read_x() == 1);
543 try expect(foo.read_x() == 1);
544544 foo.x = 2;
545 expect(foo.read_x() == 2);
545 try expect(foo.read_x() == 2);
546546 }
547547}
548548
......@@ -551,7 +551,7 @@ test "array concat of slices gives slice" {
551551 var a: []const u8 = "aoeu";
552552 var b: []const u8 = "asdf";
553553 const c = a ++ b;
554 expect(std.mem.eql(u8, c, "aoeuasdf"));
554 try expect(std.mem.eql(u8, c, "aoeuasdf"));
555555 }
556556}
557557
......@@ -568,14 +568,14 @@ test "comptime shlWithOverflow" {
568568 break :amt amt;
569569 };
570570
571 expect(ct_shifted == rt_shifted);
571 try expect(ct_shifted == rt_shifted);
572572}
573573
574574test "runtime 128 bit integer division" {
575575 var a: u128 = 152313999999999991610955792383;
576576 var b: u128 = 10000000000000000000;
577577 var c = a / b;
578 expect(c == 15231399999);
578 try expect(c == 15231399999);
579579}
580580
581581pub const Info = struct {
......@@ -588,20 +588,20 @@ test "comptime modification of const struct field" {
588588 comptime {
589589 var res = diamond_info;
590590 res.version = 1;
591 expect(diamond_info.version == 0);
592 expect(res.version == 1);
591 try expect(diamond_info.version == 0);
592 try expect(res.version == 1);
593593 }
594594}
595595
596596test "pointer to type" {
597597 comptime {
598598 var T: type = i32;
599 expect(T == i32);
599 try expect(T == i32);
600600 var ptr = &T;
601 expect(@TypeOf(ptr) == *type);
601 try expect(@TypeOf(ptr) == *type);
602602 ptr.* = f32;
603 expect(T == f32);
604 expect(*T == *f32);
603 try expect(T == f32);
604 try expect(*T == *f32);
605605 }
606606}
607607
......@@ -610,17 +610,17 @@ test "slice of type" {
610610 var types_array = [_]type{ i32, f64, type };
611611 for (types_array) |T, i| {
612612 switch (i) {
613 0 => expect(T == i32),
614 1 => expect(T == f64),
615 2 => expect(T == type),
613 0 => try expect(T == i32),
614 1 => try expect(T == f64),
615 2 => try expect(T == type),
616616 else => unreachable,
617617 }
618618 }
619619 for (types_array[0..]) |T, i| {
620620 switch (i) {
621 0 => expect(T == i32),
622 1 => expect(T == f64),
623 2 => expect(T == type),
621 0 => try expect(T == i32),
622 1 => try expect(T == f64),
623 2 => try expect(T == type),
624624 else => unreachable,
625625 }
626626 }
......@@ -637,7 +637,7 @@ fn wrap(comptime T: type) Wrapper {
637637
638638test "function which returns struct with type field causes implicit comptime" {
639639 const ty = wrap(i32).T;
640 expect(ty == i32);
640 try expect(ty == i32);
641641}
642642
643643test "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
652652 const s = S{ .a = 2 };
653653 var b = s.b();
654 expect(b == 2);
654 try expect(b == 2);
655655}
656656
657657test "@tagName of @typeInfo" {
658658 const str = @tagName(@typeInfo(u8));
659 expect(std.mem.eql(u8, str, "Int"));
659 try expect(std.mem.eql(u8, str, "Int"));
660660}
661661
662662test "setting backward branch quota just before a generic fn call" {
......@@ -670,15 +670,15 @@ fn loopNTimes(comptime n: usize) void {
670670}
671671
672672test "variable inside inline loop that has different types on different iterations" {
673 testVarInsideInlineLoop(.{ true, @as(u32, 42) });
673 try testVarInsideInlineLoop(.{ true, @as(u32, 42) });
674674}
675675
676fn testVarInsideInlineLoop(args: anytype) void {
676fn testVarInsideInlineLoop(args: anytype) !void {
677677 comptime var i = 0;
678678 inline while (i < args.len) : (i += 1) {
679679 const x = args[i];
680 if (i == 0) expect(x);
681 if (i == 1) expect(x == 42);
680 if (i == 0) try expect(x);
681 if (i == 1) try expect(x == 42);
682682 }
683683}
684684
......@@ -688,7 +688,7 @@ test "inline for with same type but different values" {
688688 var a: T = undefined;
689689 res += a.len;
690690 }
691 expect(res == 5);
691 try expect(res == 5);
692692}
693693
694694test "refer to the type of a generic function" {
......@@ -702,13 +702,13 @@ fn doNothingWithType(comptime T: type) void {}
702702test "zero extend from u0 to u1" {
703703 var zero_u0: u0 = 0;
704704 var zero_u1: u1 = zero_u0;
705 expect(zero_u1 == 0);
705 try expect(zero_u1 == 0);
706706}
707707
708708test "bit shift a u1" {
709709 var x: u1 = 1;
710710 var y = x << 0;
711 expect(y == 1);
711 try expect(y == 1);
712712}
713713
714714test "comptime pointer cast array and then slice" {
......@@ -720,8 +720,8 @@ test "comptime pointer cast array and then slice" {
720720 const ptrB: [*]const u8 = &array;
721721 const sliceB: []const u8 = ptrB[0..2];
722722
723 expect(sliceA[1] == 2);
724 expect(sliceB[1] == 2);
723 try expect(sliceA[1] == 2);
724 try expect(sliceB[1] == 2);
725725}
726726
727727test "slice bounds in comptime concatenation" {
......@@ -730,46 +730,46 @@ test "slice bounds in comptime concatenation" {
730730 break :blk b[8..9];
731731 };
732732 const str = "" ++ bs;
733 expect(str.len == 1);
734 expect(std.mem.eql(u8, str, "1"));
733 try expect(str.len == 1);
734 try expect(std.mem.eql(u8, str, "1"));
735735
736736 const str2 = bs ++ "";
737 expect(str2.len == 1);
738 expect(std.mem.eql(u8, str2, "1"));
737 try expect(str2.len == 1);
738 try expect(std.mem.eql(u8, str2, "1"));
739739}
740740
741741test "comptime bitwise operators" {
742742 comptime {
743 expect(3 & 1 == 1);
744 expect(3 & -1 == 3);
745 expect(-3 & -1 == -3);
746 expect(3 | -1 == -1);
747 expect(-3 | -1 == -1);
748 expect(3 ^ -1 == -4);
749 expect(-3 ^ -1 == 2);
750 expect(~@as(i8, -1) == 0);
751 expect(~@as(i128, -1) == 0);
752 expect(18446744073709551615 & 18446744073709551611 == 18446744073709551611);
753 expect(-18446744073709551615 & -18446744073709551611 == -18446744073709551615);
754 expect(~@as(u128, 0) == 0xffffffffffffffffffffffffffffffff);
743 try expect(3 & 1 == 1);
744 try expect(3 & -1 == 3);
745 try expect(-3 & -1 == -3);
746 try expect(3 | -1 == -1);
747 try expect(-3 | -1 == -1);
748 try expect(3 ^ -1 == -4);
749 try expect(-3 ^ -1 == 2);
750 try expect(~@as(i8, -1) == 0);
751 try expect(~@as(i128, -1) == 0);
752 try expect(18446744073709551615 & 18446744073709551611 == 18446744073709551611);
753 try expect(-18446744073709551615 & -18446744073709551611 == -18446744073709551615);
754 try expect(~@as(u128, 0) == 0xffffffffffffffffffffffffffffffff);
755755 }
756756}
757757
758758test "*align(1) u16 is the same as *align(1:0:2) u16" {
759759 comptime {
760 expect(*align(1:0:2) u16 == *align(1) u16);
761 expect(*align(2:0:2) u16 == *u16);
760 try expect(*align(1:0:2) u16 == *align(1) u16);
761 try expect(*align(2:0:2) u16 == *u16);
762762 }
763763}
764764
765765test "array concatenation forces comptime" {
766766 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 }));
768768}
769769
770770test "array multiplication forces comptime" {
771771 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 }));
773773}
774774
775775fn oneItem(x: i32) [1]i32 {
......@@ -791,7 +791,7 @@ test "comptime assign int to optional int" {
791791 var x: ?i32 = null;
792792 x = 2;
793793 x.? *= 10;
794 expectEqual(20, x.?);
794 try expectEqual(20, x.?);
795795 }
796796}
797797
test/stage1/behavior/field_parent_ptr.zig+12-12
......@@ -1,13 +1,13 @@
11const expect = @import("std").testing.expect;
22
33test "@fieldParentPtr non-first field" {
4 testParentFieldPtr(&foo.c);
5 comptime testParentFieldPtr(&foo.c);
4 try testParentFieldPtr(&foo.c);
5 comptime try testParentFieldPtr(&foo.c);
66}
77
88test "@fieldParentPtr first field" {
9 testParentFieldPtrFirst(&foo.a);
10 comptime testParentFieldPtrFirst(&foo.a);
9 try testParentFieldPtrFirst(&foo.a);
10 comptime try testParentFieldPtrFirst(&foo.a);
1111}
1212
1313const Foo = struct {
......@@ -24,18 +24,18 @@ const foo = Foo{
2424 .d = -10,
2525};
2626
27fn testParentFieldPtr(c: *const i32) void {
28 expect(c == &foo.c);
27fn testParentFieldPtr(c: *const i32) !void {
28 try expect(c == &foo.c);
2929
3030 const base = @fieldParentPtr(Foo, "c", c);
31 expect(base == &foo);
32 expect(&base.c == c);
31 try expect(base == &foo);
32 try expect(&base.c == c);
3333}
3434
35fn testParentFieldPtrFirst(a: *const bool) void {
36 expect(a == &foo.a);
35fn testParentFieldPtrFirst(a: *const bool) !void {
36 try expect(a == &foo.a);
3737
3838 const base = @fieldParentPtr(Foo, "a", a);
39 expect(base == &foo);
40 expect(&base.a == a);
39 try expect(base == &foo);
40 try expect(&base.a == a);
4141}
test/stage1/behavior/floatop.zig+161-161
......@@ -8,441 +8,441 @@ const Vector = std.meta.Vector;
88const epsilon = 0.000001;
99
1010test "@sqrt" {
11 comptime testSqrt();
12 testSqrt();
11 comptime try testSqrt();
12 try testSqrt();
1313}
1414
15fn testSqrt() void {
15fn testSqrt() !void {
1616 {
1717 var a: f16 = 4;
18 expect(@sqrt(a) == 2);
18 try expect(@sqrt(a) == 2);
1919 }
2020 {
2121 var a: f32 = 9;
22 expect(@sqrt(a) == 3);
22 try expect(@sqrt(a) == 3);
2323 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));
2525 }
2626 {
2727 var a: f64 = 25;
28 expect(@sqrt(a) == 5);
28 try expect(@sqrt(a) == 5);
2929 }
3030 {
3131 const a: comptime_float = 25.0;
32 expect(@sqrt(a) == 5.0);
32 try expect(@sqrt(a) == 5.0);
3333 }
3434 // TODO https://github.com/ziglang/zig/issues/4026
3535 //{
3636 // var a: f128 = 49;
37 // expect(@sqrt(a) == 7);
37 //try expect(@sqrt(a) == 7);
3838 //}
3939 {
4040 var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 3.3, 4.4 };
4141 var result = @sqrt(v);
42 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));
44 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));
42 try expect(math.approxEqAbs(f32, @sqrt(@as(f32, 1.1)), result[0], epsilon));
43 try expect(math.approxEqAbs(f32, @sqrt(@as(f32, 2.2)), result[1], epsilon));
44 try expect(math.approxEqAbs(f32, @sqrt(@as(f32, 3.3)), result[2], epsilon));
45 try expect(math.approxEqAbs(f32, @sqrt(@as(f32, 4.4)), result[3], epsilon));
4646 }
4747}
4848
4949test "more @sqrt f16 tests" {
5050 // TODO these are not all passing at comptime
51 expect(@sqrt(@as(f16, 0.0)) == 0.0);
52 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));
54 expect(@sqrt(@as(f16, 4.0)) == 2.0);
55 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));
57 expect(@sqrt(@as(f16, 64.0)) == 8.0);
58 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));
51 try expect(@sqrt(@as(f16, 0.0)) == 0.0);
52 try expect(math.approxEqAbs(f16, @sqrt(@as(f16, 2.0)), 1.414214, epsilon));
53 try expect(math.approxEqAbs(f16, @sqrt(@as(f16, 3.6)), 1.897367, epsilon));
54 try expect(@sqrt(@as(f16, 4.0)) == 2.0);
55 try expect(math.approxEqAbs(f16, @sqrt(@as(f16, 7.539840)), 2.745877, epsilon));
56 try expect(math.approxEqAbs(f16, @sqrt(@as(f16, 19.230934)), 4.385309, epsilon));
57 try expect(@sqrt(@as(f16, 64.0)) == 8.0);
58 try expect(math.approxEqAbs(f16, @sqrt(@as(f16, 64.1)), 8.006248, epsilon));
59 try expect(math.approxEqAbs(f16, @sqrt(@as(f16, 8942.230469)), 94.563370, epsilon));
6060
6161 // special cases
62 expect(math.isPositiveInf(@sqrt(@as(f16, math.inf(f16)))));
63 expect(@sqrt(@as(f16, 0.0)) == 0.0);
64 expect(@sqrt(@as(f16, -0.0)) == -0.0);
65 expect(math.isNan(@sqrt(@as(f16, -1.0))));
66 expect(math.isNan(@sqrt(@as(f16, math.nan(f16)))));
62 try expect(math.isPositiveInf(@sqrt(@as(f16, math.inf(f16)))));
63 try expect(@sqrt(@as(f16, 0.0)) == 0.0);
64 try expect(@sqrt(@as(f16, -0.0)) == -0.0);
65 try expect(math.isNan(@sqrt(@as(f16, -1.0))));
66 try expect(math.isNan(@sqrt(@as(f16, math.nan(f16)))));
6767}
6868
6969test "@sin" {
70 comptime testSin();
71 testSin();
70 comptime try testSin();
71 try testSin();
7272}
7373
74fn testSin() void {
74fn testSin() !void {
7575 // TODO test f128, and c_longdouble
7676 // https://github.com/ziglang/zig/issues/4026
7777 {
7878 var a: f16 = 0;
79 expect(@sin(a) == 0);
79 try expect(@sin(a) == 0);
8080 }
8181 {
8282 var a: f32 = 0;
83 expect(@sin(a) == 0);
83 try expect(@sin(a) == 0);
8484 }
8585 {
8686 var a: f64 = 0;
87 expect(@sin(a) == 0);
87 try expect(@sin(a) == 0);
8888 }
8989 {
9090 var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 3.3, 4.4 };
9191 var result = @sin(v);
92 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));
94 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));
92 try expect(math.approxEqAbs(f32, @sin(@as(f32, 1.1)), result[0], epsilon));
93 try expect(math.approxEqAbs(f32, @sin(@as(f32, 2.2)), result[1], epsilon));
94 try expect(math.approxEqAbs(f32, @sin(@as(f32, 3.3)), result[2], epsilon));
95 try expect(math.approxEqAbs(f32, @sin(@as(f32, 4.4)), result[3], epsilon));
9696 }
9797}
9898
9999test "@cos" {
100 comptime testCos();
101 testCos();
100 comptime try testCos();
101 try testCos();
102102}
103103
104fn testCos() void {
104fn testCos() !void {
105105 // TODO test f128, and c_longdouble
106106 // https://github.com/ziglang/zig/issues/4026
107107 {
108108 var a: f16 = 0;
109 expect(@cos(a) == 1);
109 try expect(@cos(a) == 1);
110110 }
111111 {
112112 var a: f32 = 0;
113 expect(@cos(a) == 1);
113 try expect(@cos(a) == 1);
114114 }
115115 {
116116 var a: f64 = 0;
117 expect(@cos(a) == 1);
117 try expect(@cos(a) == 1);
118118 }
119119 {
120120 var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 3.3, 4.4 };
121121 var result = @cos(v);
122 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));
124 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));
122 try expect(math.approxEqAbs(f32, @cos(@as(f32, 1.1)), result[0], epsilon));
123 try expect(math.approxEqAbs(f32, @cos(@as(f32, 2.2)), result[1], epsilon));
124 try expect(math.approxEqAbs(f32, @cos(@as(f32, 3.3)), result[2], epsilon));
125 try expect(math.approxEqAbs(f32, @cos(@as(f32, 4.4)), result[3], epsilon));
126126 }
127127}
128128
129129test "@exp" {
130 comptime testExp();
131 testExp();
130 comptime try testExp();
131 try testExp();
132132}
133133
134fn testExp() void {
134fn testExp() !void {
135135 // TODO test f128, and c_longdouble
136136 // https://github.com/ziglang/zig/issues/4026
137137 {
138138 var a: f16 = 0;
139 expect(@exp(a) == 1);
139 try expect(@exp(a) == 1);
140140 }
141141 {
142142 var a: f32 = 0;
143 expect(@exp(a) == 1);
143 try expect(@exp(a) == 1);
144144 }
145145 {
146146 var a: f64 = 0;
147 expect(@exp(a) == 1);
147 try expect(@exp(a) == 1);
148148 }
149149 {
150150 var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 };
151151 var result = @exp(v);
152 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));
154 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));
152 try expect(math.approxEqAbs(f32, @exp(@as(f32, 1.1)), result[0], epsilon));
153 try expect(math.approxEqAbs(f32, @exp(@as(f32, 2.2)), result[1], epsilon));
154 try expect(math.approxEqAbs(f32, @exp(@as(f32, 0.3)), result[2], epsilon));
155 try expect(math.approxEqAbs(f32, @exp(@as(f32, 0.4)), result[3], epsilon));
156156 }
157157}
158158
159159test "@exp2" {
160 comptime testExp2();
161 testExp2();
160 comptime try testExp2();
161 try testExp2();
162162}
163163
164fn testExp2() void {
164fn testExp2() !void {
165165 // TODO test f128, and c_longdouble
166166 // https://github.com/ziglang/zig/issues/4026
167167 {
168168 var a: f16 = 2;
169 expect(@exp2(a) == 4);
169 try expect(@exp2(a) == 4);
170170 }
171171 {
172172 var a: f32 = 2;
173 expect(@exp2(a) == 4);
173 try expect(@exp2(a) == 4);
174174 }
175175 {
176176 var a: f64 = 2;
177 expect(@exp2(a) == 4);
177 try expect(@exp2(a) == 4);
178178 }
179179 {
180180 var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 };
181181 var result = @exp2(v);
182 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));
184 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));
182 try expect(math.approxEqAbs(f32, @exp2(@as(f32, 1.1)), result[0], epsilon));
183 try expect(math.approxEqAbs(f32, @exp2(@as(f32, 2.2)), result[1], epsilon));
184 try expect(math.approxEqAbs(f32, @exp2(@as(f32, 0.3)), result[2], epsilon));
185 try expect(math.approxEqAbs(f32, @exp2(@as(f32, 0.4)), result[3], epsilon));
186186 }
187187}
188188
189189test "@log" {
190190 // Old musl (and glibc?), and our current math.ln implementation do not return 1
191191 // so also accept those values.
192 comptime testLog();
193 testLog();
192 comptime try testLog();
193 try testLog();
194194}
195195
196fn testLog() void {
196fn testLog() !void {
197197 // TODO test f128, and c_longdouble
198198 // https://github.com/ziglang/zig/issues/4026
199199 {
200200 var a: f16 = e;
201 expect(math.approxEqAbs(f16, @log(a), 1, epsilon));
201 try expect(math.approxEqAbs(f16, @log(a), 1, epsilon));
202202 }
203203 {
204204 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)));
206206 }
207207 {
208208 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)));
210210 }
211211 {
212212 var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 };
213213 var result = @log(v);
214 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));
216 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));
214 try expect(math.approxEqAbs(f32, @log(@as(f32, 1.1)), result[0], epsilon));
215 try expect(math.approxEqAbs(f32, @log(@as(f32, 2.2)), result[1], epsilon));
216 try expect(math.approxEqAbs(f32, @log(@as(f32, 0.3)), result[2], epsilon));
217 try expect(math.approxEqAbs(f32, @log(@as(f32, 0.4)), result[3], epsilon));
218218 }
219219}
220220
221221test "@log2" {
222 comptime testLog2();
223 testLog2();
222 comptime try testLog2();
223 try testLog2();
224224}
225225
226fn testLog2() void {
226fn testLog2() !void {
227227 // TODO test f128, and c_longdouble
228228 // https://github.com/ziglang/zig/issues/4026
229229 {
230230 var a: f16 = 4;
231 expect(@log2(a) == 2);
231 try expect(@log2(a) == 2);
232232 }
233233 {
234234 var a: f32 = 4;
235 expect(@log2(a) == 2);
235 try expect(@log2(a) == 2);
236236 }
237237 {
238238 var a: f64 = 4;
239 expect(@log2(a) == 2);
239 try expect(@log2(a) == 2);
240240 }
241241 {
242242 var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 };
243243 var result = @log2(v);
244 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));
246 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));
244 try expect(math.approxEqAbs(f32, @log2(@as(f32, 1.1)), result[0], epsilon));
245 try expect(math.approxEqAbs(f32, @log2(@as(f32, 2.2)), result[1], epsilon));
246 try expect(math.approxEqAbs(f32, @log2(@as(f32, 0.3)), result[2], epsilon));
247 try expect(math.approxEqAbs(f32, @log2(@as(f32, 0.4)), result[3], epsilon));
248248 }
249249}
250250
251251test "@log10" {
252 comptime testLog10();
253 testLog10();
252 comptime try testLog10();
253 try testLog10();
254254}
255255
256fn testLog10() void {
256fn testLog10() !void {
257257 // TODO test f128, and c_longdouble
258258 // https://github.com/ziglang/zig/issues/4026
259259 {
260260 var a: f16 = 100;
261 expect(@log10(a) == 2);
261 try expect(@log10(a) == 2);
262262 }
263263 {
264264 var a: f32 = 100;
265 expect(@log10(a) == 2);
265 try expect(@log10(a) == 2);
266266 }
267267 {
268268 var a: f64 = 1000;
269 expect(@log10(a) == 3);
269 try expect(@log10(a) == 3);
270270 }
271271 {
272272 var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 };
273273 var result = @log10(v);
274 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));
276 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));
274 try expect(math.approxEqAbs(f32, @log10(@as(f32, 1.1)), result[0], epsilon));
275 try expect(math.approxEqAbs(f32, @log10(@as(f32, 2.2)), result[1], epsilon));
276 try expect(math.approxEqAbs(f32, @log10(@as(f32, 0.3)), result[2], epsilon));
277 try expect(math.approxEqAbs(f32, @log10(@as(f32, 0.4)), result[3], epsilon));
278278 }
279279}
280280
281281test "@fabs" {
282 comptime testFabs();
283 testFabs();
282 comptime try testFabs();
283 try testFabs();
284284}
285285
286fn testFabs() void {
286fn testFabs() !void {
287287 // TODO test f128, and c_longdouble
288288 // https://github.com/ziglang/zig/issues/4026
289289 {
290290 var a: f16 = -2.5;
291291 var b: f16 = 2.5;
292 expect(@fabs(a) == 2.5);
293 expect(@fabs(b) == 2.5);
292 try expect(@fabs(a) == 2.5);
293 try expect(@fabs(b) == 2.5);
294294 }
295295 {
296296 var a: f32 = -2.5;
297297 var b: f32 = 2.5;
298 expect(@fabs(a) == 2.5);
299 expect(@fabs(b) == 2.5);
298 try expect(@fabs(a) == 2.5);
299 try expect(@fabs(b) == 2.5);
300300 }
301301 {
302302 var a: f64 = -2.5;
303303 var b: f64 = 2.5;
304 expect(@fabs(a) == 2.5);
305 expect(@fabs(b) == 2.5);
304 try expect(@fabs(a) == 2.5);
305 try expect(@fabs(b) == 2.5);
306306 }
307307 {
308308 var v: Vector(4, f32) = [_]f32{ 1.1, -2.2, 0.3, -0.4 };
309309 var result = @fabs(v);
310 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));
312 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));
310 try expect(math.approxEqAbs(f32, @fabs(@as(f32, 1.1)), result[0], epsilon));
311 try expect(math.approxEqAbs(f32, @fabs(@as(f32, -2.2)), result[1], epsilon));
312 try expect(math.approxEqAbs(f32, @fabs(@as(f32, 0.3)), result[2], epsilon));
313 try expect(math.approxEqAbs(f32, @fabs(@as(f32, -0.4)), result[3], epsilon));
314314 }
315315}
316316
317317test "@floor" {
318 comptime testFloor();
319 testFloor();
318 comptime try testFloor();
319 try testFloor();
320320}
321321
322fn testFloor() void {
322fn testFloor() !void {
323323 // TODO test f128, and c_longdouble
324324 // https://github.com/ziglang/zig/issues/4026
325325 {
326326 var a: f16 = 2.1;
327 expect(@floor(a) == 2);
327 try expect(@floor(a) == 2);
328328 }
329329 {
330330 var a: f32 = 2.1;
331 expect(@floor(a) == 2);
331 try expect(@floor(a) == 2);
332332 }
333333 {
334334 var a: f64 = 3.5;
335 expect(@floor(a) == 3);
335 try expect(@floor(a) == 3);
336336 }
337337 {
338338 var v: Vector(4, f32) = [_]f32{ 1.1, -2.2, 0.3, -0.4 };
339339 var result = @floor(v);
340 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));
342 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));
340 try expect(math.approxEqAbs(f32, @floor(@as(f32, 1.1)), result[0], epsilon));
341 try expect(math.approxEqAbs(f32, @floor(@as(f32, -2.2)), result[1], epsilon));
342 try expect(math.approxEqAbs(f32, @floor(@as(f32, 0.3)), result[2], epsilon));
343 try expect(math.approxEqAbs(f32, @floor(@as(f32, -0.4)), result[3], epsilon));
344344 }
345345}
346346
347347test "@ceil" {
348 comptime testCeil();
349 testCeil();
348 comptime try testCeil();
349 try testCeil();
350350}
351351
352fn testCeil() void {
352fn testCeil() !void {
353353 // TODO test f128, and c_longdouble
354354 // https://github.com/ziglang/zig/issues/4026
355355 {
356356 var a: f16 = 2.1;
357 expect(@ceil(a) == 3);
357 try expect(@ceil(a) == 3);
358358 }
359359 {
360360 var a: f32 = 2.1;
361 expect(@ceil(a) == 3);
361 try expect(@ceil(a) == 3);
362362 }
363363 {
364364 var a: f64 = 3.5;
365 expect(@ceil(a) == 4);
365 try expect(@ceil(a) == 4);
366366 }
367367 {
368368 var v: Vector(4, f32) = [_]f32{ 1.1, -2.2, 0.3, -0.4 };
369369 var result = @ceil(v);
370 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));
372 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));
370 try expect(math.approxEqAbs(f32, @ceil(@as(f32, 1.1)), result[0], epsilon));
371 try expect(math.approxEqAbs(f32, @ceil(@as(f32, -2.2)), result[1], epsilon));
372 try expect(math.approxEqAbs(f32, @ceil(@as(f32, 0.3)), result[2], epsilon));
373 try expect(math.approxEqAbs(f32, @ceil(@as(f32, -0.4)), result[3], epsilon));
374374 }
375375}
376376
377377test "@trunc" {
378 comptime testTrunc();
379 testTrunc();
378 comptime try testTrunc();
379 try testTrunc();
380380}
381381
382fn testTrunc() void {
382fn testTrunc() !void {
383383 // TODO test f128, and c_longdouble
384384 // https://github.com/ziglang/zig/issues/4026
385385 {
386386 var a: f16 = 2.1;
387 expect(@trunc(a) == 2);
387 try expect(@trunc(a) == 2);
388388 }
389389 {
390390 var a: f32 = 2.1;
391 expect(@trunc(a) == 2);
391 try expect(@trunc(a) == 2);
392392 }
393393 {
394394 var a: f64 = -3.5;
395 expect(@trunc(a) == -3);
395 try expect(@trunc(a) == -3);
396396 }
397397 {
398398 var v: Vector(4, f32) = [_]f32{ 1.1, -2.2, 0.3, -0.4 };
399399 var result = @trunc(v);
400 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));
402 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));
400 try expect(math.approxEqAbs(f32, @trunc(@as(f32, 1.1)), result[0], epsilon));
401 try expect(math.approxEqAbs(f32, @trunc(@as(f32, -2.2)), result[1], epsilon));
402 try expect(math.approxEqAbs(f32, @trunc(@as(f32, 0.3)), result[2], epsilon));
403 try expect(math.approxEqAbs(f32, @trunc(@as(f32, -0.4)), result[3], epsilon));
404404 }
405405}
406406
407407test "floating point comparisons" {
408 testFloatComparisons();
409 comptime testFloatComparisons();
408 try testFloatComparisons();
409 comptime try testFloatComparisons();
410410}
411411
412fn testFloatComparisons() void {
412fn testFloatComparisons() !void {
413413 inline for ([_]type{ f16, f32, f64, f128 }) |ty| {
414414 // No decimal part
415415 {
416416 const x: ty = 1.0;
417 expect(x == 1);
418 expect(x != 0);
419 expect(x > 0);
420 expect(x < 2);
421 expect(x >= 1);
422 expect(x <= 1);
417 try expect(x == 1);
418 try expect(x != 0);
419 try expect(x > 0);
420 try expect(x < 2);
421 try expect(x >= 1);
422 try expect(x <= 1);
423423 }
424424 // Non-zero decimal part
425425 {
426426 const x: ty = 1.5;
427 expect(x != 1);
428 expect(x != 2);
429 expect(x > 1);
430 expect(x < 2);
431 expect(x >= 1);
432 expect(x <= 2);
427 try expect(x != 1);
428 try expect(x != 2);
429 try expect(x > 1);
430 try expect(x < 2);
431 try expect(x >= 1);
432 try expect(x <= 2);
433433 }
434434 }
435435}
436436
437437test "different sized float comparisons" {
438 testDifferentSizedFloatComparisons();
439 comptime testDifferentSizedFloatComparisons();
438 try testDifferentSizedFloatComparisons();
439 comptime try testDifferentSizedFloatComparisons();
440440}
441441
442fn testDifferentSizedFloatComparisons() void {
442fn testDifferentSizedFloatComparisons() !void {
443443 var a: f16 = 1;
444444 var b: f64 = 2;
445 expect(a < b);
445 try expect(a < b);
446446}
447447
448448// 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 {
456456// // https://github.com/ziglang/zig/issues/4026
457457// {
458458// var a: f32 = 2.1;
459// expect(@nearbyint(a) == 2);
459// try expect(@nearbyint(a) == 2);
460460// }
461461// {
462462// var a: f64 = -3.75;
463// expect(@nearbyint(a) == -4);
463// try expect(@nearbyint(a) == -4);
464464// }
465465//}
test/stage1/behavior/fn.zig+41-41
......@@ -4,7 +4,7 @@ const expect = testing.expect;
44const expectEqual = testing.expectEqual;
55
66test "params" {
7 expect(testParamsAdd(22, 11) == 33);
7 try expect(testParamsAdd(22, 11) == 33);
88}
99fn testParamsAdd(a: i32, b: i32) i32 {
1010 return a + b;
......@@ -19,37 +19,37 @@ fn testLocVars(b: i32) void {
1919}
2020
2121test "void parameters" {
22 voidFun(1, void{}, 2, {});
22 try voidFun(1, void{}, 2, {});
2323}
24fn voidFun(a: i32, b: void, c: i32, d: void) void {
24fn voidFun(a: i32, b: void, c: i32, d: void) !void {
2525 const v = b;
2626 const vv: void = if (a == 1) v else {};
27 expect(a + c == 3);
27 try expect(a + c == 3);
2828 return vv;
2929}
3030
3131test "mutable local variables" {
3232 var zero: i32 = 0;
33 expect(zero == 0);
33 try expect(zero == 0);
3434
3535 var i = @as(i32, 0);
3636 while (i != 3) {
3737 i += 1;
3838 }
39 expect(i == 3);
39 try expect(i == 3);
4040}
4141
4242test "separate block scopes" {
4343 {
4444 const no_conflict: i32 = 5;
45 expect(no_conflict == 5);
45 try expect(no_conflict == 5);
4646 }
4747
4848 const c = x: {
4949 const no_conflict = @as(i32, 10);
5050 break :x no_conflict;
5151 };
52 expect(c == 10);
52 try expect(c == 10);
5353}
5454
5555test "call function with empty string" {
......@@ -62,7 +62,7 @@ fn @"weird function name"() i32 {
6262 return 1234;
6363}
6464test "weird function name" {
65 expect(@"weird function name"() == 1234);
65 try expect(@"weird function name"() == 1234);
6666}
6767
6868test "implicit cast function unreachable return" {
......@@ -83,7 +83,7 @@ test "function pointers" {
8383 fn4,
8484 };
8585 for (fns) |f, i| {
86 expect(f() == @intCast(u32, i) + 5);
86 try expect(f() == @intCast(u32, i) + 5);
8787 }
8888}
8989fn fn1() u32 {
......@@ -100,12 +100,12 @@ fn fn4() u32 {
100100}
101101
102102test "number literal as an argument" {
103 numberLiteralArg(3);
104 comptime numberLiteralArg(3);
103 try numberLiteralArg(3);
104 comptime try numberLiteralArg(3);
105105}
106106
107fn numberLiteralArg(a: anytype) void {
108 expect(a == 3);
107fn numberLiteralArg(a: anytype) !void {
108 try expect(a == 3);
109109}
110110
111111test "assign inline fn to const variable" {
......@@ -116,7 +116,7 @@ test "assign inline fn to const variable" {
116116fn inlineFn() callconv(.Inline) void {}
117117
118118test "pass by non-copying value" {
119 expect(addPointCoords(Point{ .x = 1, .y = 2 }) == 3);
119 try expect(addPointCoords(Point{ .x = 1, .y = 2 }) == 3);
120120}
121121
122122const Point = struct {
......@@ -129,17 +129,17 @@ fn addPointCoords(pt: Point) i32 {
129129}
130130
131131test "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);
133133}
134134
135fn addPointCoordsVar(pt: anytype) i32 {
136 comptime expect(@TypeOf(pt) == Point);
135fn addPointCoordsVar(pt: anytype) !i32 {
136 comptime try expect(@TypeOf(pt) == Point);
137137 return pt.x + pt.y;
138138}
139139
140140test "pass by non-copying value as method" {
141141 var pt = Point2{ .x = 1, .y = 2 };
142 expect(pt.addPointCoords() == 3);
142 try expect(pt.addPointCoords() == 3);
143143}
144144
145145const Point2 = struct {
......@@ -153,7 +153,7 @@ const Point2 = struct {
153153
154154test "pass by non-copying value as method, which is generic" {
155155 var pt = Point3{ .x = 1, .y = 2 };
156 expect(pt.addPointCoords(i32) == 3);
156 try expect(pt.addPointCoords(i32) == 3);
157157}
158158
159159const Point3 = struct {
......@@ -168,7 +168,7 @@ const Point3 = struct {
168168test "pass by non-copying value as method, at comptime" {
169169 comptime {
170170 var pt = Point2{ .x = 1, .y = 2 };
171 expect(pt.addPointCoords() == 3);
171 try expect(pt.addPointCoords() == 3);
172172 }
173173}
174174
......@@ -184,7 +184,7 @@ fn outer(y: u32) fn (u32) u32 {
184184
185185test "return inner function which references comptime variable of outer function" {
186186 var func = outer(10);
187 expect(func(3) == 7);
187 try expect(func(3) == 7);
188188}
189189
190190test "extern struct with stdcallcc fn pointer" {
......@@ -198,16 +198,16 @@ test "extern struct with stdcallcc fn pointer" {
198198
199199 var s: S = undefined;
200200 s.ptr = S.foo;
201 expect(s.ptr() == 1234);
201 try expect(s.ptr() == 1234);
202202}
203203
204204test "implicit cast fn call result to optional in field result" {
205205 const S = struct {
206 fn entry() void {
206 fn entry() !void {
207207 var x = Foo{
208208 .field = optionalPtr(),
209209 };
210 expect(x.field.?.* == 999);
210 try expect(x.field.?.* == 999);
211211 }
212212
213213 const glob: i32 = 999;
......@@ -220,8 +220,8 @@ test "implicit cast fn call result to optional in field result" {
220220 field: ?*const i32,
221221 };
222222 };
223 S.entry();
224 comptime S.entry();
223 try S.entry();
224 comptime try S.entry();
225225}
226226
227227test "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
246246test "function call with anon list literal" {
247247 const S = struct {
248 fn doTheTest() void {
249 consumeVec(.{ 9, 8, 7 });
248 fn doTheTest() !void {
249 try consumeVec(.{ 9, 8, 7 });
250250 }
251251
252 fn consumeVec(vec: [3]f32) void {
253 expect(vec[0] == 9);
254 expect(vec[1] == 8);
255 expect(vec[2] == 7);
252 fn consumeVec(vec: [3]f32) !void {
253 try expect(vec[0] == 9);
254 try expect(vec[1] == 8);
255 try expect(vec[2] == 7);
256256 }
257257 };
258 S.doTheTest();
259 comptime S.doTheTest();
258 try S.doTheTest();
259 comptime try S.doTheTest();
260260}
261261
262262test "ability to give comptime types and non comptime types to same parameter" {
263263 const S = struct {
264 fn doTheTest() void {
264 fn doTheTest() !void {
265265 var x: i32 = 1;
266 expect(foo(x) == 10);
267 expect(foo(i32) == 20);
266 try expect(foo(x) == 10);
267 try expect(foo(i32) == 20);
268268 }
269269
270270 fn foo(arg: anytype) i32 {
......@@ -272,8 +272,8 @@ test "ability to give comptime types and non comptime types to same parameter" {
272272 return 9 + arg;
273273 }
274274 };
275 S.doTheTest();
276 comptime S.doTheTest();
275 try S.doTheTest();
276 comptime try S.doTheTest();
277277}
278278
279279test "function with inferred error set but returning no error" {
......@@ -282,5 +282,5 @@ test "function with inferred error set but returning no error" {
282282 };
283283
284284 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);
286286}
test/stage1/behavior/fn_delegation.zig+4-4
......@@ -32,8 +32,8 @@ fn custom(comptime T: type, comptime num: u64) fn (T) u64 {
3232
3333test "fn delegation" {
3434 const foo = Foo{};
35 expect(foo.one() == 11);
36 expect(foo.two() == 12);
37 expect(foo.three() == 13);
38 expect(foo.four() == 14);
35 try expect(foo.one() == 11);
36 try expect(foo.two() == 12);
37 try expect(foo.three() == 13);
38 try expect(foo.four() == 14);
3939}
test/stage1/behavior/fn_in_struct_in_comptime.zig+1-1
......@@ -13,5 +13,5 @@ fn get_foo() fn (*u8) usize {
1313
1414test "define a function in an anonymous struct in comptime" {
1515 const foo = get_foo();
16 expect(foo(@intToPtr(*u8, 12345)) == 12345);
16 try expect(foo(@intToPtr(*u8, 12345)) == 12345);
1717}
test/stage1/behavior/for.zig+28-28
......@@ -27,12 +27,12 @@ test "for loop with pointer elem var" {
2727 var target: [source.len]u8 = undefined;
2828 mem.copy(u8, target[0..], source);
2929 mangleString(target[0..]);
30 expect(mem.eql(u8, &target, "bcdefgh"));
30 try expect(mem.eql(u8, &target, "bcdefgh"));
3131
3232 for (source) |*c, i|
33 expect(@TypeOf(c) == *const u8);
33 try expect(@TypeOf(c) == *const u8);
3434 for (target) |*c, i|
35 expect(@TypeOf(c) == *u8);
35 try expect(@TypeOf(c) == *u8);
3636}
3737
3838fn mangleString(s: []u8) void {
......@@ -75,15 +75,15 @@ test "basic for loop" {
7575 buf_index += 1;
7676 }
7777
78 expect(mem.eql(u8, buffer[0..buf_index], &expected_result));
78 try expect(mem.eql(u8, buffer[0..buf_index], &expected_result));
7979}
8080
8181test "break from outer for loop" {
82 testBreakOuter();
83 comptime testBreakOuter();
82 try testBreakOuter();
83 comptime try testBreakOuter();
8484}
8585
86fn testBreakOuter() void {
86fn testBreakOuter() !void {
8787 var array = "aoeu";
8888 var count: usize = 0;
8989 outer: for (array) |_| {
......@@ -92,15 +92,15 @@ fn testBreakOuter() void {
9292 break :outer;
9393 }
9494 }
95 expect(count == 1);
95 try expect(count == 1);
9696}
9797
9898test "continue outer for loop" {
99 testContinueOuter();
100 comptime testContinueOuter();
99 try testContinueOuter();
100 comptime try testContinueOuter();
101101}
102102
103fn testContinueOuter() void {
103fn testContinueOuter() !void {
104104 var array = "aoeu";
105105 var counter: usize = 0;
106106 outer: for (array) |_| {
......@@ -109,28 +109,28 @@ fn testContinueOuter() void {
109109 continue :outer;
110110 }
111111 }
112 expect(counter == array.len);
112 try expect(counter == array.len);
113113}
114114
115115test "2 break statements and an else" {
116116 const S = struct {
117 fn entry(t: bool, f: bool) void {
117 fn entry(t: bool, f: bool) !void {
118118 var buf: [10]u8 = undefined;
119119 var ok = false;
120120 ok = for (buf) |item| {
121121 if (f) break false;
122122 if (t) break true;
123123 } else false;
124 expect(ok);
124 try expect(ok);
125125 }
126126 };
127 S.entry(true, false);
128 comptime S.entry(true, false);
127 try S.entry(true, false);
128 comptime try S.entry(true, false);
129129}
130130
131131test "for with null and T peer types and inferred result location type" {
132132 const S = struct {
133 fn doTheTest(slice: []const u8) void {
133 fn doTheTest(slice: []const u8) !void {
134134 if (for (slice) |item| {
135135 if (item == 10) {
136136 break item;
......@@ -140,33 +140,33 @@ test "for with null and T peer types and inferred result location type" {
140140 }
141141 }
142142 };
143 S.doTheTest(&[_]u8{ 1, 2 });
144 comptime S.doTheTest(&[_]u8{ 1, 2 });
143 try S.doTheTest(&[_]u8{ 1, 2 });
144 comptime try S.doTheTest(&[_]u8{ 1, 2 });
145145}
146146
147147test "for copies its payload" {
148148 const S = struct {
149 fn doTheTest() void {
149 fn doTheTest() !void {
150150 var x = [_]usize{ 1, 2, 3 };
151151 for (x) |value, i| {
152152 // Modify the original array
153153 x[i] += 99;
154 expectEqual(value, i + 1);
154 try expectEqual(value, i + 1);
155155 }
156156 }
157157 };
158 S.doTheTest();
159 comptime S.doTheTest();
158 try S.doTheTest();
159 comptime try S.doTheTest();
160160}
161161
162162test "for on slice with allowzero ptr" {
163163 const S = struct {
164 fn doTheTest(slice: []const u8) void {
164 fn doTheTest(slice: []const u8) !void {
165165 var ptr = @ptrCast([*]allowzero const u8, slice.ptr)[0..slice.len];
166 for (ptr) |x, i| expect(x == i + 1);
167 for (ptr) |*x, i| expect(x.* == i + 1);
166 for (ptr) |x, i| try expect(x == i + 1);
167 for (ptr) |*x, i| try expect(x.* == i + 1);
168168 }
169169 };
170 S.doTheTest(&[_]u8{ 1, 2, 3, 4 });
171 comptime S.doTheTest(&[_]u8{ 1, 2, 3, 4 });
170 try S.doTheTest(&[_]u8{ 1, 2, 3, 4 });
171 comptime try S.doTheTest(&[_]u8{ 1, 2, 3, 4 });
172172}
test/stage1/behavior/generics.zig+25-25
......@@ -4,9 +4,9 @@ const expect = testing.expect;
44const expectEqual = testing.expectEqual;
55
66test "simple generic fn" {
7 expect(max(i32, 3, -1) == 3);
8 expect(max(f32, 0.123, 0.456) == 0.456);
9 expect(add(2, 3) == 5);
7 try expect(max(i32, 3, -1) == 3);
8 try expect(max(f32, 0.123, 0.456) == 0.456);
9 try expect(add(2, 3) == 5);
1010}
1111
1212fn max(comptime T: type, a: T, b: T) T {
......@@ -19,7 +19,7 @@ fn add(comptime a: i32, b: i32) i32 {
1919
2020const the_max = max(u32, 1234, 5678);
2121test "compile time generic eval" {
22 expect(the_max == 5678);
22 try expect(the_max == 5678);
2323}
2424
2525fn gimmeTheBigOne(a: u32, b: u32) u32 {
......@@ -35,19 +35,19 @@ fn sameButWithFloats(a: f64, b: f64) f64 {
3535}
3636
3737test "fn with comptime args" {
38 expect(gimmeTheBigOne(1234, 5678) == 5678);
39 expect(shouldCallSameInstance(34, 12) == 34);
40 expect(sameButWithFloats(0.43, 0.49) == 0.49);
38 try expect(gimmeTheBigOne(1234, 5678) == 5678);
39 try expect(shouldCallSameInstance(34, 12) == 34);
40 try expect(sameButWithFloats(0.43, 0.49) == 0.49);
4141}
4242
4343test "var params" {
44 expect(max_i32(12, 34) == 34);
45 expect(max_f64(1.2, 3.4) == 3.4);
44 try expect(max_i32(12, 34) == 34);
45 try expect(max_f64(1.2, 3.4) == 3.4);
4646}
4747
4848comptime {
49 expect(max_i32(12, 34) == 34);
50 expect(max_f64(1.2, 3.4) == 3.4);
49 try expect(max_i32(12, 34) == 34);
50 try expect(max_f64(1.2, 3.4) == 3.4);
5151}
5252
5353fn max_var(a: anytype, b: anytype) @TypeOf(a + b) {
......@@ -79,8 +79,8 @@ test "function with return type type" {
7979 var list2: List(i32) = undefined;
8080 list.length = 10;
8181 list2.length = 10;
82 expect(list.prealloc_items.len == 8);
83 expect(list2.prealloc_items.len == 8);
82 try expect(list.prealloc_items.len == 8);
83 try expect(list2.prealloc_items.len == 8);
8484}
8585
8686test "generic struct" {
......@@ -92,9 +92,9 @@ test "generic struct" {
9292 .value = true,
9393 .next = null,
9494 };
95 expect(a1.value == 13);
96 expect(a1.value == a1.getVal());
97 expect(b1.getVal());
95 try expect(a1.value == 13);
96 try expect(a1.value == a1.getVal());
97 try expect(b1.getVal());
9898}
9999fn GenNode(comptime T: type) type {
100100 return struct {
......@@ -107,7 +107,7 @@ fn GenNode(comptime T: type) type {
107107}
108108
109109test "const decls in struct" {
110 expect(GenericDataThing(3).count_plus_one == 4);
110 try expect(GenericDataThing(3).count_plus_one == 4);
111111}
112112fn GenericDataThing(comptime count: isize) type {
113113 return struct {
......@@ -116,15 +116,15 @@ fn GenericDataThing(comptime count: isize) type {
116116}
117117
118118test "use generic param in generic param" {
119 expect(aGenericFn(i32, 3, 4) == 7);
119 try expect(aGenericFn(i32, 3, 4) == 7);
120120}
121121fn aGenericFn(comptime T: type, comptime a: T, b: T) T {
122122 return a + b;
123123}
124124
125125test "generic fn with implicit cast" {
126 expect(getFirstByte(u8, &[_]u8{13}) == 13);
127 expect(getFirstByte(u16, &[_]u16{
126 try expect(getFirstByte(u8, &[_]u8{13}) == 13);
127 try expect(getFirstByte(u16, &[_]u16{
128128 0,
129129 13,
130130 }) == 0);
......@@ -149,21 +149,21 @@ fn foo2(arg: anytype) bool {
149149}
150150
151151test "array of generic fns" {
152 expect(foos[0](true));
153 expect(!foos[1](true));
152 try expect(foos[0](true));
153 try expect(!foos[1](true));
154154}
155155
156156test "generic fn keeps non-generic parameter types" {
157157 const A = 128;
158158
159159 const S = struct {
160 fn f(comptime T: type, s: []T) void {
161 expect(A != @typeInfo(@TypeOf(s)).Pointer.alignment);
160 fn f(comptime T: type, s: []T) !void {
161 try expect(A != @typeInfo(@TypeOf(s)).Pointer.alignment);
162162 }
163163 };
164164
165165 // The compiler monomorphizes `S.f` for `T=u8` on its first use, check that
166166 // `x` type not affect `s` parameter type.
167167 var x: [16]u8 align(A) = undefined;
168 S.f(u8, &x);
168 try S.f(u8, &x);
169169}
test/stage1/behavior/hasdecl.zig+6-6
......@@ -11,11 +11,11 @@ const Bar = struct {
1111};
1212
1313test "@hasDecl" {
14 expect(@hasDecl(Foo, "public_thing"));
15 expect(!@hasDecl(Foo, "private_thing"));
16 expect(!@hasDecl(Foo, "no_thing"));
14 try expect(@hasDecl(Foo, "public_thing"));
15 try expect(!@hasDecl(Foo, "private_thing"));
16 try expect(!@hasDecl(Foo, "no_thing"));
1717
18 expect(@hasDecl(Bar, "hi"));
19 expect(@hasDecl(Bar, "blah"));
20 expect(!@hasDecl(Bar, "nope"));
18 try expect(@hasDecl(Bar, "hi"));
19 try expect(@hasDecl(Bar, "blah"));
20 try expect(!@hasDecl(Bar, "nope"));
2121}
test/stage1/behavior/hasfield.zig+12-12
......@@ -8,10 +8,10 @@ test "@hasField" {
88
99 pub const nope = 1;
1010 };
11 expect(@hasField(struc, "a") == true);
12 expect(@hasField(struc, "b") == true);
13 expect(@hasField(struc, "non-existant") == false);
14 expect(@hasField(struc, "nope") == false);
11 try expect(@hasField(struc, "a") == true);
12 try expect(@hasField(struc, "b") == true);
13 try expect(@hasField(struc, "non-existant") == false);
14 try expect(@hasField(struc, "nope") == false);
1515
1616 const unin = union {
1717 a: u64,
......@@ -19,10 +19,10 @@ test "@hasField" {
1919
2020 pub const nope = 1;
2121 };
22 expect(@hasField(unin, "a") == true);
23 expect(@hasField(unin, "b") == true);
24 expect(@hasField(unin, "non-existant") == false);
25 expect(@hasField(unin, "nope") == false);
22 try expect(@hasField(unin, "a") == true);
23 try expect(@hasField(unin, "b") == true);
24 try expect(@hasField(unin, "non-existant") == false);
25 try expect(@hasField(unin, "nope") == false);
2626
2727 const enm = enum {
2828 a,
......@@ -30,8 +30,8 @@ test "@hasField" {
3030
3131 pub const nope = 1;
3232 };
33 expect(@hasField(enm, "a") == true);
34 expect(@hasField(enm, "b") == true);
35 expect(@hasField(enm, "non-existant") == false);
36 expect(@hasField(enm, "nope") == false);
33 try expect(@hasField(enm, "a") == true);
34 try expect(@hasField(enm, "b") == true);
35 try expect(@hasField(enm, "non-existant") == false);
36 try expect(@hasField(enm, "nope") == false);
3737}
test/stage1/behavior/if.zig+14-14
......@@ -26,7 +26,7 @@ fn firstEqlThird(a: i32, b: i32, c: i32) void {
2626}
2727
2828test "else if expression" {
29 expect(elseIfExpressionF(1) == 1);
29 try expect(elseIfExpressionF(1) == 1);
3030}
3131fn elseIfExpressionF(c: u8) u8 {
3232 if (c == 0) {
......@@ -44,14 +44,14 @@ var global_with_err: anyerror!u32 = error.SomeError;
4444
4545test "unwrap mutable global var" {
4646 if (global_with_val) |v| {
47 expect(v == 0);
47 try expect(v == 0);
4848 } else |e| {
4949 unreachable;
5050 }
5151 if (global_with_err) |_| {
5252 unreachable;
5353 } else |e| {
54 expect(e == error.SomeError);
54 try expect(e == error.SomeError);
5555 }
5656}
5757
......@@ -63,7 +63,7 @@ test "labeled break inside comptime if inside runtime if" {
6363 break :blk @as(i32, 42);
6464 };
6565 }
66 expect(answer == 42);
66 try expect(answer == 42);
6767}
6868
6969test "const result loc, runtime if cond, else unreachable" {
......@@ -74,36 +74,36 @@ test "const result loc, runtime if cond, else unreachable" {
7474
7575 var t = true;
7676 const x = if (t) Num.Two else unreachable;
77 expect(x == .Two);
77 try expect(x == .Two);
7878}
7979
8080test "if prongs cast to expected type instead of peer type resolution" {
8181 const S = struct {
82 fn doTheTest(f: bool) void {
82 fn doTheTest(f: bool) !void {
8383 var x: i32 = 0;
8484 x = if (f) 1 else 2;
85 expect(x == 2);
85 try expect(x == 2);
8686
8787 var b = true;
8888 const y: i32 = if (b) 1 else 2;
89 expect(y == 1);
89 try expect(y == 1);
9090 }
9191 };
92 S.doTheTest(false);
93 comptime S.doTheTest(false);
92 try S.doTheTest(false);
93 comptime try S.doTheTest(false);
9494}
9595
9696test "while copies its payload" {
9797 const S = struct {
98 fn doTheTest() void {
98 fn doTheTest() !void {
9999 var tmp: ?i32 = 10;
100100 if (tmp) |value| {
101101 // Modify the original variable
102102 tmp = null;
103 expectEqual(@as(i32, 10), value);
103 try expectEqual(@as(i32, 10), value);
104104 } else unreachable;
105105 }
106106 };
107 S.doTheTest();
108 comptime S.doTheTest();
107 try S.doTheTest();
108 comptime try S.doTheTest();
109109}
test/stage1/behavior/import.zig+3-3
......@@ -3,18 +3,18 @@ const expectEqual = @import("std").testing.expectEqual;
33const a_namespace = @import("import/a_namespace.zig");
44
55test "call fn via namespace lookup" {
6 expectEqual(@as(i32, 1234), a_namespace.foo());
6 try expectEqual(@as(i32, 1234), a_namespace.foo());
77}
88
99test "importing the same thing gives the same import" {
10 expect(@import("std") == @import("std"));
10 try expect(@import("std") == @import("std"));
1111}
1212
1313test "import in non-toplevel scope" {
1414 const S = struct {
1515 usingnamespace @import("import/a_namespace.zig");
1616 };
17 expectEqual(@as(i32, 1234), S.foo());
17 try expectEqual(@as(i32, 1234), S.foo());
1818}
1919
2020test "import empty file" {
test/stage1/behavior/incomplete_struct_param_tld.zig+1-1
......@@ -26,5 +26,5 @@ test "incomplete struct param top level declaration" {
2626 .c = C{ .x = 13 },
2727 },
2828 };
29 expect(foo(a) == 13);
29 try expect(foo(a) == 13);
3030}
test/stage1/behavior/inttoptr.zig-4
......@@ -1,7 +1,3 @@
1const builtin = @import("builtin");
2const std = @import("std");
3const expect = std.testing.expect;
4
51test "casting random address to function pointer" {
62 randomAddressToFunction();
73 comptime randomAddressToFunction();
test/stage1/behavior/ir_block_deps.zig+2-2
......@@ -16,6 +16,6 @@ fn getErrInt() anyerror!i32 {
1616}
1717
1818test "ir block deps" {
19 expect((foo(1) catch unreachable) == 0);
20 expect((foo(2) catch unreachable) == 0);
19 try expect((foo(1) catch unreachable) == 0);
20 try expect((foo(2) catch unreachable) == 0);
2121}
test/stage1/behavior/math.zig+357-357
......@@ -7,71 +7,71 @@ const minInt = std.math.minInt;
77const mem = std.mem;
88
99test "division" {
10 testDivision();
11 comptime testDivision();
12}
13fn testDivision() void {
14 expect(div(u32, 13, 3) == 4);
15 expect(div(f16, 1.0, 2.0) == 0.5);
16 expect(div(f32, 1.0, 2.0) == 0.5);
17
18 expect(divExact(u32, 55, 11) == 5);
19 expect(divExact(i32, -55, 11) == -5);
20 expect(divExact(f16, 55.0, 11.0) == 5.0);
21 expect(divExact(f16, -55.0, 11.0) == -5.0);
22 expect(divExact(f32, 55.0, 11.0) == 5.0);
23 expect(divExact(f32, -55.0, 11.0) == -5.0);
24
25 expect(divFloor(i32, 5, 3) == 1);
26 expect(divFloor(i32, -5, 3) == -2);
27 expect(divFloor(f16, 5.0, 3.0) == 1.0);
28 expect(divFloor(f16, -5.0, 3.0) == -2.0);
29 expect(divFloor(f32, 5.0, 3.0) == 1.0);
30 expect(divFloor(f32, -5.0, 3.0) == -2.0);
31 expect(divFloor(i32, -0x80000000, -2) == 0x40000000);
32 expect(divFloor(i32, 0, -0x80000000) == 0);
33 expect(divFloor(i32, -0x40000001, 0x40000000) == -2);
34 expect(divFloor(i32, -0x80000000, 1) == -0x80000000);
35 expect(divFloor(i32, 10, 12) == 0);
36 expect(divFloor(i32, -14, 12) == -2);
37 expect(divFloor(i32, -2, 12) == -1);
38
39 expect(divTrunc(i32, 5, 3) == 1);
40 expect(divTrunc(i32, -5, 3) == -1);
41 expect(divTrunc(f16, 5.0, 3.0) == 1.0);
42 expect(divTrunc(f16, -5.0, 3.0) == -1.0);
43 expect(divTrunc(f32, 5.0, 3.0) == 1.0);
44 expect(divTrunc(f32, -5.0, 3.0) == -1.0);
45 expect(divTrunc(f64, 5.0, 3.0) == 1.0);
46 expect(divTrunc(f64, -5.0, 3.0) == -1.0);
47 expect(divTrunc(i32, 10, 12) == 0);
48 expect(divTrunc(i32, -14, 12) == -1);
49 expect(divTrunc(i32, -2, 12) == 0);
50
51 expect(mod(i32, 10, 12) == 10);
52 expect(mod(i32, -14, 12) == 10);
53 expect(mod(i32, -2, 12) == 10);
10 try testDivision();
11 comptime try testDivision();
12}
13fn testDivision() !void {
14 try expect(div(u32, 13, 3) == 4);
15 try expect(div(f16, 1.0, 2.0) == 0.5);
16 try expect(div(f32, 1.0, 2.0) == 0.5);
17
18 try expect(divExact(u32, 55, 11) == 5);
19 try expect(divExact(i32, -55, 11) == -5);
20 try expect(divExact(f16, 55.0, 11.0) == 5.0);
21 try expect(divExact(f16, -55.0, 11.0) == -5.0);
22 try expect(divExact(f32, 55.0, 11.0) == 5.0);
23 try expect(divExact(f32, -55.0, 11.0) == -5.0);
24
25 try expect(divFloor(i32, 5, 3) == 1);
26 try expect(divFloor(i32, -5, 3) == -2);
27 try expect(divFloor(f16, 5.0, 3.0) == 1.0);
28 try expect(divFloor(f16, -5.0, 3.0) == -2.0);
29 try expect(divFloor(f32, 5.0, 3.0) == 1.0);
30 try expect(divFloor(f32, -5.0, 3.0) == -2.0);
31 try expect(divFloor(i32, -0x80000000, -2) == 0x40000000);
32 try expect(divFloor(i32, 0, -0x80000000) == 0);
33 try expect(divFloor(i32, -0x40000001, 0x40000000) == -2);
34 try expect(divFloor(i32, -0x80000000, 1) == -0x80000000);
35 try expect(divFloor(i32, 10, 12) == 0);
36 try expect(divFloor(i32, -14, 12) == -2);
37 try expect(divFloor(i32, -2, 12) == -1);
38
39 try expect(divTrunc(i32, 5, 3) == 1);
40 try expect(divTrunc(i32, -5, 3) == -1);
41 try expect(divTrunc(f16, 5.0, 3.0) == 1.0);
42 try expect(divTrunc(f16, -5.0, 3.0) == -1.0);
43 try expect(divTrunc(f32, 5.0, 3.0) == 1.0);
44 try expect(divTrunc(f32, -5.0, 3.0) == -1.0);
45 try expect(divTrunc(f64, 5.0, 3.0) == 1.0);
46 try expect(divTrunc(f64, -5.0, 3.0) == -1.0);
47 try expect(divTrunc(i32, 10, 12) == 0);
48 try expect(divTrunc(i32, -14, 12) == -1);
49 try expect(divTrunc(i32, -2, 12) == 0);
50
51 try expect(mod(i32, 10, 12) == 10);
52 try expect(mod(i32, -14, 12) == 10);
53 try expect(mod(i32, -2, 12) == 10);
5454
5555 comptime {
56 expect(
56 try expect(
5757 1194735857077236777412821811143690633098347576 % 508740759824825164163191790951174292733114988 == 177254337427586449086438229241342047632117600,
5858 );
59 expect(
59 try expect(
6060 @rem(-1194735857077236777412821811143690633098347576, 508740759824825164163191790951174292733114988) == -177254337427586449086438229241342047632117600,
6161 );
62 expect(
62 try expect(
6363 1194735857077236777412821811143690633098347576 / 508740759824825164163191790951174292733114988 == 2,
6464 );
65 expect(
65 try expect(
6666 @divTrunc(-1194735857077236777412821811143690633098347576, 508740759824825164163191790951174292733114988) == -2,
6767 );
68 expect(
68 try expect(
6969 @divTrunc(1194735857077236777412821811143690633098347576, -508740759824825164163191790951174292733114988) == -2,
7070 );
71 expect(
71 try expect(
7272 @divTrunc(-1194735857077236777412821811143690633098347576, -508740759824825164163191790951174292733114988) == 2,
7373 );
74 expect(
74 try expect(
7575 4126227191251978491697987544882340798050766755606969681711 % 10 == 1,
7676 );
7777 }
......@@ -94,9 +94,9 @@ fn mod(comptime T: type, a: T, b: T) T {
9494
9595test "@addWithOverflow" {
9696 var result: u8 = undefined;
97 expect(@addWithOverflow(u8, 250, 100, &result));
98 expect(!@addWithOverflow(u8, 100, 150, &result));
99 expect(result == 250);
97 try expect(@addWithOverflow(u8, 250, 100, &result));
98 try expect(!@addWithOverflow(u8, 100, 150, &result));
99 try expect(result == 250);
100100}
101101
102102// TODO test mulWithOverflow
......@@ -104,31 +104,31 @@ test "@addWithOverflow" {
104104
105105test "@shlWithOverflow" {
106106 var result: u16 = undefined;
107 expect(@shlWithOverflow(u16, 0b0010111111111111, 3, &result));
108 expect(!@shlWithOverflow(u16, 0b0010111111111111, 2, &result));
109 expect(result == 0b1011111111111100);
107 try expect(@shlWithOverflow(u16, 0b0010111111111111, 3, &result));
108 try expect(!@shlWithOverflow(u16, 0b0010111111111111, 2, &result));
109 try expect(result == 0b1011111111111100);
110110}
111111
112112test "@*WithOverflow with u0 values" {
113113 var result: u0 = undefined;
114 expect(!@addWithOverflow(u0, 0, 0, &result));
115 expect(!@subWithOverflow(u0, 0, 0, &result));
116 expect(!@mulWithOverflow(u0, 0, 0, &result));
117 expect(!@shlWithOverflow(u0, 0, 0, &result));
114 try expect(!@addWithOverflow(u0, 0, 0, &result));
115 try expect(!@subWithOverflow(u0, 0, 0, &result));
116 try expect(!@mulWithOverflow(u0, 0, 0, &result));
117 try expect(!@shlWithOverflow(u0, 0, 0, &result));
118118}
119119
120120test "@clz" {
121 testClz();
122 comptime testClz();
121 try testClz();
122 comptime try testClz();
123123}
124124
125fn testClz() void {
126 expect(clz(u8, 0b10001010) == 0);
127 expect(clz(u8, 0b00001010) == 4);
128 expect(clz(u8, 0b00011010) == 3);
129 expect(clz(u8, 0b00000000) == 8);
130 expect(clz(u128, 0xffffffffffffffff) == 64);
131 expect(clz(u128, 0x10000000000000000) == 63);
125fn testClz() !void {
126 try expect(clz(u8, 0b10001010) == 0);
127 try expect(clz(u8, 0b00001010) == 4);
128 try expect(clz(u8, 0b00011010) == 3);
129 try expect(clz(u8, 0b00000000) == 8);
130 try expect(clz(u128, 0xffffffffffffffff) == 64);
131 try expect(clz(u128, 0x10000000000000000) == 63);
132132}
133133
134134fn clz(comptime T: type, x: T) usize {
......@@ -136,15 +136,15 @@ fn clz(comptime T: type, x: T) usize {
136136}
137137
138138test "@ctz" {
139 testCtz();
140 comptime testCtz();
139 try testCtz();
140 comptime try testCtz();
141141}
142142
143fn testCtz() void {
144 expect(ctz(u8, 0b10100000) == 5);
145 expect(ctz(u8, 0b10001010) == 1);
146 expect(ctz(u8, 0b00000000) == 8);
147 expect(ctz(u16, 0b00000000) == 16);
143fn testCtz() !void {
144 try expect(ctz(u8, 0b10100000) == 5);
145 try expect(ctz(u8, 0b10001010) == 1);
146 try expect(ctz(u8, 0b00000000) == 8);
147 try expect(ctz(u16, 0b00000000) == 16);
148148}
149149
150150fn ctz(comptime T: type, x: T) usize {
......@@ -154,109 +154,109 @@ fn ctz(comptime T: type, x: T) usize {
154154test "assignment operators" {
155155 var i: u32 = 0;
156156 i += 5;
157 expect(i == 5);
157 try expect(i == 5);
158158 i -= 2;
159 expect(i == 3);
159 try expect(i == 3);
160160 i *= 20;
161 expect(i == 60);
161 try expect(i == 60);
162162 i /= 3;
163 expect(i == 20);
163 try expect(i == 20);
164164 i %= 11;
165 expect(i == 9);
165 try expect(i == 9);
166166 i <<= 1;
167 expect(i == 18);
167 try expect(i == 18);
168168 i >>= 2;
169 expect(i == 4);
169 try expect(i == 4);
170170 i = 6;
171171 i &= 5;
172 expect(i == 4);
172 try expect(i == 4);
173173 i ^= 6;
174 expect(i == 2);
174 try expect(i == 2);
175175 i = 6;
176176 i |= 3;
177 expect(i == 7);
177 try expect(i == 7);
178178}
179179
180180test "three expr in a row" {
181 testThreeExprInARow(false, true);
182 comptime testThreeExprInARow(false, true);
183}
184fn testThreeExprInARow(f: bool, t: bool) void {
185 assertFalse(f or f or f);
186 assertFalse(t and t and f);
187 assertFalse(1 | 2 | 4 != 7);
188 assertFalse(3 ^ 6 ^ 8 != 13);
189 assertFalse(7 & 14 & 28 != 4);
190 assertFalse(9 << 1 << 2 != 9 << 3);
191 assertFalse(90 >> 1 >> 2 != 90 >> 3);
192 assertFalse(100 - 1 + 1000 != 1099);
193 assertFalse(5 * 4 / 2 % 3 != 1);
194 assertFalse(@as(i32, @as(i32, 5)) != 5);
195 assertFalse(!!false);
196 assertFalse(@as(i32, 7) != --(@as(i32, 7)));
197}
198fn assertFalse(b: bool) void {
199 expect(!b);
181 try testThreeExprInARow(false, true);
182 comptime try testThreeExprInARow(false, true);
183}
184fn testThreeExprInARow(f: bool, t: bool) !void {
185 try assertFalse(f or f or f);
186 try assertFalse(t and t and f);
187 try assertFalse(1 | 2 | 4 != 7);
188 try assertFalse(3 ^ 6 ^ 8 != 13);
189 try assertFalse(7 & 14 & 28 != 4);
190 try assertFalse(9 << 1 << 2 != 9 << 3);
191 try assertFalse(90 >> 1 >> 2 != 90 >> 3);
192 try assertFalse(100 - 1 + 1000 != 1099);
193 try assertFalse(5 * 4 / 2 % 3 != 1);
194 try assertFalse(@as(i32, @as(i32, 5)) != 5);
195 try assertFalse(!!false);
196 try assertFalse(@as(i32, 7) != --(@as(i32, 7)));
197}
198fn assertFalse(b: bool) !void {
199 try expect(!b);
200200}
201201
202202test "const number literal" {
203203 const one = 1;
204204 const eleven = ten + one;
205205
206 expect(eleven == 11);
206 try expect(eleven == 11);
207207}
208208const ten = 10;
209209
210210test "unsigned wrapping" {
211 testUnsignedWrappingEval(maxInt(u32));
212 comptime testUnsignedWrappingEval(maxInt(u32));
211 try testUnsignedWrappingEval(maxInt(u32));
212 comptime try testUnsignedWrappingEval(maxInt(u32));
213213}
214fn testUnsignedWrappingEval(x: u32) void {
214fn testUnsignedWrappingEval(x: u32) !void {
215215 const zero = x +% 1;
216 expect(zero == 0);
216 try expect(zero == 0);
217217 const orig = zero -% 1;
218 expect(orig == maxInt(u32));
218 try expect(orig == maxInt(u32));
219219}
220220
221221test "signed wrapping" {
222 testSignedWrappingEval(maxInt(i32));
223 comptime testSignedWrappingEval(maxInt(i32));
222 try testSignedWrappingEval(maxInt(i32));
223 comptime try testSignedWrappingEval(maxInt(i32));
224224}
225fn testSignedWrappingEval(x: i32) void {
225fn testSignedWrappingEval(x: i32) !void {
226226 const min_val = x +% 1;
227 expect(min_val == minInt(i32));
227 try expect(min_val == minInt(i32));
228228 const max_val = min_val -% 1;
229 expect(max_val == maxInt(i32));
229 try expect(max_val == maxInt(i32));
230230}
231231
232232test "signed negation wrapping" {
233 testSignedNegationWrappingEval(minInt(i16));
234 comptime testSignedNegationWrappingEval(minInt(i16));
233 try testSignedNegationWrappingEval(minInt(i16));
234 comptime try testSignedNegationWrappingEval(minInt(i16));
235235}
236fn testSignedNegationWrappingEval(x: i16) void {
237 expect(x == -32768);
236fn testSignedNegationWrappingEval(x: i16) !void {
237 try expect(x == -32768);
238238 const neg = -%x;
239 expect(neg == -32768);
239 try expect(neg == -32768);
240240}
241241
242242test "unsigned negation wrapping" {
243 testUnsignedNegationWrappingEval(1);
244 comptime testUnsignedNegationWrappingEval(1);
243 try testUnsignedNegationWrappingEval(1);
244 comptime try testUnsignedNegationWrappingEval(1);
245245}
246fn testUnsignedNegationWrappingEval(x: u16) void {
247 expect(x == 1);
246fn testUnsignedNegationWrappingEval(x: u16) !void {
247 try expect(x == 1);
248248 const neg = -%x;
249 expect(neg == maxInt(u16));
249 try expect(neg == maxInt(u16));
250250}
251251
252252test "unsigned 64-bit division" {
253 test_u64_div();
254 comptime test_u64_div();
253 try test_u64_div();
254 comptime try test_u64_div();
255255}
256fn test_u64_div() void {
256fn test_u64_div() !void {
257257 const result = divWithResult(1152921504606846976, 34359738365);
258 expect(result.quotient == 33554432);
259 expect(result.remainder == 100663296);
258 try expect(result.quotient == 33554432);
259 try expect(result.remainder == 100663296);
260260}
261261fn divWithResult(a: u64, b: u64) DivResult {
262262 return DivResult{
......@@ -270,62 +270,62 @@ const DivResult = struct {
270270};
271271
272272test "binary not" {
273 expect(comptime x: {
273 try expect(comptime x: {
274274 break :x ~@as(u16, 0b1010101010101010) == 0b0101010101010101;
275275 });
276 expect(comptime x: {
276 try expect(comptime x: {
277277 break :x ~@as(u64, 2147483647) == 18446744071562067968;
278278 });
279 testBinaryNot(0b1010101010101010);
279 try testBinaryNot(0b1010101010101010);
280280}
281281
282fn testBinaryNot(x: u16) void {
283 expect(~x == 0b0101010101010101);
282fn testBinaryNot(x: u16) !void {
283 try expect(~x == 0b0101010101010101);
284284}
285285
286286test "small int addition" {
287287 var x: u2 = 0;
288 expect(x == 0);
288 try expect(x == 0);
289289
290290 x += 1;
291 expect(x == 1);
291 try expect(x == 1);
292292
293293 x += 1;
294 expect(x == 2);
294 try expect(x == 2);
295295
296296 x += 1;
297 expect(x == 3);
297 try expect(x == 3);
298298
299299 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);
303303}
304304
305305test "float equality" {
306306 const x: f64 = 0.012;
307307 const y: f64 = x + 1.0;
308308
309 testFloatEqualityImpl(x, y);
310 comptime testFloatEqualityImpl(x, y);
309 try testFloatEqualityImpl(x, y);
310 comptime try testFloatEqualityImpl(x, y);
311311}
312312
313fn testFloatEqualityImpl(x: f64, y: f64) void {
313fn testFloatEqualityImpl(x: f64, y: f64) !void {
314314 const y2 = x + 1.0;
315 expect(y == y2);
315 try expect(y == y2);
316316}
317317
318318test "allow signed integer division/remainder when values are comptime known and positive or exact" {
319 expect(5 / 3 == 1);
320 expect(-5 / -3 == 1);
321 expect(-6 / 3 == -2);
319 try expect(5 / 3 == 1);
320 try expect(-5 / -3 == 1);
321 try expect(-6 / 3 == -2);
322322
323 expect(5 % 3 == 2);
324 expect(-6 % 3 == 0);
323 try expect(5 % 3 == 2);
324 try expect(-6 % 3 == 0);
325325}
326326
327327test "hex float literal parsing" {
328 comptime expect(0x1.0 == 1.0);
328 comptime try expect(0x1.0 == 1.0);
329329}
330330
331331test "quad hex float literal parsing in range" {
......@@ -340,29 +340,29 @@ test "quad hex float literal parsing accurate" {
340340
341341 // implied 1 is dropped, with an exponent of 0 (0x3fff) after biasing.
342342 const expected: u128 = 0x3fff1111222233334444555566667777;
343 expect(@bitCast(u128, a) == expected);
343 try expect(@bitCast(u128, a) == expected);
344344
345345 // non-normalized
346346 const b: f128 = 0x11.111222233334444555566667777p-4;
347 expect(@bitCast(u128, b) == expected);
347 try expect(@bitCast(u128, b) == expected);
348348
349349 const S = struct {
350 fn doTheTest() void {
350 fn doTheTest() !void {
351351 {
352352 var f: f128 = 0x1.2eab345678439abcdefea56782346p+5;
353 expect(@bitCast(u128, f) == 0x40042eab345678439abcdefea5678234);
353 try expect(@bitCast(u128, f) == 0x40042eab345678439abcdefea5678234);
354354 }
355355 {
356356 var f: f128 = 0x1.edcb34a235253948765432134674fp-1;
357 expect(@bitCast(u128, f) == 0x3ffeedcb34a235253948765432134674);
357 try expect(@bitCast(u128, f) == 0x3ffeedcb34a235253948765432134674);
358358 }
359359 {
360360 var f: f128 = 0x1.353e45674d89abacc3a2ebf3ff4ffp-50;
361 expect(@bitCast(u128, f) == 0x3fcd353e45674d89abacc3a2ebf3ff50);
361 try expect(@bitCast(u128, f) == 0x3fcd353e45674d89abacc3a2ebf3ff50);
362362 }
363363 {
364364 var f: f128 = 0x1.ed8764648369535adf4be3214567fp-9;
365 expect(@bitCast(u128, f) == 0x3ff6ed8764648369535adf4be3214568);
365 try expect(@bitCast(u128, f) == 0x3ff6ed8764648369535adf4be3214568);
366366 }
367367 const exp2ft = [_]f64{
368368 0x1.6a09e667f3bcdp-1,
......@@ -417,40 +417,40 @@ test "quad hex float literal parsing accurate" {
417417 };
418418
419419 for (exp2ft) |x, i| {
420 expect(@bitCast(u64, x) == answers[i]);
420 try expect(@bitCast(u64, x) == answers[i]);
421421 }
422422 }
423423 };
424 S.doTheTest();
425 comptime S.doTheTest();
424 try S.doTheTest();
425 comptime try S.doTheTest();
426426}
427427
428428test "underscore separator parsing" {
429 expect(0_0_0_0 == 0);
430 expect(1_234_567 == 1234567);
431 expect(001_234_567 == 1234567);
432 expect(0_0_1_2_3_4_5_6_7 == 1234567);
429 try expect(0_0_0_0 == 0);
430 try expect(1_234_567 == 1234567);
431 try expect(001_234_567 == 1234567);
432 try expect(0_0_1_2_3_4_5_6_7 == 1234567);
433433
434 expect(0b0_0_0_0 == 0);
435 expect(0b1010_1010 == 0b10101010);
436 expect(0b0000_1010_1010 == 0b10101010);
437 expect(0b1_0_1_0_1_0_1_0 == 0b10101010);
434 try expect(0b0_0_0_0 == 0);
435 try expect(0b1010_1010 == 0b10101010);
436 try expect(0b0000_1010_1010 == 0b10101010);
437 try expect(0b1_0_1_0_1_0_1_0 == 0b10101010);
438438
439 expect(0o0_0_0_0 == 0);
440 expect(0o1010_1010 == 0o10101010);
441 expect(0o0000_1010_1010 == 0o10101010);
442 expect(0o1_0_1_0_1_0_1_0 == 0o10101010);
439 try expect(0o0_0_0_0 == 0);
440 try expect(0o1010_1010 == 0o10101010);
441 try expect(0o0000_1010_1010 == 0o10101010);
442 try expect(0o1_0_1_0_1_0_1_0 == 0o10101010);
443443
444 expect(0x0_0_0_0 == 0);
445 expect(0x1010_1010 == 0x10101010);
446 expect(0x0000_1010_1010 == 0x10101010);
447 expect(0x1_0_1_0_1_0_1_0 == 0x10101010);
444 try expect(0x0_0_0_0 == 0);
445 try expect(0x1010_1010 == 0x10101010);
446 try expect(0x0000_1010_1010 == 0x10101010);
447 try expect(0x1_0_1_0_1_0_1_0 == 0x10101010);
448448
449 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);
449 try expect(123_456.789_000e1_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);
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);
452 try expect(0x1234_5678.9ABC_DEF0p-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);
454454}
455455
456456test "hex float literal within range" {
......@@ -460,73 +460,73 @@ test "hex float literal within range" {
460460}
461461
462462test "truncating shift left" {
463 testShlTrunc(maxInt(u16));
464 comptime testShlTrunc(maxInt(u16));
463 try testShlTrunc(maxInt(u16));
464 comptime try testShlTrunc(maxInt(u16));
465465}
466fn testShlTrunc(x: u16) void {
466fn testShlTrunc(x: u16) !void {
467467 const shifted = x << 1;
468 expect(shifted == 65534);
468 try expect(shifted == 65534);
469469}
470470
471471test "truncating shift right" {
472 testShrTrunc(maxInt(u16));
473 comptime testShrTrunc(maxInt(u16));
472 try testShrTrunc(maxInt(u16));
473 comptime try testShrTrunc(maxInt(u16));
474474}
475fn testShrTrunc(x: u16) void {
475fn testShrTrunc(x: u16) !void {
476476 const shifted = x >> 1;
477 expect(shifted == 32767);
477 try expect(shifted == 32767);
478478}
479479
480480test "exact shift left" {
481 testShlExact(0b00110101);
482 comptime testShlExact(0b00110101);
481 try testShlExact(0b00110101);
482 comptime try testShlExact(0b00110101);
483483}
484fn testShlExact(x: u8) void {
484fn testShlExact(x: u8) !void {
485485 const shifted = @shlExact(x, 2);
486 expect(shifted == 0b11010100);
486 try expect(shifted == 0b11010100);
487487}
488488
489489test "exact shift right" {
490 testShrExact(0b10110100);
491 comptime testShrExact(0b10110100);
490 try testShrExact(0b10110100);
491 comptime try testShrExact(0b10110100);
492492}
493fn testShrExact(x: u8) void {
493fn testShrExact(x: u8) !void {
494494 const shifted = @shrExact(x, 2);
495 expect(shifted == 0b00101101);
495 try expect(shifted == 0b00101101);
496496}
497497
498498test "shift left/right on u0 operand" {
499499 const S = struct {
500 fn doTheTest() void {
500 fn doTheTest() !void {
501501 var x: u0 = 0;
502502 var y: u0 = 0;
503 expectEqual(@as(u0, 0), x << 0);
504 expectEqual(@as(u0, 0), x >> 0);
505 expectEqual(@as(u0, 0), x << y);
506 expectEqual(@as(u0, 0), x >> y);
507 expectEqual(@as(u0, 0), @shlExact(x, 0));
508 expectEqual(@as(u0, 0), @shrExact(x, 0));
509 expectEqual(@as(u0, 0), @shlExact(x, y));
510 expectEqual(@as(u0, 0), @shrExact(x, y));
503 try expectEqual(@as(u0, 0), x << 0);
504 try expectEqual(@as(u0, 0), x >> 0);
505 try expectEqual(@as(u0, 0), x << y);
506 try expectEqual(@as(u0, 0), x >> y);
507 try expectEqual(@as(u0, 0), @shlExact(x, 0));
508 try expectEqual(@as(u0, 0), @shrExact(x, 0));
509 try expectEqual(@as(u0, 0), @shlExact(x, y));
510 try expectEqual(@as(u0, 0), @shrExact(x, y));
511511 }
512512 };
513 S.doTheTest();
514 comptime S.doTheTest();
513 try S.doTheTest();
514 comptime try S.doTheTest();
515515}
516516
517517test "comptime_int addition" {
518518 comptime {
519 expect(35361831660712422535336160538497375248 + 101752735581729509668353361206450473702 == 137114567242441932203689521744947848950);
520 expect(594491908217841670578297176641415611445982232488944558774612 + 390603545391089362063884922208143568023166603618446395589768 == 985095453608931032642182098849559179469148836107390954364380);
519 try expect(35361831660712422535336160538497375248 + 101752735581729509668353361206450473702 == 137114567242441932203689521744947848950);
520 try expect(594491908217841670578297176641415611445982232488944558774612 + 390603545391089362063884922208143568023166603618446395589768 == 985095453608931032642182098849559179469148836107390954364380);
521521 }
522522}
523523
524524test "comptime_int multiplication" {
525525 comptime {
526 expect(
526 try expect(
527527 45960427431263824329884196484953148229 * 128339149605334697009938835852565949723 == 5898522172026096622534201617172456926982464453350084962781392314016180490567,
528528 );
529 expect(
529 try expect(
530530 594491908217841670578297176641415611445982232488944558774612 * 390603545391089362063884922208143568023166603618446395589768 == 232210647056203049913662402532976186578842425262306016094292237500303028346593132411865381225871291702600263463125370016,
531531 );
532532 }
......@@ -534,7 +534,7 @@ test "comptime_int multiplication" {
534534
535535test "comptime_int shifting" {
536536 comptime {
537 expect((@as(u128, 1) << 127) == 0x80000000000000000000000000000000);
537 try expect((@as(u128, 1) << 127) == 0x80000000000000000000000000000000);
538538 }
539539}
540540
......@@ -542,16 +542,16 @@ test "comptime_int multi-limb shift and mask" {
542542 comptime {
543543 var a = 0xefffffffa0000001eeeeeeefaaaaaaab;
544544
545 expect(@as(u32, a & 0xffffffff) == 0xaaaaaaab);
545 try expect(@as(u32, a & 0xffffffff) == 0xaaaaaaab);
546546 a >>= 32;
547 expect(@as(u32, a & 0xffffffff) == 0xeeeeeeef);
547 try expect(@as(u32, a & 0xffffffff) == 0xeeeeeeef);
548548 a >>= 32;
549 expect(@as(u32, a & 0xffffffff) == 0xa0000001);
549 try expect(@as(u32, a & 0xffffffff) == 0xa0000001);
550550 a >>= 32;
551 expect(@as(u32, a & 0xffffffff) == 0xefffffff);
551 try expect(@as(u32, a & 0xffffffff) == 0xefffffff);
552552 a >>= 32;
553553
554 expect(a == 0);
554 try expect(a == 0);
555555 }
556556}
557557
......@@ -559,227 +559,227 @@ test "comptime_int multi-limb partial shift right" {
559559 comptime {
560560 var a = 0x1ffffffffeeeeeeee;
561561 a >>= 16;
562 expect(a == 0x1ffffffffeeee);
562 try expect(a == 0x1ffffffffeeee);
563563 }
564564}
565565
566566test "xor" {
567 test_xor();
568 comptime test_xor();
567 try test_xor();
568 comptime try test_xor();
569569}
570570
571fn test_xor() void {
572 expect(0xFF ^ 0x00 == 0xFF);
573 expect(0xF0 ^ 0x0F == 0xFF);
574 expect(0xFF ^ 0xF0 == 0x0F);
575 expect(0xFF ^ 0x0F == 0xF0);
576 expect(0xFF ^ 0xFF == 0x00);
571fn test_xor() !void {
572 try expect(0xFF ^ 0x00 == 0xFF);
573 try expect(0xF0 ^ 0x0F == 0xFF);
574 try expect(0xFF ^ 0xF0 == 0x0F);
575 try expect(0xFF ^ 0x0F == 0xF0);
576 try expect(0xFF ^ 0xFF == 0x00);
577577}
578578
579579test "comptime_int xor" {
580580 comptime {
581 expect(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF ^ 0x00000000000000000000000000000000 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);
582 expect(0xFFFFFFFFFFFFFFFF0000000000000000 ^ 0x0000000000000000FFFFFFFFFFFFFFFF == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);
583 expect(0xFFFFFFFFFFFFFFFF0000000000000000 ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0x0000000000000000FFFFFFFFFFFFFFFF);
584 expect(0x0000000000000000FFFFFFFFFFFFFFFF ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0xFFFFFFFFFFFFFFFF0000000000000000);
585 expect(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0x00000000000000000000000000000000);
586 expect(0xFFFFFFFF00000000FFFFFFFF00000000 ^ 0x00000000FFFFFFFF00000000FFFFFFFF == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);
587 expect(0xFFFFFFFF00000000FFFFFFFF00000000 ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0x00000000FFFFFFFF00000000FFFFFFFF);
588 expect(0x00000000FFFFFFFF00000000FFFFFFFF ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0xFFFFFFFF00000000FFFFFFFF00000000);
581 try expect(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF ^ 0x00000000000000000000000000000000 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);
582 try expect(0xFFFFFFFFFFFFFFFF0000000000000000 ^ 0x0000000000000000FFFFFFFFFFFFFFFF == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);
583 try expect(0xFFFFFFFFFFFFFFFF0000000000000000 ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0x0000000000000000FFFFFFFFFFFFFFFF);
584 try expect(0x0000000000000000FFFFFFFFFFFFFFFF ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0xFFFFFFFFFFFFFFFF0000000000000000);
585 try expect(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0x00000000000000000000000000000000);
586 try expect(0xFFFFFFFF00000000FFFFFFFF00000000 ^ 0x00000000FFFFFFFF00000000FFFFFFFF == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);
587 try expect(0xFFFFFFFF00000000FFFFFFFF00000000 ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0x00000000FFFFFFFF00000000FFFFFFFF);
588 try expect(0x00000000FFFFFFFF00000000FFFFFFFF ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0xFFFFFFFF00000000FFFFFFFF00000000);
589589 }
590590}
591591
592592test "f128" {
593 test_f128();
594 comptime test_f128();
593 try test_f128();
594 comptime try test_f128();
595595}
596596
597597fn make_f128(x: f128) f128 {
598598 return x;
599599}
600600
601fn test_f128() void {
602 expect(@sizeOf(f128) == 16);
603 expect(make_f128(1.0) == 1.0);
604 expect(make_f128(1.0) != 1.1);
605 expect(make_f128(1.0) > 0.9);
606 expect(make_f128(1.0) >= 0.9);
607 expect(make_f128(1.0) >= 1.0);
608 should_not_be_zero(1.0);
601fn test_f128() !void {
602 try expect(@sizeOf(f128) == 16);
603 try expect(make_f128(1.0) == 1.0);
604 try expect(make_f128(1.0) != 1.1);
605 try expect(make_f128(1.0) > 0.9);
606 try expect(make_f128(1.0) >= 0.9);
607 try expect(make_f128(1.0) >= 1.0);
608 try should_not_be_zero(1.0);
609609}
610610
611fn should_not_be_zero(x: f128) void {
612 expect(x != 0.0);
611fn should_not_be_zero(x: f128) !void {
612 try expect(x != 0.0);
613613}
614614
615615test "comptime float rem int" {
616616 comptime {
617617 var x = @as(f32, 1) % 2;
618 expect(x == 1.0);
618 try expect(x == 1.0);
619619 }
620620}
621621
622622test "remainder division" {
623 comptime remdiv(f16);
624 comptime remdiv(f32);
625 comptime remdiv(f64);
626 comptime remdiv(f128);
627 remdiv(f16);
628 remdiv(f64);
629 remdiv(f128);
623 comptime try remdiv(f16);
624 comptime try remdiv(f32);
625 comptime try remdiv(f64);
626 comptime try remdiv(f128);
627 try remdiv(f16);
628 try remdiv(f64);
629 try remdiv(f128);
630630}
631631
632fn remdiv(comptime T: type) void {
633 expect(@as(T, 1) == @as(T, 1) % @as(T, 2));
634 expect(@as(T, 1) == @as(T, 7) % @as(T, 3));
632fn remdiv(comptime T: type) !void {
633 try expect(@as(T, 1) == @as(T, 1) % @as(T, 2));
634 try expect(@as(T, 1) == @as(T, 7) % @as(T, 3));
635635}
636636
637637test "@sqrt" {
638 testSqrt(f64, 12.0);
639 comptime testSqrt(f64, 12.0);
640 testSqrt(f32, 13.0);
641 comptime testSqrt(f32, 13.0);
642 testSqrt(f16, 13.0);
643 comptime testSqrt(f16, 13.0);
638 try testSqrt(f64, 12.0);
639 comptime try testSqrt(f64, 12.0);
640 try testSqrt(f32, 13.0);
641 comptime try testSqrt(f32, 13.0);
642 try testSqrt(f16, 13.0);
643 comptime try testSqrt(f16, 13.0);
644644
645645 const x = 14.0;
646646 const y = x * x;
647647 const z = @sqrt(y);
648 comptime expect(z == x);
648 comptime try expect(z == x);
649649}
650650
651fn testSqrt(comptime T: type, x: T) void {
652 expect(@sqrt(x * x) == x);
651fn testSqrt(comptime T: type, x: T) !void {
652 try expect(@sqrt(x * x) == x);
653653}
654654
655655test "@fabs" {
656 testFabs(f128, 12.0);
657 comptime testFabs(f128, 12.0);
658 testFabs(f64, 12.0);
659 comptime testFabs(f64, 12.0);
660 testFabs(f32, 12.0);
661 comptime testFabs(f32, 12.0);
662 testFabs(f16, 12.0);
663 comptime testFabs(f16, 12.0);
656 try testFabs(f128, 12.0);
657 comptime try testFabs(f128, 12.0);
658 try testFabs(f64, 12.0);
659 comptime try testFabs(f64, 12.0);
660 try testFabs(f32, 12.0);
661 comptime try testFabs(f32, 12.0);
662 try testFabs(f16, 12.0);
663 comptime try testFabs(f16, 12.0);
664664
665665 const x = 14.0;
666666 const y = -x;
667667 const z = @fabs(y);
668 comptime expectEqual(x, z);
668 comptime try expectEqual(x, z);
669669}
670670
671fn testFabs(comptime T: type, x: T) void {
671fn testFabs(comptime T: type, x: T) !void {
672672 const y = -x;
673673 const z = @fabs(y);
674 expectEqual(x, z);
674 try expectEqual(x, z);
675675}
676676
677677test "@floor" {
678678 // FIXME: Generates a floorl function call
679679 // testFloor(f128, 12.0);
680 comptime testFloor(f128, 12.0);
681 testFloor(f64, 12.0);
682 comptime testFloor(f64, 12.0);
683 testFloor(f32, 12.0);
684 comptime testFloor(f32, 12.0);
685 testFloor(f16, 12.0);
686 comptime testFloor(f16, 12.0);
680 comptime try testFloor(f128, 12.0);
681 try testFloor(f64, 12.0);
682 comptime try testFloor(f64, 12.0);
683 try testFloor(f32, 12.0);
684 comptime try testFloor(f32, 12.0);
685 try testFloor(f16, 12.0);
686 comptime try testFloor(f16, 12.0);
687687
688688 const x = 14.0;
689689 const y = x + 0.7;
690690 const z = @floor(y);
691 comptime expectEqual(x, z);
691 comptime try expectEqual(x, z);
692692}
693693
694fn testFloor(comptime T: type, x: T) void {
694fn testFloor(comptime T: type, x: T) !void {
695695 const y = x + 0.6;
696696 const z = @floor(y);
697 expectEqual(x, z);
697 try expectEqual(x, z);
698698}
699699
700700test "@ceil" {
701701 // FIXME: Generates a ceill function call
702702 //testCeil(f128, 12.0);
703 comptime testCeil(f128, 12.0);
704 testCeil(f64, 12.0);
705 comptime testCeil(f64, 12.0);
706 testCeil(f32, 12.0);
707 comptime testCeil(f32, 12.0);
708 testCeil(f16, 12.0);
709 comptime testCeil(f16, 12.0);
703 comptime try testCeil(f128, 12.0);
704 try testCeil(f64, 12.0);
705 comptime try testCeil(f64, 12.0);
706 try testCeil(f32, 12.0);
707 comptime try testCeil(f32, 12.0);
708 try testCeil(f16, 12.0);
709 comptime try testCeil(f16, 12.0);
710710
711711 const x = 14.0;
712712 const y = x - 0.7;
713713 const z = @ceil(y);
714 comptime expectEqual(x, z);
714 comptime try expectEqual(x, z);
715715}
716716
717fn testCeil(comptime T: type, x: T) void {
717fn testCeil(comptime T: type, x: T) !void {
718718 const y = x - 0.8;
719719 const z = @ceil(y);
720 expectEqual(x, z);
720 try expectEqual(x, z);
721721}
722722
723723test "@trunc" {
724724 // FIXME: Generates a truncl function call
725725 //testTrunc(f128, 12.0);
726 comptime testTrunc(f128, 12.0);
727 testTrunc(f64, 12.0);
728 comptime testTrunc(f64, 12.0);
729 testTrunc(f32, 12.0);
730 comptime testTrunc(f32, 12.0);
731 testTrunc(f16, 12.0);
732 comptime testTrunc(f16, 12.0);
726 comptime try testTrunc(f128, 12.0);
727 try testTrunc(f64, 12.0);
728 comptime try testTrunc(f64, 12.0);
729 try testTrunc(f32, 12.0);
730 comptime try testTrunc(f32, 12.0);
731 try testTrunc(f16, 12.0);
732 comptime try testTrunc(f16, 12.0);
733733
734734 const x = 14.0;
735735 const y = x + 0.7;
736736 const z = @trunc(y);
737 comptime expectEqual(x, z);
737 comptime try expectEqual(x, z);
738738}
739739
740fn testTrunc(comptime T: type, x: T) void {
740fn testTrunc(comptime T: type, x: T) !void {
741741 {
742742 const y = x + 0.8;
743743 const z = @trunc(y);
744 expectEqual(x, z);
744 try expectEqual(x, z);
745745 }
746746
747747 {
748748 const y = -x - 0.8;
749749 const z = @trunc(y);
750 expectEqual(-x, z);
750 try expectEqual(-x, z);
751751 }
752752}
753753
754754test "@round" {
755755 // FIXME: Generates a roundl function call
756756 //testRound(f128, 12.0);
757 comptime testRound(f128, 12.0);
758 testRound(f64, 12.0);
759 comptime testRound(f64, 12.0);
760 testRound(f32, 12.0);
761 comptime testRound(f32, 12.0);
762 testRound(f16, 12.0);
763 comptime testRound(f16, 12.0);
757 comptime try testRound(f128, 12.0);
758 try testRound(f64, 12.0);
759 comptime try testRound(f64, 12.0);
760 try testRound(f32, 12.0);
761 comptime try testRound(f32, 12.0);
762 try testRound(f16, 12.0);
763 comptime try testRound(f16, 12.0);
764764
765765 const x = 14.0;
766766 const y = x + 0.4;
767767 const z = @round(y);
768 comptime expectEqual(x, z);
768 comptime try expectEqual(x, z);
769769}
770770
771fn testRound(comptime T: type, x: T) void {
771fn testRound(comptime T: type, x: T) !void {
772772 const y = x - 0.5;
773773 const z = @round(y);
774 expectEqual(x, z);
774 try expectEqual(x, z);
775775}
776776
777777test "comptime_int param and return" {
778778 const a = comptimeAdd(35361831660712422535336160538497375248, 101752735581729509668353361206450473702);
779 expect(a == 137114567242441932203689521744947848950);
779 try expect(a == 137114567242441932203689521744947848950);
780780
781781 const b = comptimeAdd(594491908217841670578297176641415611445982232488944558774612, 390603545391089362063884922208143568023166603618446395589768);
782 expect(b == 985095453608931032642182098849559179469148836107390954364380);
782 try expect(b == 985095453608931032642182098849559179469148836107390954364380);
783783}
784784
785785fn 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
789789test "vector integer addition" {
790790 const S = struct {
791 fn doTheTest() void {
791 fn doTheTest() !void {
792792 var a: std.meta.Vector(4, i32) = [_]i32{ 1, 2, 3, 4 };
793793 var b: std.meta.Vector(4, i32) = [_]i32{ 5, 6, 7, 8 };
794794 var result = a + b;
795795 var result_array: [4]i32 = result;
796796 const expected = [_]i32{ 6, 8, 10, 12 };
797 expectEqualSlices(i32, &expected, &result_array);
797 try expectEqualSlices(i32, &expected, &result_array);
798798 }
799799 };
800 S.doTheTest();
801 comptime S.doTheTest();
800 try S.doTheTest();
801 comptime try S.doTheTest();
802802}
803803
804804test "NaN comparison" {
805 testNanEqNan(f16);
806 testNanEqNan(f32);
807 testNanEqNan(f64);
808 testNanEqNan(f128);
809 comptime testNanEqNan(f16);
810 comptime testNanEqNan(f32);
811 comptime testNanEqNan(f64);
812 comptime testNanEqNan(f128);
805 try testNanEqNan(f16);
806 try testNanEqNan(f32);
807 try testNanEqNan(f64);
808 try testNanEqNan(f128);
809 comptime try testNanEqNan(f16);
810 comptime try testNanEqNan(f32);
811 comptime try testNanEqNan(f64);
812 comptime try testNanEqNan(f128);
813813}
814814
815fn testNanEqNan(comptime F: type) void {
815fn testNanEqNan(comptime F: type) !void {
816816 var nan1 = std.math.nan(F);
817817 var nan2 = std.math.nan(F);
818 expect(nan1 != nan2);
819 expect(!(nan1 == nan2));
820 expect(!(nan1 > nan2));
821 expect(!(nan1 >= nan2));
822 expect(!(nan1 < nan2));
823 expect(!(nan1 <= nan2));
818 try expect(nan1 != nan2);
819 try expect(!(nan1 == nan2));
820 try expect(!(nan1 > nan2));
821 try expect(!(nan1 >= nan2));
822 try expect(!(nan1 < nan2));
823 try expect(!(nan1 <= nan2));
824824}
825825
826826test "128-bit multiplication" {
827827 var a: i128 = 3;
828828 var b: i128 = 2;
829829 var c = a * b;
830 expect(c == 6);
830 try expect(c == 6);
831831}
832832
833833test "vector comparison" {
834834 const S = struct {
835 fn doTheTest() void {
835 fn doTheTest() !void {
836836 var a: std.meta.Vector(6, i32) = [_]i32{ 1, 3, -1, 5, 7, 9 };
837837 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 }));
839 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 }));
841 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 }));
843 expect(mem.eql(bool, &@as([6]bool, a >= b), &[_]bool{ true, true, false, false, false, true }));
838 try expect(mem.eql(bool, &@as([6]bool, a < b), &[_]bool{ false, false, true, true, true, false }));
839 try expect(mem.eql(bool, &@as([6]bool, a <= b), &[_]bool{ false, true, true, true, true, false }));
840 try expect(mem.eql(bool, &@as([6]bool, a == b), &[_]bool{ false, true, false, false, false, false }));
841 try expect(mem.eql(bool, &@as([6]bool, a != b), &[_]bool{ true, false, true, true, true, true }));
842 try expect(mem.eql(bool, &@as([6]bool, a > b), &[_]bool{ true, false, false, false, false, true }));
843 try expect(mem.eql(bool, &@as([6]bool, a >= b), &[_]bool{ true, true, false, false, false, true }));
844844 }
845845 };
846 S.doTheTest();
847 comptime S.doTheTest();
846 try S.doTheTest();
847 comptime try S.doTheTest();
848848}
849849
850850test "compare undefined literal with comptime_int" {
851851 var x = undefined == 1;
852852 // x is now undefined with type bool
853853 x = true;
854 expect(x);
854 try expect(x);
855855}
856856
857857test "signed zeros are represented properly" {
858858 const S = struct {
859 fn doTheTest() void {
859 fn doTheTest() !void {
860860 inline for ([_]type{ f16, f32, f64, f128 }) |T| {
861861 const ST = std.meta.Int(.unsigned, @typeInfo(T).Float.bits);
862862 var as_fp_val = -@as(T, 0.0);
863863 var as_uint_val = @bitCast(ST, as_fp_val);
864864 // 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);
866866 }
867867 }
868868 };
869869
870 S.doTheTest();
871 comptime S.doTheTest();
870 try S.doTheTest();
871 comptime try S.doTheTest();
872872}
test/stage1/behavior/misc.zig+107-107
......@@ -25,18 +25,18 @@ test "call disabled extern fn" {
2525}
2626
2727test "short circuit" {
28 testShortCircuit(false, true);
29 comptime testShortCircuit(false, true);
28 try testShortCircuit(false, true);
29 comptime try testShortCircuit(false, true);
3030}
3131
32fn testShortCircuit(f: bool, t: bool) void {
32fn testShortCircuit(f: bool, t: bool) !void {
3333 var hit_1 = f;
3434 var hit_2 = f;
3535 var hit_3 = f;
3636 var hit_4 = f;
3737
3838 if (t or x: {
39 expect(f);
39 try expect(f);
4040 break :x f;
4141 }) {
4242 hit_1 = t;
......@@ -45,31 +45,31 @@ fn testShortCircuit(f: bool, t: bool) void {
4545 hit_2 = t;
4646 break :x f;
4747 }) {
48 expect(f);
48 try expect(f);
4949 }
5050
5151 if (t and x: {
5252 hit_3 = t;
5353 break :x f;
5454 }) {
55 expect(f);
55 try expect(f);
5656 }
5757 if (f and x: {
58 expect(f);
58 try expect(f);
5959 break :x f;
6060 }) {
61 expect(f);
61 try expect(f);
6262 } else {
6363 hit_4 = t;
6464 }
65 expect(hit_1);
66 expect(hit_2);
67 expect(hit_3);
68 expect(hit_4);
65 try expect(hit_1);
66 try expect(hit_2);
67 try expect(hit_3);
68 try expect(hit_4);
6969}
7070
7171test "truncate" {
72 expect(testTruncate(0x10fd) == 0xfd);
72 try expect(testTruncate(0x10fd) == 0xfd);
7373}
7474fn testTruncate(x: u32) u8 {
7575 return @truncate(u8, x);
......@@ -80,16 +80,16 @@ fn first4KeysOfHomeRow() []const u8 {
8080}
8181
8282test "return string from function" {
83 expect(mem.eql(u8, first4KeysOfHomeRow(), "aoeu"));
83 try expect(mem.eql(u8, first4KeysOfHomeRow(), "aoeu"));
8484}
8585
8686const g1: i32 = 1233 + 1;
8787var g2: i32 = 0;
8888
8989test "global variables" {
90 expect(g2 == 0);
90 try expect(g2 == 0);
9191 g2 = g1;
92 expect(g2 == 1234);
92 try expect(g2 == 1234);
9393}
9494
9595test "memcpy and memset intrinsics" {
......@@ -106,7 +106,7 @@ test "builtin static eval" {
106106 const x: i32 = comptime x: {
107107 break :x 1 + 2 + 3;
108108 };
109 expect(x == comptime 6);
109 try expect(x == comptime 6);
110110}
111111
112112test "slicing" {
......@@ -127,7 +127,7 @@ test "slicing" {
127127
128128test "constant equal function pointers" {
129129 const alias = emptyFn;
130 expect(comptime x: {
130 try expect(comptime x: {
131131 break :x emptyFn == alias;
132132 });
133133}
......@@ -135,25 +135,25 @@ test "constant equal function pointers" {
135135fn emptyFn() void {}
136136
137137test "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"));
139139}
140140
141141test "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"));
143143}
144144
145145test "array mult operator" {
146 expect(mem.eql(u8, "ab" ** 5, "ababababab"));
146 try expect(mem.eql(u8, "ab" ** 5, "ababababab"));
147147}
148148
149149test "string escapes" {
150 expect(mem.eql(u8, "\"", "\x22"));
151 expect(mem.eql(u8, "\'", "\x27"));
152 expect(mem.eql(u8, "\n", "\x0a"));
153 expect(mem.eql(u8, "\r", "\x0d"));
154 expect(mem.eql(u8, "\t", "\x09"));
155 expect(mem.eql(u8, "\\", "\x5c"));
156 expect(mem.eql(u8, "\u{1234}\u{069}\u{1}", "\xe1\x88\xb4\x69\x01"));
150 try expect(mem.eql(u8, "\"", "\x22"));
151 try expect(mem.eql(u8, "\'", "\x27"));
152 try expect(mem.eql(u8, "\n", "\x0a"));
153 try expect(mem.eql(u8, "\r", "\x0d"));
154 try expect(mem.eql(u8, "\t", "\x09"));
155 try expect(mem.eql(u8, "\\", "\x5c"));
156 try expect(mem.eql(u8, "\u{1234}\u{069}\u{1}", "\xe1\x88\xb4\x69\x01"));
157157}
158158
159159test "multiline string" {
......@@ -163,7 +163,7 @@ test "multiline string" {
163163 \\three
164164 ;
165165 const s2 = "one\ntwo)\nthree";
166 expect(mem.eql(u8, s1, s2));
166 try expect(mem.eql(u8, s1, s2));
167167}
168168
169169test "multiline string comments at start" {
......@@ -173,7 +173,7 @@ test "multiline string comments at start" {
173173 \\three
174174 ;
175175 const s2 = "two)\nthree";
176 expect(mem.eql(u8, s1, s2));
176 try expect(mem.eql(u8, s1, s2));
177177}
178178
179179test "multiline string comments at end" {
......@@ -183,7 +183,7 @@ test "multiline string comments at end" {
183183 //\\three
184184 ;
185185 const s2 = "one\ntwo)";
186 expect(mem.eql(u8, s1, s2));
186 try expect(mem.eql(u8, s1, s2));
187187}
188188
189189test "multiline string comments in middle" {
......@@ -193,7 +193,7 @@ test "multiline string comments in middle" {
193193 \\three
194194 ;
195195 const s2 = "one\nthree";
196 expect(mem.eql(u8, s1, s2));
196 try expect(mem.eql(u8, s1, s2));
197197}
198198
199199test "multiline string comments at multiple places" {
......@@ -205,7 +205,7 @@ test "multiline string comments at multiple places" {
205205 \\five
206206 ;
207207 const s2 = "one\nthree\nfive";
208 expect(mem.eql(u8, s1, s2));
208 try expect(mem.eql(u8, s1, s2));
209209}
210210
211211test "multiline C string" {
......@@ -215,11 +215,11 @@ test "multiline C string" {
215215 \\three
216216 ;
217217 const s2 = "one\ntwo)\nthree";
218 expect(std.cstr.cmp(s1, s2) == 0);
218 try expect(std.cstr.cmp(s1, s2) == 0);
219219}
220220
221221test "type equality" {
222 expect(*const u8 != *u8);
222 try expect(*const u8 != *u8);
223223}
224224
225225const global_a: i32 = 1234;
......@@ -227,7 +227,7 @@ const global_b: *const i32 = &global_a;
227227const global_c: *const f32 = @ptrCast(*const f32, global_b);
228228test "compile time global reinterpret" {
229229 const d = @ptrCast(*const i32, global_c);
230 expect(d.* == 1234);
230 try expect(d.* == 1234);
231231}
232232
233233test "explicit cast maybe pointers" {
......@@ -253,8 +253,8 @@ test "cast undefined" {
253253fn testCastUndefined(x: []const u8) void {}
254254
255255test "cast small unsigned to larger signed" {
256 expect(castSmallUnsignedToLargerSigned1(200) == @as(i16, 200));
257 expect(castSmallUnsignedToLargerSigned2(9999) == @as(i64, 9999));
256 try expect(castSmallUnsignedToLargerSigned1(200) == @as(i16, 200));
257 try expect(castSmallUnsignedToLargerSigned2(9999) == @as(i64, 9999));
258258}
259259fn castSmallUnsignedToLargerSigned1(x: u8) i16 {
260260 return x;
......@@ -264,7 +264,7 @@ fn castSmallUnsignedToLargerSigned2(x: u16) i64 {
264264}
265265
266266test "implicit cast after unreachable" {
267 expect(outer() == 1234);
267 try expect(outer() == 1234);
268268}
269269fn inner() i32 {
270270 return 1234;
......@@ -279,13 +279,13 @@ test "pointer dereferencing" {
279279
280280 y.* += 1;
281281
282 expect(x == 4);
283 expect(y.* == 4);
282 try expect(x == 4);
283 try expect(y.* == 4);
284284}
285285
286286test "call result of if else expression" {
287 expect(mem.eql(u8, f2(true), "a"));
288 expect(mem.eql(u8, f2(false), "b"));
287 try expect(mem.eql(u8, f2(true), "a"));
288 try expect(mem.eql(u8, f2(false), "b"));
289289}
290290fn f2(x: bool) []const u8 {
291291 return (if (x) fA else fB)();
......@@ -305,8 +305,8 @@ test "const expression eval handling of variables" {
305305}
306306
307307test "constant enum initialization with differing sizes" {
308 test3_1(test3_foo);
309 test3_2(test3_bar);
308 try test3_1(test3_foo);
309 try test3_2(test3_bar);
310310}
311311const Test3Foo = union(enum) {
312312 One: void,
......@@ -324,41 +324,41 @@ const test3_foo = Test3Foo{
324324 },
325325};
326326const test3_bar = Test3Foo{ .Two = 13 };
327fn test3_1(f: Test3Foo) void {
327fn test3_1(f: Test3Foo) !void {
328328 switch (f) {
329329 Test3Foo.Three => |pt| {
330 expect(pt.x == 3);
331 expect(pt.y == 4);
330 try expect(pt.x == 3);
331 try expect(pt.y == 4);
332332 },
333333 else => unreachable,
334334 }
335335}
336fn test3_2(f: Test3Foo) void {
336fn test3_2(f: Test3Foo) !void {
337337 switch (f) {
338338 Test3Foo.Two => |x| {
339 expect(x == 13);
339 try expect(x == 13);
340340 },
341341 else => unreachable,
342342 }
343343}
344344
345345test "character literals" {
346 expect('\'' == single_quote);
346 try expect('\'' == single_quote);
347347}
348348const single_quote = '\'';
349349
350350test "take address of parameter" {
351 testTakeAddressOfParameter(12.34);
351 try testTakeAddressOfParameter(12.34);
352352}
353fn testTakeAddressOfParameter(f: f32) void {
353fn testTakeAddressOfParameter(f: f32) !void {
354354 const f_ptr = &f;
355 expect(f_ptr.* == 12.34);
355 try expect(f_ptr.* == 12.34);
356356}
357357
358358test "pointer comparison" {
359359 const a = @as([]const u8, "a");
360360 const b = &a;
361 expect(ptrEql(b, b));
361 try expect(ptrEql(b, b));
362362}
363363fn ptrEql(a: *const []const u8, b: *const []const u8) bool {
364364 return a == b;
......@@ -368,19 +368,19 @@ test "string concatenation" {
368368 const a = "OK" ++ " IT " ++ "WORKED";
369369 const b = "OK IT WORKED";
370370
371 comptime expect(@TypeOf(a) == *const [12:0]u8);
372 comptime expect(@TypeOf(b) == *const [12:0]u8);
371 comptime try expect(@TypeOf(a) == *const [12:0]u8);
372 comptime try expect(@TypeOf(b) == *const [12:0]u8);
373373
374374 const len = mem.len(b);
375375 const len_with_null = len + 1;
376376 {
377377 var i: u32 = 0;
378378 while (i < len_with_null) : (i += 1) {
379 expect(a[i] == b[i]);
379 try expect(a[i] == b[i]);
380380 }
381381 }
382 expect(a[len] == 0);
383 expect(b[len] == 0);
382 try expect(a[len] == 0);
383 try expect(b[len] == 0);
384384}
385385
386386test "pointer to void return type" {
......@@ -397,7 +397,7 @@ fn testPointerToVoidReturnType2() *const void {
397397
398398test "non const ptr to aliased type" {
399399 const int = i32;
400 expect(?*int == ?*i32);
400 try expect(?*int == ?*i32);
401401}
402402
403403test "array 2D const double ptr" {
......@@ -405,13 +405,13 @@ test "array 2D const double ptr" {
405405 [_]f32{1.0},
406406 [_]f32{2.0},
407407 };
408 testArray2DConstDoublePtr(&rect_2d_vertexes[0][0]);
408 try testArray2DConstDoublePtr(&rect_2d_vertexes[0][0]);
409409}
410410
411fn testArray2DConstDoublePtr(ptr: *const f32) void {
411fn testArray2DConstDoublePtr(ptr: *const f32) !void {
412412 const ptr2 = @ptrCast([*]const f32, ptr);
413 expect(ptr2[0] == 1.0);
414 expect(ptr2[1] == 2.0);
413 try expect(ptr2[0] == 1.0);
414 try expect(ptr2[1] == 2.0);
415415}
416416
417417const AStruct = struct {
......@@ -439,13 +439,13 @@ test "@typeName" {
439439 Unused,
440440 };
441441 comptime {
442 expect(mem.eql(u8, @typeName(i64), "i64"));
443 expect(mem.eql(u8, @typeName(*usize), "*usize"));
442 try expect(mem.eql(u8, @typeName(i64), "i64"));
443 try expect(mem.eql(u8, @typeName(*usize), "*usize"));
444444 // https://github.com/ziglang/zig/issues/675
445 expect(mem.eql(u8, "behavior.misc.TypeFromFn(u8)", @typeName(TypeFromFn(u8))));
446 expect(mem.eql(u8, @typeName(Struct), "Struct"));
447 expect(mem.eql(u8, @typeName(Union), "Union"));
448 expect(mem.eql(u8, @typeName(Enum), "Enum"));
445 try expect(mem.eql(u8, "behavior.misc.TypeFromFn(u8)", @typeName(TypeFromFn(u8))));
446 try expect(mem.eql(u8, @typeName(Struct), "Struct"));
447 try expect(mem.eql(u8, @typeName(Union), "Union"));
448 try expect(mem.eql(u8, @typeName(Enum), "Enum"));
449449 }
450450}
451451
......@@ -455,14 +455,14 @@ fn TypeFromFn(comptime T: type) type {
455455
456456test "double implicit cast in same expression" {
457457 var x = @as(i32, @as(u16, nine()));
458 expect(x == 9);
458 try expect(x == 9);
459459}
460460fn nine() u8 {
461461 return 9;
462462}
463463
464464test "global variable initialized to global variable array element" {
465 expect(global_ptr == &gdt[0]);
465 try expect(global_ptr == &gdt[0]);
466466}
467467const GDTEntry = struct {
468468 field: i32,
......@@ -483,9 +483,9 @@ export fn writeToVRam() void {
483483const OpaqueA = opaque {};
484484const OpaqueB = opaque {};
485485test "opaque types" {
486 expect(*OpaqueA != *OpaqueB);
487 expect(mem.eql(u8, @typeName(OpaqueA), "OpaqueA"));
488 expect(mem.eql(u8, @typeName(OpaqueB), "OpaqueB"));
486 try expect(*OpaqueA != *OpaqueB);
487 try expect(mem.eql(u8, @typeName(OpaqueA), "OpaqueA"));
488 try expect(mem.eql(u8, @typeName(OpaqueB), "OpaqueB"));
489489}
490490
491491test "variable is allowed to be a pointer to an opaque type" {
......@@ -525,7 +525,7 @@ fn fnThatClosesOverLocalConst() type {
525525
526526test "function closes over local const" {
527527 const x = fnThatClosesOverLocalConst().g();
528 expect(x == 1);
528 try expect(x == 1);
529529}
530530
531531test "cold function" {
......@@ -562,21 +562,21 @@ export fn testPackedStuff(a: *const PackedStruct, b: *const PackedUnion, c: Pack
562562test "slicing zero length array" {
563563 const s1 = ""[0..];
564564 const s2 = ([_]u32{})[0..];
565 expect(s1.len == 0);
566 expect(s2.len == 0);
567 expect(mem.eql(u8, s1, ""));
568 expect(mem.eql(u32, s2, &[_]u32{}));
565 try expect(s1.len == 0);
566 try expect(s2.len == 0);
567 try expect(mem.eql(u8, s1, ""));
568 try expect(mem.eql(u32, s2, &[_]u32{}));
569569}
570570
571571const addr1 = @ptrCast(*const u8, emptyFn);
572572test "comptime cast fn to ptr" {
573573 const addr2 = @ptrCast(*const u8, emptyFn);
574 comptime expect(addr1 == addr2);
574 comptime try expect(addr1 == addr2);
575575}
576576
577577test "equality compare fn ptrs" {
578578 var a = emptyFn;
579 expect(a == a);
579 try expect(a == a);
580580}
581581
582582test "self reference through fn ptr field" {
......@@ -591,34 +591,34 @@ test "self reference through fn ptr field" {
591591 };
592592 var a: S.A = undefined;
593593 a.f = S.foo;
594 expect(a.f(a) == 12);
594 try expect(a.f(a) == 12);
595595}
596596
597597test "volatile load and store" {
598598 var number: i32 = 1234;
599599 const ptr = @as(*volatile i32, &number);
600600 ptr.* += 1;
601 expect(ptr.* == 1235);
601 try expect(ptr.* == 1235);
602602}
603603
604604test "slice string literal has correct type" {
605605 comptime {
606 expect(@TypeOf("aoeu"[0..]) == *const [4:0]u8);
606 try expect(@TypeOf("aoeu"[0..]) == *const [4:0]u8);
607607 const array = [_]i32{ 1, 2, 3, 4 };
608 expect(@TypeOf(array[0..]) == *const [4]i32);
608 try expect(@TypeOf(array[0..]) == *const [4]i32);
609609 }
610610 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);
612612 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);
614614}
615615
616616test "struct inside function" {
617 testStructInFn();
618 comptime testStructInFn();
617 try testStructInFn();
618 comptime try testStructInFn();
619619}
620620
621fn testStructInFn() void {
621fn testStructInFn() !void {
622622 const BlockKind = u32;
623623
624624 const Block = struct {
......@@ -629,11 +629,11 @@ fn testStructInFn() void {
629629
630630 block.kind += 1;
631631
632 expect(block.kind == 1235);
632 try expect(block.kind == 1235);
633633}
634634
635635test "fn call returning scalar optional in equality expression" {
636 expect(getNull() == null);
636 try expect(getNull() == null);
637637}
638638
639639fn getNull() ?*i32 {
......@@ -645,16 +645,16 @@ test "thread local variable" {
645645 threadlocal var t: i32 = 1234;
646646 };
647647 S.t += 1;
648 expect(S.t == 1235);
648 try expect(S.t == 1235);
649649}
650650
651651test "unicode escape in character literal" {
652652 var a: u24 = '\u{01f4a9}';
653 expect(a == 128169);
653 try expect(a == 128169);
654654}
655655
656656test "unicode character in character literal" {
657 expect('💩' == 128169);
657 try expect('💩' == 128169);
658658}
659659
660660test "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
662662 entries: []u32,
663663 };
664664 var foo = E{ .entries = &[_]u32{} };
665 expect(foo.entries.len == 0);
665 try expect(foo.entries.len == 0);
666666}
667667
668668var global_foo: *i32 = undefined;
......@@ -677,7 +677,7 @@ test "global variable assignment with optional unwrapping with var initialized t
677677 global_foo = S.foo() orelse {
678678 @panic("bad");
679679 };
680 expect(global_foo.* == 1234);
680 try expect(global_foo.* == 1234);
681681}
682682
683683test "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
696696 bleh: i32,
697697 };
698698 };
699 expect(S.doTheTest(0) == 1234);
700 expect(S.doTheTest(1) == 1234);
699 try expect(S.doTheTest(0) == 1234);
700 try expect(S.doTheTest(1) == 1234);
701701}
702702
703703test "nested optional field in struct" {
......@@ -710,7 +710,7 @@ test "nested optional field in struct" {
710710 var s = S1{
711711 .x = S2{ .y = 127 },
712712 };
713 expect(s.x.?.y == 127);
713 try expect(s.x.?.y == 127);
714714}
715715
716716fn maybe(x: bool) anyerror!?u32 {
......@@ -722,7 +722,7 @@ fn maybe(x: bool) anyerror!?u32 {
722722
723723test "result location is optional inside error union" {
724724 const x = maybe(true) catch unreachable;
725 expect(x.? == 42);
725 try expect(x.? == 42);
726726}
727727
728728threadlocal var buffer: [11]u8 = undefined;
......@@ -730,7 +730,7 @@ threadlocal var buffer: [11]u8 = undefined;
730730test "pointer to thread local array" {
731731 const s = "Hello world";
732732 std.mem.copy(u8, buffer[0..], s);
733 std.testing.expectEqualSlices(u8, buffer[0..], s);
733 try std.testing.expectEqualSlices(u8, buffer[0..], s);
734734}
735735
736736test "auto created variables have correct alignment" {
......@@ -742,15 +742,15 @@ test "auto created variables have correct alignment" {
742742 return 0;
743743 }
744744 };
745 expect(S.foo("\x7a\x7a\x7a\x7a") == 0x7a7a7a7a);
746 comptime expect(S.foo("\x7a\x7a\x7a\x7a") == 0x7a7a7a7a);
745 try expect(S.foo("\x7a\x7a\x7a\x7a") == 0x7a7a7a7a);
746 comptime try expect(S.foo("\x7a\x7a\x7a\x7a") == 0x7a7a7a7a);
747747}
748748
749749extern var opaque_extern_var: opaque {};
750750var var_to_export: u32 = 42;
751751test "extern variable with non-pointer opaque type" {
752752 @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);
754754}
755755
756756test "lazy typeInfo value as generic parameter" {
test/stage1/behavior/muladd.zig+7-7
......@@ -1,34 +1,34 @@
11const expect = @import("std").testing.expect;
22
33test "@mulAdd" {
4 comptime testMulAdd();
5 testMulAdd();
4 comptime try testMulAdd();
5 try testMulAdd();
66}
77
8fn testMulAdd() void {
8fn testMulAdd() !void {
99 {
1010 var a: f16 = 5.5;
1111 var b: f16 = 2.5;
1212 var c: f16 = 6.25;
13 expect(@mulAdd(f16, a, b, c) == 20);
13 try expect(@mulAdd(f16, a, b, c) == 20);
1414 }
1515 {
1616 var a: f32 = 5.5;
1717 var b: f32 = 2.5;
1818 var c: f32 = 6.25;
19 expect(@mulAdd(f32, a, b, c) == 20);
19 try expect(@mulAdd(f32, a, b, c) == 20);
2020 }
2121 {
2222 var a: f64 = 5.5;
2323 var b: f64 = 2.5;
2424 var c: f64 = 6.25;
25 expect(@mulAdd(f64, a, b, c) == 20);
25 try expect(@mulAdd(f64, a, b, c) == 20);
2626 }
2727 // Awaits implementation in libm.zig
2828 //{
2929 // var a: f16 = 5.5;
3030 // var b: f128 = 2.5;
3131 // var c: f128 = 6.25;
32 // expect(@mulAdd(f128, a, b, c) == 20);
32 //try expect(@mulAdd(f128, a, b, c) == 20);
3333 //}
3434}
test/stage1/behavior/namespace_depends_on_compile_var.zig+2-2
......@@ -3,9 +3,9 @@ const expect = std.testing.expect;
33
44test "namespace depends on compile var" {
55 if (some_namespace.a_bool) {
6 expect(some_namespace.a_bool);
6 try expect(some_namespace.a_bool);
77 } else {
8 expect(!some_namespace.a_bool);
8 try expect(!some_namespace.a_bool);
99 }
1010}
1111const some_namespace = switch (std.builtin.os.tag) {
test/stage1/behavior/null.zig+24-24
......@@ -17,13 +17,13 @@ test "optional type" {
1717
1818 const z = next_x orelse 1234;
1919
20 expect(z == 1234);
20 try expect(z == 1234);
2121
2222 const final_x: ?i32 = 13;
2323
2424 const num = final_x orelse unreachable;
2525
26 expect(num == 13);
26 try expect(num == 13);
2727}
2828
2929test "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" {
3333 b.* = false;
3434 }
3535
36 expect(maybe_bool.? == false);
36 try expect(maybe_bool.? == false);
3737}
3838
3939test "rhs maybe unwrap return" {
......@@ -42,14 +42,14 @@ test "rhs maybe unwrap return" {
4242}
4343
4444test "maybe return" {
45 maybeReturnImpl();
46 comptime maybeReturnImpl();
45 try maybeReturnImpl();
46 comptime try maybeReturnImpl();
4747}
4848
49fn maybeReturnImpl() void {
50 expect(foo(1235).?);
49fn maybeReturnImpl() !void {
50 try expect(foo(1235).?);
5151 if (foo(null) != null) unreachable;
52 expect(!foo(1234).?);
52 try expect(!foo(1234).?);
5353}
5454
5555fn foo(x: ?i32) ?bool {
......@@ -58,7 +58,7 @@ fn foo(x: ?i32) ?bool {
5858}
5959
6060test "if var maybe pointer" {
61 expect(shouldBeAPlus1(Particle{
61 try expect(shouldBeAPlus1(Particle{
6262 .a = 14,
6363 .b = 1,
6464 .c = 1,
......@@ -84,10 +84,10 @@ const Particle = struct {
8484
8585test "null literal outside function" {
8686 const is_null = here_is_a_null_literal.context == null;
87 expect(is_null);
87 try expect(is_null);
8888
8989 const is_non_null = here_is_a_null_literal.context != null;
90 expect(!is_non_null);
90 try expect(!is_non_null);
9191}
9292const SillyStruct = struct {
9393 context: ?i32,
......@@ -95,21 +95,21 @@ const SillyStruct = struct {
9595const here_is_a_null_literal = SillyStruct{ .context = null };
9696
9797test "test null runtime" {
98 testTestNullRuntime(null);
98 try testTestNullRuntime(null);
9999}
100fn testTestNullRuntime(x: ?i32) void {
101 expect(x == null);
102 expect(!(x != null));
100fn testTestNullRuntime(x: ?i32) !void {
101 try expect(x == null);
102 try expect(!(x != null));
103103}
104104
105105test "optional void" {
106 optionalVoidImpl();
107 comptime optionalVoidImpl();
106 try optionalVoidImpl();
107 comptime try optionalVoidImpl();
108108}
109109
110fn optionalVoidImpl() void {
111 expect(bar(null) == null);
112 expect(bar({}) != null);
110fn optionalVoidImpl() !void {
111 try expect(bar(null) == null);
112 try expect(bar({}) != null);
113113}
114114
115115fn bar(x: ?void) ?void {
......@@ -133,7 +133,7 @@ test "unwrap optional which is field of global var" {
133133 }
134134 struct_with_optional.field = 1234;
135135 if (struct_with_optional.field) |payload| {
136 expect(payload == 1234);
136 try expect(payload == 1234);
137137 } else {
138138 unreachable;
139139 }
......@@ -141,13 +141,13 @@ test "unwrap optional which is field of global var" {
141141
142142test "null with default unwrap" {
143143 const x: i32 = null orelse 1;
144 expect(x == 1);
144 try expect(x == 1);
145145}
146146
147147test "optional types" {
148148 comptime {
149149 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);
151151 }
152152}
153153
......@@ -158,5 +158,5 @@ const StructWithOptionalType = struct {
158158test "optional pointer to 0 bit type null value at runtime" {
159159 const EmptyStruct = struct {};
160160 var x: ?*EmptyStruct = null;
161 expect(x == null);
161 try expect(x == null);
162162}
test/stage1/behavior/optional.zig+51-51
......@@ -8,28 +8,28 @@ pub const EmptyStruct = struct {};
88test "optional pointer to size zero struct" {
99 var e = EmptyStruct{};
1010 var o: ?*EmptyStruct = &e;
11 expect(o != null);
11 try expect(o != null);
1212}
1313
1414test "equality compare nullable pointers" {
15 testNullPtrsEql();
16 comptime testNullPtrsEql();
15 try testNullPtrsEql();
16 comptime try testNullPtrsEql();
1717}
1818
19fn testNullPtrsEql() void {
19fn testNullPtrsEql() !void {
2020 var number: i32 = 1234;
2121
2222 var x: ?*i32 = null;
2323 var y: ?*i32 = null;
24 expect(x == y);
24 try expect(x == y);
2525 y = &number;
26 expect(x != y);
27 expect(x != &number);
28 expect(&number != x);
26 try expect(x != y);
27 try expect(x != &number);
28 try expect(&number != x);
2929 x = &number;
30 expect(x == y);
31 expect(x == &number);
32 expect(&number == x);
30 try expect(x == y);
31 try expect(x == &number);
32 try expect(&number == x);
3333}
3434
3535test "address of unwrap optional" {
......@@ -46,23 +46,23 @@ test "address of unwrap optional" {
4646 };
4747 S.global = S.Foo{ .a = 1234 };
4848 const foo = S.getFoo() catch unreachable;
49 expect(foo.a == 1234);
49 try expect(foo.a == 1234);
5050}
5151
5252test "equality compare optional with non-optional" {
53 test_cmp_optional_non_optional();
54 comptime test_cmp_optional_non_optional();
53 try test_cmp_optional_non_optional();
54 comptime try test_cmp_optional_non_optional();
5555}
5656
57fn test_cmp_optional_non_optional() void {
57fn test_cmp_optional_non_optional() !void {
5858 var ten: i32 = 10;
5959 var opt_ten: ?i32 = 10;
6060 var five: i32 = 5;
6161 var int_n: ?i32 = null;
6262
63 expect(int_n != ten);
64 expect(opt_ten == ten);
65 expect(opt_ten != five);
63 try expect(int_n != ten);
64 try expect(opt_ten == ten);
65 try expect(opt_ten != five);
6666
6767 // test evaluation is always lexical
6868 // ensure that the optional isn't always computed before the non-optional
......@@ -71,14 +71,14 @@ fn test_cmp_optional_non_optional() void {
7171 mutable_state += 1;
7272 break :blk1 @as(?f64, 10.0);
7373 } != blk2: {
74 expect(mutable_state == 1);
74 try expect(mutable_state == 1);
7575 break :blk2 @as(f64, 5.0);
7676 };
7777 _ = blk1: {
7878 mutable_state += 1;
7979 break :blk1 @as(f64, 10.0);
8080 } != blk2: {
81 expect(mutable_state == 2);
81 try expect(mutable_state == 2);
8282 break :blk2 @as(?f64, 5.0);
8383 };
8484}
......@@ -94,15 +94,15 @@ test "passing an optional integer as a parameter" {
9494 return x.? == 1234;
9595 }
9696 };
97 expect(S.entry());
98 comptime expect(S.entry());
97 try expect(S.entry());
98 comptime try expect(S.entry());
9999}
100100
101101test "unwrap function call with optional pointer return value" {
102102 const S = struct {
103 fn entry() void {
104 expect(foo().?.* == 1234);
105 expect(bar() == null);
103 fn entry() !void {
104 try expect(foo().?.* == 1234);
105 try expect(bar() == null);
106106 }
107107 const global: i32 = 1234;
108108 fn foo() ?*const i32 {
......@@ -112,14 +112,14 @@ test "unwrap function call with optional pointer return value" {
112112 return null;
113113 }
114114 };
115 S.entry();
116 comptime S.entry();
115 try S.entry();
116 comptime try S.entry();
117117}
118118
119119test "nested orelse" {
120120 const S = struct {
121 fn entry() void {
122 expect(func() == null);
121 fn entry() !void {
122 try expect(func() == null);
123123 }
124124 fn maybe() ?Foo {
125125 return null;
......@@ -134,8 +134,8 @@ test "nested orelse" {
134134 field: i32,
135135 };
136136 };
137 S.entry();
138 comptime S.entry();
137 try S.entry();
138 comptime try S.entry();
139139}
140140
141141test "self-referential struct through a slice of optional" {
......@@ -154,7 +154,7 @@ test "self-referential struct through a slice of optional" {
154154 };
155155
156156 var n = S.Node.new();
157 expect(n.data == null);
157 try expect(n.data == null);
158158}
159159
160160test "assigning to an unwrapped optional field in an inline loop" {
......@@ -173,14 +173,14 @@ test "coerce an anon struct literal to optional struct" {
173173 const Struct = struct {
174174 field: u32,
175175 };
176 export fn doTheTest() void {
176 fn doTheTest() !void {
177177 var maybe_dims: ?Struct = null;
178178 maybe_dims = .{ .field = 1 };
179 expect(maybe_dims.?.field == 1);
179 try expect(maybe_dims.?.field == 1);
180180 }
181181 };
182 S.doTheTest();
183 comptime S.doTheTest();
182 try S.doTheTest();
183 comptime try S.doTheTest();
184184}
185185
186186test "optional with void type" {
......@@ -188,15 +188,15 @@ test "optional with void type" {
188188 x: ?void,
189189 };
190190 var x = Foo{ .x = null };
191 expect(x.x == null);
191 try expect(x.x == null);
192192}
193193
194194test "0-bit child type coerced to optional return ptr result location" {
195195 const S = struct {
196 fn doTheTest() void {
196 fn doTheTest() !void {
197197 var y = Foo{};
198198 var z = y.thing();
199 expect(z != null);
199 try expect(z != null);
200200 }
201201
202202 const Foo = struct {
......@@ -209,17 +209,17 @@ test "0-bit child type coerced to optional return ptr result location" {
209209 }
210210 };
211211 };
212 S.doTheTest();
213 comptime S.doTheTest();
212 try S.doTheTest();
213 comptime try S.doTheTest();
214214}
215215
216216test "0-bit child type coerced to optional" {
217217 const S = struct {
218 fn doTheTest() void {
218 fn doTheTest() !void {
219219 var it: Foo = .{
220220 .list = undefined,
221221 };
222 expect(it.foo() != null);
222 try expect(it.foo() != null);
223223 }
224224
225225 const Empty = struct {};
......@@ -232,8 +232,8 @@ test "0-bit child type coerced to optional" {
232232 }
233233 };
234234 };
235 S.doTheTest();
236 comptime S.doTheTest();
235 try S.doTheTest();
236 comptime try S.doTheTest();
237237}
238238
239239test "array of optional unaligned types" {
......@@ -255,15 +255,15 @@ test "array of optional unaligned types" {
255255
256256 // The index must be a runtime value
257257 var i: usize = 0;
258 expectEqual(Enum.one, values[i].?.Num);
258 try expectEqual(Enum.one, values[i].?.Num);
259259 i += 1;
260 expectEqual(Enum.two, values[i].?.Num);
260 try expectEqual(Enum.two, values[i].?.Num);
261261 i += 1;
262 expectEqual(Enum.three, values[i].?.Num);
262 try expectEqual(Enum.three, values[i].?.Num);
263263 i += 1;
264 expectEqual(Enum.one, values[i].?.Num);
264 try expectEqual(Enum.one, values[i].?.Num);
265265 i += 1;
266 expectEqual(Enum.two, values[i].?.Num);
266 try expectEqual(Enum.two, values[i].?.Num);
267267 i += 1;
268 expectEqual(Enum.three, values[i].?.Num);
268 try expectEqual(Enum.three, values[i].?.Num);
269269}
test/stage1/behavior/pointers.zig+108-108
......@@ -4,15 +4,15 @@ const expect = testing.expect;
44const expectError = testing.expectError;
55
66test "dereference pointer" {
7 comptime testDerefPtr();
8 testDerefPtr();
7 comptime try testDerefPtr();
8 try testDerefPtr();
99}
1010
11fn testDerefPtr() void {
11fn testDerefPtr() !void {
1212 var x: i32 = 1234;
1313 var y = &x;
1414 y.* += 1;
15 expect(x == 1235);
15 try expect(x == 1235);
1616}
1717
1818const Foo1 = struct {
......@@ -20,41 +20,41 @@ const Foo1 = struct {
2020};
2121
2222test "dereference pointer again" {
23 testDerefPtrOneVal();
24 comptime testDerefPtrOneVal();
23 try testDerefPtrOneVal();
24 comptime try testDerefPtrOneVal();
2525}
2626
27fn testDerefPtrOneVal() void {
27fn testDerefPtrOneVal() !void {
2828 // Foo1 satisfies the OnePossibleValueYes criteria
2929 const x = &Foo1{ .x = {} };
3030 const y = x.*;
31 expect(@TypeOf(y.x) == void);
31 try expect(@TypeOf(y.x) == void);
3232}
3333
3434test "pointer arithmetic" {
3535 var ptr: [*]const u8 = "abcd";
3636
37 expect(ptr[0] == 'a');
37 try expect(ptr[0] == 'a');
3838 ptr += 1;
39 expect(ptr[0] == 'b');
39 try expect(ptr[0] == 'b');
4040 ptr += 1;
41 expect(ptr[0] == 'c');
41 try expect(ptr[0] == 'c');
4242 ptr += 1;
43 expect(ptr[0] == 'd');
43 try expect(ptr[0] == 'd');
4444 ptr += 1;
45 expect(ptr[0] == 0);
45 try expect(ptr[0] == 0);
4646 ptr -= 1;
47 expect(ptr[0] == 'd');
47 try expect(ptr[0] == 'd');
4848 ptr -= 1;
49 expect(ptr[0] == 'c');
49 try expect(ptr[0] == 'c');
5050 ptr -= 1;
51 expect(ptr[0] == 'b');
51 try expect(ptr[0] == 'b');
5252 ptr -= 1;
53 expect(ptr[0] == 'a');
53 try expect(ptr[0] == 'a');
5454}
5555
5656test "double pointer parsing" {
57 comptime expect(PtrOf(PtrOf(i32)) == **i32);
57 comptime try expect(PtrOf(PtrOf(i32)) == **i32);
5858}
5959
6060fn PtrOf(comptime T: type) type {
......@@ -72,33 +72,33 @@ test "implicit cast single item pointer to C pointer and back" {
7272 var x: [*c]u8 = &y;
7373 var z: *u8 = x;
7474 z.* += 1;
75 expect(y == 12);
75 try expect(y == 12);
7676}
7777
7878test "C pointer comparison and arithmetic" {
7979 const S = struct {
80 fn doTheTest() void {
80 fn doTheTest() !void {
8181 var one: usize = 1;
8282 var ptr1: [*c]u32 = 0;
8383 var ptr2 = ptr1 + 10;
84 expect(ptr1 == 0);
85 expect(ptr1 >= 0);
86 expect(ptr1 <= 0);
84 try expect(ptr1 == 0);
85 try expect(ptr1 >= 0);
86 try expect(ptr1 <= 0);
8787 // expect(ptr1 < 1);
8888 // expect(ptr1 < one);
8989 // expect(1 > ptr1);
9090 // expect(one > ptr1);
91 expect(ptr1 < ptr2);
92 expect(ptr2 > ptr1);
93 expect(ptr2 >= 40);
94 expect(ptr2 == 40);
95 expect(ptr2 <= 40);
91 try expect(ptr1 < ptr2);
92 try expect(ptr2 > ptr1);
93 try expect(ptr2 >= 40);
94 try expect(ptr2 == 40);
95 try expect(ptr2 <= 40);
9696 ptr2 -= 10;
97 expect(ptr1 == ptr2);
97 try expect(ptr1 == ptr2);
9898 }
9999 };
100 S.doTheTest();
101 comptime S.doTheTest();
100 try S.doTheTest();
101 comptime try S.doTheTest();
102102}
103103
104104test "peer type resolution with C pointers" {
......@@ -110,10 +110,10 @@ test "peer type resolution with C pointers" {
110110 var x2 = if (t) ptr_many else ptr_c;
111111 var x3 = if (t) ptr_c else ptr_one;
112112 var x4 = if (t) ptr_c else ptr_many;
113 expect(@TypeOf(x1) == [*c]u8);
114 expect(@TypeOf(x2) == [*c]u8);
115 expect(@TypeOf(x3) == [*c]u8);
116 expect(@TypeOf(x4) == [*c]u8);
113 try expect(@TypeOf(x1) == [*c]u8);
114 try expect(@TypeOf(x2) == [*c]u8);
115 try expect(@TypeOf(x3) == [*c]u8);
116 try expect(@TypeOf(x4) == [*c]u8);
117117}
118118
119119test "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" {
121121 const opt_many_ptr: ?[*]const u8 = slice.ptr;
122122 var ptr_opt_many_ptr = &opt_many_ptr;
123123 var c_ptr: [*c]const [*c]const u8 = ptr_opt_many_ptr;
124 expect(c_ptr.*.* == 'a');
124 try expect(c_ptr.*.* == 'a');
125125 ptr_opt_many_ptr = c_ptr;
126 expect(ptr_opt_many_ptr.*.?[1] == 'o');
126 try expect(ptr_opt_many_ptr.*.?[1] == 'o');
127127}
128128
129129test "implicit cast error unions with non-optional to optional pointer" {
130130 const S = struct {
131 fn doTheTest() void {
132 expectError(error.Fail, foo());
131 fn doTheTest() !void {
132 try expectError(error.Fail, foo());
133133 }
134134 fn foo() anyerror!?*u8 {
135135 return bar() orelse error.Fail;
......@@ -138,111 +138,111 @@ test "implicit cast error unions with non-optional to optional pointer" {
138138 return null;
139139 }
140140 };
141 S.doTheTest();
142 comptime S.doTheTest();
141 try S.doTheTest();
142 comptime try S.doTheTest();
143143}
144144
145145test "initialize const optional C pointer to null" {
146146 const a: ?[*c]i32 = null;
147 expect(a == null);
148 comptime expect(a == null);
147 try expect(a == null);
148 comptime try expect(a == null);
149149}
150150
151151test "compare equality of optional and non-optional pointer" {
152152 const a = @intToPtr(*const usize, 0x12345678);
153153 const b = @intToPtr(?*usize, 0x12345678);
154 expect(a == b);
155 expect(b == a);
154 try expect(a == b);
155 try expect(b == a);
156156}
157157
158158test "allowzero pointer and slice" {
159159 var ptr = @intToPtr([*]allowzero i32, 0);
160160 var opt_ptr: ?[*]allowzero i32 = ptr;
161 expect(opt_ptr != null);
162 expect(@ptrToInt(ptr) == 0);
161 try expect(opt_ptr != null);
162 try expect(@ptrToInt(ptr) == 0);
163163 var runtime_zero: usize = 0;
164164 var slice = ptr[runtime_zero..10];
165 comptime expect(@TypeOf(slice) == []allowzero i32);
166 expect(@ptrToInt(&slice[5]) == 20);
165 comptime try expect(@TypeOf(slice) == []allowzero i32);
166 try expect(@ptrToInt(&slice[5]) == 20);
167167
168 comptime expect(@typeInfo(@TypeOf(ptr)).Pointer.is_allowzero);
169 comptime expect(@typeInfo(@TypeOf(slice)).Pointer.is_allowzero);
168 comptime try expect(@typeInfo(@TypeOf(ptr)).Pointer.is_allowzero);
169 comptime try expect(@typeInfo(@TypeOf(slice)).Pointer.is_allowzero);
170170}
171171
172172test "assign null directly to C pointer and test null equality" {
173173 var x: [*c]i32 = null;
174 expect(x == null);
175 expect(null == x);
176 expect(!(x != null));
177 expect(!(null != x));
174 try expect(x == null);
175 try expect(null == x);
176 try expect(!(x != null));
177 try expect(!(null != x));
178178 if (x) |same_x| {
179179 @panic("fail");
180180 }
181181 var otherx: i32 = undefined;
182 expect((x orelse &otherx) == &otherx);
182 try expect((x orelse &otherx) == &otherx);
183183
184184 const y: [*c]i32 = null;
185 comptime expect(y == null);
186 comptime expect(null == y);
187 comptime expect(!(y != null));
188 comptime expect(!(null != y));
185 comptime try expect(y == null);
186 comptime try expect(null == y);
187 comptime try expect(!(y != null));
188 comptime try expect(!(null != y));
189189 if (y) |same_y| @panic("fail");
190190 const othery: i32 = undefined;
191 comptime expect((y orelse &othery) == &othery);
191 comptime try expect((y orelse &othery) == &othery);
192192
193193 var n: i32 = 1234;
194194 var x1: [*c]i32 = &n;
195 expect(!(x1 == null));
196 expect(!(null == x1));
197 expect(x1 != null);
198 expect(null != x1);
199 expect(x1.?.* == 1234);
195 try expect(!(x1 == null));
196 try expect(!(null == x1));
197 try expect(x1 != null);
198 try expect(null != x1);
199 try expect(x1.?.* == 1234);
200200 if (x1) |same_x1| {
201 expect(same_x1.* == 1234);
201 try expect(same_x1.* == 1234);
202202 } else {
203203 @panic("fail");
204204 }
205 expect((x1 orelse &otherx) == x1);
205 try expect((x1 orelse &otherx) == x1);
206206
207207 const nc: i32 = 1234;
208208 const y1: [*c]const i32 = &nc;
209 comptime expect(!(y1 == null));
210 comptime expect(!(null == y1));
211 comptime expect(y1 != null);
212 comptime expect(null != y1);
213 comptime expect(y1.?.* == 1234);
209 comptime try expect(!(y1 == null));
210 comptime try expect(!(null == y1));
211 comptime try expect(y1 != null);
212 comptime try expect(null != y1);
213 comptime try expect(y1.?.* == 1234);
214214 if (y1) |same_y1| {
215 expect(same_y1.* == 1234);
215 try expect(same_y1.* == 1234);
216216 } else {
217217 @compileError("fail");
218218 }
219 comptime expect((y1 orelse &othery) == y1);
219 comptime try expect((y1 orelse &othery) == y1);
220220}
221221
222222test "null terminated pointer" {
223223 const S = struct {
224 fn doTheTest() void {
224 fn doTheTest() !void {
225225 var array_with_zero = [_:0]u8{ 'h', 'e', 'l', 'l', 'o' };
226226 var zero_ptr: [*:0]const u8 = @ptrCast([*:0]const u8, &array_with_zero);
227227 var no_zero_ptr: [*]const u8 = zero_ptr;
228228 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"));
230230 }
231231 };
232 S.doTheTest();
233 comptime S.doTheTest();
232 try S.doTheTest();
233 comptime try S.doTheTest();
234234}
235235
236236test "allow any sentinel" {
237237 const S = struct {
238 fn doTheTest() void {
238 fn doTheTest() !void {
239239 var array = [_:std.math.minInt(i32)]i32{ 1, 2, 3, 4 };
240240 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));
242242 }
243243 };
244 S.doTheTest();
245 comptime S.doTheTest();
244 try S.doTheTest();
245 comptime try S.doTheTest();
246246}
247247
248248test "pointer sentinel with enums" {
......@@ -253,42 +253,42 @@ test "pointer sentinel with enums" {
253253 sentinel,
254254 };
255255
256 fn doTheTest() void {
256 fn doTheTest() !void {
257257 var ptr: [*:.sentinel]const Number = &[_:.sentinel]Number{ .one, .two, .two, .one };
258 expect(ptr[4] == .sentinel); // TODO this should be comptime expect, see #3731
258 try expect(ptr[4] == .sentinel); // TODO this should be comptime try expect, see #3731
259259 }
260260 };
261 S.doTheTest();
262 comptime S.doTheTest();
261 try S.doTheTest();
262 comptime try S.doTheTest();
263263}
264264
265265test "pointer sentinel with optional element" {
266266 const S = struct {
267 fn doTheTest() void {
267 fn doTheTest() !void {
268268 var ptr: [*:null]const ?i32 = &[_:null]?i32{ 1, 2, 3, 4 };
269 expect(ptr[4] == null); // TODO this should be comptime expect, see #3731
269 try expect(ptr[4] == null); // TODO this should be comptime try expect, see #3731
270270 }
271271 };
272 S.doTheTest();
273 comptime S.doTheTest();
272 try S.doTheTest();
273 comptime try S.doTheTest();
274274}
275275
276276test "pointer sentinel with +inf" {
277277 const S = struct {
278 fn doTheTest() void {
278 fn doTheTest() !void {
279279 const inf = std.math.inf_f32;
280280 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 #3731
281 try expect(ptr[4] == inf); // TODO this should be comptime try expect, see #3731
282282 }
283283 };
284 S.doTheTest();
285 comptime S.doTheTest();
284 try S.doTheTest();
285 comptime try S.doTheTest();
286286}
287287
288288test "pointer to array at fixed address" {
289289 const array = @intToPtr(*volatile [1]u32, 0x10);
290290 // Silly check just to reference `array`
291 expect(@ptrToInt(&array[0]) == 0x10);
291 try expect(@ptrToInt(&array[0]) == 0x10);
292292}
293293
294294test "pointer arithmetic affects the alignment" {
......@@ -296,28 +296,28 @@ test "pointer arithmetic affects the alignment" {
296296 var ptr: [*]align(8) u32 = undefined;
297297 var x: usize = 1;
298298
299 expect(@typeInfo(@TypeOf(ptr)).Pointer.alignment == 8);
299 try expect(@typeInfo(@TypeOf(ptr)).Pointer.alignment == 8);
300300 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);
302302 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);
304304 const ptr3 = ptr + 0; // no-op
305 expect(@typeInfo(@TypeOf(ptr3)).Pointer.alignment == 8);
305 try expect(@typeInfo(@TypeOf(ptr3)).Pointer.alignment == 8);
306306 const ptr4 = ptr + x; // runtime-known addend
307 expect(@typeInfo(@TypeOf(ptr4)).Pointer.alignment == 4);
307 try expect(@typeInfo(@TypeOf(ptr4)).Pointer.alignment == 4);
308308 }
309309 {
310310 var ptr: [*]align(8) [3]u8 = undefined;
311311 var x: usize = 1;
312312
313313 const ptr1 = ptr + 17; // 3 * 17 = 51
314 expect(@typeInfo(@TypeOf(ptr1)).Pointer.alignment == 1);
314 try expect(@typeInfo(@TypeOf(ptr1)).Pointer.alignment == 1);
315315 const ptr2 = ptr + x; // runtime-known addend
316 expect(@typeInfo(@TypeOf(ptr2)).Pointer.alignment == 1);
316 try expect(@typeInfo(@TypeOf(ptr2)).Pointer.alignment == 1);
317317 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);
319319 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);
321321 }
322322}
323323
......@@ -325,15 +325,15 @@ test "@ptrToInt on null optional at comptime" {
325325 {
326326 const pointer = @intToPtr(?*u8, 0x000);
327327 const x = @ptrToInt(pointer);
328 comptime expect(0 == @ptrToInt(pointer));
328 comptime try expect(0 == @ptrToInt(pointer));
329329 }
330330 {
331331 const pointer = @intToPtr(?*u8, 0xf00);
332 comptime expect(0xf00 == @ptrToInt(pointer));
332 comptime try expect(0xf00 == @ptrToInt(pointer));
333333 }
334334}
335335
336336test "indexing array with sentinel returns correct type" {
337337 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])));
339339}
test/stage1/behavior/popcount.zig+12-12
......@@ -1,43 +1,43 @@
11const expect = @import("std").testing.expect;
22
33test "@popCount" {
4 comptime testPopCount();
5 testPopCount();
4 comptime try testPopCount();
5 try testPopCount();
66}
77
8fn testPopCount() void {
8fn testPopCount() !void {
99 {
1010 var x: u32 = 0xffffffff;
11 expect(@popCount(u32, x) == 32);
11 try expect(@popCount(u32, x) == 32);
1212 }
1313 {
1414 var x: u5 = 0x1f;
15 expect(@popCount(u5, x) == 5);
15 try expect(@popCount(u5, x) == 5);
1616 }
1717 {
1818 var x: u32 = 0xaa;
19 expect(@popCount(u32, x) == 4);
19 try expect(@popCount(u32, x) == 4);
2020 }
2121 {
2222 var x: u32 = 0xaaaaaaaa;
23 expect(@popCount(u32, x) == 16);
23 try expect(@popCount(u32, x) == 16);
2424 }
2525 {
2626 var x: u32 = 0xaaaaaaaa;
27 expect(@popCount(u32, x) == 16);
27 try expect(@popCount(u32, x) == 16);
2828 }
2929 {
3030 var x: i16 = -1;
31 expect(@popCount(i16, x) == 16);
31 try expect(@popCount(i16, x) == 16);
3232 }
3333 {
3434 var x: i8 = -120;
35 expect(@popCount(i8, x) == 2);
35 try expect(@popCount(i8, x) == 2);
3636 }
3737 comptime {
38 expect(@popCount(u8, @bitCast(u8, @as(i8, -120))) == 2);
38 try expect(@popCount(u8, @bitCast(u8, @as(i8, -120))) == 2);
3939 }
4040 comptime {
41 expect(@popCount(i128, 0b11111111000110001100010000100001000011000011100101010001) == 24);
41 try expect(@popCount(i128, 0b11111111000110001100010000100001000011000011100101010001) == 24);
4242 }
4343}
test/stage1/behavior/ptrcast.zig+12-12
......@@ -3,25 +3,25 @@ const builtin = std.builtin;
33const expect = std.testing.expect;
44
55test "reinterpret bytes as integer with nonzero offset" {
6 testReinterpretBytesAsInteger();
7 comptime testReinterpretBytesAsInteger();
6 try testReinterpretBytesAsInteger();
7 comptime try testReinterpretBytesAsInteger();
88}
99
10fn testReinterpretBytesAsInteger() void {
10fn testReinterpretBytesAsInteger() !void {
1111 const bytes = "\x12\x34\x56\x78\xab";
1212 const expected = switch (builtin.endian) {
1313 builtin.Endian.Little => 0xab785634,
1414 builtin.Endian.Big => 0x345678ab,
1515 };
16 expect(@ptrCast(*align(1) const u32, bytes[1..5]).* == expected);
16 try expect(@ptrCast(*align(1) const u32, bytes[1..5]).* == expected);
1717}
1818
1919test "reinterpret bytes of an array into an extern struct" {
20 testReinterpretBytesAsExternStruct();
21 comptime testReinterpretBytesAsExternStruct();
20 try testReinterpretBytesAsExternStruct();
21 comptime try testReinterpretBytesAsExternStruct();
2222}
2323
24fn testReinterpretBytesAsExternStruct() void {
24fn testReinterpretBytesAsExternStruct() !void {
2525 var bytes align(2) = [_]u8{ 1, 2, 3, 4, 5, 6 };
2626
2727 const S = extern struct {
......@@ -32,15 +32,15 @@ fn testReinterpretBytesAsExternStruct() void {
3232
3333 var ptr = @ptrCast(*const S, &bytes);
3434 var val = ptr.c;
35 expect(val == 5);
35 try expect(val == 5);
3636}
3737
3838test "reinterpret struct field at comptime" {
3939 const numNative = comptime Bytes.init(0x12345678);
4040 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));
4242 } 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));
4444 }
4545}
4646
......@@ -59,7 +59,7 @@ test "comptime ptrcast keeps larger alignment" {
5959 comptime {
6060 const a: u32 = 1234;
6161 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);
6363 }
6464}
6565
......@@ -68,5 +68,5 @@ test "implicit optional pointer to optional c_void pointer" {
6868 var x: ?[*]u8 = &buf;
6969 var y: ?*c_void = x;
7070 var z = @ptrCast(*[4]u8, y);
71 expect(std.mem.eql(u8, z, "aoeu"));
71 try expect(std.mem.eql(u8, z, "aoeu"));
7272}
test/stage1/behavior/pub_enum.zig+4-4
......@@ -2,12 +2,12 @@ const other = @import("pub_enum/other.zig");
22const expect = @import("std").testing.expect;
33
44test "pub enum" {
5 pubEnumTest(other.APubEnum.Two);
5 try pubEnumTest(other.APubEnum.Two);
66}
7fn pubEnumTest(foo: other.APubEnum) void {
8 expect(foo == other.APubEnum.Two);
7fn pubEnumTest(foo: other.APubEnum) !void {
8 try expect(foo == other.APubEnum.Two);
99}
1010
1111test "cast with imported symbol" {
12 expect(@as(other.size_t, 42) == 42);
12 try expect(@as(other.size_t, 42) == 42);
1313}
test/stage1/behavior/ref_var_in_if_after_if_2nd_switch_prong.zig+10-10
......@@ -3,12 +3,12 @@ const mem = @import("std").mem;
33
44var ok: bool = false;
55test "reference a variable in an if after an if in the 2nd switch prong" {
6 foo(true, Num.Two, false, "aoeu");
7 expect(!ok);
8 foo(false, Num.One, false, "aoeu");
9 expect(!ok);
10 foo(true, Num.One, false, "aoeu");
11 expect(ok);
6 try foo(true, Num.Two, false, "aoeu");
7 try expect(!ok);
8 try foo(false, Num.One, false, "aoeu");
9 try expect(!ok);
10 try foo(true, Num.One, false, "aoeu");
11 try expect(ok);
1212}
1313
1414const Num = enum {
......@@ -16,7 +16,7 @@ const Num = enum {
1616 Two,
1717};
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 {
2020 switch (k) {
2121 Num.Two => {},
2222 Num.One => {
......@@ -25,13 +25,13 @@ fn foo(c: bool, k: Num, c2: bool, b: []const u8) void {
2525
2626 if (c2) {}
2727
28 a(output_path);
28 try a(output_path);
2929 }
3030 },
3131 }
3232}
3333
34fn a(x: []const u8) void {
35 expect(mem.eql(u8, x, "aoeu"));
34fn a(x: []const u8) !void {
35 try expect(mem.eql(u8, x, "aoeu"));
3636 ok = true;
3737}
test/stage1/behavior/reflection.zig+17-17
......@@ -5,12 +5,12 @@ const reflection = @This();
55test "reflection: function return type, var args, and param types" {
66 comptime {
77 const info = @typeInfo(@TypeOf(dummy)).Fn;
8 expect(info.return_type.? == i32);
9 expect(!info.is_var_args);
10 expect(info.args.len == 3);
11 expect(info.args[0].arg_type.? == bool);
12 expect(info.args[1].arg_type.? == i32);
13 expect(info.args[2].arg_type.? == f32);
8 try expect(info.return_type.? == i32);
9 try expect(!info.is_var_args);
10 try expect(info.args.len == 3);
11 try expect(info.args[0].arg_type.? == bool);
12 try expect(info.args[1].arg_type.? == i32);
13 try expect(info.args[2].arg_type.? == f32);
1414 }
1515}
1616
......@@ -25,18 +25,18 @@ test "reflection: @field" {
2525 .three = void{},
2626 };
2727
28 expect(f.one == f.one);
29 expect(@field(f, "o" ++ "ne") == f.one);
30 expect(@field(f, "t" ++ "wo") == f.two);
31 expect(@field(f, "th" ++ "ree") == f.three);
32 expect(@field(Foo, "const" ++ "ant") == Foo.constant);
33 expect(@field(Bar, "O" ++ "ne") == Bar.One);
34 expect(@field(Bar, "T" ++ "wo") == Bar.Two);
35 expect(@field(Bar, "Th" ++ "ree") == Bar.Three);
36 expect(@field(Bar, "F" ++ "our") == Bar.Four);
37 expect(@field(reflection, "dum" ++ "my")(true, 1, 2) == dummy(true, 1, 2));
28 try expect(f.one == f.one);
29 try expect(@field(f, "o" ++ "ne") == f.one);
30 try expect(@field(f, "t" ++ "wo") == f.two);
31 try expect(@field(f, "th" ++ "ree") == f.three);
32 try expect(@field(Foo, "const" ++ "ant") == Foo.constant);
33 try expect(@field(Bar, "O" ++ "ne") == Bar.One);
34 try expect(@field(Bar, "T" ++ "wo") == Bar.Two);
35 try expect(@field(Bar, "Th" ++ "ree") == Bar.Three);
36 try expect(@field(Bar, "F" ++ "our") == Bar.Four);
37 try expect(@field(reflection, "dum" ++ "my")(true, 1, 2) == dummy(true, 1, 2));
3838 @field(f, "o" ++ "ne") = 4;
39 expect(f.one == 4);
39 try expect(f.one == 4);
4040}
4141
4242const Foo = struct {
test/stage1/behavior/shuffle.zig+10-10
......@@ -9,33 +9,33 @@ test "@shuffle" {
99 if (builtin.os.tag == .wasi) return error.SkipZigTest;
1010
1111 const S = struct {
12 fn doTheTest() void {
12 fn doTheTest() !void {
1313 var v: Vector(4, i32) = [4]i32{ 2147483647, -2, 30, 40 };
1414 var x: Vector(4, i32) = [4]i32{ 1, 2147483647, 3, 4 };
1515 const mask: Vector(4, i32) = [4]i32{ 0, ~@as(i32, 2), 3, ~@as(i32, 3) };
1616 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
1919 // Implicit cast from array (of mask)
2020 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
2323 // Undefined
2424 const mask2: Vector(4, i32) = [4]i32{ 3, 1, 2, 0 };
2525 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
2828 // Upcasting of b
2929 var v2: Vector(2, i32) = [2]i32{ 2147483647, undefined };
3030 const mask3: Vector(4, i32) = [4]i32{ ~@as(i32, 0), 2, ~@as(i32, 0), 3 };
3131 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
3434 // Upcasting of a
3535 var v3: Vector(2, i32) = [2]i32{ 2147483647, -2 };
3636 const mask4: Vector(4, i32) = [4]i32{ 0, ~@as(i32, 2), 1, ~@as(i32, 3) };
3737 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
4040 // bool
4141 // Disabled because of #3317
......@@ -44,7 +44,7 @@ test "@shuffle" {
4444 var v4: Vector(2, bool) = [2]bool{ true, false };
4545 const mask5: Vector(4, i32) = [4]i32{ 0, ~@as(i32, 1), 1, 2 };
4646 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 }));
4848 }
4949
5050 // TODO re-enable when LLVM codegen is fixed
......@@ -54,10 +54,10 @@ test "@shuffle" {
5454 var v4: Vector(2, bool) = [2]bool{ true, false };
5555 const mask5: Vector(4, i32) = [4]i32{ 0, ~@as(i32, 1), 1, 2 };
5656 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 }));
5858 }
5959 }
6060 };
61 S.doTheTest();
62 comptime S.doTheTest();
61 try S.doTheTest();
62 comptime try S.doTheTest();
6363}
test/stage1/behavior/sizeof_and_typeof.zig+75-75
......@@ -5,7 +5,7 @@ const expectEqual = std.testing.expectEqual;
55
66test "@sizeOf and @TypeOf" {
77 const y: @TypeOf(x) = 120;
8 expect(@sizeOf(@TypeOf(y)) == 2);
8 try expect(@sizeOf(@TypeOf(y)) == 2);
99}
1010const x: u16 = 13;
1111const z: @TypeOf(x) = 19;
......@@ -36,27 +36,27 @@ const P = packed struct {
3636
3737test "@byteOffsetOf" {
3838 // Packed structs have fixed memory layout
39 expect(@byteOffsetOf(P, "a") == 0);
40 expect(@byteOffsetOf(P, "b") == 1);
41 expect(@byteOffsetOf(P, "c") == 5);
42 expect(@byteOffsetOf(P, "d") == 6);
43 expect(@byteOffsetOf(P, "e") == 6);
44 expect(@byteOffsetOf(P, "f") == 7);
45 expect(@byteOffsetOf(P, "g") == 9);
46 expect(@byteOffsetOf(P, "h") == 11);
47 expect(@byteOffsetOf(P, "i") == 12);
39 try expect(@byteOffsetOf(P, "a") == 0);
40 try expect(@byteOffsetOf(P, "b") == 1);
41 try expect(@byteOffsetOf(P, "c") == 5);
42 try expect(@byteOffsetOf(P, "d") == 6);
43 try expect(@byteOffsetOf(P, "e") == 6);
44 try expect(@byteOffsetOf(P, "f") == 7);
45 try expect(@byteOffsetOf(P, "g") == 9);
46 try expect(@byteOffsetOf(P, "h") == 11);
47 try expect(@byteOffsetOf(P, "i") == 12);
4848
4949 // Normal struct fields can be moved/padded
5050 var a: A = undefined;
51 expect(@ptrToInt(&a.a) - @ptrToInt(&a) == @byteOffsetOf(A, "a"));
52 expect(@ptrToInt(&a.b) - @ptrToInt(&a) == @byteOffsetOf(A, "b"));
53 expect(@ptrToInt(&a.c) - @ptrToInt(&a) == @byteOffsetOf(A, "c"));
54 expect(@ptrToInt(&a.d) - @ptrToInt(&a) == @byteOffsetOf(A, "d"));
55 expect(@ptrToInt(&a.e) - @ptrToInt(&a) == @byteOffsetOf(A, "e"));
56 expect(@ptrToInt(&a.f) - @ptrToInt(&a) == @byteOffsetOf(A, "f"));
57 expect(@ptrToInt(&a.g) - @ptrToInt(&a) == @byteOffsetOf(A, "g"));
58 expect(@ptrToInt(&a.h) - @ptrToInt(&a) == @byteOffsetOf(A, "h"));
59 expect(@ptrToInt(&a.i) - @ptrToInt(&a) == @byteOffsetOf(A, "i"));
51 try expect(@ptrToInt(&a.a) - @ptrToInt(&a) == @byteOffsetOf(A, "a"));
52 try expect(@ptrToInt(&a.b) - @ptrToInt(&a) == @byteOffsetOf(A, "b"));
53 try expect(@ptrToInt(&a.c) - @ptrToInt(&a) == @byteOffsetOf(A, "c"));
54 try expect(@ptrToInt(&a.d) - @ptrToInt(&a) == @byteOffsetOf(A, "d"));
55 try expect(@ptrToInt(&a.e) - @ptrToInt(&a) == @byteOffsetOf(A, "e"));
56 try expect(@ptrToInt(&a.f) - @ptrToInt(&a) == @byteOffsetOf(A, "f"));
57 try expect(@ptrToInt(&a.g) - @ptrToInt(&a) == @byteOffsetOf(A, "g"));
58 try expect(@ptrToInt(&a.h) - @ptrToInt(&a) == @byteOffsetOf(A, "h"));
59 try expect(@ptrToInt(&a.i) - @ptrToInt(&a) == @byteOffsetOf(A, "i"));
6060}
6161
6262test "@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
6565 a: [p3a_len]u8,
6666 b: usize,
6767 };
68 std.testing.expectEqual(0, @byteOffsetOf(P3, "a"));
69 std.testing.expectEqual(p3a_len, @byteOffsetOf(P3, "b"));
68 try std.testing.expectEqual(0, @byteOffsetOf(P3, "a"));
69 try std.testing.expectEqual(p3a_len, @byteOffsetOf(P3, "b"));
7070
7171 const p5a_len = 5;
7272 const P5 = packed struct {
7373 a: [p5a_len]u8,
7474 b: usize,
7575 };
76 std.testing.expectEqual(0, @byteOffsetOf(P5, "a"));
77 std.testing.expectEqual(p5a_len, @byteOffsetOf(P5, "b"));
76 try std.testing.expectEqual(0, @byteOffsetOf(P5, "a"));
77 try std.testing.expectEqual(p5a_len, @byteOffsetOf(P5, "b"));
7878
7979 const p6a_len = 6;
8080 const P6 = packed struct {
8181 a: [p6a_len]u8,
8282 b: usize,
8383 };
84 std.testing.expectEqual(0, @byteOffsetOf(P6, "a"));
85 std.testing.expectEqual(p6a_len, @byteOffsetOf(P6, "b"));
84 try std.testing.expectEqual(0, @byteOffsetOf(P6, "a"));
85 try std.testing.expectEqual(p6a_len, @byteOffsetOf(P6, "b"));
8686
8787 const p7a_len = 7;
8888 const P7 = packed struct {
8989 a: [p7a_len]u8,
9090 b: usize,
9191 };
92 std.testing.expectEqual(0, @byteOffsetOf(P7, "a"));
93 std.testing.expectEqual(p7a_len, @byteOffsetOf(P7, "b"));
92 try std.testing.expectEqual(0, @byteOffsetOf(P7, "a"));
93 try std.testing.expectEqual(p7a_len, @byteOffsetOf(P7, "b"));
9494
9595 const p9a_len = 9;
9696 const P9 = packed struct {
9797 a: [p9a_len]u8,
9898 b: usize,
9999 };
100 std.testing.expectEqual(0, @byteOffsetOf(P9, "a"));
101 std.testing.expectEqual(p9a_len, @byteOffsetOf(P9, "b"));
100 try std.testing.expectEqual(0, @byteOffsetOf(P9, "a"));
101 try std.testing.expectEqual(p9a_len, @byteOffsetOf(P9, "b"));
102102
103103 // 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 25 etc. are further cases
104104}
105105
106106test "@bitOffsetOf" {
107107 // Packed structs have fixed memory layout
108 expect(@bitOffsetOf(P, "a") == 0);
109 expect(@bitOffsetOf(P, "b") == 8);
110 expect(@bitOffsetOf(P, "c") == 40);
111 expect(@bitOffsetOf(P, "d") == 48);
112 expect(@bitOffsetOf(P, "e") == 51);
113 expect(@bitOffsetOf(P, "f") == 56);
114 expect(@bitOffsetOf(P, "g") == 72);
108 try expect(@bitOffsetOf(P, "a") == 0);
109 try expect(@bitOffsetOf(P, "b") == 8);
110 try expect(@bitOffsetOf(P, "c") == 40);
111 try expect(@bitOffsetOf(P, "d") == 48);
112 try expect(@bitOffsetOf(P, "e") == 51);
113 try expect(@bitOffsetOf(P, "f") == 56);
114 try expect(@bitOffsetOf(P, "g") == 72);
115115
116 expect(@byteOffsetOf(A, "a") * 8 == @bitOffsetOf(A, "a"));
117 expect(@byteOffsetOf(A, "b") * 8 == @bitOffsetOf(A, "b"));
118 expect(@byteOffsetOf(A, "c") * 8 == @bitOffsetOf(A, "c"));
119 expect(@byteOffsetOf(A, "d") * 8 == @bitOffsetOf(A, "d"));
120 expect(@byteOffsetOf(A, "e") * 8 == @bitOffsetOf(A, "e"));
121 expect(@byteOffsetOf(A, "f") * 8 == @bitOffsetOf(A, "f"));
122 expect(@byteOffsetOf(A, "g") * 8 == @bitOffsetOf(A, "g"));
116 try expect(@byteOffsetOf(A, "a") * 8 == @bitOffsetOf(A, "a"));
117 try expect(@byteOffsetOf(A, "b") * 8 == @bitOffsetOf(A, "b"));
118 try expect(@byteOffsetOf(A, "c") * 8 == @bitOffsetOf(A, "c"));
119 try expect(@byteOffsetOf(A, "d") * 8 == @bitOffsetOf(A, "d"));
120 try expect(@byteOffsetOf(A, "e") * 8 == @bitOffsetOf(A, "e"));
121 try expect(@byteOffsetOf(A, "f") * 8 == @bitOffsetOf(A, "f"));
122 try expect(@byteOffsetOf(A, "g") * 8 == @bitOffsetOf(A, "g"));
123123}
124124
125125test "@sizeOf on compile-time types" {
126 expect(@sizeOf(comptime_int) == 0);
127 expect(@sizeOf(comptime_float) == 0);
128 expect(@sizeOf(@TypeOf(.hi)) == 0);
129 expect(@sizeOf(@TypeOf(type)) == 0);
126 try expect(@sizeOf(comptime_int) == 0);
127 try expect(@sizeOf(comptime_float) == 0);
128 try expect(@sizeOf(@TypeOf(.hi)) == 0);
129 try expect(@sizeOf(@TypeOf(type)) == 0);
130130}
131131
132132test "@sizeOf(T) == 0 doesn't force resolving struct size" {
......@@ -140,8 +140,8 @@ test "@sizeOf(T) == 0 doesn't force resolving struct size" {
140140 };
141141 };
142142
143 expect(@sizeOf(S.Foo) == 4);
144 expect(@sizeOf(S.Bar) == 8);
143 try expect(@sizeOf(S.Foo) == 4);
144 try expect(@sizeOf(S.Bar) == 8);
145145}
146146
147147test "@TypeOf() has no runtime side effects" {
......@@ -153,8 +153,8 @@ test "@TypeOf() has no runtime side effects" {
153153 };
154154 var data: i32 = 0;
155155 const T = @TypeOf(S.foo(i32, &data));
156 comptime expect(T == i32);
157 expect(data == 0);
156 comptime try expect(T == i32);
157 try expect(data == 0);
158158}
159159
160160test "@TypeOf() with multiple arguments" {
......@@ -162,21 +162,21 @@ test "@TypeOf() with multiple arguments" {
162162 var var_1: u32 = undefined;
163163 var var_2: u8 = undefined;
164164 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);
166166 }
167167 {
168168 var var_1: f16 = undefined;
169169 var var_2: f32 = undefined;
170170 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);
172172 }
173173 {
174174 var var_1: u16 = undefined;
175 comptime expect(@TypeOf(var_1, 0xffff) == u16);
175 comptime try expect(@TypeOf(var_1, 0xffff) == u16);
176176 }
177177 {
178178 var var_1: f32 = undefined;
179 comptime expect(@TypeOf(var_1, 3.1415) == f32);
179 comptime try expect(@TypeOf(var_1, 3.1415) == f32);
180180 }
181181}
182182
......@@ -189,8 +189,8 @@ test "branching logic inside @TypeOf" {
189189 }
190190 };
191191 const T = @TypeOf(S.foo() catch undefined);
192 comptime expect(T == i32);
193 expect(S.data == 0);
192 comptime try expect(T == i32);
193 try expect(S.data == 0);
194194}
195195
196196fn fn1(alpha: bool) void {
......@@ -203,12 +203,12 @@ test "lazy @sizeOf result is checked for definedness" {
203203}
204204
205205test "@bitSizeOf" {
206 expect(@bitSizeOf(u2) == 2);
207 expect(@bitSizeOf(u8) == @sizeOf(u8) * 8);
208 expect(@bitSizeOf(struct {
206 try expect(@bitSizeOf(u2) == 2);
207 try expect(@bitSizeOf(u8) == @sizeOf(u8) * 8);
208 try expect(@bitSizeOf(struct {
209209 a: u2,
210210 }) == 8);
211 expect(@bitSizeOf(packed struct {
211 try expect(@bitSizeOf(packed struct {
212212 a: u2,
213213 }) == 2);
214214}
......@@ -241,24 +241,24 @@ test "@sizeOf comparison against zero" {
241241 f2: H(***@This()),
242242 };
243243 const S = struct {
244 fn doTheTest(comptime T: type, comptime result: bool) void {
245 expectEqual(result, @sizeOf(T) > 0);
244 fn doTheTest(comptime T: type, comptime result: bool) !void {
245 try expectEqual(result, @sizeOf(T) > 0);
246246 }
247247 };
248248 // Zero-sized type
249 S.doTheTest(u0, false);
250 S.doTheTest(*u0, false);
249 try S.doTheTest(u0, false);
250 try S.doTheTest(*u0, false);
251251 // Non byte-sized type
252 S.doTheTest(u1, true);
253 S.doTheTest(*u1, true);
252 try S.doTheTest(u1, true);
253 try S.doTheTest(*u1, true);
254254 // Regular type
255 S.doTheTest(u8, true);
256 S.doTheTest(*u8, true);
257 S.doTheTest(f32, true);
258 S.doTheTest(*f32, true);
255 try S.doTheTest(u8, true);
256 try S.doTheTest(*u8, true);
257 try S.doTheTest(f32, true);
258 try S.doTheTest(*f32, true);
259259 // Container with ptr pointing to themselves
260 S.doTheTest(S0, true);
261 S.doTheTest(U0, true);
262 S.doTheTest(S1, true);
263 S.doTheTest(U1, true);
260 try S.doTheTest(S0, true);
261 try S.doTheTest(U0, true);
262 try S.doTheTest(S1, true);
263 try S.doTheTest(U1, true);
264264}
test/stage1/behavior/slice.zig+119-119
......@@ -7,11 +7,11 @@ const mem = std.mem;
77const x = @intToPtr([*]i32, 0x1000)[0..0x500];
88const y = x[0x100..];
99test "compile time slice of pointer to hard coded address" {
10 expect(@ptrToInt(x) == 0x1000);
11 expect(x.len == 0x500);
10 try expect(@ptrToInt(x) == 0x1000);
11 try expect(x.len == 0x500);
1212
13 expect(@ptrToInt(y) == 0x1100);
14 expect(y.len == 0x400);
13 try expect(@ptrToInt(y) == 0x1100);
14 try expect(y.len == 0x400);
1515}
1616
1717test "runtime safety lets us slice from len..len" {
......@@ -20,7 +20,7 @@ test "runtime safety lets us slice from len..len" {
2020 2,
2121 3,
2222 };
23 expect(mem.eql(u8, sliceFromLenToLen(an_array[0..], 3, 3), ""));
23 try expect(mem.eql(u8, sliceFromLenToLen(an_array[0..], 3, 3), ""));
2424}
2525
2626fn sliceFromLenToLen(a_slice: []u8, start: usize, end: usize) []u8 {
......@@ -29,18 +29,18 @@ fn sliceFromLenToLen(a_slice: []u8, start: usize, end: usize) []u8 {
2929
3030test "implicitly cast array of size 0 to slice" {
3131 var msg = [_]u8{};
32 assertLenIsZero(&msg);
32 try assertLenIsZero(&msg);
3333}
3434
35fn assertLenIsZero(msg: []const u8) void {
36 expect(msg.len == 0);
35fn assertLenIsZero(msg: []const u8) !void {
36 try expect(msg.len == 0);
3737}
3838
3939test "C pointer" {
4040 var buf: [*c]const u8 = "kjdhfkjdhfdkjhfkfjhdfkjdhfkdjhfdkjhf";
4141 var len: u32 = 10;
4242 var slice = buf[0..len];
43 expectEqualSlices(u8, "kjdhfkjdhf", slice);
43 try expectEqualSlices(u8, "kjdhfkjdhf", slice);
4444}
4545
4646test "C pointer slice access" {
......@@ -48,11 +48,11 @@ test "C pointer slice access" {
4848 const c_ptr = @ptrCast([*c]const u32, &buf);
4949
5050 var runtime_zero: usize = 0;
51 comptime expectEqual([]const u32, @TypeOf(c_ptr[runtime_zero..1]));
52 comptime expectEqual(*const [1]u32, @TypeOf(c_ptr[0..1]));
51 comptime try expectEqual([]const u32, @TypeOf(c_ptr[runtime_zero..1]));
52 comptime try expectEqual(*const [1]u32, @TypeOf(c_ptr[0..1]));
5353
5454 for (c_ptr[0..5]) |*cl| {
55 expectEqual(@as(u32, 42), cl.*);
55 try expectEqual(@as(u32, 42), cl.*);
5656 }
5757}
5858
......@@ -65,8 +65,8 @@ fn sliceSum(comptime q: []const u8) i32 {
6565}
6666
6767test "comptime slices are disambiguated" {
68 expect(sliceSum(&[_]u8{ 1, 2 }) == 3);
69 expect(sliceSum(&[_]u8{ 3, 4 }) == 7);
68 try expect(sliceSum(&[_]u8{ 1, 2 }) == 3);
69 try expect(sliceSum(&[_]u8{ 3, 4 }) == 7);
7070}
7171
7272test "slice type with custom alignment" {
......@@ -77,20 +77,20 @@ test "slice type with custom alignment" {
7777 var array: [10]LazilyResolvedType align(32) = undefined;
7878 slice = &array;
7979 slice[1].anything = 42;
80 expect(array[1].anything == 42);
80 try expect(array[1].anything == 42);
8181}
8282
8383test "access len index of sentinel-terminated slice" {
8484 const S = struct {
85 fn doTheTest() void {
85 fn doTheTest() !void {
8686 var slice: [:0]const u8 = "hello";
8787
88 expect(slice.len == 5);
89 expect(slice[5] == 0);
88 try expect(slice.len == 5);
89 try expect(slice[5] == 0);
9090 }
9191 };
92 S.doTheTest();
93 comptime S.doTheTest();
92 try S.doTheTest();
93 comptime try S.doTheTest();
9494}
9595
9696test "obtaining a null terminated slice" {
......@@ -108,230 +108,230 @@ test "obtaining a null terminated slice" {
108108 var runtime_len: usize = 3;
109109 const ptr2 = buf[0..runtime_len :0];
110110 // ptr2 is a null-terminated slice
111 comptime expect(@TypeOf(ptr2) == [:0]u8);
112 comptime expect(@TypeOf(ptr2[0..2]) == *[2]u8);
111 comptime try expect(@TypeOf(ptr2) == [:0]u8);
112 comptime try expect(@TypeOf(ptr2[0..2]) == *[2]u8);
113113 var runtime_zero: usize = 0;
114 comptime expect(@TypeOf(ptr2[runtime_zero..2]) == []u8);
114 comptime try expect(@TypeOf(ptr2[runtime_zero..2]) == []u8);
115115}
116116
117117test "empty array to slice" {
118118 const S = struct {
119 fn doTheTest() void {
119 fn doTheTest() !void {
120120 const empty: []align(16) u8 = &[_]u8{};
121121 const align_1: []align(1) u8 = empty;
122122 const align_4: []align(4) u8 = empty;
123123 const align_16: []align(16) u8 = empty;
124 expectEqual(1, @typeInfo(@TypeOf(align_1)).Pointer.alignment);
125 expectEqual(4, @typeInfo(@TypeOf(align_4)).Pointer.alignment);
126 expectEqual(16, @typeInfo(@TypeOf(align_16)).Pointer.alignment);
124 try expectEqual(1, @typeInfo(@TypeOf(align_1)).Pointer.alignment);
125 try expectEqual(4, @typeInfo(@TypeOf(align_4)).Pointer.alignment);
126 try expectEqual(16, @typeInfo(@TypeOf(align_16)).Pointer.alignment);
127127 }
128128 };
129129
130 S.doTheTest();
131 comptime S.doTheTest();
130 try S.doTheTest();
131 comptime try S.doTheTest();
132132}
133133
134134test "@ptrCast slice to pointer" {
135135 const S = struct {
136 fn doTheTest() void {
136 fn doTheTest() !void {
137137 var array align(@alignOf(u16)) = [5]u8{ 0xff, 0xff, 0xff, 0xff, 0xff };
138138 var slice: []u8 = &array;
139139 var ptr = @ptrCast(*u16, slice);
140 expect(ptr.* == 65535);
140 try expect(ptr.* == 65535);
141141 }
142142 };
143143
144 S.doTheTest();
145 comptime S.doTheTest();
144 try S.doTheTest();
145 comptime try S.doTheTest();
146146}
147147
148148test "slice syntax resulting in pointer-to-array" {
149149 const S = struct {
150 fn doTheTest() void {
151 testArray();
152 testArrayZ();
153 testArray0();
154 testArrayAlign();
155 testPointer();
156 testPointerZ();
157 testPointer0();
158 testPointerAlign();
159 testSlice();
160 testSliceZ();
161 testSlice0();
162 testSliceOpt();
163 testSliceAlign();
150 fn doTheTest() !void {
151 try testArray();
152 try testArrayZ();
153 try testArray0();
154 try testArrayAlign();
155 try testPointer();
156 try testPointerZ();
157 try testPointer0();
158 try testPointerAlign();
159 try testSlice();
160 try testSliceZ();
161 try testSlice0();
162 try testSliceOpt();
163 try testSliceAlign();
164164 }
165165
166 fn testArray() void {
166 fn testArray() !void {
167167 var array = [5]u8{ 1, 2, 3, 4, 5 };
168168 var slice = array[1..3];
169 comptime expect(@TypeOf(slice) == *[2]u8);
170 expect(slice[0] == 2);
171 expect(slice[1] == 3);
169 comptime try expect(@TypeOf(slice) == *[2]u8);
170 try expect(slice[0] == 2);
171 try expect(slice[1] == 3);
172172 }
173173
174 fn testArrayZ() void {
174 fn testArrayZ() !void {
175175 var array = [5:0]u8{ 1, 2, 3, 4, 5 };
176 comptime expect(@TypeOf(array[1..3]) == *[2]u8);
177 comptime expect(@TypeOf(array[1..5]) == *[4:0]u8);
178 comptime expect(@TypeOf(array[1..]) == *[4:0]u8);
179 comptime expect(@TypeOf(array[1..3 :4]) == *[2:4]u8);
176 comptime try expect(@TypeOf(array[1..3]) == *[2]u8);
177 comptime try expect(@TypeOf(array[1..5]) == *[4:0]u8);
178 comptime try expect(@TypeOf(array[1..]) == *[4:0]u8);
179 comptime try expect(@TypeOf(array[1..3 :4]) == *[2:4]u8);
180180 }
181181
182 fn testArray0() void {
182 fn testArray0() !void {
183183 {
184184 var array = [0]u8{};
185185 var slice = array[0..0];
186 comptime expect(@TypeOf(slice) == *[0]u8);
186 comptime try expect(@TypeOf(slice) == *[0]u8);
187187 }
188188 {
189189 var array = [0:0]u8{};
190190 var slice = array[0..0];
191 comptime expect(@TypeOf(slice) == *[0:0]u8);
192 expect(slice[0] == 0);
191 comptime try expect(@TypeOf(slice) == *[0:0]u8);
192 try expect(slice[0] == 0);
193193 }
194194 }
195195
196 fn testArrayAlign() void {
196 fn testArrayAlign() !void {
197197 var array align(4) = [5]u8{ 1, 2, 3, 4, 5 };
198198 var slice = array[4..5];
199 comptime expect(@TypeOf(slice) == *align(4) [1]u8);
200 expect(slice[0] == 5);
201 comptime expect(@TypeOf(array[0..2]) == *align(4) [2]u8);
199 comptime try expect(@TypeOf(slice) == *align(4) [1]u8);
200 try expect(slice[0] == 5);
201 comptime try expect(@TypeOf(array[0..2]) == *align(4) [2]u8);
202202 }
203203
204 fn testPointer() void {
204 fn testPointer() !void {
205205 var array = [5]u8{ 1, 2, 3, 4, 5 };
206206 var pointer: [*]u8 = &array;
207207 var slice = pointer[1..3];
208 comptime expect(@TypeOf(slice) == *[2]u8);
209 expect(slice[0] == 2);
210 expect(slice[1] == 3);
208 comptime try expect(@TypeOf(slice) == *[2]u8);
209 try expect(slice[0] == 2);
210 try expect(slice[1] == 3);
211211 }
212212
213 fn testPointerZ() void {
213 fn testPointerZ() !void {
214214 var array = [5:0]u8{ 1, 2, 3, 4, 5 };
215215 var pointer: [*:0]u8 = &array;
216 comptime expect(@TypeOf(pointer[1..3]) == *[2]u8);
217 comptime expect(@TypeOf(pointer[1..3 :4]) == *[2:4]u8);
216 comptime try expect(@TypeOf(pointer[1..3]) == *[2]u8);
217 comptime try expect(@TypeOf(pointer[1..3 :4]) == *[2:4]u8);
218218 }
219219
220 fn testPointer0() void {
220 fn testPointer0() !void {
221221 var pointer: [*]const u0 = &[1]u0{0};
222222 var slice = pointer[0..1];
223 comptime expect(@TypeOf(slice) == *const [1]u0);
224 expect(slice[0] == 0);
223 comptime try expect(@TypeOf(slice) == *const [1]u0);
224 try expect(slice[0] == 0);
225225 }
226226
227 fn testPointerAlign() void {
227 fn testPointerAlign() !void {
228228 var array align(4) = [5]u8{ 1, 2, 3, 4, 5 };
229229 var pointer: [*]align(4) u8 = &array;
230230 var slice = pointer[4..5];
231 comptime expect(@TypeOf(slice) == *align(4) [1]u8);
232 expect(slice[0] == 5);
233 comptime expect(@TypeOf(pointer[0..2]) == *align(4) [2]u8);
231 comptime try expect(@TypeOf(slice) == *align(4) [1]u8);
232 try expect(slice[0] == 5);
233 comptime try expect(@TypeOf(pointer[0..2]) == *align(4) [2]u8);
234234 }
235235
236 fn testSlice() void {
236 fn testSlice() !void {
237237 var array = [5]u8{ 1, 2, 3, 4, 5 };
238238 var src_slice: []u8 = &array;
239239 var slice = src_slice[1..3];
240 comptime expect(@TypeOf(slice) == *[2]u8);
241 expect(slice[0] == 2);
242 expect(slice[1] == 3);
240 comptime try expect(@TypeOf(slice) == *[2]u8);
241 try expect(slice[0] == 2);
242 try expect(slice[1] == 3);
243243 }
244244
245 fn testSliceZ() void {
245 fn testSliceZ() !void {
246246 var array = [5:0]u8{ 1, 2, 3, 4, 5 };
247247 var slice: [:0]u8 = &array;
248 comptime expect(@TypeOf(slice[1..3]) == *[2]u8);
249 comptime expect(@TypeOf(slice[1..]) == [:0]u8);
250 comptime expect(@TypeOf(slice[1..3 :4]) == *[2:4]u8);
248 comptime try expect(@TypeOf(slice[1..3]) == *[2]u8);
249 comptime try expect(@TypeOf(slice[1..]) == [:0]u8);
250 comptime try expect(@TypeOf(slice[1..3 :4]) == *[2:4]u8);
251251 }
252252
253 fn testSliceOpt() void {
253 fn testSliceOpt() !void {
254254 var array: [2]u8 = [2]u8{ 1, 2 };
255255 var slice: ?[]u8 = &array;
256 comptime expect(@TypeOf(&array, slice) == ?[]u8);
257 comptime expect(@TypeOf(slice.?[0..2]) == *[2]u8);
256 comptime try expect(@TypeOf(&array, slice) == ?[]u8);
257 comptime try expect(@TypeOf(slice.?[0..2]) == *[2]u8);
258258 }
259259
260 fn testSlice0() void {
260 fn testSlice0() !void {
261261 {
262262 var array = [0]u8{};
263263 var src_slice: []u8 = &array;
264264 var slice = src_slice[0..0];
265 comptime expect(@TypeOf(slice) == *[0]u8);
265 comptime try expect(@TypeOf(slice) == *[0]u8);
266266 }
267267 {
268268 var array = [0:0]u8{};
269269 var src_slice: [:0]u8 = &array;
270270 var slice = src_slice[0..0];
271 comptime expect(@TypeOf(slice) == *[0]u8);
271 comptime try expect(@TypeOf(slice) == *[0]u8);
272272 }
273273 }
274274
275 fn testSliceAlign() void {
275 fn testSliceAlign() !void {
276276 var array align(4) = [5]u8{ 1, 2, 3, 4, 5 };
277277 var src_slice: []align(4) u8 = &array;
278278 var slice = src_slice[4..5];
279 comptime expect(@TypeOf(slice) == *align(4) [1]u8);
280 expect(slice[0] == 5);
281 comptime expect(@TypeOf(src_slice[0..2]) == *align(4) [2]u8);
279 comptime try expect(@TypeOf(slice) == *align(4) [1]u8);
280 try expect(slice[0] == 5);
281 comptime try expect(@TypeOf(src_slice[0..2]) == *align(4) [2]u8);
282282 }
283283
284 fn testConcatStrLiterals() void {
285 expectEqualSlices("a"[0..] ++ "b"[0..], "ab");
286 expectEqualSlices("a"[0..:0] ++ "b"[0..:0], "ab");
284 fn testConcatStrLiterals() !void {
285 try expectEqualSlices("a"[0..] ++ "b"[0..], "ab");
286 try expectEqualSlices("a"[0.. :0] ++ "b"[0.. :0], "ab");
287287 }
288288 };
289289
290 S.doTheTest();
291 comptime S.doTheTest();
290 try S.doTheTest();
291 comptime try S.doTheTest();
292292}
293293
294294test "slice of hardcoded address to pointer" {
295295 const S = struct {
296 fn doTheTest() void {
296 fn doTheTest() !void {
297297 const pointer = @intToPtr([*]u8, 0x04)[0..2];
298 comptime expect(@TypeOf(pointer) == *[2]u8);
298 comptime try expect(@TypeOf(pointer) == *[2]u8);
299299 const slice: []const u8 = pointer;
300 expect(@ptrToInt(slice.ptr) == 4);
301 expect(slice.len == 2);
300 try expect(@ptrToInt(slice.ptr) == 4);
301 try expect(slice.len == 2);
302302 }
303303 };
304304
305 S.doTheTest();
305 try S.doTheTest();
306306}
307307
308308test "type coercion of pointer to anon struct literal to pointer to slice" {
309309 const S = struct {
310 const U = union{
310 const U = union {
311311 a: u32,
312312 b: bool,
313313 c: []const u8,
314314 };
315315
316 fn doTheTest() void {
316 fn doTheTest() !void {
317317 var x1: u8 = 42;
318318 const t1 = &.{ x1, 56, 54 };
319319 var slice1: []const u8 = t1;
320 expect(slice1.len == 3);
321 expect(slice1[0] == 42);
322 expect(slice1[1] == 56);
323 expect(slice1[2] == 54);
324
320 try expect(slice1.len == 3);
321 try expect(slice1[0] == 42);
322 try expect(slice1[1] == 56);
323 try expect(slice1[2] == 54);
324
325325 var x2: []const u8 = "hello";
326326 const t2 = &.{ x2, ", ", "world!" };
327327 // @compileLog(@TypeOf(t2));
328328 var slice2: []const []const u8 = t2;
329 expect(slice2.len == 3);
330 expect(mem.eql(u8, slice2[0], "hello"));
331 expect(mem.eql(u8, slice2[1], ", "));
332 expect(mem.eql(u8, slice2[2], "world!"));
329 try expect(slice2.len == 3);
330 try expect(mem.eql(u8, slice2[0], "hello"));
331 try expect(mem.eql(u8, slice2[1], ", "));
332 try expect(mem.eql(u8, slice2[2], "world!"));
333333 }
334334 };
335 // S.doTheTest();
336 comptime S.doTheTest();
335 // try S.doTheTest();
336 comptime try S.doTheTest();
337337}
test/stage1/behavior/src.zig+8-8
......@@ -2,16 +2,16 @@ const std = @import("std");
22const expect = std.testing.expect;
33
44test "@src" {
5 doTheTest();
5 try doTheTest();
66}
77
8fn doTheTest() void {
8fn doTheTest() !void {
99 const src = @src();
1010
11 expect(src.line == 9);
12 expect(src.column == 17);
13 expect(std.mem.endsWith(u8, src.fn_name, "doTheTest"));
14 expect(std.mem.endsWith(u8, src.file, "src.zig"));
15 expect(src.fn_name[src.fn_name.len] == 0);
16 expect(src.file[src.file.len] == 0);
11 try expect(src.line == 9);
12 try expect(src.column == 17);
13 try expect(std.mem.endsWith(u8, src.fn_name, "doTheTest"));
14 try expect(std.mem.endsWith(u8, src.file, "src.zig"));
15 try expect(src.fn_name[src.fn_name.len] == 0);
16 try expect(src.file[src.file.len] == 0);
1717}
test/stage1/behavior/struct.zig+190-190
......@@ -18,12 +18,12 @@ test "top level fields" {
1818 .top_level_field = 1234,
1919 };
2020 instance.top_level_field += 1;
21 expectEqual(@as(i32, 1235), instance.top_level_field);
21 try expectEqual(@as(i32, 1235), instance.top_level_field);
2222}
2323
2424test "call struct static method" {
2525 const result = StructWithNoFields.add(3, 4);
26 expect(result == 7);
26 try expect(result == 7);
2727}
2828
2929test "return empty struct instance" {
......@@ -36,7 +36,7 @@ fn returnEmptyStructInstance() StructWithNoFields {
3636const should_be_11 = StructWithNoFields.add(5, 6);
3737
3838test "invoke static method in global scope" {
39 expect(should_be_11 == 11);
39 try expect(should_be_11 == 11);
4040}
4141
4242test "void struct fields" {
......@@ -45,8 +45,8 @@ test "void struct fields" {
4545 .b = 1,
4646 .c = void{},
4747 };
48 expect(foo.b == 1);
49 expect(@sizeOf(VoidStructFieldsFoo) == 4);
48 try expect(foo.b == 1);
49 try expect(@sizeOf(VoidStructFieldsFoo) == 4);
5050}
5151const VoidStructFieldsFoo = struct {
5252 a: void,
......@@ -59,17 +59,17 @@ test "structs" {
5959 @memset(@ptrCast([*]u8, &foo), 0, @sizeOf(StructFoo));
6060 foo.a += 1;
6161 foo.b = foo.a == 1;
62 testFoo(foo);
62 try testFoo(foo);
6363 testMutation(&foo);
64 expect(foo.c == 100);
64 try expect(foo.c == 100);
6565}
6666const StructFoo = struct {
6767 a: i32,
6868 b: bool,
6969 c: f32,
7070};
71fn testFoo(foo: StructFoo) void {
72 expect(foo.b);
71fn testFoo(foo: StructFoo) !void {
72 try expect(foo.b);
7373}
7474fn testMutation(foo: *StructFoo) void {
7575 foo.c = 100;
......@@ -94,7 +94,7 @@ test "struct point to self" {
9494
9595 root.next = &node;
9696
97 expect(node.next.next.next.val.x == 1);
97 try expect(node.next.next.next.val.x == 1);
9898}
9999
100100test "struct byval assign" {
......@@ -103,14 +103,14 @@ test "struct byval assign" {
103103
104104 foo1.a = 1234;
105105 foo2.a = 0;
106 expect(foo2.a == 0);
106 try expect(foo2.a == 0);
107107 foo2 = foo1;
108 expect(foo2.a == 1234);
108 try expect(foo2.a == 1234);
109109}
110110
111111fn structInitializer() void {
112112 const val = Val{ .x = 42 };
113 expect(val.x == 42);
113 try expect(val.x == 42);
114114}
115115
116116test "fn call of struct field" {
......@@ -127,14 +127,14 @@ test "fn call of struct field" {
127127 }
128128 };
129129
130 expect(S.callStructField(Foo{ .ptr = S.aFunc }) == 13);
130 try expect(S.callStructField(Foo{ .ptr = S.aFunc }) == 13);
131131}
132132
133133test "store member function in variable" {
134134 const instance = MemberFnTestFoo{ .x = 1234 };
135135 const memberFn = MemberFnTestFoo.member;
136136 const result = memberFn(instance);
137 expect(result == 1234);
137 try expect(result == 1234);
138138}
139139const MemberFnTestFoo = struct {
140140 x: i32,
......@@ -146,12 +146,12 @@ const MemberFnTestFoo = struct {
146146test "call member function directly" {
147147 const instance = MemberFnTestFoo{ .x = 1234 };
148148 const result = MemberFnTestFoo.member(instance);
149 expect(result == 1234);
149 try expect(result == 1234);
150150}
151151
152152test "member functions" {
153153 const r = MemberFnRand{ .seed = 1234 };
154 expect(r.getSeed() == 1234);
154 try expect(r.getSeed() == 1234);
155155}
156156const MemberFnRand = struct {
157157 seed: u32,
......@@ -162,7 +162,7 @@ const MemberFnRand = struct {
162162
163163test "return struct byval from function" {
164164 const bar = makeBar(1234, 5678);
165 expect(bar.y == 5678);
165 try expect(bar.y == 5678);
166166}
167167const Bar = struct {
168168 x: i32,
......@@ -177,7 +177,7 @@ fn makeBar(x: i32, y: i32) Bar {
177177
178178test "empty struct method call" {
179179 const es = EmptyStruct{};
180 expect(es.method() == 1234);
180 try expect(es.method() == 1234);
181181}
182182const EmptyStruct = struct {
183183 fn method(es: *const EmptyStruct) i32 {
......@@ -194,7 +194,7 @@ fn testReturnEmptyStructFromFn() EmptyStruct2 {
194194}
195195
196196test "pass slice of empty struct to fn" {
197 expect(testPassSliceOfEmptyStructToFn(&[_]EmptyStruct2{EmptyStruct2{}}) == 1);
197 try expect(testPassSliceOfEmptyStructToFn(&[_]EmptyStruct2{EmptyStruct2{}}) == 1);
198198}
199199fn testPassSliceOfEmptyStructToFn(slice: []const EmptyStruct2) usize {
200200 return slice.len;
......@@ -212,7 +212,7 @@ test "packed struct" {
212212 };
213213 foo.y += 1;
214214 const four = foo.x + foo.y;
215 expect(four == 4);
215 try expect(four == 4);
216216}
217217
218218const BitField1 = packed struct {
......@@ -229,17 +229,17 @@ const bit_field_1 = BitField1{
229229
230230test "bit field access" {
231231 var data = bit_field_1;
232 expect(getA(&data) == 1);
233 expect(getB(&data) == 2);
234 expect(getC(&data) == 3);
235 comptime expect(@sizeOf(BitField1) == 1);
232 try expect(getA(&data) == 1);
233 try expect(getB(&data) == 2);
234 try expect(getC(&data) == 3);
235 comptime try expect(@sizeOf(BitField1) == 1);
236236
237237 data.b += 1;
238 expect(data.b == 3);
238 try expect(data.b == 3);
239239
240240 data.a += 1;
241 expect(data.a == 2);
242 expect(data.b == 3);
241 try expect(data.a == 2);
242 try expect(data.b == 3);
243243}
244244
245245fn getA(data: *const BitField1) u3 {
......@@ -266,11 +266,11 @@ const Foo96Bits = packed struct {
266266
267267test "packed struct 24bits" {
268268 comptime {
269 expect(@sizeOf(Foo24Bits) == 4);
269 try expect(@sizeOf(Foo24Bits) == 4);
270270 if (@sizeOf(usize) == 4) {
271 expect(@sizeOf(Foo96Bits) == 12);
271 try expect(@sizeOf(Foo96Bits) == 12);
272272 } else {
273 expect(@sizeOf(Foo96Bits) == 16);
273 try expect(@sizeOf(Foo96Bits) == 16);
274274 }
275275 }
276276
......@@ -281,28 +281,28 @@ test "packed struct 24bits" {
281281 .d = 0,
282282 };
283283 value.a += 1;
284 expect(value.a == 1);
285 expect(value.b == 0);
286 expect(value.c == 0);
287 expect(value.d == 0);
284 try expect(value.a == 1);
285 try expect(value.b == 0);
286 try expect(value.c == 0);
287 try expect(value.d == 0);
288288
289289 value.b += 1;
290 expect(value.a == 1);
291 expect(value.b == 1);
292 expect(value.c == 0);
293 expect(value.d == 0);
290 try expect(value.a == 1);
291 try expect(value.b == 1);
292 try expect(value.c == 0);
293 try expect(value.d == 0);
294294
295295 value.c += 1;
296 expect(value.a == 1);
297 expect(value.b == 1);
298 expect(value.c == 1);
299 expect(value.d == 0);
296 try expect(value.a == 1);
297 try expect(value.b == 1);
298 try expect(value.c == 1);
299 try expect(value.d == 0);
300300
301301 value.d += 1;
302 expect(value.a == 1);
303 expect(value.b == 1);
304 expect(value.c == 1);
305 expect(value.d == 1);
302 try expect(value.a == 1);
303 try expect(value.b == 1);
304 try expect(value.c == 1);
305 try expect(value.d == 1);
306306}
307307
308308const Foo32Bits = packed struct {
......@@ -319,43 +319,43 @@ const FooArray24Bits = packed struct {
319319// TODO revisit this test when doing https://github.com/ziglang/zig/issues/1512
320320test "packed array 24bits" {
321321 comptime {
322 expect(@sizeOf([9]Foo32Bits) == 9 * 4);
323 expect(@sizeOf(FooArray24Bits) == 2 + 2 * 4 + 2);
322 try expect(@sizeOf([9]Foo32Bits) == 9 * 4);
323 try expect(@sizeOf(FooArray24Bits) == 2 + 2 * 4 + 2);
324324 }
325325
326326 var bytes = [_]u8{0} ** (@sizeOf(FooArray24Bits) + 1);
327327 bytes[bytes.len - 1] = 0xaa;
328328 const ptr = &std.mem.bytesAsSlice(FooArray24Bits, bytes[0 .. bytes.len - 1])[0];
329 expect(ptr.a == 0);
330 expect(ptr.b[0].field == 0);
331 expect(ptr.b[1].field == 0);
332 expect(ptr.c == 0);
329 try expect(ptr.a == 0);
330 try expect(ptr.b[0].field == 0);
331 try expect(ptr.b[1].field == 0);
332 try expect(ptr.c == 0);
333333
334334 ptr.a = maxInt(u16);
335 expect(ptr.a == maxInt(u16));
336 expect(ptr.b[0].field == 0);
337 expect(ptr.b[1].field == 0);
338 expect(ptr.c == 0);
335 try expect(ptr.a == maxInt(u16));
336 try expect(ptr.b[0].field == 0);
337 try expect(ptr.b[1].field == 0);
338 try expect(ptr.c == 0);
339339
340340 ptr.b[0].field = maxInt(u24);
341 expect(ptr.a == maxInt(u16));
342 expect(ptr.b[0].field == maxInt(u24));
343 expect(ptr.b[1].field == 0);
344 expect(ptr.c == 0);
341 try expect(ptr.a == maxInt(u16));
342 try expect(ptr.b[0].field == maxInt(u24));
343 try expect(ptr.b[1].field == 0);
344 try expect(ptr.c == 0);
345345
346346 ptr.b[1].field = maxInt(u24);
347 expect(ptr.a == maxInt(u16));
348 expect(ptr.b[0].field == maxInt(u24));
349 expect(ptr.b[1].field == maxInt(u24));
350 expect(ptr.c == 0);
347 try expect(ptr.a == maxInt(u16));
348 try expect(ptr.b[0].field == maxInt(u24));
349 try expect(ptr.b[1].field == maxInt(u24));
350 try expect(ptr.c == 0);
351351
352352 ptr.c = maxInt(u16);
353 expect(ptr.a == maxInt(u16));
354 expect(ptr.b[0].field == maxInt(u24));
355 expect(ptr.b[1].field == maxInt(u24));
356 expect(ptr.c == maxInt(u16));
353 try expect(ptr.a == maxInt(u16));
354 try expect(ptr.b[0].field == maxInt(u24));
355 try expect(ptr.b[1].field == maxInt(u24));
356 try expect(ptr.c == maxInt(u16));
357357
358 expect(bytes[bytes.len - 1] == 0xaa);
358 try expect(bytes[bytes.len - 1] == 0xaa);
359359}
360360
361361const FooStructAligned = packed struct {
......@@ -369,17 +369,17 @@ const FooArrayOfAligned = packed struct {
369369
370370test "aligned array of packed struct" {
371371 comptime {
372 expect(@sizeOf(FooStructAligned) == 2);
373 expect(@sizeOf(FooArrayOfAligned) == 2 * 2);
372 try expect(@sizeOf(FooStructAligned) == 2);
373 try expect(@sizeOf(FooArrayOfAligned) == 2 * 2);
374374 }
375375
376376 var bytes = [_]u8{0xbb} ** @sizeOf(FooArrayOfAligned);
377377 const ptr = &std.mem.bytesAsSlice(FooArrayOfAligned, bytes[0..])[0];
378378
379 expect(ptr.a[0].a == 0xbb);
380 expect(ptr.a[0].b == 0xbb);
381 expect(ptr.a[1].a == 0xbb);
382 expect(ptr.a[1].b == 0xbb);
379 try expect(ptr.a[0].a == 0xbb);
380 try expect(ptr.a[0].b == 0xbb);
381 try expect(ptr.a[1].a == 0xbb);
382 try expect(ptr.a[1].b == 0xbb);
383383}
384384
385385test "runtime struct initialization of bitfield" {
......@@ -392,10 +392,10 @@ test "runtime struct initialization of bitfield" {
392392 .y = @intCast(u4, x2),
393393 };
394394
395 expect(s1.x == x1);
396 expect(s1.y == x1);
397 expect(s2.x == @intCast(u4, x2));
398 expect(s2.y == @intCast(u4, x2));
395 try expect(s1.x == x1);
396 try expect(s1.y == x1);
397 try expect(s2.x == @intCast(u4, x2));
398 try expect(s2.y == @intCast(u4, x2));
399399}
400400
401401var x1 = @as(u4, 1);
......@@ -425,18 +425,18 @@ test "native bit field understands endianness" {
425425 @memcpy(&bytes, @ptrCast([*]u8, &all), 8);
426426 var bitfields = @ptrCast(*Bitfields, &bytes).*;
427427
428 expect(bitfields.f1 == 0x1111);
429 expect(bitfields.f2 == 0x2222);
430 expect(bitfields.f3 == 0x33);
431 expect(bitfields.f4 == 0x44);
432 expect(bitfields.f5 == 0x5);
433 expect(bitfields.f6 == 0x6);
434 expect(bitfields.f7 == 0x77);
428 try expect(bitfields.f1 == 0x1111);
429 try expect(bitfields.f2 == 0x2222);
430 try expect(bitfields.f3 == 0x33);
431 try expect(bitfields.f4 == 0x44);
432 try expect(bitfields.f5 == 0x5);
433 try expect(bitfields.f6 == 0x6);
434 try expect(bitfields.f7 == 0x77);
435435}
436436
437437test "align 1 field before self referential align 8 field as slice return type" {
438438 const result = alloc(Expr);
439 expect(result.len == 0);
439 try expect(result.len == 0);
440440}
441441
442442const Expr = union(enum) {
......@@ -459,10 +459,10 @@ test "call method with mutable reference to struct with no fields" {
459459 };
460460
461461 var s = S{};
462 expect(S.doC(&s));
463 expect(s.doC());
464 expect(S.do(&s));
465 expect(s.do());
462 try expect(S.doC(&s));
463 try expect(s.doC());
464 try expect(S.do(&s));
465 try expect(s.do());
466466}
467467
468468test "implicit cast packed struct field to const ptr" {
......@@ -478,7 +478,7 @@ test "implicit cast packed struct field to const ptr" {
478478 var lup: LevelUpMove = undefined;
479479 lup.level = 12;
480480 const res = LevelUpMove.toInt(lup.level);
481 expect(res == 12);
481 try expect(res == 12);
482482}
483483
484484test "pointer to packed struct member in a stack variable" {
......@@ -489,9 +489,9 @@ test "pointer to packed struct member in a stack variable" {
489489
490490 var s = S{ .a = 2, .b = 0 };
491491 var b_ptr = &s.b;
492 expect(s.b == 0);
492 try expect(s.b == 0);
493493 b_ptr.* = 2;
494 expect(s.b == 2);
494 try expect(s.b == 2);
495495}
496496
497497test "non-byte-aligned array inside packed struct" {
......@@ -500,20 +500,20 @@ test "non-byte-aligned array inside packed struct" {
500500 b: [0x16]u8,
501501 };
502502 const S = struct {
503 fn bar(slice: []const u8) void {
504 expectEqualSlices(u8, slice, "abcdefghijklmnopqurstu");
503 fn bar(slice: []const u8) !void {
504 try expectEqualSlices(u8, slice, "abcdefghijklmnopqurstu");
505505 }
506 fn doTheTest() void {
506 fn doTheTest() !void {
507507 var foo = Foo{
508508 .a = true,
509509 .b = "abcdefghijklmnopqurstu".*,
510510 };
511511 const value = foo.b;
512 bar(&value);
512 try bar(&value);
513513 }
514514 };
515 S.doTheTest();
516 comptime S.doTheTest();
515 try S.doTheTest();
516 comptime try S.doTheTest();
517517}
518518
519519test "packed struct with u0 field access" {
......@@ -521,7 +521,7 @@ test "packed struct with u0 field access" {
521521 f0: u0,
522522 };
523523 var s = S{ .f0 = 0 };
524 comptime expect(s.f0 == 0);
524 comptime try expect(s.f0 == 0);
525525}
526526
527527const S0 = struct {
......@@ -540,7 +540,7 @@ var g_foo: S0 = S0.init();
540540
541541test "access to global struct fields" {
542542 g_foo.bar.value = 42;
543 expect(g_foo.bar.value == 42);
543 try expect(g_foo.bar.value == 42);
544544}
545545
546546test "packed struct with fp fields" {
......@@ -559,9 +559,9 @@ test "packed struct with fp fields" {
559559 s.data[1] = 2.0;
560560 s.data[2] = 3.0;
561561 s.frob();
562 expectEqual(@as(f32, 6.0), s.data[0]);
563 expectEqual(@as(f32, 11.0), s.data[1]);
564 expectEqual(@as(f32, 20.0), s.data[2]);
562 try expectEqual(@as(f32, 6.0), s.data[0]);
563 try expectEqual(@as(f32, 11.0), s.data[1]);
564 try expectEqual(@as(f32, 20.0), s.data[2]);
565565}
566566
567567test "use within struct scope" {
......@@ -572,7 +572,7 @@ test "use within struct scope" {
572572 }
573573 };
574574 };
575 expectEqual(@as(i32, 42), S.inner());
575 try expectEqual(@as(i32, 42), S.inner());
576576}
577577
578578test "default struct initialization fields" {
......@@ -590,14 +590,14 @@ test "default struct initialization fields" {
590590 const y = S{
591591 .b = five,
592592 };
593 expectEqual(1239, x.a + x.b);
593 try expectEqual(1239, x.a + x.b);
594594}
595595
596596test "fn with C calling convention returns struct by value" {
597597 const S = struct {
598 fn entry() void {
598 fn entry() !void {
599599 var x = makeBar(10);
600 expectEqual(@as(i32, 10), x.handle);
600 try expectEqual(@as(i32, 10), x.handle);
601601 }
602602
603603 const ExternBar = extern struct {
......@@ -610,8 +610,8 @@ test "fn with C calling convention returns struct by value" {
610610 };
611611 }
612612 };
613 S.entry();
614 comptime S.entry();
613 try S.entry();
614 comptime try S.entry();
615615}
616616
617617test "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" {
632632 }
633633 };
634634
635 fn doTheTest() void {
635 fn doTheTest() !void {
636636 var objects: ArrayList = undefined;
637637
638638 for (objects.toSlice()) |obj| {
......@@ -641,10 +641,10 @@ test "for loop over pointers to struct, getting field from struct pointer" {
641641 }
642642 }
643643
644 expect(ok);
644 try expect(ok);
645645 }
646646 };
647 S.doTheTest();
647 try S.doTheTest();
648648}
649649
650650test "zero-bit field in packed struct" {
......@@ -657,20 +657,20 @@ test "zero-bit field in packed struct" {
657657
658658test "struct field init with catch" {
659659 const S = struct {
660 fn doTheTest() void {
660 fn doTheTest() !void {
661661 var x: anyerror!isize = 1;
662662 var req = Foo{
663663 .field = x catch undefined,
664664 };
665 expect(req.field == 1);
665 try expect(req.field == 1);
666666 }
667667
668668 pub const Foo = extern struct {
669669 field: isize,
670670 };
671671 };
672 S.doTheTest();
673 comptime S.doTheTest();
672 try S.doTheTest();
673 comptime try S.doTheTest();
674674}
675675
676676test "packed struct with non-ABI-aligned field" {
......@@ -681,8 +681,8 @@ test "packed struct with non-ABI-aligned field" {
681681 var s: S = undefined;
682682 s.x = 1;
683683 s.y = 42;
684 expect(s.x == 1);
685 expect(s.y == 42);
684 try expect(s.x == 1);
685 try expect(s.y == 42);
686686}
687687
688688test "non-packed struct with u128 entry in union" {
......@@ -698,10 +698,10 @@ test "non-packed struct with u128 entry in union" {
698698
699699 var sx: S = undefined;
700700 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"));
702702 var v2 = U{ .Num = 123 };
703703 s.f2 = v2;
704 std.testing.expect(s.f2.Num == 123);
704 try std.testing.expect(s.f2.Num == 123);
705705}
706706
707707test "packed struct field passed to generic function" {
......@@ -721,7 +721,7 @@ test "packed struct field passed to generic function" {
721721 var p: S.P = undefined;
722722 p.b = 29;
723723 var loaded = S.genericReadPackedField(&p.b);
724 expect(loaded == 29);
724 try expect(loaded == 29);
725725}
726726
727727test "anonymous struct literal syntax" {
......@@ -731,63 +731,63 @@ test "anonymous struct literal syntax" {
731731 y: i32,
732732 };
733733
734 fn doTheTest() void {
734 fn doTheTest() !void {
735735 var p: Point = .{
736736 .x = 1,
737737 .y = 2,
738738 };
739 expect(p.x == 1);
740 expect(p.y == 2);
739 try expect(p.x == 1);
740 try expect(p.y == 2);
741741 }
742742 };
743 S.doTheTest();
744 comptime S.doTheTest();
743 try S.doTheTest();
744 comptime try S.doTheTest();
745745}
746746
747747test "fully anonymous struct" {
748748 const S = struct {
749 fn doTheTest() void {
750 dump(.{
749 fn doTheTest() !void {
750 try dump(.{
751751 .int = @as(u32, 1234),
752752 .float = @as(f64, 12.34),
753753 .b = true,
754754 .s = "hi",
755755 });
756756 }
757 fn dump(args: anytype) void {
758 expect(args.int == 1234);
759 expect(args.float == 12.34);
760 expect(args.b);
761 expect(args.s[0] == 'h');
762 expect(args.s[1] == 'i');
757 fn dump(args: anytype) !void {
758 try expect(args.int == 1234);
759 try expect(args.float == 12.34);
760 try expect(args.b);
761 try expect(args.s[0] == 'h');
762 try expect(args.s[1] == 'i');
763763 }
764764 };
765 S.doTheTest();
766 comptime S.doTheTest();
765 try S.doTheTest();
766 comptime try S.doTheTest();
767767}
768768
769769test "fully anonymous list literal" {
770770 const S = struct {
771 fn doTheTest() void {
772 dump(.{ @as(u32, 1234), @as(f64, 12.34), true, "hi" });
771 fn doTheTest() !void {
772 try dump(.{ @as(u32, 1234), @as(f64, 12.34), true, "hi" });
773773 }
774 fn dump(args: anytype) void {
775 expect(args.@"0" == 1234);
776 expect(args.@"1" == 12.34);
777 expect(args.@"2");
778 expect(args.@"3"[0] == 'h');
779 expect(args.@"3"[1] == 'i');
774 fn dump(args: anytype) !void {
775 try expect(args.@"0" == 1234);
776 try expect(args.@"1" == 12.34);
777 try expect(args.@"2");
778 try expect(args.@"3"[0] == 'h');
779 try expect(args.@"3"[1] == 'i');
780780 }
781781 };
782 S.doTheTest();
783 comptime S.doTheTest();
782 try S.doTheTest();
783 comptime try S.doTheTest();
784784}
785785
786786test "anonymous struct literal assigned to variable" {
787787 var vec = .{ @as(i32, 22), @as(i32, 55), @as(i32, 99) };
788 expect(vec.@"0" == 22);
789 expect(vec.@"1" == 55);
790 expect(vec.@"2" == 99);
788 try expect(vec.@"0" == 22);
789 try expect(vec.@"1" == 55);
790 try expect(vec.@"2" == 99);
791791}
792792
793793test "struct with var field" {
......@@ -799,8 +799,8 @@ test "struct with var field" {
799799 .x = 1,
800800 .y = 2,
801801 };
802 expect(pt.x == 1);
803 expect(pt.y == 2);
802 try expect(pt.x == 1);
803 try expect(pt.y == 2);
804804}
805805
806806test "comptime struct field" {
......@@ -810,21 +810,21 @@ test "comptime struct field" {
810810 };
811811
812812 var foo: T = undefined;
813 comptime expect(foo.b == 1234);
813 comptime try expect(foo.b == 1234);
814814}
815815
816816test "anon struct literal field value initialized with fn call" {
817817 const S = struct {
818 fn doTheTest() void {
818 fn doTheTest() !void {
819819 var x = .{foo()};
820 expectEqualSlices(u8, x[0], "hi");
820 try expectEqualSlices(u8, x[0], "hi");
821821 }
822822 fn foo() []const u8 {
823823 return "hi";
824824 }
825825 };
826 S.doTheTest();
827 comptime S.doTheTest();
826 try S.doTheTest();
827 comptime try S.doTheTest();
828828}
829829
830830test "self-referencing struct via array member" {
......@@ -833,7 +833,7 @@ test "self-referencing struct via array member" {
833833 };
834834 var x: T = undefined;
835835 x = T{ .children = .{&x} };
836 expect(x.children[0] == &x);
836 try expect(x.children[0] == &x);
837837}
838838
839839test "struct with union field" {
......@@ -848,8 +848,8 @@ test "struct with union field" {
848848 var True = Value{
849849 .kind = .{ .Bool = true },
850850 };
851 expectEqual(@as(u32, 2), True.ref);
852 expectEqual(true, True.kind.Bool);
851 try expectEqual(@as(u32, 2), True.ref);
852 try expectEqual(true, True.kind.Bool);
853853}
854854
855855test "type coercion of anon struct literal to struct" {
......@@ -865,24 +865,24 @@ test "type coercion of anon struct literal to struct" {
865865 field: i32 = 1234,
866866 };
867867
868 fn doTheTest() void {
868 fn doTheTest() !void {
869869 var y: u32 = 42;
870870 const t0 = .{ .A = 123, .B = "foo", .C = {} };
871871 const t1 = .{ .A = y, .B = "foo", .C = {} };
872872 const y0: S2 = t0;
873873 var y1: S2 = t1;
874 expect(y0.A == 123);
875 expect(std.mem.eql(u8, y0.B, "foo"));
876 expect(y0.C == {});
877 expect(y0.D.field == 1234);
878 expect(y1.A == y);
879 expect(std.mem.eql(u8, y1.B, "foo"));
880 expect(y1.C == {});
881 expect(y1.D.field == 1234);
874 try expect(y0.A == 123);
875 try expect(std.mem.eql(u8, y0.B, "foo"));
876 try expect(y0.C == {});
877 try expect(y0.D.field == 1234);
878 try expect(y1.A == y);
879 try expect(std.mem.eql(u8, y1.B, "foo"));
880 try expect(y1.C == {});
881 try expect(y1.D.field == 1234);
882882 }
883883 };
884 S.doTheTest();
885 comptime S.doTheTest();
884 try S.doTheTest();
885 comptime try S.doTheTest();
886886}
887887
888888test "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" {
898898 field: i32 = 1234,
899899 };
900900
901 fn doTheTest() void {
901 fn doTheTest() !void {
902902 var y: u32 = 42;
903903 const t0 = &.{ .A = 123, .B = "foo", .C = {} };
904904 const t1 = &.{ .A = y, .B = "foo", .C = {} };
905905 const y0: *const S2 = t0;
906906 var y1: *const S2 = t1;
907 expect(y0.A == 123);
908 expect(std.mem.eql(u8, y0.B, "foo"));
909 expect(y0.C == {});
910 expect(y0.D.field == 1234);
911 expect(y1.A == y);
912 expect(std.mem.eql(u8, y1.B, "foo"));
913 expect(y1.C == {});
914 expect(y1.D.field == 1234);
907 try expect(y0.A == 123);
908 try expect(std.mem.eql(u8, y0.B, "foo"));
909 try expect(y0.C == {});
910 try expect(y0.D.field == 1234);
911 try expect(y1.A == y);
912 try expect(std.mem.eql(u8, y1.B, "foo"));
913 try expect(y1.C == {});
914 try expect(y1.D.field == 1234);
915915 }
916916 };
917 S.doTheTest();
918 comptime S.doTheTest();
917 try S.doTheTest();
918 comptime try S.doTheTest();
919919}
920920
921921test "packed struct with undefined initializers" {
......@@ -929,16 +929,16 @@ test "packed struct with undefined initializers" {
929929 _c: u3 = undefined,
930930 };
931931
932 fn doTheTest() void {
932 fn doTheTest() !void {
933933 var p: P = undefined;
934934 p = P{ .a = 2, .b = 4, .c = 6 };
935935 // Make sure the compiler doesn't touch the unprefixed fields.
936 expectEqual(@as(u3, 2), p.a);
937 expectEqual(@as(u3, 4), p.b);
938 expectEqual(@as(u3, 6), p.c);
936 try expectEqual(@as(u3, 2), p.a);
937 try expectEqual(@as(u3, 4), p.b);
938 try expectEqual(@as(u3, 6), p.c);
939939 }
940940 };
941941
942 S.doTheTest();
943 comptime S.doTheTest();
942 try S.doTheTest();
943 comptime try S.doTheTest();
944944}
test/stage1/behavior/struct_contains_null_ptr_itself.zig+1-1
......@@ -3,7 +3,7 @@ const expect = std.testing.expect;
33
44test "struct contains null pointer which contains original struct" {
55 var x: ?*NodeLineComment = null;
6 expect(x == null);
6 try expect(x == null);
77}
88
99pub const Node = struct {
test/stage1/behavior/struct_contains_slice_of_itself.zig+12-12
......@@ -39,12 +39,12 @@ test "struct contains slice of itself" {
3939 .payload = 1234,
4040 .children = nodes[0..],
4141 };
42 expect(root.payload == 1234);
43 expect(root.children[0].payload == 1);
44 expect(root.children[1].payload == 2);
45 expect(root.children[2].payload == 3);
46 expect(root.children[2].children[0].payload == 31);
47 expect(root.children[2].children[1].payload == 32);
42 try expect(root.payload == 1234);
43 try expect(root.children[0].payload == 1);
44 try expect(root.children[1].payload == 2);
45 try expect(root.children[2].payload == 3);
46 try expect(root.children[2].children[0].payload == 31);
47 try expect(root.children[2].children[1].payload == 32);
4848}
4949
5050test "struct contains aligned slice of itself" {
......@@ -76,10 +76,10 @@ test "struct contains aligned slice of itself" {
7676 .payload = 1234,
7777 .children = nodes[0..],
7878 };
79 expect(root.payload == 1234);
80 expect(root.children[0].payload == 1);
81 expect(root.children[1].payload == 2);
82 expect(root.children[2].payload == 3);
83 expect(root.children[2].children[0].payload == 31);
84 expect(root.children[2].children[1].payload == 32);
79 try expect(root.payload == 1234);
80 try expect(root.children[0].payload == 1);
81 try expect(root.children[1].payload == 2);
82 try expect(root.children[2].payload == 3);
83 try expect(root.children[2].children[0].payload == 31);
84 try expect(root.children[2].children[1].payload == 32);
8585}
test/stage1/behavior/switch.zig+104-104
......@@ -4,23 +4,23 @@ const expectError = std.testing.expectError;
44const expectEqual = std.testing.expectEqual;
55
66test "switch with numbers" {
7 testSwitchWithNumbers(13);
7 try testSwitchWithNumbers(13);
88}
99
10fn testSwitchWithNumbers(x: u32) void {
10fn testSwitchWithNumbers(x: u32) !void {
1111 const result = switch (x) {
1212 1, 2, 3, 4...8 => false,
1313 13 => true,
1414 else => false,
1515 };
16 expect(result);
16 try expect(result);
1717}
1818
1919test "switch with all ranges" {
20 expect(testSwitchWithAllRanges(50, 3) == 1);
21 expect(testSwitchWithAllRanges(101, 0) == 2);
22 expect(testSwitchWithAllRanges(300, 5) == 3);
23 expect(testSwitchWithAllRanges(301, 6) == 6);
20 try expect(testSwitchWithAllRanges(50, 3) == 1);
21 try expect(testSwitchWithAllRanges(101, 0) == 2);
22 try expect(testSwitchWithAllRanges(300, 5) == 3);
23 try expect(testSwitchWithAllRanges(301, 6) == 6);
2424}
2525
2626fn testSwitchWithAllRanges(x: u32, y: u32) u32 {
......@@ -43,7 +43,7 @@ test "implicit comptime switch" {
4343 };
4444
4545 comptime {
46 expect(result + 1 == 14);
46 try expect(result + 1 == 14);
4747 }
4848}
4949
......@@ -65,16 +65,16 @@ fn nonConstSwitchOnEnum(fruit: Fruit) void {
6565}
6666
6767test "switch statement" {
68 nonConstSwitch(SwitchStatmentFoo.C);
68 try nonConstSwitch(SwitchStatmentFoo.C);
6969}
70fn nonConstSwitch(foo: SwitchStatmentFoo) void {
70fn nonConstSwitch(foo: SwitchStatmentFoo) !void {
7171 const val = switch (foo) {
7272 SwitchStatmentFoo.A => @as(i32, 1),
7373 SwitchStatmentFoo.B => 2,
7474 SwitchStatmentFoo.C => 3,
7575 SwitchStatmentFoo.D => 4,
7676 };
77 expect(val == 3);
77 try expect(val == 3);
7878}
7979const SwitchStatmentFoo = enum {
8080 A,
......@@ -84,22 +84,22 @@ const SwitchStatmentFoo = enum {
8484};
8585
8686test "switch prong with variable" {
87 switchProngWithVarFn(SwitchProngWithVarEnum{ .One = 13 });
88 switchProngWithVarFn(SwitchProngWithVarEnum{ .Two = 13.0 });
89 switchProngWithVarFn(SwitchProngWithVarEnum{ .Meh = {} });
87 try switchProngWithVarFn(SwitchProngWithVarEnum{ .One = 13 });
88 try switchProngWithVarFn(SwitchProngWithVarEnum{ .Two = 13.0 });
89 try switchProngWithVarFn(SwitchProngWithVarEnum{ .Meh = {} });
9090}
9191const SwitchProngWithVarEnum = union(enum) {
9292 One: i32,
9393 Two: f32,
9494 Meh: void,
9595};
96fn switchProngWithVarFn(a: SwitchProngWithVarEnum) void {
96fn switchProngWithVarFn(a: SwitchProngWithVarEnum) !void {
9797 switch (a) {
9898 SwitchProngWithVarEnum.One => |x| {
99 expect(x == 13);
99 try expect(x == 13);
100100 },
101101 SwitchProngWithVarEnum.Two => |x| {
102 expect(x == 13.0);
102 try expect(x == 13.0);
103103 },
104104 SwitchProngWithVarEnum.Meh => |x| {
105105 const v: void = x;
......@@ -108,18 +108,18 @@ fn switchProngWithVarFn(a: SwitchProngWithVarEnum) void {
108108}
109109
110110test "switch on enum using pointer capture" {
111 testSwitchEnumPtrCapture();
112 comptime testSwitchEnumPtrCapture();
111 try testSwitchEnumPtrCapture();
112 comptime try testSwitchEnumPtrCapture();
113113}
114114
115fn testSwitchEnumPtrCapture() void {
115fn testSwitchEnumPtrCapture() !void {
116116 var value = SwitchProngWithVarEnum{ .One = 1234 };
117117 switch (value) {
118118 SwitchProngWithVarEnum.One => |*x| x.* += 1,
119119 else => unreachable,
120120 }
121121 switch (value) {
122 SwitchProngWithVarEnum.One => |x| expect(x == 1235),
122 SwitchProngWithVarEnum.One => |x| try expect(x == 1235),
123123 else => unreachable,
124124 }
125125}
......@@ -130,7 +130,7 @@ test "switch with multiple expressions" {
130130 4, 5, 6 => 2,
131131 else => @as(i32, 3),
132132 };
133 expect(x == 2);
133 try expect(x == 2);
134134}
135135fn returnsFive() i32 {
136136 return 5;
......@@ -152,12 +152,12 @@ fn returnsFalse() bool {
152152 }
153153}
154154test "switch on const enum with var" {
155 expect(!returnsFalse());
155 try expect(!returnsFalse());
156156}
157157
158158test "switch on type" {
159 expect(trueIfBoolFalseOtherwise(bool));
160 expect(!trueIfBoolFalseOtherwise(i32));
159 try expect(trueIfBoolFalseOtherwise(bool));
160 try expect(!trueIfBoolFalseOtherwise(i32));
161161}
162162
163163fn trueIfBoolFalseOtherwise(comptime T: type) bool {
......@@ -168,21 +168,21 @@ fn trueIfBoolFalseOtherwise(comptime T: type) bool {
168168}
169169
170170test "switch handles all cases of number" {
171 testSwitchHandleAllCases();
172 comptime testSwitchHandleAllCases();
171 try testSwitchHandleAllCases();
172 comptime try testSwitchHandleAllCases();
173173}
174174
175fn testSwitchHandleAllCases() void {
176 expect(testSwitchHandleAllCasesExhaustive(0) == 3);
177 expect(testSwitchHandleAllCasesExhaustive(1) == 2);
178 expect(testSwitchHandleAllCasesExhaustive(2) == 1);
179 expect(testSwitchHandleAllCasesExhaustive(3) == 0);
175fn testSwitchHandleAllCases() !void {
176 try expect(testSwitchHandleAllCasesExhaustive(0) == 3);
177 try expect(testSwitchHandleAllCasesExhaustive(1) == 2);
178 try expect(testSwitchHandleAllCasesExhaustive(2) == 1);
179 try expect(testSwitchHandleAllCasesExhaustive(3) == 0);
180180
181 expect(testSwitchHandleAllCasesRange(100) == 0);
182 expect(testSwitchHandleAllCasesRange(200) == 1);
183 expect(testSwitchHandleAllCasesRange(201) == 2);
184 expect(testSwitchHandleAllCasesRange(202) == 4);
185 expect(testSwitchHandleAllCasesRange(230) == 3);
181 try expect(testSwitchHandleAllCasesRange(100) == 0);
182 try expect(testSwitchHandleAllCasesRange(200) == 1);
183 try expect(testSwitchHandleAllCasesRange(201) == 2);
184 try expect(testSwitchHandleAllCasesRange(202) == 4);
185 try expect(testSwitchHandleAllCasesRange(230) == 3);
186186}
187187
188188fn testSwitchHandleAllCasesExhaustive(x: u2) u2 {
......@@ -205,13 +205,13 @@ fn testSwitchHandleAllCasesRange(x: u8) u8 {
205205}
206206
207207test "switch all prongs unreachable" {
208 testAllProngsUnreachable();
209 comptime testAllProngsUnreachable();
208 try testAllProngsUnreachable();
209 comptime try testAllProngsUnreachable();
210210}
211211
212fn testAllProngsUnreachable() void {
213 expect(switchWithUnreachable(1) == 2);
214 expect(switchWithUnreachable(2) == 10);
212fn testAllProngsUnreachable() !void {
213 try expect(switchWithUnreachable(1) == 2);
214 try expect(switchWithUnreachable(2) == 10);
215215}
216216
217217fn switchWithUnreachable(x: i32) i32 {
......@@ -233,23 +233,23 @@ test "capture value of switch with all unreachable prongs" {
233233 const x = return_a_number() catch |err| switch (err) {
234234 else => unreachable,
235235 };
236 expect(x == 1);
236 try expect(x == 1);
237237}
238238
239239test "switching on booleans" {
240 testSwitchOnBools();
241 comptime testSwitchOnBools();
240 try testSwitchOnBools();
241 comptime try testSwitchOnBools();
242242}
243243
244fn testSwitchOnBools() void {
245 expect(testSwitchOnBoolsTrueAndFalse(true) == false);
246 expect(testSwitchOnBoolsTrueAndFalse(false) == true);
244fn testSwitchOnBools() !void {
245 try expect(testSwitchOnBoolsTrueAndFalse(true) == false);
246 try expect(testSwitchOnBoolsTrueAndFalse(false) == true);
247247
248 expect(testSwitchOnBoolsTrueWithElse(true) == false);
249 expect(testSwitchOnBoolsTrueWithElse(false) == true);
248 try expect(testSwitchOnBoolsTrueWithElse(true) == false);
249 try expect(testSwitchOnBoolsTrueWithElse(false) == true);
250250
251 expect(testSwitchOnBoolsFalseWithElse(true) == false);
252 expect(testSwitchOnBoolsFalseWithElse(false) == true);
251 try expect(testSwitchOnBoolsFalseWithElse(true) == false);
252 try expect(testSwitchOnBoolsFalseWithElse(false) == true);
253253}
254254
255255fn testSwitchOnBoolsTrueAndFalse(x: bool) bool {
......@@ -276,14 +276,14 @@ fn testSwitchOnBoolsFalseWithElse(x: bool) bool {
276276test "u0" {
277277 var val: u0 = 0;
278278 switch (val) {
279 0 => expect(val == 0),
279 0 => try expect(val == 0),
280280 }
281281}
282282
283283test "undefined.u0" {
284284 var val: u0 = undefined;
285285 switch (val) {
286 0 => expect(val == 0),
286 0 => try expect(val == 0),
287287 }
288288}
289289
......@@ -295,15 +295,15 @@ test "anon enum literal used in switch on union enum" {
295295 var foo = Foo{ .a = 1234 };
296296 switch (foo) {
297297 .a => |x| {
298 expect(x == 1234);
298 try expect(x == 1234);
299299 },
300300 }
301301}
302302
303303test "else prong of switch on error set excludes other cases" {
304304 const S = struct {
305 fn doTheTest() void {
306 expectError(error.C, bar());
305 fn doTheTest() !void {
306 try expectError(error.C, bar());
307307 }
308308 const E = error{
309309 A,
......@@ -326,14 +326,14 @@ test "else prong of switch on error set excludes other cases" {
326326 };
327327 }
328328 };
329 S.doTheTest();
330 comptime S.doTheTest();
329 try S.doTheTest();
330 comptime try S.doTheTest();
331331}
332332
333333test "switch prongs with error set cases make a new error set type for capture value" {
334334 const S = struct {
335 fn doTheTest() void {
336 expectError(error.B, bar());
335 fn doTheTest() !void {
336 try expectError(error.B, bar());
337337 }
338338 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
358358 };
359359 }
360360 };
361 S.doTheTest();
362 comptime S.doTheTest();
361 try S.doTheTest();
362 comptime try S.doTheTest();
363363}
364364
365365test "return result loc and then switch with range implicit casted to error union" {
366366 const S = struct {
367 fn doTheTest() void {
368 expect((func(0xb) catch unreachable) == 0xb);
367 fn doTheTest() !void {
368 try expect((func(0xb) catch unreachable) == 0xb);
369369 }
370370 fn func(d: u8) anyerror!u8 {
371371 return switch (d) {
......@@ -374,13 +374,13 @@ test "return result loc and then switch with range implicit casted to error unio
374374 };
375375 }
376376 };
377 S.doTheTest();
378 comptime S.doTheTest();
377 try S.doTheTest();
378 comptime try S.doTheTest();
379379}
380380
381381test "switch with null and T peer types and inferred result location type" {
382382 const S = struct {
383 fn doTheTest(c: u8) void {
383 fn doTheTest(c: u8) !void {
384384 if (switch (c) {
385385 0 => true,
386386 else => null,
......@@ -389,8 +389,8 @@ test "switch with null and T peer types and inferred result location type" {
389389 }
390390 }
391391 };
392 S.doTheTest(1);
393 comptime S.doTheTest(1);
392 try S.doTheTest(1);
393 comptime try S.doTheTest(1);
394394}
395395
396396test "switch prongs with cases with identical payload types" {
......@@ -400,31 +400,31 @@ test "switch prongs with cases with identical payload types" {
400400 C: usize,
401401 };
402402 const S = struct {
403 fn doTheTest() void {
404 doTheSwitch1(Union{ .A = 8 });
405 doTheSwitch2(Union{ .B = -8 });
403 fn doTheTest() !void {
404 try doTheSwitch1(Union{ .A = 8 });
405 try doTheSwitch2(Union{ .B = -8 });
406406 }
407 fn doTheSwitch1(u: Union) void {
407 fn doTheSwitch1(u: Union) !void {
408408 switch (u) {
409409 .A, .C => |e| {
410 expect(@TypeOf(e) == usize);
411 expect(e == 8);
410 try expect(@TypeOf(e) == usize);
411 try expect(e == 8);
412412 },
413413 .B => |e| @panic("fail"),
414414 }
415415 }
416 fn doTheSwitch2(u: Union) void {
416 fn doTheSwitch2(u: Union) !void {
417417 switch (u) {
418418 .A, .C => |e| @panic("fail"),
419419 .B => |e| {
420 expect(@TypeOf(e) == isize);
421 expect(e == -8);
420 try expect(@TypeOf(e) == isize);
421 try expect(e == -8);
422422 },
423423 }
424424 }
425425 };
426 S.doTheTest();
427 comptime S.doTheTest();
426 try S.doTheTest();
427 comptime try S.doTheTest();
428428}
429429
430430test "switch with disjoint range" {
......@@ -438,19 +438,19 @@ test "switch with disjoint range" {
438438
439439test "switch variable for range and multiple prongs" {
440440 const S = struct {
441 fn doTheTest() void {
441 fn doTheTest() !void {
442442 var u: u8 = 16;
443 doTheSwitch(u);
444 comptime doTheSwitch(u);
443 try doTheSwitch(u);
444 comptime try doTheSwitch(u);
445445 var v: u8 = 42;
446 doTheSwitch(v);
447 comptime doTheSwitch(v);
446 try doTheSwitch(v);
447 comptime try doTheSwitch(v);
448448 }
449 fn doTheSwitch(q: u8) void {
449 fn doTheSwitch(q: u8) !void {
450450 switch (q) {
451 0...40 => |x| expect(x == 16),
452 41, 42, 43 => |x| expect(x == 42),
453 else => expect(false),
451 0...40 => |x| try expect(x == 16),
452 41, 42, 43 => |x| try expect(x == 42),
453 else => try expect(false),
454454 }
455455 }
456456 };
......@@ -493,31 +493,31 @@ test "switch on pointer type" {
493493 }
494494 };
495495
496 expect(1 == S.doTheTest(S.P1));
497 expect(2 == S.doTheTest(S.P2));
498 expect(3 == S.doTheTest(S.P3));
499 comptime expect(1 == S.doTheTest(S.P1));
500 comptime expect(2 == S.doTheTest(S.P2));
501 comptime expect(3 == S.doTheTest(S.P3));
496 try expect(1 == S.doTheTest(S.P1));
497 try expect(2 == S.doTheTest(S.P2));
498 try expect(3 == S.doTheTest(S.P3));
499 comptime try expect(1 == S.doTheTest(S.P1));
500 comptime try expect(2 == S.doTheTest(S.P2));
501 comptime try expect(3 == S.doTheTest(S.P3));
502502}
503503
504504test "switch on error set with single else" {
505505 const S = struct {
506 fn doTheTest() void {
506 fn doTheTest() !void {
507507 var some: error{Foo} = error.Foo;
508 expect(switch (some) {
508 try expect(switch (some) {
509509 else => |a| true,
510510 });
511511 }
512512 };
513513
514 S.doTheTest();
515 comptime S.doTheTest();
514 try S.doTheTest();
515 comptime try S.doTheTest();
516516}
517517
518518test "while copies its payload" {
519519 const S = struct {
520 fn doTheTest() void {
520 fn doTheTest() !void {
521521 var tmp: union(enum) {
522522 A: u8,
523523 B: u32,
......@@ -526,12 +526,12 @@ test "while copies its payload" {
526526 .A => |value| {
527527 // Modify the original union
528528 tmp = .{ .B = 0x10101010 };
529 expectEqual(@as(u8, 42), value);
529 try expectEqual(@as(u8, 42), value);
530530 },
531531 else => unreachable,
532532 }
533533 }
534534 };
535 S.doTheTest();
536 comptime S.doTheTest();
535 try S.doTheTest();
536 comptime try S.doTheTest();
537537}
test/stage1/behavior/switch_prong_err_enum.zig+2-2
......@@ -22,9 +22,9 @@ fn doThing(form_id: u64) anyerror!FormValue {
2222test "switch prong returns error enum" {
2323 switch (doThing(17) catch unreachable) {
2424 FormValue.Address => |payload| {
25 expect(payload == 1);
25 try expect(payload == 1);
2626 },
2727 else => unreachable,
2828 }
29 expect(read_count == 1);
29 try expect(read_count == 1);
3030}
test/stage1/behavior/switch_prong_implicit_cast.zig+1-1
......@@ -18,5 +18,5 @@ test "switch prong implicit cast" {
1818 FormValue.One => false,
1919 FormValue.Two => |x| x,
2020 };
21 expect(result);
21 try expect(result);
2222}
test/stage1/behavior/this.zig+3-3
......@@ -20,7 +20,7 @@ fn add(x: i32, y: i32) i32 {
2020}
2121
2222test "this refer to module call private fn" {
23 expect(module.add(1, 2) == 3);
23 try expect(module.add(1, 2) == 3);
2424}
2525
2626test "this refer to container" {
......@@ -29,6 +29,6 @@ test "this refer to container" {
2929 .y = 34,
3030 };
3131 pt.addOne();
32 expect(pt.x == 13);
33 expect(pt.y == 35);
32 try expect(pt.x == 13);
33 try expect(pt.y == 35);
3434}
test/stage1/behavior/translate_c_macros.zig+4-4
......@@ -4,7 +4,7 @@ const expectEqual = @import("std").testing.expectEqual;
44const h = @cImport(@cInclude("stage1/behavior/translate_c_macros.h"));
55
66test "initializer list expression" {
7 expectEqual(h.Color{
7 try expectEqual(h.Color{
88 .r = 200,
99 .g = 200,
1010 .b = 200,
......@@ -13,10 +13,10 @@ test "initializer list expression" {
1313}
1414
1515test "sizeof in macros" {
16 expectEqual(@as(c_int, @sizeOf(u32)), h.MY_SIZEOF(u32));
17 expectEqual(@as(c_int, @sizeOf(u32)), h.MY_SIZEOF2(u32));
16 try expectEqual(@as(c_int, @sizeOf(u32)), h.MY_SIZEOF(u32));
17 try expectEqual(@as(c_int, @sizeOf(u32)), h.MY_SIZEOF2(u32));
1818}
1919
2020test "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);
2222}
test/stage1/behavior/truncate.zig+6-6
......@@ -4,33 +4,33 @@ const expect = std.testing.expect;
44test "truncate u0 to larger integer allowed and has comptime known result" {
55 var x: u0 = 0;
66 const y = @truncate(u8, x);
7 comptime expect(y == 0);
7 comptime try expect(y == 0);
88}
99
1010test "truncate.u0.literal" {
1111 var z = @truncate(u0, 0);
12 expect(z == 0);
12 try expect(z == 0);
1313}
1414
1515test "truncate.u0.const" {
1616 const c0: usize = 0;
1717 var z = @truncate(u0, c0);
18 expect(z == 0);
18 try expect(z == 0);
1919}
2020
2121test "truncate.u0.var" {
2222 var d: u8 = 2;
2323 var z = @truncate(u0, d);
24 expect(z == 0);
24 try expect(z == 0);
2525}
2626
2727test "truncate sign mismatch but comptime known so it works anyway" {
2828 const x: u32 = 10;
2929 var result = @truncate(i8, x);
30 expect(result == 10);
30 try expect(result == 10);
3131}
3232
3333test "truncate on comptime integer" {
3434 var x = @truncate(u16, 9999);
35 expect(x == 9999);
35 try expect(x == 9999);
3636}
test/stage1/behavior/try.zig+7-7
......@@ -1,17 +1,17 @@
11const expect = @import("std").testing.expect;
22
33test "try on error union" {
4 tryOnErrorUnionImpl();
5 comptime tryOnErrorUnionImpl();
4 try tryOnErrorUnionImpl();
5 comptime try tryOnErrorUnionImpl();
66}
77
8fn tryOnErrorUnionImpl() void {
8fn tryOnErrorUnionImpl() !void {
99 const x = if (returnsTen()) |val| val + 1 else |err| switch (err) {
1010 error.ItBroke, error.NoMem => 1,
1111 error.CrappedOut => @as(i32, 2),
1212 else => unreachable,
1313 };
14 expect(x == 11);
14 try expect(x == 11);
1515}
1616
1717fn returnsTen() anyerror!i32 {
......@@ -20,10 +20,10 @@ fn returnsTen() anyerror!i32 {
2020
2121test "try without vars" {
2222 const result1 = if (failIfTrue(true)) 1 else |_| @as(i32, 2);
23 expect(result1 == 2);
23 try expect(result1 == 2);
2424
2525 const result2 = if (failIfTrue(false)) 1 else |_| @as(i32, 2);
26 expect(result2 == 1);
26 try expect(result2 == 1);
2727}
2828
2929fn failIfTrue(ok: bool) anyerror!void {
......@@ -38,6 +38,6 @@ test "try then not executed with assignment" {
3838 if (failIfTrue(true)) {
3939 unreachable;
4040 } else |err| {
41 expect(err == error.ItBroke);
41 try expect(err == error.ItBroke);
4242 }
4343}
test/stage1/behavior/tuple.zig+44-44
......@@ -5,93 +5,93 @@ const expectEqual = testing.expectEqual;
55
66test "tuple concatenation" {
77 const S = struct {
8 fn doTheTest() void {
8 fn doTheTest() !void {
99 var a: i32 = 1;
1010 var b: i32 = 2;
1111 var x = .{a};
1212 var y = .{b};
1313 var c = x ++ y;
14 expectEqual(@as(i32, 1), c[0]);
15 expectEqual(@as(i32, 2), c[1]);
14 try expectEqual(@as(i32, 1), c[0]);
15 try expectEqual(@as(i32, 2), c[1]);
1616 }
1717 };
18 S.doTheTest();
19 comptime S.doTheTest();
18 try S.doTheTest();
19 comptime try S.doTheTest();
2020}
2121
2222test "tuple multiplication" {
2323 const S = struct {
24 fn doTheTest() void {
24 fn doTheTest() !void {
2525 {
2626 const t = .{} ** 4;
27 expectEqual(0, @typeInfo(@TypeOf(t)).Struct.fields.len);
27 try expectEqual(0, @typeInfo(@TypeOf(t)).Struct.fields.len);
2828 }
2929 {
3030 const t = .{'a'} ** 4;
31 expectEqual(4, @typeInfo(@TypeOf(t)).Struct.fields.len);
32 inline for (t) |x| expectEqual('a', x);
31 try expectEqual(4, @typeInfo(@TypeOf(t)).Struct.fields.len);
32 inline for (t) |x| try expectEqual('a', x);
3333 }
3434 {
3535 const t = .{ 1, 2, 3 } ** 4;
36 expectEqual(12, @typeInfo(@TypeOf(t)).Struct.fields.len);
37 inline for (t) |x, i| expectEqual(1 + i % 3, x);
36 try expectEqual(12, @typeInfo(@TypeOf(t)).Struct.fields.len);
37 inline for (t) |x, i| try expectEqual(1 + i % 3, x);
3838 }
3939 }
4040 };
41 S.doTheTest();
42 comptime S.doTheTest();
41 try S.doTheTest();
42 comptime try S.doTheTest();
4343
4444 const T = struct {
45 fn consume_tuple(tuple: anytype, len: usize) void {
46 expect(tuple.len == len);
45 fn consume_tuple(tuple: anytype, len: usize) !void {
46 try expect(tuple.len == len);
4747 }
4848
49 fn doTheTest() void {
49 fn doTheTest() !void {
5050 const t1 = .{};
5151
5252 var rt_var: u8 = 42;
5353 const t2 = .{rt_var} ++ .{};
5454
55 expect(t2.len == 1);
56 expect(t2.@"0" == rt_var);
57 expect(t2.@"0" == 42);
58 expect(&t2.@"0" != &rt_var);
55 try expect(t2.len == 1);
56 try expect(t2.@"0" == rt_var);
57 try expect(t2.@"0" == 42);
58 try expect(&t2.@"0" != &rt_var);
5959
60 consume_tuple(t1 ++ t1, 0);
61 consume_tuple(.{} ++ .{}, 0);
62 consume_tuple(.{0} ++ .{}, 1);
63 consume_tuple(.{0} ++ .{1}, 2);
64 consume_tuple(.{ 0, 1, 2 } ++ .{ u8, 1, noreturn }, 6);
65 consume_tuple(t2 ++ t1, 1);
66 consume_tuple(t1 ++ t2, 1);
67 consume_tuple(t2 ++ t2, 2);
68 consume_tuple(.{rt_var} ++ .{}, 1);
69 consume_tuple(.{rt_var} ++ t1, 1);
70 consume_tuple(.{} ++ .{rt_var}, 1);
71 consume_tuple(t2 ++ .{void}, 2);
72 consume_tuple(t2 ++ .{0}, 2);
73 consume_tuple(.{0} ++ t2, 2);
74 consume_tuple(.{void} ++ t2, 2);
75 consume_tuple(.{u8} ++ .{rt_var} ++ .{true}, 3);
60 try consume_tuple(t1 ++ t1, 0);
61 try consume_tuple(.{} ++ .{}, 0);
62 try consume_tuple(.{0} ++ .{}, 1);
63 try consume_tuple(.{0} ++ .{1}, 2);
64 try consume_tuple(.{ 0, 1, 2 } ++ .{ u8, 1, noreturn }, 6);
65 try consume_tuple(t2 ++ t1, 1);
66 try consume_tuple(t1 ++ t2, 1);
67 try consume_tuple(t2 ++ t2, 2);
68 try consume_tuple(.{rt_var} ++ .{}, 1);
69 try consume_tuple(.{rt_var} ++ t1, 1);
70 try consume_tuple(.{} ++ .{rt_var}, 1);
71 try consume_tuple(t2 ++ .{void}, 2);
72 try consume_tuple(t2 ++ .{0}, 2);
73 try consume_tuple(.{0} ++ t2, 2);
74 try consume_tuple(.{void} ++ t2, 2);
75 try consume_tuple(.{u8} ++ .{rt_var} ++ .{true}, 3);
7676 }
7777 };
7878
79 T.doTheTest();
80 comptime T.doTheTest();
79 try T.doTheTest();
80 comptime try T.doTheTest();
8181}
8282
8383test "pass tuple to comptime var parameter" {
8484 const S = struct {
85 fn Foo(comptime args: anytype) void {
86 expect(args[0] == 1);
85 fn Foo(comptime args: anytype) !void {
86 try expect(args[0] == 1);
8787 }
8888
89 fn doTheTest() void {
90 Foo(.{1});
89 fn doTheTest() !void {
90 try Foo(.{1});
9191 }
9292 };
93 S.doTheTest();
94 comptime S.doTheTest();
93 try S.doTheTest();
94 comptime try S.doTheTest();
9595}
9696
9797test "tuple initializer for var" {
test/stage1/behavior/type.zig+84-84
......@@ -4,52 +4,52 @@ const TypeInfo = builtin.TypeInfo;
44const std = @import("std");
55const testing = std.testing;
66
7fn testTypes(comptime types: []const type) void {
7fn testTypes(comptime types: []const type) !void {
88 inline for (types) |testType| {
9 testing.expect(testType == @Type(@typeInfo(testType)));
9 try testing.expect(testType == @Type(@typeInfo(testType)));
1010 }
1111}
1212
1313test "Type.MetaType" {
14 testing.expect(type == @Type(TypeInfo{ .Type = undefined }));
15 testTypes(&[_]type{type});
14 try testing.expect(type == @Type(TypeInfo{ .Type = undefined }));
15 try testTypes(&[_]type{type});
1616}
1717
1818test "Type.Void" {
19 testing.expect(void == @Type(TypeInfo{ .Void = undefined }));
20 testTypes(&[_]type{void});
19 try testing.expect(void == @Type(TypeInfo{ .Void = undefined }));
20 try testTypes(&[_]type{void});
2121}
2222
2323test "Type.Bool" {
24 testing.expect(bool == @Type(TypeInfo{ .Bool = undefined }));
25 testTypes(&[_]type{bool});
24 try testing.expect(bool == @Type(TypeInfo{ .Bool = undefined }));
25 try testTypes(&[_]type{bool});
2626}
2727
2828test "Type.NoReturn" {
29 testing.expect(noreturn == @Type(TypeInfo{ .NoReturn = undefined }));
30 testTypes(&[_]type{noreturn});
29 try testing.expect(noreturn == @Type(TypeInfo{ .NoReturn = undefined }));
30 try testTypes(&[_]type{noreturn});
3131}
3232
3333test "Type.Int" {
34 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 } }));
36 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 } }));
38 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 } }));
40 testTypes(&[_]type{ u8, u32, i64 });
34 try testing.expect(u1 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .signedness = .unsigned, .bits = 1 } }));
35 try testing.expect(i1 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .signedness = .signed, .bits = 1 } }));
36 try testing.expect(u8 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .signedness = .unsigned, .bits = 8 } }));
37 try testing.expect(i8 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .signedness = .signed, .bits = 8 } }));
38 try testing.expect(u64 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .signedness = .unsigned, .bits = 64 } }));
39 try testing.expect(i64 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .signedness = .signed, .bits = 64 } }));
40 try testTypes(&[_]type{ u8, u32, i64 });
4141}
4242
4343test "Type.Float" {
44 testing.expect(f16 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 16 } }));
45 testing.expect(f32 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 32 } }));
46 testing.expect(f64 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 64 } }));
47 testing.expect(f128 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 128 } }));
48 testTypes(&[_]type{ f16, f32, f64, f128 });
44 try testing.expect(f16 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 16 } }));
45 try testing.expect(f32 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 32 } }));
46 try testing.expect(f64 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 64 } }));
47 try testing.expect(f128 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 128 } }));
48 try testTypes(&[_]type{ f16, f32, f64, f128 });
4949}
5050
5151test "Type.Pointer" {
52 testTypes(&[_]type{
52 try testTypes(&[_]type{
5353 // One Value Pointer Types
5454 *u8, *const u8,
5555 *volatile u8, *const volatile u8,
......@@ -94,41 +94,41 @@ test "Type.Pointer" {
9494}
9595
9696test "Type.Array" {
97 testing.expect([123]u8 == @Type(TypeInfo{
97 try testing.expect([123]u8 == @Type(TypeInfo{
9898 .Array = TypeInfo.Array{
9999 .len = 123,
100100 .child = u8,
101101 .sentinel = null,
102102 },
103103 }));
104 testing.expect([2]u32 == @Type(TypeInfo{
104 try testing.expect([2]u32 == @Type(TypeInfo{
105105 .Array = TypeInfo.Array{
106106 .len = 2,
107107 .child = u32,
108108 .sentinel = null,
109109 },
110110 }));
111 testing.expect([2:0]u32 == @Type(TypeInfo{
111 try testing.expect([2:0]u32 == @Type(TypeInfo{
112112 .Array = TypeInfo.Array{
113113 .len = 2,
114114 .child = u32,
115115 .sentinel = 0,
116116 },
117117 }));
118 testTypes(&[_]type{ [1]u8, [30]usize, [7]bool });
118 try testTypes(&[_]type{ [1]u8, [30]usize, [7]bool });
119119}
120120
121121test "Type.ComptimeFloat" {
122 testTypes(&[_]type{comptime_float});
122 try testTypes(&[_]type{comptime_float});
123123}
124124test "Type.ComptimeInt" {
125 testTypes(&[_]type{comptime_int});
125 try testTypes(&[_]type{comptime_int});
126126}
127127test "Type.Undefined" {
128 testTypes(&[_]type{@TypeOf(undefined)});
128 try testTypes(&[_]type{@TypeOf(undefined)});
129129}
130130test "Type.Null" {
131 testTypes(&[_]type{@TypeOf(null)});
131 try testTypes(&[_]type{@TypeOf(null)});
132132}
133133test "@Type create slice with null sentinel" {
134134 const Slice = @Type(builtin.TypeInfo{
......@@ -142,10 +142,10 @@ test "@Type create slice with null sentinel" {
142142 .sentinel = null,
143143 },
144144 });
145 testing.expect(Slice == []align(8) const *i32);
145 try testing.expect(Slice == []align(8) const *i32);
146146}
147147test "@Type picks up the sentinel value from TypeInfo" {
148 testTypes(&[_]type{
148 try testTypes(&[_]type{
149149 [11:0]u8, [4:10]u8,
150150 [*:0]u8, [*:0]const u8,
151151 [*:0]volatile u8, [*:0]const volatile u8,
......@@ -173,7 +173,7 @@ test "@Type picks up the sentinel value from TypeInfo" {
173173}
174174
175175test "Type.Optional" {
176 testTypes(&[_]type{
176 try testTypes(&[_]type{
177177 ?u8,
178178 ?*u8,
179179 ?[]u8,
......@@ -183,7 +183,7 @@ test "Type.Optional" {
183183}
184184
185185test "Type.ErrorUnion" {
186 testTypes(&[_]type{
186 try testTypes(&[_]type{
187187 error{}!void,
188188 error{Error}!void,
189189 });
......@@ -195,8 +195,8 @@ test "Type.Opaque" {
195195 .decls = &[_]TypeInfo.Declaration{},
196196 },
197197 });
198 testing.expect(Opaque != opaque {});
199 testing.expectEqualSlices(
198 try testing.expect(Opaque != opaque {});
199 try testing.expectEqualSlices(
200200 TypeInfo.Declaration,
201201 &[_]TypeInfo.Declaration{},
202202 @typeInfo(Opaque).Opaque.decls,
......@@ -204,7 +204,7 @@ test "Type.Opaque" {
204204}
205205
206206test "Type.Vector" {
207 testTypes(&[_]type{
207 try testTypes(&[_]type{
208208 @Vector(0, u8),
209209 @Vector(4, u8),
210210 @Vector(8, *u8),
......@@ -215,7 +215,7 @@ test "Type.Vector" {
215215}
216216
217217test "Type.AnyFrame" {
218 testTypes(&[_]type{
218 try testTypes(&[_]type{
219219 anyframe,
220220 anyframe->u8,
221221 anyframe->anyframe->u8,
......@@ -223,7 +223,7 @@ test "Type.AnyFrame" {
223223}
224224
225225test "Type.EnumLiteral" {
226 testTypes(&[_]type{
226 try testTypes(&[_]type{
227227 @TypeOf(.Dummy),
228228 });
229229}
......@@ -233,7 +233,7 @@ fn add(a: i32, b: i32) i32 {
233233}
234234
235235test "Type.Frame" {
236 testTypes(&[_]type{
236 try testTypes(&[_]type{
237237 @Frame(add),
238238 });
239239}
......@@ -248,45 +248,45 @@ test "Type.ErrorSet" {
248248test "Type.Struct" {
249249 const A = @Type(@typeInfo(struct { x: u8, y: u32 }));
250250 const infoA = @typeInfo(A).Struct;
251 testing.expectEqual(TypeInfo.ContainerLayout.Auto, infoA.layout);
252 testing.expectEqualSlices(u8, "x", infoA.fields[0].name);
253 testing.expectEqual(u8, infoA.fields[0].field_type);
254 testing.expectEqual(@as(?u8, null), infoA.fields[0].default_value);
255 testing.expectEqualSlices(u8, "y", infoA.fields[1].name);
256 testing.expectEqual(u32, infoA.fields[1].field_type);
257 testing.expectEqual(@as(?u32, null), infoA.fields[1].default_value);
258 testing.expectEqualSlices(TypeInfo.Declaration, &[_]TypeInfo.Declaration{}, infoA.decls);
259 testing.expectEqual(@as(bool, false), infoA.is_tuple);
251 try testing.expectEqual(TypeInfo.ContainerLayout.Auto, infoA.layout);
252 try testing.expectEqualSlices(u8, "x", infoA.fields[0].name);
253 try testing.expectEqual(u8, infoA.fields[0].field_type);
254 try testing.expectEqual(@as(?u8, null), infoA.fields[0].default_value);
255 try testing.expectEqualSlices(u8, "y", infoA.fields[1].name);
256 try testing.expectEqual(u32, infoA.fields[1].field_type);
257 try testing.expectEqual(@as(?u32, null), infoA.fields[1].default_value);
258 try testing.expectEqualSlices(TypeInfo.Declaration, &[_]TypeInfo.Declaration{}, infoA.decls);
259 try testing.expectEqual(@as(bool, false), infoA.is_tuple);
260260
261261 var a = A{ .x = 0, .y = 1 };
262 testing.expectEqual(@as(u8, 0), a.x);
263 testing.expectEqual(@as(u32, 1), a.y);
262 try testing.expectEqual(@as(u8, 0), a.x);
263 try testing.expectEqual(@as(u32, 1), a.y);
264264 a.y += 1;
265 testing.expectEqual(@as(u32, 2), a.y);
265 try testing.expectEqual(@as(u32, 2), a.y);
266266
267267 const B = @Type(@typeInfo(extern struct { x: u8, y: u32 = 5 }));
268268 const infoB = @typeInfo(B).Struct;
269 testing.expectEqual(TypeInfo.ContainerLayout.Extern, infoB.layout);
270 testing.expectEqualSlices(u8, "x", infoB.fields[0].name);
271 testing.expectEqual(u8, infoB.fields[0].field_type);
272 testing.expectEqual(@as(?u8, null), infoB.fields[0].default_value);
273 testing.expectEqualSlices(u8, "y", infoB.fields[1].name);
274 testing.expectEqual(u32, infoB.fields[1].field_type);
275 testing.expectEqual(@as(?u32, 5), infoB.fields[1].default_value);
276 testing.expectEqual(@as(usize, 0), infoB.decls.len);
277 testing.expectEqual(@as(bool, false), infoB.is_tuple);
269 try testing.expectEqual(TypeInfo.ContainerLayout.Extern, infoB.layout);
270 try testing.expectEqualSlices(u8, "x", infoB.fields[0].name);
271 try testing.expectEqual(u8, infoB.fields[0].field_type);
272 try testing.expectEqual(@as(?u8, null), infoB.fields[0].default_value);
273 try testing.expectEqualSlices(u8, "y", infoB.fields[1].name);
274 try testing.expectEqual(u32, infoB.fields[1].field_type);
275 try testing.expectEqual(@as(?u32, 5), infoB.fields[1].default_value);
276 try testing.expectEqual(@as(usize, 0), infoB.decls.len);
277 try testing.expectEqual(@as(bool, false), infoB.is_tuple);
278278
279279 const C = @Type(@typeInfo(packed struct { x: u8 = 3, y: u32 = 5 }));
280280 const infoC = @typeInfo(C).Struct;
281 testing.expectEqual(TypeInfo.ContainerLayout.Packed, infoC.layout);
282 testing.expectEqualSlices(u8, "x", infoC.fields[0].name);
283 testing.expectEqual(u8, infoC.fields[0].field_type);
284 testing.expectEqual(@as(?u8, 3), infoC.fields[0].default_value);
285 testing.expectEqualSlices(u8, "y", infoC.fields[1].name);
286 testing.expectEqual(u32, infoC.fields[1].field_type);
287 testing.expectEqual(@as(?u32, 5), infoC.fields[1].default_value);
288 testing.expectEqual(@as(usize, 0), infoC.decls.len);
289 testing.expectEqual(@as(bool, false), infoC.is_tuple);
281 try testing.expectEqual(TypeInfo.ContainerLayout.Packed, infoC.layout);
282 try testing.expectEqualSlices(u8, "x", infoC.fields[0].name);
283 try testing.expectEqual(u8, infoC.fields[0].field_type);
284 try testing.expectEqual(@as(?u8, 3), infoC.fields[0].default_value);
285 try testing.expectEqualSlices(u8, "y", infoC.fields[1].name);
286 try testing.expectEqual(u32, infoC.fields[1].field_type);
287 try testing.expectEqual(@as(?u32, 5), infoC.fields[1].default_value);
288 try testing.expectEqual(@as(usize, 0), infoC.decls.len);
289 try testing.expectEqual(@as(bool, false), infoC.is_tuple);
290290}
291291
292292test "Type.Enum" {
......@@ -302,9 +302,9 @@ test "Type.Enum" {
302302 .is_exhaustive = true,
303303 },
304304 });
305 testing.expectEqual(true, @typeInfo(Foo).Enum.is_exhaustive);
306 testing.expectEqual(@as(u8, 1), @enumToInt(Foo.a));
307 testing.expectEqual(@as(u8, 5), @enumToInt(Foo.b));
305 try testing.expectEqual(true, @typeInfo(Foo).Enum.is_exhaustive);
306 try testing.expectEqual(@as(u8, 1), @enumToInt(Foo.a));
307 try testing.expectEqual(@as(u8, 5), @enumToInt(Foo.b));
308308 const Bar = @Type(.{
309309 .Enum = .{
310310 .layout = .Extern,
......@@ -317,10 +317,10 @@ test "Type.Enum" {
317317 .is_exhaustive = false,
318318 },
319319 });
320 testing.expectEqual(false, @typeInfo(Bar).Enum.is_exhaustive);
321 testing.expectEqual(@as(u32, 1), @enumToInt(Bar.a));
322 testing.expectEqual(@as(u32, 5), @enumToInt(Bar.b));
323 testing.expectEqual(@as(u32, 6), @enumToInt(@intToEnum(Bar, 6)));
320 try testing.expectEqual(false, @typeInfo(Bar).Enum.is_exhaustive);
321 try testing.expectEqual(@as(u32, 1), @enumToInt(Bar.a));
322 try testing.expectEqual(@as(u32, 5), @enumToInt(Bar.b));
323 try testing.expectEqual(@as(u32, 6), @enumToInt(@intToEnum(Bar, 6)));
324324}
325325
326326test "Type.Union" {
......@@ -338,7 +338,7 @@ test "Type.Union" {
338338 var untagged = Untagged{ .int = 1 };
339339 untagged.float = 2.0;
340340 untagged.int = 3;
341 testing.expectEqual(@as(i32, 3), untagged.int);
341 try testing.expectEqual(@as(i32, 3), untagged.int);
342342
343343 const PackedUntagged = @Type(.{
344344 .Union = .{
......@@ -352,8 +352,8 @@ test "Type.Union" {
352352 },
353353 });
354354 var packed_untagged = PackedUntagged{ .signed = -1 };
355 testing.expectEqual(@as(i32, -1), packed_untagged.signed);
356 testing.expectEqual(~@as(u32, 0), packed_untagged.unsigned);
355 try testing.expectEqual(@as(i32, -1), packed_untagged.signed);
356 try testing.expectEqual(~@as(u32, 0), packed_untagged.unsigned);
357357
358358 const Tag = @Type(.{
359359 .Enum = .{
......@@ -379,9 +379,9 @@ test "Type.Union" {
379379 },
380380 });
381381 var tagged = Tagged{ .signed = -1 };
382 testing.expectEqual(Tag.signed, tagged);
382 try testing.expectEqual(Tag.signed, tagged);
383383 tagged = .{ .unsigned = 1 };
384 testing.expectEqual(Tag.unsigned, tagged);
384 try testing.expectEqual(Tag.unsigned, tagged);
385385}
386386
387387test "Type.Union from Type.Enum" {
......@@ -447,7 +447,7 @@ test "Type.BoundFn" {
447447 pub fn foo(self: *const @This()) align(4) callconv(.Unspecified) void {}
448448 };
449449 const test_instance: TestStruct = undefined;
450 testing.expect(std.meta.eql(
450 try testing.expect(std.meta.eql(
451451 @typeName(@TypeOf(test_instance.foo)),
452452 @typeName(@Type(@typeInfo(@TypeOf(test_instance.foo)))),
453453 ));
test/stage1/behavior/type_info.zig+187-187
......@@ -9,151 +9,151 @@ const expect = std.testing.expect;
99const expectEqualStrings = std.testing.expectEqualStrings;
1010
1111test "type info: tag type, void info" {
12 testBasic();
13 comptime testBasic();
12 try testBasic();
13 comptime try testBasic();
1414}
1515
16fn testBasic() void {
17 expect(@typeInfo(TypeInfo).Union.tag_type == TypeId);
16fn testBasic() !void {
17 try expect(@typeInfo(TypeInfo).Union.tag_type == TypeId);
1818 const void_info = @typeInfo(void);
19 expect(void_info == TypeId.Void);
20 expect(void_info.Void == {});
19 try expect(void_info == TypeId.Void);
20 try expect(void_info.Void == {});
2121}
2222
2323test "type info: integer, floating point type info" {
24 testIntFloat();
25 comptime testIntFloat();
24 try testIntFloat();
25 comptime try testIntFloat();
2626}
2727
28fn testIntFloat() void {
28fn testIntFloat() !void {
2929 const u8_info = @typeInfo(u8);
30 expect(u8_info == .Int);
31 expect(u8_info.Int.signedness == .unsigned);
32 expect(u8_info.Int.bits == 8);
30 try expect(u8_info == .Int);
31 try expect(u8_info.Int.signedness == .unsigned);
32 try expect(u8_info.Int.bits == 8);
3333
3434 const f64_info = @typeInfo(f64);
35 expect(f64_info == .Float);
36 expect(f64_info.Float.bits == 64);
35 try expect(f64_info == .Float);
36 try expect(f64_info.Float.bits == 64);
3737}
3838
3939test "type info: pointer type info" {
40 testPointer();
41 comptime testPointer();
40 try testPointer();
41 comptime try testPointer();
4242}
4343
44fn testPointer() void {
44fn testPointer() !void {
4545 const u32_ptr_info = @typeInfo(*u32);
46 expect(u32_ptr_info == .Pointer);
47 expect(u32_ptr_info.Pointer.size == TypeInfo.Pointer.Size.One);
48 expect(u32_ptr_info.Pointer.is_const == false);
49 expect(u32_ptr_info.Pointer.is_volatile == false);
50 expect(u32_ptr_info.Pointer.alignment == @alignOf(u32));
51 expect(u32_ptr_info.Pointer.child == u32);
52 expect(u32_ptr_info.Pointer.sentinel == null);
46 try expect(u32_ptr_info == .Pointer);
47 try expect(u32_ptr_info.Pointer.size == TypeInfo.Pointer.Size.One);
48 try expect(u32_ptr_info.Pointer.is_const == false);
49 try expect(u32_ptr_info.Pointer.is_volatile == false);
50 try expect(u32_ptr_info.Pointer.alignment == @alignOf(u32));
51 try expect(u32_ptr_info.Pointer.child == u32);
52 try expect(u32_ptr_info.Pointer.sentinel == null);
5353}
5454
5555test "type info: unknown length pointer type info" {
56 testUnknownLenPtr();
57 comptime testUnknownLenPtr();
56 try testUnknownLenPtr();
57 comptime try testUnknownLenPtr();
5858}
5959
60fn testUnknownLenPtr() void {
60fn testUnknownLenPtr() !void {
6161 const u32_ptr_info = @typeInfo([*]const volatile f64);
62 expect(u32_ptr_info == .Pointer);
63 expect(u32_ptr_info.Pointer.size == TypeInfo.Pointer.Size.Many);
64 expect(u32_ptr_info.Pointer.is_const == true);
65 expect(u32_ptr_info.Pointer.is_volatile == true);
66 expect(u32_ptr_info.Pointer.sentinel == null);
67 expect(u32_ptr_info.Pointer.alignment == @alignOf(f64));
68 expect(u32_ptr_info.Pointer.child == f64);
62 try expect(u32_ptr_info == .Pointer);
63 try expect(u32_ptr_info.Pointer.size == TypeInfo.Pointer.Size.Many);
64 try expect(u32_ptr_info.Pointer.is_const == true);
65 try expect(u32_ptr_info.Pointer.is_volatile == true);
66 try expect(u32_ptr_info.Pointer.sentinel == null);
67 try expect(u32_ptr_info.Pointer.alignment == @alignOf(f64));
68 try expect(u32_ptr_info.Pointer.child == f64);
6969}
7070
7171test "type info: null terminated pointer type info" {
72 testNullTerminatedPtr();
73 comptime testNullTerminatedPtr();
72 try testNullTerminatedPtr();
73 comptime try testNullTerminatedPtr();
7474}
7575
76fn testNullTerminatedPtr() void {
76fn testNullTerminatedPtr() !void {
7777 const ptr_info = @typeInfo([*:0]u8);
78 expect(ptr_info == .Pointer);
79 expect(ptr_info.Pointer.size == TypeInfo.Pointer.Size.Many);
80 expect(ptr_info.Pointer.is_const == false);
81 expect(ptr_info.Pointer.is_volatile == false);
82 expect(ptr_info.Pointer.sentinel.? == 0);
78 try expect(ptr_info == .Pointer);
79 try expect(ptr_info.Pointer.size == TypeInfo.Pointer.Size.Many);
80 try expect(ptr_info.Pointer.is_const == false);
81 try expect(ptr_info.Pointer.is_volatile == false);
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);
8585}
8686
8787test "type info: C pointer type info" {
88 testCPtr();
89 comptime testCPtr();
88 try testCPtr();
89 comptime try testCPtr();
9090}
9191
92fn testCPtr() void {
92fn testCPtr() !void {
9393 const ptr_info = @typeInfo([*c]align(4) const i8);
94 expect(ptr_info == .Pointer);
95 expect(ptr_info.Pointer.size == .C);
96 expect(ptr_info.Pointer.is_const);
97 expect(!ptr_info.Pointer.is_volatile);
98 expect(ptr_info.Pointer.alignment == 4);
99 expect(ptr_info.Pointer.child == i8);
94 try expect(ptr_info == .Pointer);
95 try expect(ptr_info.Pointer.size == .C);
96 try expect(ptr_info.Pointer.is_const);
97 try expect(!ptr_info.Pointer.is_volatile);
98 try expect(ptr_info.Pointer.alignment == 4);
99 try expect(ptr_info.Pointer.child == i8);
100100}
101101
102102test "type info: slice type info" {
103 testSlice();
104 comptime testSlice();
103 try testSlice();
104 comptime try testSlice();
105105}
106106
107fn testSlice() void {
107fn testSlice() !void {
108108 const u32_slice_info = @typeInfo([]u32);
109 expect(u32_slice_info == .Pointer);
110 expect(u32_slice_info.Pointer.size == .Slice);
111 expect(u32_slice_info.Pointer.is_const == false);
112 expect(u32_slice_info.Pointer.is_volatile == false);
113 expect(u32_slice_info.Pointer.alignment == 4);
114 expect(u32_slice_info.Pointer.child == u32);
109 try expect(u32_slice_info == .Pointer);
110 try expect(u32_slice_info.Pointer.size == .Slice);
111 try expect(u32_slice_info.Pointer.is_const == false);
112 try expect(u32_slice_info.Pointer.is_volatile == false);
113 try expect(u32_slice_info.Pointer.alignment == 4);
114 try expect(u32_slice_info.Pointer.child == u32);
115115}
116116
117117test "type info: array type info" {
118 testArray();
119 comptime testArray();
118 try testArray();
119 comptime try testArray();
120120}
121121
122fn testArray() void {
122fn testArray() !void {
123123 {
124124 const info = @typeInfo([42]u8);
125 expect(info == .Array);
126 expect(info.Array.len == 42);
127 expect(info.Array.child == u8);
128 expect(info.Array.sentinel == null);
125 try expect(info == .Array);
126 try expect(info.Array.len == 42);
127 try expect(info.Array.child == u8);
128 try expect(info.Array.sentinel == null);
129129 }
130130
131131 {
132132 const info = @typeInfo([10:0]u8);
133 expect(info.Array.len == 10);
134 expect(info.Array.child == u8);
135 expect(info.Array.sentinel.? == @as(u8, 0));
136 expect(@sizeOf([10:0]u8) == info.Array.len + 1);
133 try expect(info.Array.len == 10);
134 try expect(info.Array.child == u8);
135 try expect(info.Array.sentinel.? == @as(u8, 0));
136 try expect(@sizeOf([10:0]u8) == info.Array.len + 1);
137137 }
138138}
139139
140140test "type info: optional type info" {
141 testOptional();
142 comptime testOptional();
141 try testOptional();
142 comptime try testOptional();
143143}
144144
145fn testOptional() void {
145fn testOptional() !void {
146146 const null_info = @typeInfo(?void);
147 expect(null_info == .Optional);
148 expect(null_info.Optional.child == void);
147 try expect(null_info == .Optional);
148 try expect(null_info.Optional.child == void);
149149}
150150
151151test "type info: error set, error union info" {
152 testErrorSet();
153 comptime testErrorSet();
152 try testErrorSet();
153 comptime try testErrorSet();
154154}
155155
156fn testErrorSet() void {
156fn testErrorSet() !void {
157157 const TestErrorSet = error{
158158 First,
159159 Second,
......@@ -161,26 +161,26 @@ fn testErrorSet() void {
161161 };
162162
163163 const error_set_info = @typeInfo(TestErrorSet);
164 expect(error_set_info == .ErrorSet);
165 expect(error_set_info.ErrorSet.?.len == 3);
166 expect(mem.eql(u8, error_set_info.ErrorSet.?[0].name, "First"));
164 try expect(error_set_info == .ErrorSet);
165 try expect(error_set_info.ErrorSet.?.len == 3);
166 try expect(mem.eql(u8, error_set_info.ErrorSet.?[0].name, "First"));
167167
168168 const error_union_info = @typeInfo(TestErrorSet!usize);
169 expect(error_union_info == .ErrorUnion);
170 expect(error_union_info.ErrorUnion.error_set == TestErrorSet);
171 expect(error_union_info.ErrorUnion.payload == usize);
169 try expect(error_union_info == .ErrorUnion);
170 try expect(error_union_info.ErrorUnion.error_set == TestErrorSet);
171 try expect(error_union_info.ErrorUnion.payload == usize);
172172
173173 const global_info = @typeInfo(anyerror);
174 expect(global_info == .ErrorSet);
175 expect(global_info.ErrorSet == null);
174 try expect(global_info == .ErrorSet);
175 try expect(global_info.ErrorSet == null);
176176}
177177
178178test "type info: enum info" {
179 testEnum();
180 comptime testEnum();
179 try testEnum();
180 comptime try testEnum();
181181}
182182
183fn testEnum() void {
183fn testEnum() !void {
184184 const Os = enum {
185185 Windows,
186186 Macos,
......@@ -189,28 +189,28 @@ fn testEnum() void {
189189 };
190190
191191 const os_info = @typeInfo(Os);
192 expect(os_info == .Enum);
193 expect(os_info.Enum.layout == .Auto);
194 expect(os_info.Enum.fields.len == 4);
195 expect(mem.eql(u8, os_info.Enum.fields[1].name, "Macos"));
196 expect(os_info.Enum.fields[3].value == 3);
197 expect(os_info.Enum.tag_type == u2);
198 expect(os_info.Enum.decls.len == 0);
192 try expect(os_info == .Enum);
193 try expect(os_info.Enum.layout == .Auto);
194 try expect(os_info.Enum.fields.len == 4);
195 try expect(mem.eql(u8, os_info.Enum.fields[1].name, "Macos"));
196 try expect(os_info.Enum.fields[3].value == 3);
197 try expect(os_info.Enum.tag_type == u2);
198 try expect(os_info.Enum.decls.len == 0);
199199}
200200
201201test "type info: union info" {
202 testUnion();
203 comptime testUnion();
202 try testUnion();
203 comptime try testUnion();
204204}
205205
206fn testUnion() void {
206fn testUnion() !void {
207207 const typeinfo_info = @typeInfo(TypeInfo);
208 expect(typeinfo_info == .Union);
209 expect(typeinfo_info.Union.layout == .Auto);
210 expect(typeinfo_info.Union.tag_type.? == TypeId);
211 expect(typeinfo_info.Union.fields.len == 25);
212 expect(typeinfo_info.Union.fields[4].field_type == @TypeOf(@typeInfo(u8).Int));
213 expect(typeinfo_info.Union.decls.len == 22);
208 try expect(typeinfo_info == .Union);
209 try expect(typeinfo_info.Union.layout == .Auto);
210 try expect(typeinfo_info.Union.tag_type.? == TypeId);
211 try expect(typeinfo_info.Union.fields.len == 25);
212 try expect(typeinfo_info.Union.fields[4].field_type == @TypeOf(@typeInfo(u8).Int));
213 try expect(typeinfo_info.Union.decls.len == 22);
214214
215215 const TestNoTagUnion = union {
216216 Foo: void,
......@@ -218,52 +218,52 @@ fn testUnion() void {
218218 };
219219
220220 const notag_union_info = @typeInfo(TestNoTagUnion);
221 expect(notag_union_info == .Union);
222 expect(notag_union_info.Union.tag_type == null);
223 expect(notag_union_info.Union.layout == .Auto);
224 expect(notag_union_info.Union.fields.len == 2);
225 expect(notag_union_info.Union.fields[0].alignment == @alignOf(void));
226 expect(notag_union_info.Union.fields[1].field_type == u32);
227 expect(notag_union_info.Union.fields[1].alignment == @alignOf(u32));
221 try expect(notag_union_info == .Union);
222 try expect(notag_union_info.Union.tag_type == null);
223 try expect(notag_union_info.Union.layout == .Auto);
224 try expect(notag_union_info.Union.fields.len == 2);
225 try expect(notag_union_info.Union.fields[0].alignment == @alignOf(void));
226 try expect(notag_union_info.Union.fields[1].field_type == u32);
227 try expect(notag_union_info.Union.fields[1].alignment == @alignOf(u32));
228228
229229 const TestExternUnion = extern union {
230230 foo: *c_void,
231231 };
232232
233233 const extern_union_info = @typeInfo(TestExternUnion);
234 expect(extern_union_info.Union.layout == .Extern);
235 expect(extern_union_info.Union.tag_type == null);
236 expect(extern_union_info.Union.fields[0].field_type == *c_void);
234 try expect(extern_union_info.Union.layout == .Extern);
235 try expect(extern_union_info.Union.tag_type == null);
236 try expect(extern_union_info.Union.fields[0].field_type == *c_void);
237237}
238238
239239test "type info: struct info" {
240 testStruct();
241 comptime testStruct();
240 try testStruct();
241 comptime try testStruct();
242242}
243243
244fn testStruct() void {
244fn testStruct() !void {
245245 const unpacked_struct_info = @typeInfo(TestUnpackedStruct);
246 expect(unpacked_struct_info.Struct.is_tuple == false);
247 expect(unpacked_struct_info.Struct.fields[0].alignment == @alignOf(u32));
248 expect(unpacked_struct_info.Struct.fields[0].default_value.? == 4);
249 expectEqualStrings("foobar", unpacked_struct_info.Struct.fields[1].default_value.?);
246 try expect(unpacked_struct_info.Struct.is_tuple == false);
247 try expect(unpacked_struct_info.Struct.fields[0].alignment == @alignOf(u32));
248 try expect(unpacked_struct_info.Struct.fields[0].default_value.? == 4);
249 try expectEqualStrings("foobar", unpacked_struct_info.Struct.fields[1].default_value.?);
250250
251251 const struct_info = @typeInfo(TestStruct);
252 expect(struct_info == .Struct);
253 expect(struct_info.Struct.is_tuple == false);
254 expect(struct_info.Struct.layout == .Packed);
255 expect(struct_info.Struct.fields.len == 4);
256 expect(struct_info.Struct.fields[0].alignment == 2 * @alignOf(usize));
257 expect(struct_info.Struct.fields[2].field_type == *TestStruct);
258 expect(struct_info.Struct.fields[2].default_value == null);
259 expect(struct_info.Struct.fields[3].default_value.? == 4);
260 expect(struct_info.Struct.fields[3].alignment == 1);
261 expect(struct_info.Struct.decls.len == 2);
262 expect(struct_info.Struct.decls[0].is_pub);
263 expect(!struct_info.Struct.decls[0].data.Fn.is_extern);
264 expect(struct_info.Struct.decls[0].data.Fn.lib_name == null);
265 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);
252 try expect(struct_info == .Struct);
253 try expect(struct_info.Struct.is_tuple == false);
254 try expect(struct_info.Struct.layout == .Packed);
255 try expect(struct_info.Struct.fields.len == 4);
256 try expect(struct_info.Struct.fields[0].alignment == 2 * @alignOf(usize));
257 try expect(struct_info.Struct.fields[2].field_type == *TestStruct);
258 try expect(struct_info.Struct.fields[2].default_value == null);
259 try expect(struct_info.Struct.fields[3].default_value.? == 4);
260 try expect(struct_info.Struct.fields[3].alignment == 1);
261 try expect(struct_info.Struct.decls.len == 2);
262 try expect(struct_info.Struct.decls[0].is_pub);
263 try expect(!struct_info.Struct.decls[0].data.Fn.is_extern);
264 try expect(struct_info.Struct.decls[0].data.Fn.lib_name == null);
265 try expect(struct_info.Struct.decls[0].data.Fn.return_type == void);
266 try expect(struct_info.Struct.decls[0].data.Fn.fn_type == fn (*const TestStruct) void);
267267}
268268
269269const TestUnpackedStruct = struct {
......@@ -282,43 +282,43 @@ const TestStruct = packed struct {
282282};
283283
284284test "type info: opaque info" {
285 testOpaque();
286 comptime testOpaque();
285 try testOpaque();
286 comptime try testOpaque();
287287}
288288
289fn testOpaque() void {
289fn testOpaque() !void {
290290 const Foo = opaque {
291291 const A = 1;
292292 fn b() void {}
293293 };
294294
295295 const foo_info = @typeInfo(Foo);
296 expect(foo_info.Opaque.decls.len == 2);
296 try expect(foo_info.Opaque.decls.len == 2);
297297}
298298
299299test "type info: function type info" {
300300 // wasm doesn't support align attributes on functions
301301 if (builtin.arch == .wasm32 or builtin.arch == .wasm64) return error.SkipZigTest;
302 testFunction();
303 comptime testFunction();
302 try testFunction();
303 comptime try testFunction();
304304}
305305
306fn testFunction() void {
306fn testFunction() !void {
307307 const fn_info = @typeInfo(@TypeOf(foo));
308 expect(fn_info == .Fn);
309 expect(fn_info.Fn.alignment > 0);
310 expect(fn_info.Fn.calling_convention == .C);
311 expect(!fn_info.Fn.is_generic);
312 expect(fn_info.Fn.args.len == 2);
313 expect(fn_info.Fn.is_var_args);
314 expect(fn_info.Fn.return_type.? == usize);
308 try expect(fn_info == .Fn);
309 try expect(fn_info.Fn.alignment > 0);
310 try expect(fn_info.Fn.calling_convention == .C);
311 try expect(!fn_info.Fn.is_generic);
312 try expect(fn_info.Fn.args.len == 2);
313 try expect(fn_info.Fn.is_var_args);
314 try expect(fn_info.Fn.return_type.? == usize);
315315 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
318318 const test_instance: TestStruct = undefined;
319319 const bound_fn_info = @typeInfo(@TypeOf(test_instance.foo));
320 expect(bound_fn_info == .BoundFn);
321 expect(bound_fn_info.BoundFn.args[0].arg_type.? == *const TestStruct);
320 try expect(bound_fn_info == .BoundFn);
321 try expect(bound_fn_info.BoundFn.args[0].arg_type.? == *const TestStruct);
322322}
323323
324324extern fn foo(a: usize, b: bool, ...) callconv(.C) usize;
......@@ -332,33 +332,33 @@ test "typeInfo with comptime parameter in struct fn def" {
332332}
333333
334334test "type info: vectors" {
335 testVector();
336 comptime testVector();
335 try testVector();
336 comptime try testVector();
337337}
338338
339fn testVector() void {
339fn testVector() !void {
340340 const vec_info = @typeInfo(std.meta.Vector(4, i32));
341 expect(vec_info == .Vector);
342 expect(vec_info.Vector.len == 4);
343 expect(vec_info.Vector.child == i32);
341 try expect(vec_info == .Vector);
342 try expect(vec_info.Vector.len == 4);
343 try expect(vec_info.Vector.child == i32);
344344}
345345
346346test "type info: anyframe and anyframe->T" {
347 testAnyFrame();
348 comptime testAnyFrame();
347 try testAnyFrame();
348 comptime try testAnyFrame();
349349}
350350
351fn testAnyFrame() void {
351fn testAnyFrame() !void {
352352 {
353353 const anyframe_info = @typeInfo(anyframe->i32);
354 expect(anyframe_info == .AnyFrame);
355 expect(anyframe_info.AnyFrame.child.? == i32);
354 try expect(anyframe_info == .AnyFrame);
355 try expect(anyframe_info.AnyFrame.child.? == i32);
356356 }
357357
358358 {
359359 const anyframe_info = @typeInfo(anyframe);
360 expect(anyframe_info == .AnyFrame);
361 expect(anyframe_info.AnyFrame.child == null);
360 try expect(anyframe_info == .AnyFrame);
361 try expect(anyframe_info.AnyFrame.child == null);
362362 }
363363}
364364
......@@ -385,9 +385,9 @@ test "type info: extern fns with and without lib names" {
385385 comptime {
386386 for (info.Struct.decls) |decl| {
387387 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);
389389 } else {
390 expectEqualStrings("cool", decl.data.Fn.lib_name.?);
390 try expectEqualStrings("cool", decl.data.Fn.lib_name.?);
391391 }
392392 }
393393 }
......@@ -397,12 +397,12 @@ test "data field is a compile-time value" {
397397 const S = struct {
398398 const Bar = @as(isize, -1);
399399 };
400 comptime expect(@typeInfo(S).Struct.decls[0].data.Var == isize);
400 comptime try expect(@typeInfo(S).Struct.decls[0].data.Var == isize);
401401}
402402
403403test "sentinel of opaque pointer type" {
404404 const c_void_info = @typeInfo(*c_void);
405 expect(c_void_info.Pointer.sentinel == null);
405 try expect(c_void_info.Pointer.sentinel == null);
406406}
407407
408408test "@typeInfo does not force declarations into existence" {
......@@ -413,12 +413,12 @@ test "@typeInfo does not force declarations into existence" {
413413 @compileError("test failed");
414414 }
415415 };
416 comptime expect(@typeInfo(S).Struct.fields.len == 1);
416 comptime try expect(@typeInfo(S).Struct.fields.len == 1);
417417}
418418
419419test "defaut value for a var-typed field" {
420420 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);
422422}
423423
424424fn add(a: i32, b: i32) i32 {
......@@ -428,7 +428,7 @@ fn add(a: i32, b: i32) i32 {
428428test "type info for async frames" {
429429 switch (@typeInfo(@Frame(add))) {
430430 .Frame => |frame| {
431 expect(frame.function == add);
431 try expect(frame.function == add);
432432 },
433433 else => unreachable,
434434 }
......@@ -438,7 +438,7 @@ test "type info: value is correctly copied" {
438438 comptime {
439439 var ptrInfo = @typeInfo([]u32);
440440 ptrInfo.Pointer.size = .One;
441 expect(@typeInfo([]u32).Pointer.size == .Slice);
441 try expect(@typeInfo([]u32).Pointer.size == .Slice);
442442 }
443443}
444444
......@@ -451,22 +451,22 @@ test "Declarations are returned in declaration order" {
451451 const e = 5;
452452 };
453453 const d = @typeInfo(S).Struct.decls;
454 expect(std.mem.eql(u8, d[0].name, "a"));
455 expect(std.mem.eql(u8, d[1].name, "b"));
456 expect(std.mem.eql(u8, d[2].name, "c"));
457 expect(std.mem.eql(u8, d[3].name, "d"));
458 expect(std.mem.eql(u8, d[4].name, "e"));
454 try expect(std.mem.eql(u8, d[0].name, "a"));
455 try expect(std.mem.eql(u8, d[1].name, "b"));
456 try expect(std.mem.eql(u8, d[2].name, "c"));
457 try expect(std.mem.eql(u8, d[3].name, "d"));
458 try expect(std.mem.eql(u8, d[4].name, "e"));
459459}
460460
461461test "Struct.is_tuple" {
462 expect(@typeInfo(@TypeOf(.{0})).Struct.is_tuple);
463 expect(!@typeInfo(@TypeOf(.{ .a = 0 })).Struct.is_tuple);
462 try expect(@typeInfo(@TypeOf(.{0})).Struct.is_tuple);
463 try expect(!@typeInfo(@TypeOf(.{ .a = 0 })).Struct.is_tuple);
464464}
465465
466466test "StructField.is_comptime" {
467467 const info = @typeInfo(struct { x: u8 = 3, comptime y: u32 = 5 }).Struct;
468 expect(!info.fields[0].is_comptime);
469 expect(info.fields[1].is_comptime);
468 try expect(!info.fields[0].is_comptime);
469 try expect(info.fields[1].is_comptime);
470470}
471471
472472test "typeInfo resolves usingnamespace declarations" {
......@@ -479,6 +479,6 @@ test "typeInfo resolves usingnamespace declarations" {
479479 usingnamespace A;
480480 };
481481
482 expect(@typeInfo(B).Struct.decls.len == 2);
482 try expect(@typeInfo(B).Struct.decls.len == 2);
483483 //a
484484}
test/stage1/behavior/typename.zig+1-1
......@@ -3,5 +3,5 @@ const expect = std.testing.expect;
33const expectEqualSlices = std.testing.expectEqualSlices;
44
55test "slice" {
6 expectEqualSlices(u8, "[]u8", @typeName([]u8));
6 try expectEqualSlices(u8, "[]u8", @typeName([]u8));
77}
test/stage1/behavior/undefined.zig+13-13
......@@ -12,16 +12,16 @@ fn initStaticArray() [10]i32 {
1212}
1313const static_array = initStaticArray();
1414test "init static array to undefined" {
15 expect(static_array[0] == 1);
16 expect(static_array[4] == 2);
17 expect(static_array[7] == 3);
18 expect(static_array[9] == 4);
15 try expect(static_array[0] == 1);
16 try expect(static_array[4] == 2);
17 try expect(static_array[7] == 3);
18 try expect(static_array[9] == 4);
1919
2020 comptime {
21 expect(static_array[0] == 1);
22 expect(static_array[4] == 2);
23 expect(static_array[7] == 3);
24 expect(static_array[9] == 4);
21 try expect(static_array[0] == 1);
22 try expect(static_array[4] == 2);
23 try expect(static_array[7] == 3);
24 try expect(static_array[9] == 4);
2525 }
2626}
2727
......@@ -41,12 +41,12 @@ test "assign undefined to struct" {
4141 comptime {
4242 var foo: Foo = undefined;
4343 setFooX(&foo);
44 expect(foo.x == 2);
44 try expect(foo.x == 2);
4545 }
4646 {
4747 var foo: Foo = undefined;
4848 setFooX(&foo);
49 expect(foo.x == 2);
49 try expect(foo.x == 2);
5050 }
5151}
5252
......@@ -54,16 +54,16 @@ test "assign undefined to struct with method" {
5454 comptime {
5555 var foo: Foo = undefined;
5656 foo.setFooXMethod();
57 expect(foo.x == 3);
57 try expect(foo.x == 3);
5858 }
5959 {
6060 var foo: Foo = undefined;
6161 foo.setFooXMethod();
62 expect(foo.x == 3);
62 try expect(foo.x == 3);
6363 }
6464}
6565
6666test "type name of undefined" {
6767 const x = undefined;
68 expect(mem.eql(u8, @typeName(@TypeOf(x)), "(undefined)"));
68 try expect(mem.eql(u8, @typeName(@TypeOf(x)), "(undefined)"));
6969}
test/stage1/behavior/union.zig+142-142
......@@ -30,11 +30,11 @@ const array = [_]Value{
3030
3131test "unions embedded in aggregate types" {
3232 switch (array[1]) {
33 Value.Array => |arr| expect(arr[4] == 3),
33 Value.Array => |arr| try expect(arr[4] == 3),
3434 else => unreachable,
3535 }
3636 switch ((err catch unreachable).val1) {
37 Value.Int => |x| expect(x == 1234),
37 Value.Int => |x| try expect(x == 1234),
3838 else => unreachable,
3939 }
4040}
......@@ -46,18 +46,18 @@ const Foo = union {
4646
4747test "basic unions" {
4848 var foo = Foo{ .int = 1 };
49 expect(foo.int == 1);
49 try expect(foo.int == 1);
5050 foo = Foo{ .float = 12.34 };
51 expect(foo.float == 12.34);
51 try expect(foo.float == 12.34);
5252}
5353
5454test "comptime union field access" {
5555 comptime {
5656 var foo = Foo{ .int = 0 };
57 expect(foo.int == 0);
57 try expect(foo.int == 0);
5858
5959 foo = Foo{ .float = 42.42 };
60 expect(foo.float == 42.42);
60 try expect(foo.float == 42.42);
6161 }
6262}
6363
......@@ -65,10 +65,10 @@ test "init union with runtime value" {
6565 var foo: Foo = undefined;
6666
6767 setFloat(&foo, 12.34);
68 expect(foo.float == 12.34);
68 try expect(foo.float == 12.34);
6969
7070 setInt(&foo, 42);
71 expect(foo.int == 42);
71 try expect(foo.int == 42);
7272}
7373
7474fn setFloat(foo: *Foo, x: f64) void {
......@@ -86,9 +86,9 @@ const FooExtern = extern union {
8686
8787test "basic extern unions" {
8888 var foo = FooExtern{ .int = 1 };
89 expect(foo.int == 1);
89 try expect(foo.int == 1);
9090 foo.float = 12.34;
91 expect(foo.float == 12.34);
91 try expect(foo.float == 12.34);
9292}
9393
9494const Letter = enum {
......@@ -103,16 +103,16 @@ const Payload = union(Letter) {
103103};
104104
105105test "union with specified enum tag" {
106 doTest();
107 comptime doTest();
106 try doTest();
107 comptime try doTest();
108108}
109109
110fn doTest() void {
111 expect(bar(Payload{ .A = 1234 }) == -10);
110fn doTest() !void {
111 try expect((try bar(Payload{ .A = 1234 })) == -10);
112112}
113113
114fn bar(value: Payload) i32 {
115 expect(@as(Letter, value) == Letter.A);
114fn bar(value: Payload) !i32 {
115 try expect(@as(Letter, value) == Letter.A);
116116 return switch (value) {
117117 Payload.A => |x| return x - 1244,
118118 Payload.B => |x| if (x == 12.34) @as(i32, 20) else 21,
......@@ -128,8 +128,8 @@ const MultipleChoice = union(enum(u32)) {
128128};
129129test "simple union(enum(u32))" {
130130 var x = MultipleChoice.C;
131 expect(x == MultipleChoice.C);
132 expect(@enumToInt(@as(Tag(MultipleChoice), x)) == 60);
131 try expect(x == MultipleChoice.C);
132 try expect(@enumToInt(@as(Tag(MultipleChoice), x)) == 60);
133133}
134134
135135const MultipleChoice2 = union(enum(u32)) {
......@@ -145,14 +145,14 @@ const MultipleChoice2 = union(enum(u32)) {
145145};
146146
147147test "union(enum(u32)) with specified and unspecified tag values" {
148 comptime expect(Tag(Tag(MultipleChoice2)) == u32);
149 testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2{ .C = 123 });
150 comptime testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2{ .C = 123 });
148 comptime try expect(Tag(Tag(MultipleChoice2)) == u32);
149 try testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2{ .C = 123 });
150 comptime try testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2{ .C = 123 });
151151}
152152
153fn testEnumWithSpecifiedAndUnspecifiedTagValues(x: MultipleChoice2) void {
154 expect(@enumToInt(@as(Tag(MultipleChoice2), x)) == 60);
155 expect(1123 == switch (x) {
153fn testEnumWithSpecifiedAndUnspecifiedTagValues(x: MultipleChoice2) !void {
154 try expect(@enumToInt(@as(Tag(MultipleChoice2), x)) == 60);
155 try expect(1123 == switch (x) {
156156 MultipleChoice2.A => 1,
157157 MultipleChoice2.B => 2,
158158 MultipleChoice2.C => |v| @as(i32, 1000) + v,
......@@ -170,7 +170,7 @@ const ExternPtrOrInt = extern union {
170170 int: u64,
171171};
172172test "extern union size" {
173 comptime expect(@sizeOf(ExternPtrOrInt) == 8);
173 comptime try expect(@sizeOf(ExternPtrOrInt) == 8);
174174}
175175
176176const PackedPtrOrInt = packed union {
......@@ -178,14 +178,14 @@ const PackedPtrOrInt = packed union {
178178 int: u64,
179179};
180180test "extern union size" {
181 comptime expect(@sizeOf(PackedPtrOrInt) == 8);
181 comptime try expect(@sizeOf(PackedPtrOrInt) == 8);
182182}
183183
184184const ZeroBits = union {
185185 OnlyField: void,
186186};
187187test "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);
189189}
190190
191191const TheTag = enum {
......@@ -199,23 +199,23 @@ const TheUnion = union(TheTag) {
199199 C: i32,
200200};
201201test "union field access gives the enum values" {
202 expect(TheUnion.A == TheTag.A);
203 expect(TheUnion.B == TheTag.B);
204 expect(TheUnion.C == TheTag.C);
202 try expect(TheUnion.A == TheTag.A);
203 try expect(TheUnion.B == TheTag.B);
204 try expect(TheUnion.C == TheTag.C);
205205}
206206
207207test "cast union to tag type of union" {
208 testCastUnionToTag(TheUnion{ .B = 1234 });
209 comptime testCastUnionToTag(TheUnion{ .B = 1234 });
208 try testCastUnionToTag(TheUnion{ .B = 1234 });
209 comptime try testCastUnionToTag(TheUnion{ .B = 1234 });
210210}
211211
212fn testCastUnionToTag(x: TheUnion) void {
213 expect(@as(TheTag, x) == TheTag.B);
212fn testCastUnionToTag(x: TheUnion) !void {
213 try expect(@as(TheTag, x) == TheTag.B);
214214}
215215
216216test "cast tag type of union to union" {
217217 var x: Value2 = Letter2.B;
218 expect(@as(Letter2, x) == Letter2.B);
218 try expect(@as(Letter2, x) == Letter2.B);
219219}
220220const Letter2 = enum {
221221 A,
......@@ -230,11 +230,11 @@ const Value2 = union(Letter2) {
230230
231231test "implicit cast union to its tag type" {
232232 var x: Value2 = Letter2.B;
233 expect(x == Letter2.B);
234 giveMeLetterB(x);
233 try expect(x == Letter2.B);
234 try giveMeLetterB(x);
235235}
236fn giveMeLetterB(x: Letter2) void {
237 expect(x == Value2.B);
236fn giveMeLetterB(x: Letter2) !void {
237 try expect(x == Value2.B);
238238}
239239
240240pub const PackThis = union(enum) {
......@@ -243,11 +243,11 @@ pub const PackThis = union(enum) {
243243};
244244
245245test "constant packed union" {
246 testConstPackedUnion(&[_]PackThis{PackThis{ .StringLiteral = 1 }});
246 try testConstPackedUnion(&[_]PackThis{PackThis{ .StringLiteral = 1 }});
247247}
248248
249fn testConstPackedUnion(expected_tokens: []const PackThis) void {
250 expect(expected_tokens[0].StringLiteral == 1);
249fn testConstPackedUnion(expected_tokens: []const PackThis) !void {
250 try expect(expected_tokens[0].StringLiteral == 1);
251251}
252252
253253test "switch on union with only 1 field" {
......@@ -259,7 +259,7 @@ test "switch on union with only 1 field" {
259259 z = PartialInstWithPayload{ .Compiled = 1234 };
260260 switch (z) {
261261 PartialInstWithPayload.Compiled => |x| {
262 expect(x == 1234);
262 try expect(x == 1234);
263263 return;
264264 },
265265 }
......@@ -285,11 +285,11 @@ test "access a member of tagged union with conflicting enum tag name" {
285285 const B = void;
286286 };
287287
288 comptime expect(Bar.A == u8);
288 comptime try expect(Bar.A == u8);
289289}
290290
291291test "tagged union initialization with runtime void" {
292 expect(testTaggedUnionInit({}));
292 try expect(testTaggedUnionInit({}));
293293}
294294
295295const TaggedUnionWithAVoid = union(enum) {
......@@ -327,9 +327,9 @@ test "union with only 1 field casted to its enum type" {
327327
328328 var e = Expr{ .Literal = Literal{ .Bool = true } };
329329 const ExprTag = Tag(Expr);
330 comptime expect(Tag(ExprTag) == u0);
330 comptime try expect(Tag(ExprTag) == u0);
331331 var t = @as(ExprTag, e);
332 expect(t == Expr.Literal);
332 try expect(t == Expr.Literal);
333333}
334334
335335test "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
347347 };
348348
349349 var e = Expr{ .Literal = Literal{ .Bool = true } };
350 comptime expect(Tag(ExprTag) == comptime_int);
350 comptime try expect(Tag(ExprTag) == comptime_int);
351351 var t = @as(ExprTag, e);
352 expect(t == Expr.Literal);
353 expect(@enumToInt(t) == 33);
354 comptime expect(@enumToInt(t) == 33);
352 try expect(t == Expr.Literal);
353 try expect(@enumToInt(t) == 33);
354 comptime try expect(@enumToInt(t) == 33);
355355}
356356
357357test "@enumToInt works on unions" {
......@@ -364,9 +364,9 @@ test "@enumToInt works on unions" {
364364 const a = Bar{ .A = true };
365365 var b = Bar{ .B = undefined };
366366 var c = Bar.C;
367 expect(@enumToInt(a) == 0);
368 expect(@enumToInt(b) == 1);
369 expect(@enumToInt(c) == 2);
367 try expect(@enumToInt(a) == 0);
368 try expect(@enumToInt(b) == 1);
369 try expect(@enumToInt(c) == 2);
370370}
371371
372372const Attribute = union(enum) {
......@@ -393,23 +393,23 @@ test "comptime union field value equality" {
393393 const b1 = Setter(Attribute{ .B = 9 });
394394 const b2 = Setter(Attribute{ .B = 5 });
395395
396 expect(a0 == a0);
397 expect(a1 == a1);
398 expect(a0 == a2);
396 try expect(a0 == a0);
397 try expect(a1 == a1);
398 try expect(a0 == a2);
399399
400 expect(b0 == b0);
401 expect(b1 == b1);
402 expect(b0 == b2);
400 try expect(b0 == b0);
401 try expect(b1 == b1);
402 try expect(b0 == b2);
403403
404 expect(a0 != b0);
405 expect(a0 != a1);
406 expect(b0 != b1);
404 try expect(a0 != b0);
405 try expect(a0 != a1);
406 try expect(b0 != b1);
407407}
408408
409409test "return union init with void payload" {
410410 const S = struct {
411 fn entry() void {
412 expect(func().state == State.one);
411 fn entry() !void {
412 try expect(func().state == State.one);
413413 }
414414 const Outer = union(enum) {
415415 state: State,
......@@ -422,8 +422,8 @@ test "return union init with void payload" {
422422 return Outer{ .state = State{ .one = {} } };
423423 }
424424 };
425 S.entry();
426 comptime S.entry();
425 try S.entry();
426 comptime try S.entry();
427427}
428428
429429test "@unionInit can modify a union type" {
......@@ -435,14 +435,14 @@ test "@unionInit can modify a union type" {
435435 var value: UnionInitEnum = undefined;
436436
437437 value = @unionInit(UnionInitEnum, "Boolean", true);
438 expect(value.Boolean == true);
438 try expect(value.Boolean == true);
439439 value.Boolean = false;
440 expect(value.Boolean == false);
440 try expect(value.Boolean == false);
441441
442442 value = @unionInit(UnionInitEnum, "Byte", 2);
443 expect(value.Byte == 2);
443 try expect(value.Byte == 2);
444444 value.Byte = 3;
445 expect(value.Byte == 3);
445 try expect(value.Byte == 3);
446446}
447447
448448test "@unionInit can modify a pointer value" {
......@@ -455,10 +455,10 @@ test "@unionInit can modify a pointer value" {
455455 var value_ptr = &value;
456456
457457 value_ptr.* = @unionInit(UnionInitEnum, "Boolean", true);
458 expect(value.Boolean == true);
458 try expect(value.Boolean == true);
459459
460460 value_ptr.* = @unionInit(UnionInitEnum, "Byte", 2);
461 expect(value.Byte == 2);
461 try expect(value.Byte == 2);
462462}
463463
464464test "union no tag with struct member" {
......@@ -471,38 +471,38 @@ test "union no tag with struct member" {
471471 u.foo();
472472}
473473
474fn testComparison() void {
474fn testComparison() !void {
475475 var x = Payload{ .A = 42 };
476 expect(x == .A);
477 expect(x != .B);
478 expect(x != .C);
479 expect((x == .B) == false);
480 expect((x == .C) == false);
481 expect((x != .A) == false);
476 try expect(x == .A);
477 try expect(x != .B);
478 try expect(x != .C);
479 try expect((x == .B) == false);
480 try expect((x == .C) == false);
481 try expect((x != .A) == false);
482482}
483483
484484test "comparison between union and enum literal" {
485 testComparison();
486 comptime testComparison();
485 try testComparison();
486 comptime try testComparison();
487487}
488488
489489test "packed union generates correctly aligned LLVM type" {
490490 const U = packed union {
491 f1: fn () void,
491 f1: fn () error{TestUnexpectedResult}!void,
492492 f2: u32,
493493 };
494494 var foo = [_]U{
495495 U{ .f1 = doTest },
496496 U{ .f2 = 0 },
497497 };
498 foo[0].f1();
498 try foo[0].f1();
499499}
500500
501501test "union with one member defaults to u0 tag type" {
502502 const U0 = union(enum) {
503503 X: u32,
504504 };
505 comptime expect(Tag(Tag(U0)) == u0);
505 comptime try expect(Tag(Tag(U0)) == u0);
506506}
507507
508508test "union with comptime_int tag" {
......@@ -511,7 +511,7 @@ test "union with comptime_int tag" {
511511 Y: u16,
512512 Z: u8,
513513 };
514 comptime expect(Tag(Tag(Union)) == comptime_int);
514 comptime try expect(Tag(Tag(Union)) == comptime_int);
515515}
516516
517517test "extern union doesn't trigger field check at comptime" {
......@@ -521,7 +521,7 @@ test "extern union doesn't trigger field check at comptime" {
521521 };
522522
523523 const x = U{ .x = 0x55AAAA55 };
524 comptime expect(x.y == 0x55);
524 comptime try expect(x.y == 0x55);
525525}
526526
527527const Foo1 = union(enum) {
......@@ -535,7 +535,7 @@ test "global union with single field is correctly initialized" {
535535 glbl = Foo1{
536536 .f = @typeInfo(Foo1).Union.fields[0].field_type{ .x = 123 },
537537 };
538 expect(glbl.f.x == 123);
538 try expect(glbl.f.x == 123);
539539}
540540
541541pub const FooUnion = union(enum) {
......@@ -548,8 +548,8 @@ var glbl_array: [2]FooUnion = undefined;
548548test "initialize global array of union" {
549549 glbl_array[1] = FooUnion{ .U1 = 2 };
550550 glbl_array[0] = FooUnion{ .U0 = 1 };
551 expect(glbl_array[0].U0 == 1);
552 expect(glbl_array[1].U1 == 2);
551 try expect(glbl_array[0].U0 == 1);
552 try expect(glbl_array[1].U1 == 2);
553553}
554554
555555test "anonymous union literal syntax" {
......@@ -559,19 +559,19 @@ test "anonymous union literal syntax" {
559559 float: f64,
560560 };
561561
562 fn doTheTest() void {
562 fn doTheTest() !void {
563563 var i: Number = .{ .int = 42 };
564564 var f = makeNumber();
565 expect(i.int == 42);
566 expect(f.float == 12.34);
565 try expect(i.int == 42);
566 try expect(f.float == 12.34);
567567 }
568568
569569 fn makeNumber() Number {
570570 return .{ .float = 12.34 };
571571 }
572572 };
573 S.doTheTest();
574 comptime S.doTheTest();
573 try S.doTheTest();
574 comptime try S.doTheTest();
575575}
576576
577577test "update the tag value for zero-sized unions" {
......@@ -580,9 +580,9 @@ test "update the tag value for zero-sized unions" {
580580 U1: void,
581581 };
582582 var x = S{ .U0 = {} };
583 expect(x == .U0);
583 try expect(x == .U0);
584584 x = S{ .U1 = {} };
585 expect(x == .U1);
585 try expect(x == .U1);
586586}
587587
588588test "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
595595 const ArchTag = Tag(Arch);
596596
597 fn doTheTest() void {
597 fn doTheTest() !void {
598598 var x: ArchTag = getArch1();
599 expect(x == .One);
599 try expect(x == .One);
600600
601601 var y: ArchTag = getArch2();
602 expect(y == .Two);
602 try expect(y == .Two);
603603 }
604604
605605 pub fn getArch1() Arch {
......@@ -610,8 +610,8 @@ test "function call result coerces from tagged union to the tag" {
610610 return .{ .Two = 99 };
611611 }
612612 };
613 S.doTheTest();
614 comptime S.doTheTest();
613 try S.doTheTest();
614 comptime try S.doTheTest();
615615}
616616
617617test "0-sized extern union definition" {
......@@ -620,7 +620,7 @@ test "0-sized extern union definition" {
620620 const f = 1;
621621 };
622622
623 expect(U.f == 1);
623 try expect(U.f == 1);
624624}
625625
626626test "union initializer generates padding only if needed" {
......@@ -629,7 +629,7 @@ test "union initializer generates padding only if needed" {
629629 };
630630
631631 var v = U{ .A = 532 };
632 expect(v.A == 532);
632 try expect(v.A == 532);
633633}
634634
635635test "runtime tag name with single field" {
......@@ -638,7 +638,7 @@ test "runtime tag name with single field" {
638638 };
639639
640640 var v = U{ .A = 42 };
641 expect(std.mem.eql(u8, @tagName(v), "A"));
641 try expect(std.mem.eql(u8, @tagName(v), "A"));
642642}
643643
644644test "cast from anonymous struct to union" {
......@@ -648,7 +648,7 @@ test "cast from anonymous struct to union" {
648648 B: []const u8,
649649 C: void,
650650 };
651 fn doTheTest() void {
651 fn doTheTest() !void {
652652 var y: u32 = 42;
653653 const t0 = .{ .A = 123 };
654654 const t1 = .{ .B = "foo" };
......@@ -658,14 +658,14 @@ test "cast from anonymous struct to union" {
658658 var x1: U = t1;
659659 const x2: U = t2;
660660 var x3: U = t3;
661 expect(x0.A == 123);
662 expect(std.mem.eql(u8, x1.B, "foo"));
663 expect(x2 == .C);
664 expect(x3.A == y);
661 try expect(x0.A == 123);
662 try expect(std.mem.eql(u8, x1.B, "foo"));
663 try expect(x2 == .C);
664 try expect(x3.A == y);
665665 }
666666 };
667 S.doTheTest();
668 comptime S.doTheTest();
667 try S.doTheTest();
668 comptime try S.doTheTest();
669669}
670670
671671test "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" {
675675 B: []const u8,
676676 C: void,
677677 };
678 fn doTheTest() void {
678 fn doTheTest() !void {
679679 var y: u32 = 42;
680680 const t0 = &.{ .A = 123 };
681681 const t1 = &.{ .B = "foo" };
......@@ -685,14 +685,14 @@ test "cast from pointer to anonymous struct to pointer to union" {
685685 var x1: *const U = t1;
686686 const x2: *const U = t2;
687687 var x3: *const U = t3;
688 expect(x0.A == 123);
689 expect(std.mem.eql(u8, x1.B, "foo"));
690 expect(x2.* == .C);
691 expect(x3.A == y);
688 try expect(x0.A == 123);
689 try expect(std.mem.eql(u8, x1.B, "foo"));
690 try expect(x2.* == .C);
691 try expect(x3.A == y);
692692 }
693693 };
694 S.doTheTest();
695 comptime S.doTheTest();
694 try S.doTheTest();
695 comptime try S.doTheTest();
696696}
697697
698698test "method call on an empty union" {
......@@ -707,13 +707,13 @@ test "method call on an empty union" {
707707 }
708708 };
709709
710 fn doTheTest() void {
710 fn doTheTest() !void {
711711 var u = MyUnion{ .X1 = [0]u8{} };
712 expect(u.useIt());
712 try expect(u.useIt());
713713 }
714714 };
715 S.doTheTest();
716 comptime S.doTheTest();
715 try S.doTheTest();
716 comptime try S.doTheTest();
717717}
718718
719719test "switching on non exhaustive union" {
......@@ -727,16 +727,16 @@ test "switching on non exhaustive union" {
727727 a: i32,
728728 b: u32,
729729 };
730 fn doTheTest() void {
730 fn doTheTest() !void {
731731 var a = U{ .a = 2 };
732732 switch (a) {
733 .a => |val| expect(val == 2),
733 .a => |val| try expect(val == 2),
734734 .b => unreachable,
735735 }
736736 }
737737 };
738 S.doTheTest();
739 comptime S.doTheTest();
738 try S.doTheTest();
739 comptime try S.doTheTest();
740740}
741741
742742test "containers with single-field enums" {
......@@ -746,21 +746,21 @@ test "containers with single-field enums" {
746746 const C = struct { a: A };
747747 const D = struct { a: B };
748748
749 fn doTheTest() void {
749 fn doTheTest() !void {
750750 var array1 = [1]A{A{ .f1 = {} }};
751751 var array2 = [1]B{B{ .f1 = {} }};
752 expect(array1[0] == .f1);
753 expect(array2[0] == .f1);
752 try expect(array1[0] == .f1);
753 try expect(array2[0] == .f1);
754754
755755 var struct1 = C{ .a = A{ .f1 = {} } };
756756 var struct2 = D{ .a = B{ .f1 = {} } };
757 expect(struct1.a == .f1);
758 expect(struct2.a == .f1);
757 try expect(struct1.a == .f1);
758 try expect(struct2.a == .f1);
759759 }
760760 };
761761
762 S.doTheTest();
763 comptime S.doTheTest();
762 try S.doTheTest();
763 comptime try S.doTheTest();
764764}
765765
766766test "@unionInit on union w/ tag but no fields" {
......@@ -776,18 +776,18 @@ test "@unionInit on union w/ tag but no fields" {
776776 };
777777
778778 comptime {
779 expect(@sizeOf(Data) != 0);
779 try expect(@sizeOf(Data) != 0);
780780 }
781781
782 fn doTheTest() void {
782 fn doTheTest() !void {
783783 var data: Data = .{ .no_op = .{} };
784784 var o = Data.decode(&[_]u8{});
785 expectEqual(Type.no_op, o);
785 try expectEqual(Type.no_op, o);
786786 }
787787 };
788788
789 S.doTheTest();
790 comptime S.doTheTest();
789 try S.doTheTest();
790 comptime try S.doTheTest();
791791}
792792
793793test "union enum type gets a separate scope" {
......@@ -797,10 +797,10 @@ test "union enum type gets a separate scope" {
797797 const foo = 1;
798798 };
799799
800 fn doTheTest() void {
801 expect(!@hasDecl(Tag(U), "foo"));
800 fn doTheTest() !void {
801 try expect(!@hasDecl(Tag(U), "foo"));
802802 }
803803 };
804804
805 S.doTheTest();
805 try S.doTheTest();
806806}
test/stage1/behavior/usingnamespace.zig+3-3
......@@ -9,8 +9,8 @@ fn Foo(comptime T: type) type {
99test "usingnamespace inside a generic struct" {
1010 const std2 = Foo(std);
1111 const testing2 = Foo(std.testing);
12 std2.testing.expect(true);
13 testing2.expect(true);
12 try std2.testing.expect(true);
13 try testing2.expect(true);
1414}
1515
1616usingnamespace struct {
......@@ -18,5 +18,5 @@ usingnamespace struct {
1818};
1919
2020test "usingnamespace does not redeclare an imported variable" {
21 comptime std.testing.expect(foo == 42);
21 comptime try std.testing.expect(foo == 42);
2222}
test/stage1/behavior/var_args.zig+17-17
......@@ -12,9 +12,9 @@ fn add(args: anytype) i32 {
1212}
1313
1414test "add arbitrary args" {
15 expect(add(.{ @as(i32, 1), @as(i32, 2), @as(i32, 3), @as(i32, 4) }) == 10);
16 expect(add(.{@as(i32, 1234)}) == 1234);
17 expect(add(.{}) == 0);
15 try expect(add(.{ @as(i32, 1), @as(i32, 2), @as(i32, 3), @as(i32, 4) }) == 10);
16 try expect(add(.{@as(i32, 1234)}) == 1234);
17 try expect(add(.{}) == 0);
1818}
1919
2020fn readFirstVarArg(args: anytype) void {
......@@ -26,9 +26,9 @@ test "send void arg to var args" {
2626}
2727
2828test "pass args directly" {
29 expect(addSomeStuff(.{ @as(i32, 1), @as(i32, 2), @as(i32, 3), @as(i32, 4) }) == 10);
30 expect(addSomeStuff(.{@as(i32, 1234)}) == 1234);
31 expect(addSomeStuff(.{}) == 0);
29 try expect(addSomeStuff(.{ @as(i32, 1), @as(i32, 2), @as(i32, 3), @as(i32, 4) }) == 10);
30 try expect(addSomeStuff(.{@as(i32, 1234)}) == 1234);
31 try expect(addSomeStuff(.{}) == 0);
3232}
3333
3434fn addSomeStuff(args: anytype) i32 {
......@@ -36,23 +36,23 @@ fn addSomeStuff(args: anytype) i32 {
3636}
3737
3838test "runtime parameter before var args" {
39 expect(extraFn(10, .{}) == 0);
40 expect(extraFn(10, .{false}) == 1);
41 expect(extraFn(10, .{ false, true }) == 2);
39 try expect((try extraFn(10, .{})) == 0);
40 try expect((try extraFn(10, .{false})) == 1);
41 try expect((try extraFn(10, .{ false, true })) == 2);
4242
4343 comptime {
44 expect(extraFn(10, .{}) == 0);
45 expect(extraFn(10, .{false}) == 1);
46 expect(extraFn(10, .{ false, true }) == 2);
44 try expect((try extraFn(10, .{})) == 0);
45 try expect((try extraFn(10, .{false})) == 1);
46 try expect((try extraFn(10, .{ false, true })) == 2);
4747 }
4848}
4949
50fn extraFn(extra: u32, args: anytype) usize {
50fn extraFn(extra: u32, args: anytype) !usize {
5151 if (args.len >= 1) {
52 expect(args[0] == false);
52 try expect(args[0] == false);
5353 }
5454 if (args.len >= 2) {
55 expect(args[1] == true);
55 try expect(args[1] == true);
5656 }
5757 return args.len;
5858}
......@@ -70,8 +70,8 @@ fn foo2(args: anytype) bool {
7070}
7171
7272test "array of var args functions" {
73 expect(foos[0](.{}));
74 expect(!foos[1](.{}));
73 try expect(foos[0](.{}));
74 try expect(!foos[1](.{}));
7575}
7676
7777test "pass zero length array to var args param" {
test/stage1/behavior/vector.zig+280-280
......@@ -9,104 +9,104 @@ const Vector = std.meta.Vector;
99
1010test "implicit cast vector to array - bool" {
1111 const S = struct {
12 fn doTheTest() void {
12 fn doTheTest() !void {
1313 const a: Vector(4, bool) = [_]bool{ true, false, true, false };
1414 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 }));
1616 }
1717 };
18 S.doTheTest();
19 comptime S.doTheTest();
18 try S.doTheTest();
19 comptime try S.doTheTest();
2020}
2121
2222test "vector wrap operators" {
2323 const S = struct {
24 fn doTheTest() void {
24 fn doTheTest() !void {
2525 var v: Vector(4, i32) = [4]i32{ 2147483647, -2, 30, 40 };
2626 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 }));
28 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 }));
27 try expect(mem.eql(i32, &@as([4]i32, v +% x), &[4]i32{ -2147483648, 2147483645, 33, 44 }));
28 try expect(mem.eql(i32, &@as([4]i32, v -% x), &[4]i32{ 2147483646, 2147483647, 27, 36 }));
29 try expect(mem.eql(i32, &@as([4]i32, v *% x), &[4]i32{ 2147483647, 2, 90, 160 }));
3030 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 }));
3232 }
3333 };
34 S.doTheTest();
35 comptime S.doTheTest();
34 try S.doTheTest();
35 comptime try S.doTheTest();
3636}
3737
3838test "vector bin compares with mem.eql" {
3939 const S = struct {
40 fn doTheTest() void {
40 fn doTheTest() !void {
4141 var v: Vector(4, i32) = [4]i32{ 2147483647, -2, 30, 40 };
4242 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 }));
44 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 }));
46 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 }));
48 expect(mem.eql(bool, &@as([4]bool, v >= x), &[4]bool{ true, false, true, true }));
43 try expect(mem.eql(bool, &@as([4]bool, v == x), &[4]bool{ false, false, true, false }));
44 try expect(mem.eql(bool, &@as([4]bool, v != x), &[4]bool{ true, true, false, true }));
45 try expect(mem.eql(bool, &@as([4]bool, v < x), &[4]bool{ false, true, false, false }));
46 try expect(mem.eql(bool, &@as([4]bool, v > x), &[4]bool{ true, false, false, true }));
47 try expect(mem.eql(bool, &@as([4]bool, v <= x), &[4]bool{ false, true, true, false }));
48 try expect(mem.eql(bool, &@as([4]bool, v >= x), &[4]bool{ true, false, true, true }));
4949 }
5050 };
51 S.doTheTest();
52 comptime S.doTheTest();
51 try S.doTheTest();
52 comptime try S.doTheTest();
5353}
5454
5555test "vector int operators" {
5656 const S = struct {
57 fn doTheTest() void {
57 fn doTheTest() !void {
5858 var v: Vector(4, i32) = [4]i32{ 10, 20, 30, 40 };
5959 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 }));
61 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 }));
63 expect(mem.eql(i32, &@as([4]i32, -v), &[4]i32{ -10, -20, -30, -40 }));
60 try expect(mem.eql(i32, &@as([4]i32, v + x), &[4]i32{ 11, 22, 33, 44 }));
61 try expect(mem.eql(i32, &@as([4]i32, v - x), &[4]i32{ 9, 18, 27, 36 }));
62 try expect(mem.eql(i32, &@as([4]i32, v * x), &[4]i32{ 10, 40, 90, 160 }));
63 try expect(mem.eql(i32, &@as([4]i32, -v), &[4]i32{ -10, -20, -30, -40 }));
6464 }
6565 };
66 S.doTheTest();
67 comptime S.doTheTest();
66 try S.doTheTest();
67 comptime try S.doTheTest();
6868}
6969
7070test "vector float operators" {
7171 const S = struct {
72 fn doTheTest() void {
72 fn doTheTest() !void {
7373 var v: Vector(4, f32) = [4]f32{ 10, 20, 30, 40 };
7474 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 }));
76 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 }));
78 expect(mem.eql(f32, &@as([4]f32, -x), &[4]f32{ -1, -2, -3, -4 }));
75 try expect(mem.eql(f32, &@as([4]f32, v + x), &[4]f32{ 11, 22, 33, 44 }));
76 try expect(mem.eql(f32, &@as([4]f32, v - x), &[4]f32{ 9, 18, 27, 36 }));
77 try expect(mem.eql(f32, &@as([4]f32, v * x), &[4]f32{ 10, 40, 90, 160 }));
78 try expect(mem.eql(f32, &@as([4]f32, -x), &[4]f32{ -1, -2, -3, -4 }));
7979 }
8080 };
81 S.doTheTest();
82 comptime S.doTheTest();
81 try S.doTheTest();
82 comptime try S.doTheTest();
8383}
8484
8585test "vector bit operators" {
8686 const S = struct {
87 fn doTheTest() void {
87 fn doTheTest() !void {
8888 var v: Vector(4, u8) = [4]u8{ 0b10101010, 0b10101010, 0b10101010, 0b10101010 };
8989 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 }));
91 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 }));
90 try expect(mem.eql(u8, &@as([4]u8, v ^ x), &[4]u8{ 0b01011010, 0b10100101, 0b00000000, 0b11111111 }));
91 try expect(mem.eql(u8, &@as([4]u8, v | x), &[4]u8{ 0b11111010, 0b10101111, 0b10101010, 0b11111111 }));
92 try expect(mem.eql(u8, &@as([4]u8, v & x), &[4]u8{ 0b10100000, 0b00001010, 0b10101010, 0b00000000 }));
9393 }
9494 };
95 S.doTheTest();
96 comptime S.doTheTest();
95 try S.doTheTest();
96 comptime try S.doTheTest();
9797}
9898
9999test "implicit cast vector to array" {
100100 const S = struct {
101 fn doTheTest() void {
101 fn doTheTest() !void {
102102 var a: Vector(4, i32) = [_]i32{ 1, 2, 3, 4 };
103103 var result_array: [4]i32 = a;
104104 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 }));
106106 }
107107 };
108 S.doTheTest();
109 comptime S.doTheTest();
108 try S.doTheTest();
109 comptime try S.doTheTest();
110110}
111111
112112test "array to vector" {
......@@ -120,141 +120,141 @@ test "vector casts of sizes not divisable by 8" {
120120 if (std.Target.current.os.tag == .dragonfly) return error.SkipZigTest;
121121
122122 const S = struct {
123 fn doTheTest() void {
123 fn doTheTest() !void {
124124 {
125125 var v: Vector(4, u3) = [4]u3{ 5, 2, 3, 0 };
126126 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)));
128128 }
129129 {
130130 var v: Vector(4, u2) = [4]u2{ 1, 2, 3, 0 };
131131 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)));
133133 }
134134 {
135135 var v: Vector(4, u1) = [4]u1{ 1, 0, 1, 0 };
136136 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)));
138138 }
139139 {
140140 var v: Vector(4, bool) = [4]bool{ false, false, true, false };
141141 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)));
143143 }
144144 }
145145 };
146 S.doTheTest();
147 comptime S.doTheTest();
146 try S.doTheTest();
147 comptime try S.doTheTest();
148148}
149149
150150test "vector @splat" {
151151 const S = struct {
152 fn testForT(comptime N: comptime_int, v: anytype) void {
152 fn testForT(comptime N: comptime_int, v: anytype) !void {
153153 const T = @TypeOf(v);
154154 var vec = @splat(N, v);
155 expectEqual(Vector(N, T), @TypeOf(vec));
155 try expectEqual(Vector(N, T), @TypeOf(vec));
156156 var as_array = @as([N]T, vec);
157 for (as_array) |elem| expectEqual(v, elem);
157 for (as_array) |elem| try expectEqual(v, elem);
158158 }
159 fn doTheTest() void {
159 fn doTheTest() !void {
160160 // Splats with multiple-of-8 bit types that fill a 128bit vector.
161 testForT(16, @as(u8, 0xEE));
162 testForT(8, @as(u16, 0xBEEF));
163 testForT(4, @as(u32, 0xDEADBEEF));
164 testForT(2, @as(u64, 0xCAFEF00DDEADBEEF));
161 try testForT(16, @as(u8, 0xEE));
162 try testForT(8, @as(u16, 0xBEEF));
163 try testForT(4, @as(u32, 0xDEADBEEF));
164 try testForT(2, @as(u64, 0xCAFEF00DDEADBEEF));
165165
166 testForT(8, @as(f16, 3.1415));
167 testForT(4, @as(f32, 3.1415));
168 testForT(2, @as(f64, 3.1415));
166 try testForT(8, @as(f16, 3.1415));
167 try testForT(4, @as(f32, 3.1415));
168 try testForT(2, @as(f64, 3.1415));
169169
170170 // Same but fill more than 128 bits.
171 testForT(16 * 2, @as(u8, 0xEE));
172 testForT(8 * 2, @as(u16, 0xBEEF));
173 testForT(4 * 2, @as(u32, 0xDEADBEEF));
174 testForT(2 * 2, @as(u64, 0xCAFEF00DDEADBEEF));
175
176 testForT(8 * 2, @as(f16, 3.1415));
177 testForT(4 * 2, @as(f32, 3.1415));
178 testForT(2 * 2, @as(f64, 3.1415));
171 try testForT(16 * 2, @as(u8, 0xEE));
172 try testForT(8 * 2, @as(u16, 0xBEEF));
173 try testForT(4 * 2, @as(u32, 0xDEADBEEF));
174 try testForT(2 * 2, @as(u64, 0xCAFEF00DDEADBEEF));
175
176 try testForT(8 * 2, @as(f16, 3.1415));
177 try testForT(4 * 2, @as(f32, 3.1415));
178 try testForT(2 * 2, @as(f64, 3.1415));
179179 }
180180 };
181 S.doTheTest();
182 comptime S.doTheTest();
181 try S.doTheTest();
182 comptime try S.doTheTest();
183183}
184184
185185test "load vector elements via comptime index" {
186186 const S = struct {
187 fn doTheTest() void {
187 fn doTheTest() !void {
188188 var v: Vector(4, i32) = [_]i32{ 1, 2, 3, undefined };
189 expect(v[0] == 1);
190 expect(v[1] == 2);
191 expect(loadv(&v[2]) == 3);
189 try expect(v[0] == 1);
190 try expect(v[1] == 2);
191 try expect(loadv(&v[2]) == 3);
192192 }
193193 fn loadv(ptr: anytype) i32 {
194194 return ptr.*;
195195 }
196196 };
197197
198 S.doTheTest();
199 comptime S.doTheTest();
198 try S.doTheTest();
199 comptime try S.doTheTest();
200200}
201201
202202test "store vector elements via comptime index" {
203203 const S = struct {
204 fn doTheTest() void {
204 fn doTheTest() !void {
205205 var v: Vector(4, i32) = [_]i32{ 1, 5, 3, undefined };
206206
207207 v[2] = 42;
208 expect(v[1] == 5);
208 try expect(v[1] == 5);
209209 v[3] = -364;
210 expect(v[2] == 42);
211 expect(-364 == v[3]);
210 try expect(v[2] == 42);
211 try expect(-364 == v[3]);
212212
213213 storev(&v[0], 100);
214 expect(v[0] == 100);
214 try expect(v[0] == 100);
215215 }
216216 fn storev(ptr: anytype, x: i32) void {
217217 ptr.* = x;
218218 }
219219 };
220220
221 S.doTheTest();
222 comptime S.doTheTest();
221 try S.doTheTest();
222 comptime try S.doTheTest();
223223}
224224
225225test "load vector elements via runtime index" {
226226 const S = struct {
227 fn doTheTest() void {
227 fn doTheTest() !void {
228228 var v: Vector(4, i32) = [_]i32{ 1, 2, 3, undefined };
229229 var i: u32 = 0;
230 expect(v[i] == 1);
230 try expect(v[i] == 1);
231231 i += 1;
232 expect(v[i] == 2);
232 try expect(v[i] == 2);
233233 i += 1;
234 expect(v[i] == 3);
234 try expect(v[i] == 3);
235235 }
236236 };
237237
238 S.doTheTest();
239 comptime S.doTheTest();
238 try S.doTheTest();
239 comptime try S.doTheTest();
240240}
241241
242242test "store vector elements via runtime index" {
243243 const S = struct {
244 fn doTheTest() void {
244 fn doTheTest() !void {
245245 var v: Vector(4, i32) = [_]i32{ 1, 5, 3, undefined };
246246 var i: u32 = 2;
247247 v[i] = 1;
248 expect(v[1] == 5);
249 expect(v[2] == 1);
248 try expect(v[1] == 5);
249 try expect(v[2] == 1);
250250 i += 1;
251251 v[i] = -364;
252 expect(-364 == v[3]);
252 try expect(-364 == v[3]);
253253 }
254254 };
255255
256 S.doTheTest();
257 comptime S.doTheTest();
256 try S.doTheTest();
257 comptime try S.doTheTest();
258258}
259259
260260test "initialize vector which is a struct field" {
......@@ -263,155 +263,155 @@ test "initialize vector which is a struct field" {
263263 };
264264
265265 const S = struct {
266 fn doTheTest() void {
266 fn doTheTest() !void {
267267 var foo = Vec4Obj{
268268 .data = [_]f32{ 1, 2, 3, 4 },
269269 };
270270 }
271271 };
272 S.doTheTest();
273 comptime S.doTheTest();
272 try S.doTheTest();
273 comptime try S.doTheTest();
274274}
275275
276276test "vector comparison operators" {
277277 const S = struct {
278 fn doTheTest() void {
278 fn doTheTest() !void {
279279 {
280280 const v1: Vector(4, bool) = [_]bool{ true, false, true, false };
281281 const v2: Vector(4, bool) = [_]bool{ false, true, false, true };
282 expectEqual(@splat(4, true), v1 == v1);
283 expectEqual(@splat(4, false), v1 == v2);
284 expectEqual(@splat(4, true), v1 != v2);
285 expectEqual(@splat(4, false), v2 != v2);
282 try expectEqual(@splat(4, true), v1 == v1);
283 try expectEqual(@splat(4, false), v1 == v2);
284 try expectEqual(@splat(4, true), v1 != v2);
285 try expectEqual(@splat(4, false), v2 != v2);
286286 }
287287 {
288288 const v1 = @splat(4, @as(u32, 0xc0ffeeee));
289289 const v2: Vector(4, c_uint) = v1;
290290 const v3 = @splat(4, @as(u32, 0xdeadbeef));
291 expectEqual(@splat(4, true), v1 == v2);
292 expectEqual(@splat(4, false), v1 == v3);
293 expectEqual(@splat(4, true), v1 != v3);
294 expectEqual(@splat(4, false), v1 != v2);
291 try expectEqual(@splat(4, true), v1 == v2);
292 try expectEqual(@splat(4, false), v1 == v3);
293 try expectEqual(@splat(4, true), v1 != v3);
294 try expectEqual(@splat(4, false), v1 != v2);
295295 }
296296 {
297297 // Comptime-known LHS/RHS
298298 var v1: @Vector(4, u32) = [_]u32{ 2, 1, 2, 1 };
299299 const v2 = @splat(4, @as(u32, 2));
300300 const v3: @Vector(4, bool) = [_]bool{ true, false, true, false };
301 expectEqual(v3, v1 == v2);
302 expectEqual(v3, v2 == v1);
301 try expectEqual(v3, v1 == v2);
302 try expectEqual(v3, v2 == v1);
303303 }
304304 }
305305 };
306 S.doTheTest();
307 comptime S.doTheTest();
306 try S.doTheTest();
307 comptime try S.doTheTest();
308308}
309309
310310test "vector division operators" {
311311 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 {
313313 if (!comptime std.meta.trait.isSignedInt(T)) {
314314 const d0 = x / y;
315315 for (@as([4]T, d0)) |v, i| {
316 expectEqual(x[i] / y[i], v);
316 try expectEqual(x[i] / y[i], v);
317317 }
318318 }
319319 const d1 = @divExact(x, y);
320320 for (@as([4]T, d1)) |v, i| {
321 expectEqual(@divExact(x[i], y[i]), v);
321 try expectEqual(@divExact(x[i], y[i]), v);
322322 }
323323 const d2 = @divFloor(x, y);
324324 for (@as([4]T, d2)) |v, i| {
325 expectEqual(@divFloor(x[i], y[i]), v);
325 try expectEqual(@divFloor(x[i], y[i]), v);
326326 }
327327 const d3 = @divTrunc(x, y);
328328 for (@as([4]T, d3)) |v, i| {
329 expectEqual(@divTrunc(x[i], y[i]), v);
329 try expectEqual(@divTrunc(x[i], y[i]), v);
330330 }
331331 }
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 {
334334 if ((!comptime std.meta.trait.isSignedInt(T)) and @typeInfo(T) != .Float) {
335335 const r0 = x % y;
336336 for (@as([4]T, r0)) |v, i| {
337 expectEqual(x[i] % y[i], v);
337 try expectEqual(x[i] % y[i], v);
338338 }
339339 }
340340 const r1 = @mod(x, y);
341341 for (@as([4]T, r1)) |v, i| {
342 expectEqual(@mod(x[i], y[i]), v);
342 try expectEqual(@mod(x[i], y[i]), v);
343343 }
344344 const r2 = @rem(x, y);
345345 for (@as([4]T, r2)) |v, i| {
346 expectEqual(@rem(x[i], y[i]), v);
346 try expectEqual(@rem(x[i], y[i]), v);
347347 }
348348 }
349349
350 fn doTheTest() void {
350 fn doTheTest() !void {
351351 // https://github.com/ziglang/zig/issues/4952
352352 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 });
354354 }
355355
356 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 });
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 try doTheTestDiv(f64, [4]f64{ 4.0, -4.0, 4.0, -4.0 }, [4]f64{ 1.0, 2.0, -1.0, -2.0 });
358358
359359 // https://github.com/ziglang/zig/issues/4952
360360 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 });
362362 }
363 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 });
365
366 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 });
368 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 });
370
371 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 });
373 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 });
375
376 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 });
378 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 });
380
381 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 });
383 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 });
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 try doTheTestMod(f64, [4]f64{ 4.0, -4.0, 4.0, -4.0 }, [4]f64{ 1.0, 2.0, 0.5, 3.0 });
365
366 try doTheTestDiv(i8, [4]i8{ 4, -4, 4, -4 }, [4]i8{ 1, 2, -1, -2 });
367 try doTheTestDiv(i16, [4]i16{ 4, -4, 4, -4 }, [4]i16{ 1, 2, -1, -2 });
368 try doTheTestDiv(i32, [4]i32{ 4, -4, 4, -4 }, [4]i32{ 1, 2, -1, -2 });
369 try doTheTestDiv(i64, [4]i64{ 4, -4, 4, -4 }, [4]i64{ 1, 2, -1, -2 });
370
371 try doTheTestMod(i8, [4]i8{ 4, -4, 4, -4 }, [4]i8{ 1, 2, 4, 8 });
372 try doTheTestMod(i16, [4]i16{ 4, -4, 4, -4 }, [4]i16{ 1, 2, 4, 8 });
373 try doTheTestMod(i32, [4]i32{ 4, -4, 4, -4 }, [4]i32{ 1, 2, 4, 8 });
374 try doTheTestMod(i64, [4]i64{ 4, -4, 4, -4 }, [4]i64{ 1, 2, 4, 8 });
375
376 try doTheTestDiv(u8, [4]u8{ 1, 2, 4, 8 }, [4]u8{ 1, 1, 2, 4 });
377 try doTheTestDiv(u16, [4]u16{ 1, 2, 4, 8 }, [4]u16{ 1, 1, 2, 4 });
378 try doTheTestDiv(u32, [4]u32{ 1, 2, 4, 8 }, [4]u32{ 1, 1, 2, 4 });
379 try doTheTestDiv(u64, [4]u64{ 1, 2, 4, 8 }, [4]u64{ 1, 1, 2, 4 });
380
381 try doTheTestMod(u8, [4]u8{ 1, 2, 4, 8 }, [4]u8{ 1, 1, 2, 4 });
382 try doTheTestMod(u16, [4]u16{ 1, 2, 4, 8 }, [4]u16{ 1, 1, 2, 4 });
383 try doTheTestMod(u32, [4]u32{ 1, 2, 4, 8 }, [4]u32{ 1, 1, 2, 4 });
384 try doTheTestMod(u64, [4]u64{ 1, 2, 4, 8 }, [4]u64{ 1, 1, 2, 4 });
385385 }
386386 };
387387
388 S.doTheTest();
389 comptime S.doTheTest();
388 try S.doTheTest();
389 comptime try S.doTheTest();
390390}
391391
392392test "vector bitwise not operator" {
393393 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 {
395395 var y = ~x;
396396 for (@as([4]T, y)) |v, i| {
397 expectEqual(~x[i], v);
397 try expectEqual(~x[i], v);
398398 }
399399 }
400 fn doTheTest() void {
401 doTheTestNot(u8, [_]u8{ 0, 2, 4, 255 });
402 doTheTestNot(u16, [_]u16{ 0, 2, 4, 255 });
403 doTheTestNot(u32, [_]u32{ 0, 2, 4, 255 });
404 doTheTestNot(u64, [_]u64{ 0, 2, 4, 255 });
405
406 doTheTestNot(u8, [_]u8{ 0, 2, 4, 255 });
407 doTheTestNot(u16, [_]u16{ 0, 2, 4, 255 });
408 doTheTestNot(u32, [_]u32{ 0, 2, 4, 255 });
409 doTheTestNot(u64, [_]u64{ 0, 2, 4, 255 });
400 fn doTheTest() !void {
401 try doTheTestNot(u8, [_]u8{ 0, 2, 4, 255 });
402 try doTheTestNot(u16, [_]u16{ 0, 2, 4, 255 });
403 try doTheTestNot(u32, [_]u32{ 0, 2, 4, 255 });
404 try doTheTestNot(u64, [_]u64{ 0, 2, 4, 255 });
405
406 try doTheTestNot(u8, [_]u8{ 0, 2, 4, 255 });
407 try doTheTestNot(u16, [_]u16{ 0, 2, 4, 255 });
408 try doTheTestNot(u32, [_]u32{ 0, 2, 4, 255 });
409 try doTheTestNot(u64, [_]u64{ 0, 2, 4, 255 });
410410 }
411411 };
412412
413 S.doTheTest();
414 comptime S.doTheTest();
413 try S.doTheTest();
414 comptime try S.doTheTest();
415415}
416416
417417test "vector shift operators" {
......@@ -419,7 +419,7 @@ test "vector shift operators" {
419419 if (builtin.os.tag == .wasi) return error.SkipZigTest;
420420
421421 const S = struct {
422 fn doTheTestShift(x: anytype, y: anytype) void {
422 fn doTheTestShift(x: anytype, y: anytype) !void {
423423 const N = @typeInfo(@TypeOf(x)).Array.len;
424424 const TX = @typeInfo(@TypeOf(x)).Array.child;
425425 const TY = @typeInfo(@TypeOf(y)).Array.child;
......@@ -429,14 +429,14 @@ test "vector shift operators" {
429429
430430 var z0 = xv >> yv;
431431 for (@as([N]TX, z0)) |v, i| {
432 expectEqual(x[i] >> y[i], v);
432 try expectEqual(x[i] >> y[i], v);
433433 }
434434 var z1 = xv << yv;
435435 for (@as([N]TX, z1)) |v, i| {
436 expectEqual(x[i] << y[i], v);
436 try expectEqual(x[i] << y[i], v);
437437 }
438438 }
439 fn doTheTestShiftExact(x: anytype, y: anytype, dir: enum { Left, Right }) void {
439 fn doTheTestShiftExact(x: anytype, y: anytype, dir: enum { Left, Right }) !void {
440440 const N = @typeInfo(@TypeOf(x)).Array.len;
441441 const TX = @typeInfo(@TypeOf(x)).Array.child;
442442 const TY = @typeInfo(@TypeOf(y)).Array.child;
......@@ -447,33 +447,33 @@ test "vector shift operators" {
447447 var z = if (dir == .Left) @shlExact(xv, yv) else @shrExact(xv, yv);
448448 for (@as([N]TX, z)) |v, i| {
449449 const check = if (dir == .Left) x[i] << y[i] else x[i] >> y[i];
450 expectEqual(check, v);
450 try expectEqual(check, v);
451451 }
452452 }
453 fn doTheTest() void {
454 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 });
456 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 });
458 doTheTestShift([_]u64{ 0xfe, math.maxInt(u64) }, [_]u6{ 0, 63 });
459
460 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 });
462 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 });
464 doTheTestShift([_]i64{ 0xfe, math.maxInt(i64) }, [_]u6{ 0, 63 });
465
466 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);
468 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);
470 doTheTestShiftExact([_]u64{ 1 << 63, 1 }, [_]u6{ 63, 0 }, .Right);
471
472 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);
474 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);
476 doTheTestShiftExact([_]u64{ 1 << 63, 1 }, [_]u6{ 0, 63 }, .Left);
453 fn doTheTest() !void {
454 try doTheTestShift([_]u8{ 0, 2, 4, math.maxInt(u8) }, [_]u3{ 2, 0, 2, 7 });
455 try doTheTestShift([_]u16{ 0, 2, 4, math.maxInt(u16) }, [_]u4{ 2, 0, 2, 15 });
456 try doTheTestShift([_]u24{ 0, 2, 4, math.maxInt(u24) }, [_]u5{ 2, 0, 2, 23 });
457 try doTheTestShift([_]u32{ 0, 2, 4, math.maxInt(u32) }, [_]u5{ 2, 0, 2, 31 });
458 try doTheTestShift([_]u64{ 0xfe, math.maxInt(u64) }, [_]u6{ 0, 63 });
459
460 try doTheTestShift([_]i8{ 0, 2, 4, math.maxInt(i8) }, [_]u3{ 2, 0, 2, 7 });
461 try doTheTestShift([_]i16{ 0, 2, 4, math.maxInt(i16) }, [_]u4{ 2, 0, 2, 7 });
462 try doTheTestShift([_]i24{ 0, 2, 4, math.maxInt(i24) }, [_]u5{ 2, 0, 2, 7 });
463 try doTheTestShift([_]i32{ 0, 2, 4, math.maxInt(i32) }, [_]u5{ 2, 0, 2, 7 });
464 try doTheTestShift([_]i64{ 0xfe, math.maxInt(i64) }, [_]u6{ 0, 63 });
465
466 try doTheTestShiftExact([_]u8{ 0, 1, 1 << 7, math.maxInt(u8) ^ 1 }, [_]u3{ 4, 0, 7, 1 }, .Right);
467 try doTheTestShiftExact([_]u16{ 0, 1, 1 << 15, math.maxInt(u16) ^ 1 }, [_]u4{ 4, 0, 15, 1 }, .Right);
468 try doTheTestShiftExact([_]u24{ 0, 1, 1 << 23, math.maxInt(u24) ^ 1 }, [_]u5{ 4, 0, 23, 1 }, .Right);
469 try doTheTestShiftExact([_]u32{ 0, 1, 1 << 31, math.maxInt(u32) ^ 1 }, [_]u5{ 4, 0, 31, 1 }, .Right);
470 try doTheTestShiftExact([_]u64{ 1 << 63, 1 }, [_]u6{ 63, 0 }, .Right);
471
472 try doTheTestShiftExact([_]u8{ 0, 1, 1, math.maxInt(u8) ^ (1 << 7) }, [_]u3{ 4, 0, 7, 1 }, .Left);
473 try doTheTestShiftExact([_]u16{ 0, 1, 1, math.maxInt(u16) ^ (1 << 15) }, [_]u4{ 4, 0, 15, 1 }, .Left);
474 try doTheTestShiftExact([_]u24{ 0, 1, 1, math.maxInt(u24) ^ (1 << 23) }, [_]u5{ 4, 0, 23, 1 }, .Left);
475 try doTheTestShiftExact([_]u32{ 0, 1, 1, math.maxInt(u32) ^ (1 << 31) }, [_]u5{ 4, 0, 31, 1 }, .Left);
476 try doTheTestShiftExact([_]u64{ 1 << 63, 1 }, [_]u6{ 0, 63 }, .Left);
477477 }
478478 };
479479
......@@ -500,19 +500,19 @@ test "vector shift operators" {
500500 else => {},
501501 }
502502
503 S.doTheTest();
504 comptime S.doTheTest();
503 try S.doTheTest();
504 comptime try S.doTheTest();
505505}
506506
507507test "vector reduce operation" {
508508 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 {
510510 const N = @typeInfo(@TypeOf(x)).Array.len;
511511 const TX = @typeInfo(@TypeOf(x)).Array.child;
512512
513513 var r = @reduce(op, @as(Vector(N, TX), x));
514514 switch (@typeInfo(TX)) {
515 .Int, .Bool => expectEqual(expected, r),
515 .Int, .Bool => try expectEqual(expected, r),
516516 .Float => {
517517 const expected_nan = math.isNan(expected);
518518 const got_nan = math.isNan(r);
......@@ -521,120 +521,120 @@ test "vector reduce operation" {
521521 // Do this check explicitly as two NaN values are never
522522 // equal.
523523 } else {
524 expectApproxEqRel(expected, r, math.sqrt(math.epsilon(TX)));
524 try expectApproxEqRel(expected, r, math.sqrt(math.epsilon(TX)));
525525 }
526526 },
527527 else => unreachable,
528528 }
529529 }
530 fn doTheTest() void {
531 doTheTestReduce(.Add, [4]i16{ -9, -99, -999, -9999 }, @as(i32, -11106));
532 doTheTestReduce(.Add, [4]u16{ 9, 99, 999, 9999 }, @as(u32, 11106));
533 doTheTestReduce(.Add, [4]i32{ -9, -99, -999, -9999 }, @as(i32, -11106));
534 doTheTestReduce(.Add, [4]u32{ 9, 99, 999, 9999 }, @as(u32, 11106));
535 doTheTestReduce(.Add, [4]i64{ -9, -99, -999, -9999 }, @as(i64, -11106));
536 doTheTestReduce(.Add, [4]u64{ 9, 99, 999, 9999 }, @as(u64, 11106));
537 doTheTestReduce(.Add, [4]i128{ -9, -99, -999, -9999 }, @as(i128, -11106));
538 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));
540 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));
542
543 doTheTestReduce(.And, [4]bool{ true, false, true, true }, @as(bool, false));
544 doTheTestReduce(.And, [4]u1{ 1, 0, 1, 1 }, @as(u1, 0));
545 doTheTestReduce(.And, [4]u16{ 0xffff, 0xff55, 0xaaff, 0x1010 }, @as(u16, 0x10));
546 doTheTestReduce(.And, [4]u32{ 0xffffffff, 0xffff5555, 0xaaaaffff, 0x10101010 }, @as(u32, 0x1010));
547 doTheTestReduce(.And, [4]u64{ 0xffffffff, 0xffff5555, 0xaaaaffff, 0x10101010 }, @as(u64, 0x1010));
548
549 doTheTestReduce(.Min, [4]i16{ -1, 2, 3, 4 }, @as(i16, -1));
550 doTheTestReduce(.Min, [4]u16{ 1, 2, 3, 4 }, @as(u16, 1));
551 doTheTestReduce(.Min, [4]i32{ 1234567, -386, 0, 3 }, @as(i32, -386));
552 doTheTestReduce(.Min, [4]u32{ 99, 9999, 9, 99999 }, @as(u32, 9));
530 fn doTheTest() !void {
531 try doTheTestReduce(.Add, [4]i16{ -9, -99, -999, -9999 }, @as(i32, -11106));
532 try doTheTestReduce(.Add, [4]u16{ 9, 99, 999, 9999 }, @as(u32, 11106));
533 try doTheTestReduce(.Add, [4]i32{ -9, -99, -999, -9999 }, @as(i32, -11106));
534 try doTheTestReduce(.Add, [4]u32{ 9, 99, 999, 9999 }, @as(u32, 11106));
535 try doTheTestReduce(.Add, [4]i64{ -9, -99, -999, -9999 }, @as(i64, -11106));
536 try doTheTestReduce(.Add, [4]u64{ 9, 99, 999, 9999 }, @as(u64, 11106));
537 try doTheTestReduce(.Add, [4]i128{ -9, -99, -999, -9999 }, @as(i128, -11106));
538 try doTheTestReduce(.Add, [4]u128{ 9, 99, 999, 9999 }, @as(u128, 11106));
539 try doTheTestReduce(.Add, [4]f16{ -1.9, 5.1, -60.3, 100.0 }, @as(f16, 42.9));
540 try doTheTestReduce(.Add, [4]f32{ -1.9, 5.1, -60.3, 100.0 }, @as(f32, 42.9));
541 try doTheTestReduce(.Add, [4]f64{ -1.9, 5.1, -60.3, 100.0 }, @as(f64, 42.9));
542
543 try doTheTestReduce(.And, [4]bool{ true, false, true, true }, @as(bool, false));
544 try doTheTestReduce(.And, [4]u1{ 1, 0, 1, 1 }, @as(u1, 0));
545 try doTheTestReduce(.And, [4]u16{ 0xffff, 0xff55, 0xaaff, 0x1010 }, @as(u16, 0x10));
546 try doTheTestReduce(.And, [4]u32{ 0xffffffff, 0xffff5555, 0xaaaaffff, 0x10101010 }, @as(u32, 0x1010));
547 try doTheTestReduce(.And, [4]u64{ 0xffffffff, 0xffff5555, 0xaaaaffff, 0x10101010 }, @as(u64, 0x1010));
548
549 try doTheTestReduce(.Min, [4]i16{ -1, 2, 3, 4 }, @as(i16, -1));
550 try doTheTestReduce(.Min, [4]u16{ 1, 2, 3, 4 }, @as(u16, 1));
551 try doTheTestReduce(.Min, [4]i32{ 1234567, -386, 0, 3 }, @as(i32, -386));
552 try doTheTestReduce(.Min, [4]u32{ 99, 9999, 9, 99999 }, @as(u32, 9));
553553
554554 // LLVM 11 ERROR: Cannot select type
555555 // https://github.com/ziglang/zig/issues/7138
556556 if (std.builtin.arch != .aarch64) {
557 doTheTestReduce(.Min, [4]i64{ 1234567, -386, 0, 3 }, @as(i64, -386));
558 doTheTestReduce(.Min, [4]u64{ 99, 9999, 9, 99999 }, @as(u64, 9));
557 try doTheTestReduce(.Min, [4]i64{ 1234567, -386, 0, 3 }, @as(i64, -386));
558 try doTheTestReduce(.Min, [4]u64{ 99, 9999, 9, 99999 }, @as(u64, 9));
559559 }
560560
561 doTheTestReduce(.Min, [4]i128{ 1234567, -386, 0, 3 }, @as(i128, -386));
562 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));
564 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));
561 try doTheTestReduce(.Min, [4]i128{ 1234567, -386, 0, 3 }, @as(i128, -386));
562 try doTheTestReduce(.Min, [4]u128{ 99, 9999, 9, 99999 }, @as(u128, 9));
563 try doTheTestReduce(.Min, [4]f16{ -10.3, 10.0e9, 13.0, -100.0 }, @as(f16, -100.0));
564 try doTheTestReduce(.Min, [4]f32{ -10.3, 10.0e9, 13.0, -100.0 }, @as(f32, -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));
568 doTheTestReduce(.Max, [4]u16{ 1, 2, 3, 4 }, @as(u16, 4));
569 doTheTestReduce(.Max, [4]i32{ 1234567, -386, 0, 3 }, @as(i32, 1234567));
570 doTheTestReduce(.Max, [4]u32{ 99, 9999, 9, 99999 }, @as(u32, 99999));
567 try doTheTestReduce(.Max, [4]i16{ -1, 2, 3, 4 }, @as(i16, 4));
568 try doTheTestReduce(.Max, [4]u16{ 1, 2, 3, 4 }, @as(u16, 4));
569 try doTheTestReduce(.Max, [4]i32{ 1234567, -386, 0, 3 }, @as(i32, 1234567));
570 try doTheTestReduce(.Max, [4]u32{ 99, 9999, 9, 99999 }, @as(u32, 99999));
571571
572572 // LLVM 11 ERROR: Cannot select type
573573 // https://github.com/ziglang/zig/issues/7138
574574 if (std.builtin.arch != .aarch64) {
575 doTheTestReduce(.Max, [4]i64{ 1234567, -386, 0, 3 }, @as(i64, 1234567));
576 doTheTestReduce(.Max, [4]u64{ 99, 9999, 9, 99999 }, @as(u64, 99999));
575 try doTheTestReduce(.Max, [4]i64{ 1234567, -386, 0, 3 }, @as(i64, 1234567));
576 try doTheTestReduce(.Max, [4]u64{ 99, 9999, 9, 99999 }, @as(u64, 99999));
577577 }
578578
579 doTheTestReduce(.Max, [4]i128{ 1234567, -386, 0, 3 }, @as(i128, 1234567));
580 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));
582 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));
584
585 doTheTestReduce(.Mul, [4]i16{ -1, 2, 3, 4 }, @as(i16, -24));
586 doTheTestReduce(.Mul, [4]u16{ 1, 2, 3, 4 }, @as(u16, 24));
587 doTheTestReduce(.Mul, [4]i32{ -9, -99, -999, 999 }, @as(i32, -889218891));
588 doTheTestReduce(.Mul, [4]u32{ 1, 2, 3, 4 }, @as(u32, 24));
589 doTheTestReduce(.Mul, [4]i64{ 9, 99, 999, 9999 }, @as(i64, 8900199891));
590 doTheTestReduce(.Mul, [4]u64{ 9, 99, 999, 9999 }, @as(u64, 8900199891));
591 doTheTestReduce(.Mul, [4]i128{ -9, -99, -999, 9999 }, @as(i128, -8900199891));
592 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));
594 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));
596
597 doTheTestReduce(.Or, [4]bool{ false, true, false, false }, @as(bool, true));
598 doTheTestReduce(.Or, [4]u1{ 0, 1, 0, 0 }, @as(u1, 1));
599 doTheTestReduce(.Or, [4]u16{ 0xff00, 0xff00, 0xf0, 0xf }, ~@as(u16, 0));
600 doTheTestReduce(.Or, [4]u32{ 0xffff0000, 0xff00, 0xf0, 0xf }, ~@as(u32, 0));
601 doTheTestReduce(.Or, [4]u64{ 0xffff0000, 0xff00, 0xf0, 0xf }, @as(u64, 0xffffffff));
602 doTheTestReduce(.Or, [4]u128{ 0xffff0000, 0xff00, 0xf0, 0xf }, @as(u128, 0xffffffff));
603
604 doTheTestReduce(.Xor, [4]bool{ true, true, true, false }, @as(bool, true));
605 doTheTestReduce(.Xor, [4]u1{ 1, 1, 1, 0 }, @as(u1, 1));
606 doTheTestReduce(.Xor, [4]u16{ 0x0000, 0x3333, 0x8888, 0x4444 }, ~@as(u16, 0));
607 doTheTestReduce(.Xor, [4]u32{ 0x00000000, 0x33333333, 0x88888888, 0x44444444 }, ~@as(u32, 0));
608 doTheTestReduce(.Xor, [4]u64{ 0x00000000, 0x33333333, 0x88888888, 0x44444444 }, @as(u64, 0xffffffff));
609 doTheTestReduce(.Xor, [4]u128{ 0x00000000, 0x33333333, 0x88888888, 0x44444444 }, @as(u128, 0xffffffff));
579 try doTheTestReduce(.Max, [4]i128{ 1234567, -386, 0, 3 }, @as(i128, 1234567));
580 try doTheTestReduce(.Max, [4]u128{ 99, 9999, 9, 99999 }, @as(u128, 99999));
581 try doTheTestReduce(.Max, [4]f16{ -10.3, 10.0e9, 13.0, -100.0 }, @as(f16, 10.0e9));
582 try doTheTestReduce(.Max, [4]f32{ -10.3, 10.0e9, 13.0, -100.0 }, @as(f32, 10.0e9));
583 try doTheTestReduce(.Max, [4]f64{ -10.3, 10.0e9, 13.0, -100.0 }, @as(f64, 10.0e9));
584
585 try doTheTestReduce(.Mul, [4]i16{ -1, 2, 3, 4 }, @as(i16, -24));
586 try doTheTestReduce(.Mul, [4]u16{ 1, 2, 3, 4 }, @as(u16, 24));
587 try doTheTestReduce(.Mul, [4]i32{ -9, -99, -999, 999 }, @as(i32, -889218891));
588 try doTheTestReduce(.Mul, [4]u32{ 1, 2, 3, 4 }, @as(u32, 24));
589 try doTheTestReduce(.Mul, [4]i64{ 9, 99, 999, 9999 }, @as(i64, 8900199891));
590 try doTheTestReduce(.Mul, [4]u64{ 9, 99, 999, 9999 }, @as(u64, 8900199891));
591 try doTheTestReduce(.Mul, [4]i128{ -9, -99, -999, 9999 }, @as(i128, -8900199891));
592 try doTheTestReduce(.Mul, [4]u128{ 9, 99, 999, 9999 }, @as(u128, 8900199891));
593 try doTheTestReduce(.Mul, [4]f16{ -1.9, 5.1, -60.3, 100.0 }, @as(f16, 58430.7));
594 try doTheTestReduce(.Mul, [4]f32{ -1.9, 5.1, -60.3, 100.0 }, @as(f32, 58430.7));
595 try doTheTestReduce(.Mul, [4]f64{ -1.9, 5.1, -60.3, 100.0 }, @as(f64, 58430.7));
596
597 try doTheTestReduce(.Or, [4]bool{ false, true, false, false }, @as(bool, true));
598 try doTheTestReduce(.Or, [4]u1{ 0, 1, 0, 0 }, @as(u1, 1));
599 try doTheTestReduce(.Or, [4]u16{ 0xff00, 0xff00, 0xf0, 0xf }, ~@as(u16, 0));
600 try doTheTestReduce(.Or, [4]u32{ 0xffff0000, 0xff00, 0xf0, 0xf }, ~@as(u32, 0));
601 try doTheTestReduce(.Or, [4]u64{ 0xffff0000, 0xff00, 0xf0, 0xf }, @as(u64, 0xffffffff));
602 try doTheTestReduce(.Or, [4]u128{ 0xffff0000, 0xff00, 0xf0, 0xf }, @as(u128, 0xffffffff));
603
604 try doTheTestReduce(.Xor, [4]bool{ true, true, true, false }, @as(bool, true));
605 try doTheTestReduce(.Xor, [4]u1{ 1, 1, 1, 0 }, @as(u1, 1));
606 try doTheTestReduce(.Xor, [4]u16{ 0x0000, 0x3333, 0x8888, 0x4444 }, ~@as(u16, 0));
607 try doTheTestReduce(.Xor, [4]u32{ 0x00000000, 0x33333333, 0x88888888, 0x44444444 }, ~@as(u32, 0));
608 try doTheTestReduce(.Xor, [4]u64{ 0x00000000, 0x33333333, 0x88888888, 0x44444444 }, @as(u64, 0xffffffff));
609 try doTheTestReduce(.Xor, [4]u128{ 0x00000000, 0x33333333, 0x88888888, 0x44444444 }, @as(u128, 0xffffffff));
610610
611611 // Test the reduction on vectors containing NaNs.
612612 const f16_nan = math.nan(f16);
613613 const f32_nan = math.nan(f32);
614614 const f64_nan = math.nan(f64);
615615
616 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);
618 doTheTestReduce(.Add, [4]f64{ -1.9, 5.1, f64_nan, 100.0 }, f64_nan);
616 try doTheTestReduce(.Add, [4]f16{ -1.9, 5.1, f16_nan, 100.0 }, f16_nan);
617 try doTheTestReduce(.Add, [4]f32{ -1.9, 5.1, f32_nan, 100.0 }, f32_nan);
618 try doTheTestReduce(.Add, [4]f64{ -1.9, 5.1, f64_nan, 100.0 }, f64_nan);
619619
620620 // LLVM 11 ERROR: Cannot select type
621621 // https://github.com/ziglang/zig/issues/7138
622622 if (false) {
623 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);
625 doTheTestReduce(.Min, [4]f64{ -1.9, 5.1, f64_nan, 100.0 }, f64_nan);
623 try doTheTestReduce(.Min, [4]f16{ -1.9, 5.1, f16_nan, 100.0 }, f16_nan);
624 try doTheTestReduce(.Min, [4]f32{ -1.9, 5.1, f32_nan, 100.0 }, f32_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);
628 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);
627 try doTheTestReduce(.Max, [4]f16{ -1.9, 5.1, f16_nan, 100.0 }, f16_nan);
628 try doTheTestReduce(.Max, [4]f32{ -1.9, 5.1, f32_nan, 100.0 }, f32_nan);
629 try doTheTestReduce(.Max, [4]f64{ -1.9, 5.1, f64_nan, 100.0 }, f64_nan);
630630 }
631631
632 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);
634 doTheTestReduce(.Mul, [4]f64{ -1.9, 5.1, f64_nan, 100.0 }, f64_nan);
632 try doTheTestReduce(.Mul, [4]f16{ -1.9, 5.1, f16_nan, 100.0 }, f16_nan);
633 try doTheTestReduce(.Mul, [4]f32{ -1.9, 5.1, f32_nan, 100.0 }, f32_nan);
634 try doTheTestReduce(.Mul, [4]f64{ -1.9, 5.1, f64_nan, 100.0 }, f64_nan);
635635 }
636636 };
637637
638 S.doTheTest();
639 comptime S.doTheTest();
638 try S.doTheTest();
639 comptime try S.doTheTest();
640640}
test/stage1/behavior/void.zig+3-3
......@@ -13,14 +13,14 @@ test "compare void with void compile time known" {
1313 .b = 1,
1414 .c = {},
1515 };
16 expect(foo.a == {});
16 try expect(foo.a == {});
1717 }
1818}
1919
2020test "iterate over a void slice" {
2121 var j: usize = 0;
2222 for (times(10)) |_, i| {
23 expect(i == j);
23 try expect(i == j);
2424 j += 1;
2525 }
2626}
......@@ -31,7 +31,7 @@ fn times(n: usize) []const void {
3131
3232test "void optional" {
3333 var x: ?void = {};
34 expect(x != null);
34 try expect(x != null);
3535}
3636
3737test "void array as a local variable initializer" {
test/stage1/behavior/wasm.zig+2-2
......@@ -3,6 +3,6 @@ const expect = std.testing.expect;
33
44test "memory size and grow" {
55 var prev = @wasmMemorySize(0);
6 expect(prev == @wasmMemoryGrow(0, 1));
7 expect(prev + 1 == @wasmMemorySize(0));
6 try expect(prev == @wasmMemoryGrow(0, 1));
7 try expect(prev + 1 == @wasmMemorySize(0));
88}
test/stage1/behavior/while.zig+45-51
......@@ -6,8 +6,8 @@ test "while loop" {
66 while (i < 4) {
77 i += 1;
88 }
9 expect(i == 4);
10 expect(whileLoop1() == 1);
9 try expect(i == 4);
10 try expect(whileLoop1() == 1);
1111}
1212fn whileLoop1() i32 {
1313 return whileLoop2();
......@@ -19,7 +19,7 @@ fn whileLoop2() i32 {
1919}
2020
2121test "static eval while" {
22 expect(static_eval_while_number == 1);
22 try expect(static_eval_while_number == 1);
2323}
2424const static_eval_while_number = staticWhileLoop1();
2525fn staticWhileLoop1() i32 {
......@@ -32,11 +32,11 @@ fn staticWhileLoop2() i32 {
3232}
3333
3434test "continue and break" {
35 runContinueAndBreakTest();
36 expect(continue_and_break_counter == 8);
35 try runContinueAndBreakTest();
36 try expect(continue_and_break_counter == 8);
3737}
3838var continue_and_break_counter: i32 = 0;
39fn runContinueAndBreakTest() void {
39fn runContinueAndBreakTest() !void {
4040 var i: i32 = 0;
4141 while (true) {
4242 continue_and_break_counter += 2;
......@@ -46,7 +46,7 @@ fn runContinueAndBreakTest() void {
4646 }
4747 break;
4848 }
49 expect(i == 4);
49 try expect(i == 4);
5050}
5151
5252test "return with implicit cast from while loop" {
......@@ -67,7 +67,7 @@ test "while with continue expression" {
6767 sum += i;
6868 }
6969 }
70 expect(sum == 40);
70 try expect(sum == 40);
7171}
7272
7373test "while with else" {
......@@ -79,8 +79,8 @@ test "while with else" {
7979 } else {
8080 got_else += 1;
8181 }
82 expect(sum == 10);
83 expect(got_else == 1);
82 try expect(sum == 10);
83 try expect(got_else == 1);
8484}
8585
8686test "while with optional as condition" {
......@@ -89,7 +89,7 @@ test "while with optional as condition" {
8989 while (getNumberOrNull()) |value| {
9090 sum += value;
9191 }
92 expect(sum == 45);
92 try expect(sum == 45);
9393}
9494
9595test "while with optional as condition with else" {
......@@ -98,12 +98,12 @@ test "while with optional as condition with else" {
9898 var got_else: i32 = 0;
9999 while (getNumberOrNull()) |value| {
100100 sum += value;
101 expect(got_else == 0);
101 try expect(got_else == 0);
102102 } else {
103103 got_else += 1;
104104 }
105 expect(sum == 45);
106 expect(got_else == 1);
105 try expect(sum == 45);
106 try expect(got_else == 1);
107107}
108108
109109test "while with error union condition" {
......@@ -113,11 +113,11 @@ test "while with error union condition" {
113113 while (getNumberOrErr()) |value| {
114114 sum += value;
115115 } else |err| {
116 expect(err == error.OutOfNumbers);
116 try expect(err == error.OutOfNumbers);
117117 got_else += 1;
118118 }
119 expect(sum == 45);
120 expect(got_else == 1);
119 try expect(sum == 45);
120 try expect(got_else == 1);
121121}
122122
123123var numbers_left: i32 = undefined;
......@@ -137,49 +137,43 @@ fn getNumberOrNull() ?i32 {
137137test "while on optional with else result follow else prong" {
138138 const result = while (returnNull()) |value| {
139139 break value;
140 } else
141 @as(i32, 2);
142 expect(result == 2);
140 } else @as(i32, 2);
141 try expect(result == 2);
143142}
144143
145144test "while on optional with else result follow break prong" {
146145 const result = while (returnOptional(10)) |value| {
147146 break value;
148 } else
149 @as(i32, 2);
150 expect(result == 10);
147 } else @as(i32, 2);
148 try expect(result == 10);
151149}
152150
153151test "while on error union with else result follow else prong" {
154152 const result = while (returnError()) |value| {
155153 break value;
156 } else |err|
157 @as(i32, 2);
158 expect(result == 2);
154 } else |err| @as(i32, 2);
155 try expect(result == 2);
159156}
160157
161158test "while on error union with else result follow break prong" {
162159 const result = while (returnSuccess(10)) |value| {
163160 break value;
164 } else |err|
165 @as(i32, 2);
166 expect(result == 10);
161 } else |err| @as(i32, 2);
162 try expect(result == 10);
167163}
168164
169165test "while on bool with else result follow else prong" {
170166 const result = while (returnFalse()) {
171167 break @as(i32, 10);
172 } else
173 @as(i32, 2);
174 expect(result == 2);
168 } else @as(i32, 2);
169 try expect(result == 2);
175170}
176171
177172test "while on bool with else result follow break prong" {
178173 const result = while (returnTrue()) {
179174 break @as(i32, 10);
180 } else
181 @as(i32, 2);
182 expect(result == 10);
175 } else @as(i32, 2);
176 try expect(result == 10);
183177}
184178
185179test "break from outer while loop" {
......@@ -230,60 +224,60 @@ fn returnTrue() bool {
230224
231225test "while bool 2 break statements and an else" {
232226 const S = struct {
233 fn entry(t: bool, f: bool) void {
227 fn entry(t: bool, f: bool) !void {
234228 var ok = false;
235229 ok = while (t) {
236230 if (f) break false;
237231 if (t) break true;
238232 } else false;
239 expect(ok);
233 try expect(ok);
240234 }
241235 };
242 S.entry(true, false);
243 comptime S.entry(true, false);
236 try S.entry(true, false);
237 comptime try S.entry(true, false);
244238}
245239
246240test "while optional 2 break statements and an else" {
247241 const S = struct {
248 fn entry(opt_t: ?bool, f: bool) void {
242 fn entry(opt_t: ?bool, f: bool) !void {
249243 var ok = false;
250244 ok = while (opt_t) |t| {
251245 if (f) break false;
252246 if (t) break true;
253247 } else false;
254 expect(ok);
248 try expect(ok);
255249 }
256250 };
257 S.entry(true, false);
258 comptime S.entry(true, false);
251 try S.entry(true, false);
252 comptime try S.entry(true, false);
259253}
260254
261255test "while error 2 break statements and an else" {
262256 const S = struct {
263 fn entry(opt_t: anyerror!bool, f: bool) void {
257 fn entry(opt_t: anyerror!bool, f: bool) !void {
264258 var ok = false;
265259 ok = while (opt_t) |t| {
266260 if (f) break false;
267261 if (t) break true;
268262 } else |_| false;
269 expect(ok);
263 try expect(ok);
270264 }
271265 };
272 S.entry(true, false);
273 comptime S.entry(true, false);
266 try S.entry(true, false);
267 comptime try S.entry(true, false);
274268}
275269
276270test "while copies its payload" {
277271 const S = struct {
278 fn doTheTest() void {
272 fn doTheTest() !void {
279273 var tmp: ?i32 = 10;
280274 while (tmp) |value| {
281275 // Modify the original variable
282276 tmp = null;
283 expect(value == 10);
277 try expect(value == 10);
284278 }
285279 }
286280 };
287 S.doTheTest();
288 comptime S.doTheTest();
281 try S.doTheTest();
282 comptime try S.doTheTest();
289283}
test/stage1/behavior/widening.zig+6-6
......@@ -9,13 +9,13 @@ test "integer widening" {
99 var d: u64 = c;
1010 var e: u64 = d;
1111 var f: u128 = e;
12 expect(f == a);
12 try expect(f == a);
1313}
1414
1515test "implicit unsigned integer to signed integer" {
1616 var a: u8 = 250;
1717 var b: i16 = a;
18 expect(b == 250);
18 try expect(b == 250);
1919}
2020
2121test "float widening" {
......@@ -23,9 +23,9 @@ test "float widening" {
2323 var b: f32 = a;
2424 var c: f64 = b;
2525 var d: f128 = c;
26 expect(a == b);
27 expect(b == c);
28 expect(c == d);
26 try expect(a == b);
27 try expect(b == c);
28 try expect(c == d);
2929}
3030
3131test "float widening f16 to f128" {
......@@ -35,5 +35,5 @@ test "float widening f16 to f128" {
3535
3636 var x: f16 = 12.34;
3737 var y: f128 = x;
38 expect(x == y);
38 try expect(x == y);
3939}
test/stage1/c_abi/main.zig+58-58
......@@ -24,11 +24,11 @@ extern fn c_i64(i64) void;
2424extern fn c_five_integers(i32, i32, i32, i32, i32) void;
2525
2626export fn zig_five_integers(a: i32, b: i32, c: i32, d: i32, e: i32) void {
27 expect(a == 12);
28 expect(b == 34);
29 expect(c == 56);
30 expect(d == 78);
31 expect(e == 90);
27 expect(a == 12) catch @panic("test failure");
28 expect(b == 34) catch @panic("test failure");
29 expect(c == 56) catch @panic("test failure");
30 expect(d == 78) catch @panic("test failure");
31 expect(e == 90) catch @panic("test failure");
3232}
3333
3434test "C ABI integers" {
......@@ -45,28 +45,28 @@ test "C ABI integers" {
4545}
4646
4747export fn zig_u8(x: u8) void {
48 expect(x == 0xff);
48 expect(x == 0xff) catch @panic("test failure");
4949}
5050export fn zig_u16(x: u16) void {
51 expect(x == 0xfffe);
51 expect(x == 0xfffe) catch @panic("test failure");
5252}
5353export fn zig_u32(x: u32) void {
54 expect(x == 0xfffffffd);
54 expect(x == 0xfffffffd) catch @panic("test failure");
5555}
5656export fn zig_u64(x: u64) void {
57 expect(x == 0xfffffffffffffffc);
57 expect(x == 0xfffffffffffffffc) catch @panic("test failure");
5858}
5959export fn zig_i8(x: i8) void {
60 expect(x == -1);
60 expect(x == -1) catch @panic("test failure");
6161}
6262export fn zig_i16(x: i16) void {
63 expect(x == -2);
63 expect(x == -2) catch @panic("test failure");
6464}
6565export fn zig_i32(x: i32) void {
66 expect(x == -3);
66 expect(x == -3) catch @panic("test failure");
6767}
6868export fn zig_i64(x: i64) void {
69 expect(x == -4);
69 expect(x == -4) catch @panic("test failure");
7070}
7171
7272extern fn c_f32(f32) void;
......@@ -76,11 +76,11 @@ extern fn c_f64(f64) void;
7676extern fn c_five_floats(f32, f32, f32, f32, f32) void;
7777
7878export fn zig_five_floats(a: f32, b: f32, c: f32, d: f32, e: f32) void {
79 expect(a == 1.0);
80 expect(b == 2.0);
81 expect(c == 3.0);
82 expect(d == 4.0);
83 expect(e == 5.0);
79 expect(a == 1.0) catch @panic("test failure");
80 expect(b == 2.0) catch @panic("test failure");
81 expect(c == 3.0) catch @panic("test failure");
82 expect(d == 4.0) catch @panic("test failure");
83 expect(e == 5.0) catch @panic("test failure");
8484}
8585
8686test "C ABI floats" {
......@@ -90,10 +90,10 @@ test "C ABI floats" {
9090}
9191
9292export fn zig_f32(x: f32) void {
93 expect(x == 12.34);
93 expect(x == 12.34) catch @panic("test failure");
9494}
9595export fn zig_f64(x: f64) void {
96 expect(x == 56.78);
96 expect(x == 56.78) catch @panic("test failure");
9797}
9898
9999extern fn c_ptr(*c_void) void;
......@@ -103,7 +103,7 @@ test "C ABI pointer" {
103103}
104104
105105export fn zig_ptr(x: *c_void) void {
106 expect(@ptrToInt(x) == 0xdeadbeef);
106 expect(@ptrToInt(x) == 0xdeadbeef) catch @panic("test failure");
107107}
108108
109109extern fn c_bool(bool) void;
......@@ -113,7 +113,7 @@ test "C ABI bool" {
113113}
114114
115115export fn zig_bool(x: bool) void {
116 expect(x);
116 expect(x) catch @panic("test failure");
117117}
118118
119119const BigStruct = extern struct {
......@@ -137,11 +137,11 @@ test "C ABI big struct" {
137137}
138138
139139export fn zig_big_struct(x: BigStruct) void {
140 expect(x.a == 1);
141 expect(x.b == 2);
142 expect(x.c == 3);
143 expect(x.d == 4);
144 expect(x.e == 5);
140 expect(x.a == 1) catch @panic("test failure");
141 expect(x.b == 2) catch @panic("test failure");
142 expect(x.c == 3) catch @panic("test failure");
143 expect(x.d == 4) catch @panic("test failure");
144 expect(x.e == 5) catch @panic("test failure");
145145}
146146
147147const BigUnion = extern union {
......@@ -163,11 +163,11 @@ test "C ABI big union" {
163163}
164164
165165export fn zig_big_union(x: BigUnion) void {
166 expect(x.a.a == 1);
167 expect(x.a.b == 2);
168 expect(x.a.c == 3);
169 expect(x.a.d == 4);
170 expect(x.a.e == 5);
166 expect(x.a.a == 1) catch @panic("test failure");
167 expect(x.a.b == 2) catch @panic("test failure");
168 expect(x.a.c == 3) catch @panic("test failure");
169 expect(x.a.d == 4) catch @panic("test failure");
170 expect(x.a.e == 5) catch @panic("test failure");
171171}
172172
173173const SmallStructInts = extern struct {
......@@ -189,10 +189,10 @@ test "C ABI small struct of ints" {
189189}
190190
191191export fn zig_small_struct_ints(x: SmallStructInts) void {
192 expect(x.a == 1);
193 expect(x.b == 2);
194 expect(x.c == 3);
195 expect(x.d == 4);
192 expect(x.a == 1) catch @panic("test failure");
193 expect(x.b == 2) catch @panic("test failure");
194 expect(x.c == 3) catch @panic("test failure");
195 expect(x.d == 4) catch @panic("test failure");
196196}
197197
198198const SplitStructInt = extern struct {
......@@ -212,9 +212,9 @@ test "C ABI split struct of ints" {
212212}
213213
214214export fn zig_split_struct_ints(x: SplitStructInt) void {
215 expect(x.a == 1234);
216 expect(x.b == 100);
217 expect(x.c == 1337);
215 expect(x.a == 1234) catch @panic("test failure");
216 expect(x.b == 100) catch @panic("test failure");
217 expect(x.c == 1337) catch @panic("test failure");
218218}
219219
220220extern fn c_big_struct_both(BigStruct) BigStruct;
......@@ -228,19 +228,19 @@ test "C ABI sret and byval together" {
228228 .e = 5,
229229 };
230230 var y = c_big_struct_both(s);
231 expect(y.a == 10);
232 expect(y.b == 11);
233 expect(y.c == 12);
234 expect(y.d == 13);
235 expect(y.e == 14);
231 try expect(y.a == 10);
232 try expect(y.b == 11);
233 try expect(y.c == 12);
234 try expect(y.d == 13);
235 try expect(y.e == 14);
236236}
237237
238238export fn zig_big_struct_both(x: BigStruct) BigStruct {
239 expect(x.a == 30);
240 expect(x.b == 31);
241 expect(x.c == 32);
242 expect(x.d == 33);
243 expect(x.e == 34);
239 expect(x.a == 30) catch @panic("test failure");
240 expect(x.b == 31) catch @panic("test failure");
241 expect(x.c == 32) catch @panic("test failure");
242 expect(x.d == 33) catch @panic("test failure");
243 expect(x.e == 34) catch @panic("test failure");
244244 var s = BigStruct{
245245 .a = 20,
246246 .b = 21,
......@@ -324,15 +324,15 @@ extern fn c_ret_i32() i32;
324324extern fn c_ret_i64() i64;
325325
326326test "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);
330 expect(c_ret_u16() == 0xffff);
331 expect(c_ret_u32() == 0xffffffff);
332 expect(c_ret_u64() == 0xffffffffffffffff);
329 try expect(c_ret_u8() == 0xff);
330 try expect(c_ret_u16() == 0xffff);
331 try expect(c_ret_u32() == 0xffffffff);
332 try expect(c_ret_u64() == 0xffffffffffffffff);
333333
334 expect(c_ret_i8() == -1);
335 expect(c_ret_i16() == -1);
336 expect(c_ret_i32() == -1);
337 expect(c_ret_i64() == -1);
334 try expect(c_ret_i8() == -1);
335 try expect(c_ret_i16() == -1);
336 try expect(c_ret_i32() == -1);
337 try expect(c_ret_i64() == -1);
338338}
test/standalone/brace_expansion/main.zig+31-31
......@@ -241,52 +241,52 @@ pub fn main() !void {
241241test "invalid inputs" {
242242 global_allocator = std.testing.allocator;
243243
244 expectError("}ABC", error.InvalidInput);
245 expectError("{ABC", error.InvalidInput);
246 expectError("}{", error.InvalidInput);
247 expectError("{}", error.InvalidInput);
248 expectError("A,B,C", error.InvalidInput);
249 expectError("{A{B,C}", error.InvalidInput);
250 expectError("{A,}", error.InvalidInput);
251
252 expectError("\n", error.InvalidInput);
244 try expectError("}ABC", error.InvalidInput);
245 try expectError("{ABC", error.InvalidInput);
246 try expectError("}{", error.InvalidInput);
247 try expectError("{}", error.InvalidInput);
248 try expectError("A,B,C", error.InvalidInput);
249 try expectError("{A{B,C}", error.InvalidInput);
250 try expectError("{A,}", error.InvalidInput);
251
252 try expectError("\n", error.InvalidInput);
253253}
254254
255fn expectError(test_input: []const u8, expected_err: anyerror) void {
255fn expectError(test_input: []const u8, expected_err: anyerror) !void {
256256 var output_buf = ArrayList(u8).init(global_allocator);
257257 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));
260260}
261261
262262test "valid inputs" {
263263 global_allocator = std.testing.allocator;
264264
265 expectExpansion("{x,y,z}", "x y z");
266 expectExpansion("{A,B}{x,y}", "Ax Ay Bx By");
267 expectExpansion("{A,B{x,y}}", "A Bx By");
268
269 expectExpansion("{ABC}", "ABC");
270 expectExpansion("{A,B,C}", "A B C");
271 expectExpansion("ABC", "ABC");
272
273 expectExpansion("", "");
274 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");
276 expectExpansion("{A,B}a", "Aa Ba");
277 expectExpansion("{C,{x,y}}", "C x y");
278 expectExpansion("z{C,{x,y}}", "zC zx zy");
279 expectExpansion("a{b,c{d,e{f,g}}}", "ab acd acef aceg");
280 expectExpansion("a{x,y}b", "axb ayb");
281 expectExpansion("z{{a,b}}", "za zb");
282 expectExpansion("a{b}", "ab");
265 try expectExpansion("{x,y,z}", "x y z");
266 try expectExpansion("{A,B}{x,y}", "Ax Ay Bx By");
267 try expectExpansion("{A,B{x,y}}", "A Bx By");
268
269 try expectExpansion("{ABC}", "ABC");
270 try expectExpansion("{A,B,C}", "A B C");
271 try expectExpansion("ABC", "ABC");
272
273 try expectExpansion("", "");
274 try expectExpansion("{A,B}{C,{x,y}}{g,h}", "ACg ACh Axg Axh Ayg Ayh BCg BCh Bxg Bxh Byg Byh");
275 try expectExpansion("{A,B}{C,C{x,y}}{g,h}", "ACg ACh ACxg ACxh ACyg ACyh BCg BCh BCxg BCxh BCyg BCyh");
276 try expectExpansion("{A,B}a", "Aa Ba");
277 try expectExpansion("{C,{x,y}}", "C x y");
278 try expectExpansion("z{C,{x,y}}", "zC zx zy");
279 try expectExpansion("a{b,c{d,e{f,g}}}", "ab acd acef aceg");
280 try expectExpansion("a{x,y}b", "axb ayb");
281 try expectExpansion("z{{a,b}}", "za zb");
282 try expectExpansion("a{b}", "ab");
283283}
284284
285fn expectExpansion(test_input: []const u8, expected_result: []const u8) void {
285fn expectExpansion(test_input: []const u8, expected_result: []const u8) !void {
286286 var result = ArrayList(u8).init(global_allocator);
287287 defer result.deinit();
288288
289289 expandString(test_input, &result) catch unreachable;
290290
291 testing.expectEqualSlices(u8, expected_result, result.items);
291 try testing.expectEqualSlices(u8, expected_result, result.items);
292292}
test/standalone/empty_env/main.zig+2-2
......@@ -1,6 +1,6 @@
11const std = @import("std");
22
3pub fn main() void {
3pub fn main() !void {
44 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);
66}
test/standalone/global_linkage/main.zig+2-2
......@@ -4,6 +4,6 @@ extern var obj1_integer: usize;
44extern var obj2_integer: usize;
55
66test "access the external integers" {
7 std.testing.expect(obj1_integer == 421);
8 std.testing.expect(obj2_integer == 422);
7 try std.testing.expect(obj1_integer == 421);
8 try std.testing.expect(obj2_integer == 422);
99}
test/standalone/issue_794/main.zig+1-1
......@@ -3,5 +3,5 @@ const std = @import("std");
33const testing = std.testing;
44
55test "c import" {
6 comptime testing.expect(c.NUMBER == 1234);
6 comptime try testing.expect(c.NUMBER == 1234);
77}
test/standalone/link_interdependent_static_c_libs/main.zig+1-1
......@@ -4,5 +4,5 @@ const c = @cImport(@cInclude("b.h"));
44
55test "import C sub" {
66 const result = c.sub(2, 1);
7 expect(result == 1);
7 try expect(result == 1);
88}
test/standalone/static_c_lib/foo.zig+2-2
......@@ -4,9 +4,9 @@ const c = @cImport(@cInclude("foo.h"));
44
55test "C add" {
66 const result = c.add(1, 2);
7 expect(result == 3);
7 try expect(result == 3);
88}
99
1010test "C extern variable" {
11 expect(c.foo == 12345);
11 try expect(c.foo == 12345);
1212}
test/standalone/use_alias/main.zig+1-1
......@@ -6,5 +6,5 @@ test "symbol exists" {
66 .a = 1,
77 .b = 1,
88 };
9 expect(foo.a + foo.b == 2);
9 try expect(foo.a + foo.b == 2);
1010}