diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2018-01-08 10:34:45 -0500 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2018-01-08 10:34:45 -0500 |
| commit | 5a8d87f5042b5ab86de7c72df4ce84a314878e40 (patch) | |
| tree | d9a8e14011994c5ebdf4525ea5c5b647aae91a6e /std | |
| parent | 38658a597bc22697c2038c21bdec9f04c9973eb8 (diff) | |
| parent | 598170756cd91b6f300921d256baa72141ec3098 (diff) | |
| download | zig-5a8d87f5042b5ab86de7c72df4ce84a314878e40.tar.gz zig-5a8d87f5042b5ab86de7c72df4ce84a314878e40.zip | |
Merge branch 'master' into llvm6
Diffstat (limited to 'std')
| -rw-r--r-- | std/array_list.zig | 10 | ||||
| -rw-r--r-- | std/base64.zig | 70 | ||||
| -rw-r--r-- | std/buf_map.zig | 12 | ||||
| -rw-r--r-- | std/buf_set.zig | 6 | ||||
| -rw-r--r-- | std/buffer.zig | 12 | ||||
| -rw-r--r-- | std/build.zig | 74 | ||||
| -rw-r--r-- | std/c/index.zig | 2 | ||||
| -rw-r--r-- | std/cstr.zig | 4 | ||||
| -rw-r--r-- | std/debug/failing_allocator.zig | 4 | ||||
| -rw-r--r-- | std/debug/index.zig | 266 | ||||
| -rw-r--r-- | std/elf.zig | 106 | ||||
| -rw-r--r-- | std/endian.zig | 2 | ||||
| -rw-r--r-- | std/fmt/index.zig | 70 | ||||
| -rw-r--r-- | std/hash_map.zig | 6 | ||||
| -rw-r--r-- | std/heap.zig | 10 | ||||
| -rw-r--r-- | std/io.zig | 76 | ||||
| -rw-r--r-- | std/linked_list.zig | 2 | ||||
| -rw-r--r-- | std/mem.zig | 16 | ||||
| -rw-r--r-- | std/net.zig | 2 | ||||
| -rw-r--r-- | std/os/child_process.zig | 110 | ||||
| -rw-r--r-- | std/os/get_user_id.zig | 6 | ||||
| -rw-r--r-- | std/os/index.zig | 160 | ||||
| -rw-r--r-- | std/os/path.zig | 46 | ||||
| -rw-r--r-- | std/os/windows/util.zig | 8 | ||||
| -rw-r--r-- | std/os/zen.zig | 94 | ||||
| -rw-r--r-- | std/rand.zig | 2 | ||||
| -rw-r--r-- | std/special/bootstrap.zig | 14 | ||||
| -rw-r--r-- | std/special/build_runner.zig | 50 | ||||
| -rw-r--r-- | std/special/panic.zig | 12 | ||||
| -rw-r--r-- | std/unicode.zig | 2 |
30 files changed, 681 insertions, 573 deletions
diff --git a/std/array_list.zig b/std/array_list.zig index db5581dc08..f07b9fb7f5 100644 --- a/std/array_list.zig +++ b/std/array_list.zig @@ -60,18 +60,18 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) -> type{ } pub fn append(l: &Self, item: &const T) -> %void { - const new_item_ptr = %return l.addOne(); + const new_item_ptr = try l.addOne(); *new_item_ptr = *item; } pub fn appendSlice(l: &Self, items: []align(A) const T) -> %void { - %return l.ensureCapacity(l.len + items.len); + try l.ensureCapacity(l.len + items.len); mem.copy(T, l.items[l.len..], items); l.len += items.len; } pub fn resize(l: &Self, new_len: usize) -> %void { - %return l.ensureCapacity(new_len); + try l.ensureCapacity(new_len); l.len = new_len; } @@ -87,12 +87,12 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) -> type{ better_capacity += better_capacity / 2 + 8; if (better_capacity >= new_capacity) break; } - l.items = %return l.allocator.alignedRealloc(T, A, l.items, better_capacity); + l.items = try l.allocator.alignedRealloc(T, A, l.items, better_capacity); } pub fn addOne(l: &Self) -> %&T { const new_length = l.len + 1; - %return l.ensureCapacity(new_length); + try l.ensureCapacity(new_length); const result = &l.items[l.len]; l.len = new_length; return result; diff --git a/std/base64.zig b/std/base64.zig index 0b405b1f4c..a790227c5d 100644 --- a/std/base64.zig +++ b/std/base64.zig @@ -379,37 +379,37 @@ test "base64" { } fn testBase64() -> %void { - %return testAllApis("", ""); - %return testAllApis("f", "Zg=="); - %return testAllApis("fo", "Zm8="); - %return testAllApis("foo", "Zm9v"); - %return testAllApis("foob", "Zm9vYg=="); - %return testAllApis("fooba", "Zm9vYmE="); - %return testAllApis("foobar", "Zm9vYmFy"); - - %return testDecodeIgnoreSpace("", " "); - %return testDecodeIgnoreSpace("f", "Z g= ="); - %return testDecodeIgnoreSpace("fo", " Zm8="); - %return testDecodeIgnoreSpace("foo", "Zm9v "); - %return testDecodeIgnoreSpace("foob", "Zm9vYg = = "); - %return testDecodeIgnoreSpace("fooba", "Zm9v YmE="); - %return testDecodeIgnoreSpace("foobar", " Z m 9 v Y m F y "); + try testAllApis("", ""); + try testAllApis("f", "Zg=="); + try testAllApis("fo", "Zm8="); + try testAllApis("foo", "Zm9v"); + try testAllApis("foob", "Zm9vYg=="); + try testAllApis("fooba", "Zm9vYmE="); + try testAllApis("foobar", "Zm9vYmFy"); + + try testDecodeIgnoreSpace("", " "); + try testDecodeIgnoreSpace("f", "Z g= ="); + try testDecodeIgnoreSpace("fo", " Zm8="); + try testDecodeIgnoreSpace("foo", "Zm9v "); + try testDecodeIgnoreSpace("foob", "Zm9vYg = = "); + try testDecodeIgnoreSpace("fooba", "Zm9v YmE="); + try testDecodeIgnoreSpace("foobar", " Z m 9 v Y m F y "); // test getting some api errors - %return testError("A", error.InvalidPadding); - %return testError("AA", error.InvalidPadding); - %return testError("AAA", error.InvalidPadding); - %return testError("A..A", error.InvalidCharacter); - %return testError("AA=A", error.InvalidCharacter); - %return testError("AA/=", error.InvalidPadding); - %return testError("A/==", error.InvalidPadding); - %return testError("A===", error.InvalidCharacter); - %return testError("====", error.InvalidCharacter); - - %return testOutputTooSmallError("AA=="); - %return testOutputTooSmallError("AAA="); - %return testOutputTooSmallError("AAAA"); - %return testOutputTooSmallError("AAAAAA=="); + try testError("A", error.InvalidPadding); + try testError("AA", error.InvalidPadding); + try testError("AAA", error.InvalidPadding); + try testError("A..A", error.InvalidCharacter); + try testError("AA=A", error.InvalidCharacter); + try testError("AA/=", error.InvalidPadding); + try testError("A/==", error.InvalidPadding); + try testError("A===", error.InvalidCharacter); + try testError("====", error.InvalidCharacter); + + try testOutputTooSmallError("AA=="); + try testOutputTooSmallError("AAA="); + try testOutputTooSmallError("AAAA"); + try testOutputTooSmallError("AAAAAA=="); } fn testAllApis(expected_decoded: []const u8, expected_encoded: []const u8) -> %void { @@ -424,8 +424,8 @@ fn testAllApis(expected_decoded: []const u8, expected_encoded: []const u8) -> %v // Base64Decoder { var buffer: [0x100]u8 = undefined; - var decoded = buffer[0..%return standard_decoder.calcSize(expected_encoded)]; - %return standard_decoder.decode(decoded, expected_encoded); + var decoded = buffer[0..try standard_decoder.calcSize(expected_encoded)]; + try standard_decoder.decode(decoded, expected_encoded); assert(mem.eql(u8, decoded, expected_decoded)); } @@ -434,8 +434,8 @@ fn testAllApis(expected_decoded: []const u8, expected_encoded: []const u8) -> %v const standard_decoder_ignore_nothing = Base64DecoderWithIgnore.init( standard_alphabet_chars, standard_pad_char, ""); var buffer: [0x100]u8 = undefined; - var decoded = buffer[0..%return Base64DecoderWithIgnore.calcSizeUpperBound(expected_encoded.len)]; - var written = %return standard_decoder_ignore_nothing.decode(decoded, expected_encoded); + var decoded = buffer[0..try Base64DecoderWithIgnore.calcSizeUpperBound(expected_encoded.len)]; + var written = try standard_decoder_ignore_nothing.decode(decoded, expected_encoded); assert(written <= decoded.len); assert(mem.eql(u8, decoded[0..written], expected_decoded)); } @@ -453,8 +453,8 @@ fn testDecodeIgnoreSpace(expected_decoded: []const u8, encoded: []const u8) -> % const standard_decoder_ignore_space = Base64DecoderWithIgnore.init( standard_alphabet_chars, standard_pad_char, " "); var buffer: [0x100]u8 = undefined; - var decoded = buffer[0..%return Base64DecoderWithIgnore.calcSizeUpperBound(encoded.len)]; - var written = %return standard_decoder_ignore_space.decode(decoded, encoded); + var decoded = buffer[0..try Base64DecoderWithIgnore.calcSizeUpperBound(encoded.len)]; + var written = try standard_decoder_ignore_space.decode(decoded, encoded); assert(mem.eql(u8, decoded[0..written], expected_decoded)); } diff --git a/std/buf_map.zig b/std/buf_map.zig index 2a9bd77660..3ff0624f63 100644 --- a/std/buf_map.zig +++ b/std/buf_map.zig @@ -29,16 +29,16 @@ pub const BufMap = struct { pub fn set(self: &BufMap, key: []const u8, value: []const u8) -> %void { if (self.hash_map.get(key)) |entry| { - const value_copy = %return self.copy(value); + const value_copy = try self.copy(value); %defer self.free(value_copy); - _ = %return self.hash_map.put(key, value_copy); + _ = try self.hash_map.put(key, value_copy); self.free(entry.value); } else { - const key_copy = %return self.copy(key); + const key_copy = try self.copy(key); %defer self.free(key_copy); - const value_copy = %return self.copy(value); + const value_copy = try self.copy(value); %defer self.free(value_copy); - _ = %return self.hash_map.put(key_copy, value_copy); + _ = try self.hash_map.put(key_copy, value_copy); } } @@ -68,7 +68,7 @@ pub const BufMap = struct { } fn copy(self: &BufMap, value: []const u8) -> %[]const u8 { - const result = %return self.hash_map.allocator.alloc(u8, value.len); + const result = try self.hash_map.allocator.alloc(u8, value.len); mem.copy(u8, result, value); return result; } diff --git a/std/buf_set.zig b/std/buf_set.zig index 43002a8756..5ce54551db 100644 --- a/std/buf_set.zig +++ b/std/buf_set.zig @@ -26,9 +26,9 @@ pub const BufSet = struct { pub fn put(self: &BufSet, key: []const u8) -> %void { if (self.hash_map.get(key) == null) { - const key_copy = %return self.copy(key); + const key_copy = try self.copy(key); %defer self.free(key_copy); - _ = %return self.hash_map.put(key_copy, {}); + _ = try self.hash_map.put(key_copy, {}); } } @@ -56,7 +56,7 @@ pub const BufSet = struct { } fn copy(self: &BufSet, value: []const u8) -> %[]const u8 { - const result = %return self.hash_map.allocator.alloc(u8, value.len); + const result = try self.hash_map.allocator.alloc(u8, value.len); mem.copy(u8, result, value); return result; } diff --git a/std/buffer.zig b/std/buffer.zig index 69e5a6d673..27c6b2f216 100644 --- a/std/buffer.zig +++ b/std/buffer.zig @@ -13,7 +13,7 @@ pub const Buffer = struct { /// Must deinitialize with deinit. pub fn init(allocator: &Allocator, m: []const u8) -> %Buffer { - var self = %return initSize(allocator, m.len); + var self = try initSize(allocator, m.len); mem.copy(u8, self.list.items, m); return self; } @@ -21,7 +21,7 @@ pub const Buffer = struct { /// Must deinitialize with deinit. pub fn initSize(allocator: &Allocator, size: usize) -> %Buffer { var self = initNull(allocator); - %return self.resize(size); + try self.resize(size); return self; } @@ -81,7 +81,7 @@ pub const Buffer = struct { } pub fn resize(self: &Buffer, new_len: usize) -> %void { - %return self.list.resize(new_len + 1); + try self.list.resize(new_len + 1); self.list.items[self.len()] = 0; } @@ -95,7 +95,7 @@ pub const Buffer = struct { pub fn append(self: &Buffer, m: []const u8) -> %void { const old_len = self.len(); - %return self.resize(old_len + m.len); + try self.resize(old_len + m.len); mem.copy(u8, self.list.toSlice()[old_len..], m); } @@ -113,7 +113,7 @@ pub const Buffer = struct { pub fn appendByteNTimes(self: &Buffer, byte: u8, count: usize) -> %void { var prev_size: usize = self.len(); const new_size = prev_size + count; - %return self.resize(new_size); + try self.resize(new_size); var i: usize = prev_size; while (i < new_size) : (i += 1) { @@ -138,7 +138,7 @@ pub const Buffer = struct { } pub fn replaceContents(self: &const Buffer, m: []const u8) -> %void { - %return self.resize(m.len); + try self.resize(m.len); mem.copy(u8, self.list.toSlice(), m); } diff --git a/std/build.zig b/std/build.zig index 71ce56def3..716b94e12e 100644 --- a/std/build.zig +++ b/std/build.zig @@ -250,13 +250,13 @@ pub const Builder = struct { %%wanted_steps.append(&self.default_step); } else { for (step_names) |step_name| { - const s = %return self.getTopLevelStepByName(step_name); + const s = try self.getTopLevelStepByName(step_name); %%wanted_steps.append(s); } } for (wanted_steps.toSliceConst()) |s| { - %return self.makeOneStep(s); + try self.makeOneStep(s); } } @@ -300,7 +300,7 @@ pub const Builder = struct { s.loop_flag = true; for (s.dependencies.toSlice()) |dep| { - self.makeOneStep(dep) %% |err| { + self.makeOneStep(dep) catch |err| { if (err == error.DependencyLoopDetected) { warn(" {}\n", s.name); } @@ -310,7 +310,7 @@ pub const Builder = struct { s.loop_flag = false; - %return s.make(); + try s.make(); } fn getTopLevelStepByName(self: &Builder, name: []const u8) -> %&Step { @@ -573,7 +573,7 @@ pub const Builder = struct { child.cwd = cwd; child.env_map = env_map; - const term = child.spawnAndWait() %% |err| { + const term = child.spawnAndWait() catch |err| { warn("Unable to spawn {}: {}\n", argv[0], @errorName(err)); return err; }; @@ -596,7 +596,7 @@ pub const Builder = struct { } pub fn makePath(self: &Builder, path: []const u8) -> %void { - os.makePath(self.allocator, self.pathFromRoot(path)) %% |err| { + os.makePath(self.allocator, self.pathFromRoot(path)) catch |err| { warn("Unable to create path {}: {}\n", path, @errorName(err)); return err; }; @@ -641,11 +641,11 @@ pub const Builder = struct { const dirname = os.path.dirname(dest_path); const abs_source_path = self.pathFromRoot(source_path); - os.makePath(self.allocator, dirname) %% |err| { + os.makePath(self.allocator, dirname) catch |err| { warn("Unable to create path {}: {}\n", dirname, @errorName(err)); return err; }; - os.copyFileMode(self.allocator, abs_source_path, dest_path, mode) %% |err| { + os.copyFileMode(self.allocator, abs_source_path, dest_path, mode) catch |err| { warn("Unable to copy {} to {}: {}\n", abs_source_path, dest_path, @errorName(err)); return err; }; @@ -663,7 +663,7 @@ pub const Builder = struct { if (builtin.environ == builtin.Environ.msvc) { return "cl.exe"; } else { - return os.getEnvVarOwned(self.allocator, "CC") %% |err| + return os.getEnvVarOwned(self.allocator, "CC") catch |err| if (err == error.EnvironmentVariableNotFound) ([]const u8)("cc") else @@ -680,7 +680,7 @@ pub const Builder = struct { if (os.path.isAbsolute(name)) { return name; } - const full_path = %return os.path.join(self.allocator, search_prefix, "bin", + const full_path = try os.path.join(self.allocator, search_prefix, "bin", self.fmt("{}{}", name, exe_extension)); if (os.path.real(self.allocator, full_path)) |real_path| { return real_path; @@ -696,7 +696,7 @@ pub const Builder = struct { } var it = mem.split(PATH, []u8{os.path.delimiter}); while (it.next()) |path| { - const full_path = %return os.path.join(self.allocator, path, self.fmt("{}{}", name, exe_extension)); + const full_path = try os.path.join(self.allocator, path, self.fmt("{}{}", name, exe_extension)); if (os.path.real(self.allocator, full_path)) |real_path| { return real_path; } else |_| { @@ -710,7 +710,7 @@ pub const Builder = struct { return name; } for (paths) |path| { - const full_path = %return os.path.join(self.allocator, path, self.fmt("{}{}", name, exe_extension)); + const full_path = try os.path.join(self.allocator, path, self.fmt("{}{}", name, exe_extension)); if (os.path.real(self.allocator, full_path)) |real_path| { return real_path; } else |_| { @@ -723,7 +723,7 @@ pub const Builder = struct { pub fn exec(self: &Builder, argv: []const []const u8) -> []u8 { const max_output_size = 100 * 1024; - const result = os.ChildProcess.exec(self.allocator, argv, null, null, max_output_size) %% |err| { + const result = os.ChildProcess.exec(self.allocator, argv, null, null, max_output_size) catch |err| { std.debug.panic("Unable to spawn {}: {}", argv[0], @errorName(err)); }; switch (result.term) { @@ -800,7 +800,7 @@ const Target = union(enum) { pub fn isDarwin(self: &const Target) -> bool { return switch (self.getOs()) { - builtin.Os.darwin, builtin.Os.ios, builtin.Os.macosx => true, + builtin.Os.ios, builtin.Os.macosx => true, else => false, }; } @@ -1011,7 +1011,7 @@ pub const LibExeObjStep = struct { self.out_filename = self.builder.fmt("lib{}.a", self.name); } else { switch (self.target.getOs()) { - builtin.Os.darwin, builtin.Os.ios, builtin.Os.macosx => { + builtin.Os.ios, builtin.Os.macosx => { self.out_filename = self.builder.fmt("lib{}.{d}.{d}.{d}.dylib", self.name, self.version.major, self.version.minor, self.version.patch); self.major_only_filename = self.builder.fmt("lib{}.{d}.dylib", self.name, self.version.major); @@ -1345,10 +1345,10 @@ pub const LibExeObjStep = struct { } } - %return builder.spawnChild(zig_args.toSliceConst()); + try builder.spawnChild(zig_args.toSliceConst()); if (self.kind == Kind.Lib and !self.static and self.target.wantSharedLibSymLinks()) { - %return doAtomicSymLinks(builder.allocator, output_path, self.major_only_filename, + try doAtomicSymLinks(builder.allocator, output_path, self.major_only_filename, self.name_only_filename); } } @@ -1423,7 +1423,7 @@ pub const LibExeObjStep = struct { self.appendCompileFlags(&cc_args); - %return builder.spawnChild(cc_args.toSliceConst()); + try builder.spawnChild(cc_args.toSliceConst()); }, Kind.Lib => { for (self.source_files.toSliceConst()) |source_file| { @@ -1440,14 +1440,14 @@ pub const LibExeObjStep = struct { const cache_o_src = %%os.path.join(builder.allocator, builder.cache_root, source_file); const cache_o_dir = os.path.dirname(cache_o_src); - %return builder.makePath(cache_o_dir); + try builder.makePath(cache_o_dir); const cache_o_file = builder.fmt("{}{}", cache_o_src, self.target.oFileExt()); %%cc_args.append("-o"); %%cc_args.append(builder.pathFromRoot(cache_o_file)); self.appendCompileFlags(&cc_args); - %return builder.spawnChild(cc_args.toSliceConst()); + try builder.spawnChild(cc_args.toSliceConst()); %%self.object_files.append(cache_o_file); } @@ -1466,14 +1466,14 @@ pub const LibExeObjStep = struct { %%cc_args.append(builder.pathFromRoot(object_file)); } - %return builder.spawnChild(cc_args.toSliceConst()); + try builder.spawnChild(cc_args.toSliceConst()); // ranlib %%cc_args.resize(0); %%cc_args.append("ranlib"); %%cc_args.append(output_path); - %return builder.spawnChild(cc_args.toSliceConst()); + try builder.spawnChild(cc_args.toSliceConst()); } else { %%cc_args.resize(0); %%cc_args.append(cc); @@ -1537,10 +1537,10 @@ pub const LibExeObjStep = struct { } } - %return builder.spawnChild(cc_args.toSliceConst()); + try builder.spawnChild(cc_args.toSliceConst()); if (self.target.wantSharedLibSymLinks()) { - %return doAtomicSymLinks(builder.allocator, output_path, self.major_only_filename, + try doAtomicSymLinks(builder.allocator, output_path, self.major_only_filename, self.name_only_filename); } } @@ -1556,7 +1556,7 @@ pub const LibExeObjStep = struct { const cache_o_src = %%os.path.join(builder.allocator, builder.cache_root, source_file); const cache_o_dir = os.path.dirname(cache_o_src); - %return builder.makePath(cache_o_dir); + try builder.makePath(cache_o_dir); const cache_o_file = builder.fmt("{}{}", cache_o_src, self.target.oFileExt()); %%cc_args.append("-o"); %%cc_args.append(builder.pathFromRoot(cache_o_file)); @@ -1570,7 +1570,7 @@ pub const LibExeObjStep = struct { %%cc_args.append(builder.pathFromRoot(dir)); } - %return builder.spawnChild(cc_args.toSliceConst()); + try builder.spawnChild(cc_args.toSliceConst()); %%self.object_files.append(cache_o_file); } @@ -1619,7 +1619,7 @@ pub const LibExeObjStep = struct { } } - %return builder.spawnChild(cc_args.toSliceConst()); + try builder.spawnChild(cc_args.toSliceConst()); }, } } @@ -1770,7 +1770,7 @@ pub const TestStep = struct { %%zig_args.append(lib_path); } - %return builder.spawnChild(zig_args.toSliceConst()); + try builder.spawnChild(zig_args.toSliceConst()); } }; @@ -1847,9 +1847,9 @@ const InstallArtifactStep = struct { LibExeObjStep.Kind.Exe => usize(0o755), LibExeObjStep.Kind.Lib => if (self.artifact.static) usize(0o666) else usize(0o755), }; - %return builder.copyFileMode(self.artifact.getOutputPath(), self.dest_file, mode); + try builder.copyFileMode(self.artifact.getOutputPath(), self.dest_file, mode); if (self.artifact.kind == LibExeObjStep.Kind.Lib and !self.artifact.static) { - %return doAtomicSymLinks(builder.allocator, self.dest_file, + try doAtomicSymLinks(builder.allocator, self.dest_file, self.artifact.major_only_filename, self.artifact.name_only_filename); } } @@ -1872,7 +1872,7 @@ pub const InstallFileStep = struct { fn make(step: &Step) -> %void { const self = @fieldParentPtr(InstallFileStep, "step", step); - %return self.builder.copyFile(self.src_path, self.dest_path); + try self.builder.copyFile(self.src_path, self.dest_path); } }; @@ -1895,11 +1895,11 @@ pub const WriteFileStep = struct { const self = @fieldParentPtr(WriteFileStep, "step", step); const full_path = self.builder.pathFromRoot(self.file_path); const full_path_dir = os.path.dirname(full_path); - os.makePath(self.builder.allocator, full_path_dir) %% |err| { + os.makePath(self.builder.allocator, full_path_dir) catch |err| { warn("unable to make path {}: {}\n", full_path_dir, @errorName(err)); return err; }; - io.writeFile(full_path, self.data, self.builder.allocator) %% |err| { + io.writeFile(full_path, self.data, self.builder.allocator) catch |err| { warn("unable to write {}: {}\n", full_path, @errorName(err)); return err; }; @@ -1942,7 +1942,7 @@ pub const RemoveDirStep = struct { const self = @fieldParentPtr(RemoveDirStep, "step", step); const full_path = self.builder.pathFromRoot(self.dir_path); - os.deleteTree(self.builder.allocator, full_path) %% |err| { + os.deleteTree(self.builder.allocator, full_path) catch |err| { warn("Unable to remove {}: {}\n", full_path, @errorName(err)); return err; }; @@ -1973,7 +1973,7 @@ pub const Step = struct { if (self.done_flag) return; - %return self.makeFn(self); + try self.makeFn(self); self.done_flag = true; } @@ -1991,13 +1991,13 @@ fn doAtomicSymLinks(allocator: &Allocator, output_path: []const u8, filename_maj const out_basename = os.path.basename(output_path); // sym link for libfoo.so.1 to libfoo.so.1.2.3 const major_only_path = %%os.path.join(allocator, out_dir, filename_major_only); - os.atomicSymLink(allocator, out_basename, major_only_path) %% |err| { + os.atomicSymLink(allocator, out_basename, major_only_path) catch |err| { warn("Unable to symlink {} -> {}\n", major_only_path, out_basename); return err; }; // sym link for libfoo.so to libfoo.so.1 const name_only_path = %%os.path.join(allocator, out_dir, filename_name_only); - os.atomicSymLink(allocator, filename_major_only, name_only_path) %% |err| { + os.atomicSymLink(allocator, filename_major_only, name_only_path) catch |err| { warn("Unable to symlink {} -> {}\n", name_only_path, filename_major_only); return err; }; diff --git a/std/c/index.zig b/std/c/index.zig index e04b990633..58359ed131 100644 --- a/std/c/index.zig +++ b/std/c/index.zig @@ -4,7 +4,7 @@ const Os = builtin.Os; pub use switch(builtin.os) { Os.linux => @import("linux.zig"), Os.windows => @import("windows.zig"), - Os.darwin, Os.macosx, Os.ios => @import("darwin.zig"), + Os.macosx, Os.ios => @import("darwin.zig"), else => empty_import, }; const empty_import = @import("../empty.zig"); diff --git a/std/cstr.zig b/std/cstr.zig index 1610f12481..0f63592950 100644 --- a/std/cstr.zig +++ b/std/cstr.zig @@ -43,7 +43,7 @@ fn testCStrFnsImpl() { /// have a null byte after it. /// Caller owns the returned memory. pub fn addNullByte(allocator: &mem.Allocator, slice: []const u8) -> %[]u8 { - const result = %return allocator.alloc(u8, slice.len + 1); + const result = try allocator.alloc(u8, slice.len + 1); mem.copy(u8, result, slice); result[slice.len] = 0; return result; @@ -70,7 +70,7 @@ pub const NullTerminated2DArray = struct { const index_size = @sizeOf(usize) * new_len; // size of the ptrs byte_count += index_size; - const buf = %return allocator.alignedAlloc(u8, @alignOf(?&u8), byte_count); + const buf = try allocator.alignedAlloc(u8, @alignOf(?&u8), byte_count); %defer allocator.free(buf); var write_index = index_size; diff --git a/std/debug/failing_allocator.zig b/std/debug/failing_allocator.zig index 9b12ff5cb7..2076a4cf1f 100644 --- a/std/debug/failing_allocator.zig +++ b/std/debug/failing_allocator.zig @@ -33,7 +33,7 @@ pub const FailingAllocator = struct { if (self.index == self.fail_index) { return error.OutOfMemory; } - const result = %return self.internal_allocator.allocFn(self.internal_allocator, n, alignment); + const result = try self.internal_allocator.allocFn(self.internal_allocator, n, alignment); self.allocated_bytes += result.len; self.index += 1; return result; @@ -48,7 +48,7 @@ pub const FailingAllocator = struct { if (self.index == self.fail_index) { return error.OutOfMemory; } - const result = %return self.internal_allocator.reallocFn(self.internal_allocator, old_mem, new_size, alignment); + const result = try self.internal_allocator.reallocFn(self.internal_allocator, old_mem, new_size, alignment); self.allocated_bytes += new_size - old_mem.len; self.deallocations += 1; self.index += 1; diff --git a/std/debug/index.zig b/std/debug/index.zig index 251b6f6f93..464974b7de 100644 --- a/std/debug/index.zig +++ b/std/debug/index.zig @@ -22,14 +22,14 @@ var stderr_file: io.File = undefined; var stderr_file_out_stream: io.FileOutStream = undefined; var stderr_stream: ?&io.OutStream = null; pub fn warn(comptime fmt: []const u8, args: ...) { - const stderr = getStderrStream() %% return; - stderr.print(fmt, args) %% return; + const stderr = getStderrStream() catch return; + stderr.print(fmt, args) catch return; } fn getStderrStream() -> %&io.OutStream { if (stderr_stream) |st| { return st; } else { - stderr_file = %return io.getStdErr(); + stderr_file = try io.getStdErr(); stderr_file_out_stream = io.FileOutStream.init(&stderr_file); const st = &stderr_file_out_stream.stream; stderr_stream = st; @@ -39,8 +39,8 @@ fn getStderrStream() -> %&io.OutStream { /// Tries to print a stack trace to stderr, unbuffered, and ignores any error returned. pub fn dumpStackTrace() { - const stderr = getStderrStream() %% return; - writeStackTrace(stderr, global_allocator, stderr_file.isTty(), 1) %% return; + const stderr = getStderrStream() catch return; + writeStackTrace(stderr, global_allocator, stderr_file.isTty(), 1) catch return; } /// This function invokes undefined behavior when `ok` is `false`. @@ -86,9 +86,9 @@ pub fn panic(comptime format: []const u8, args: ...) -> noreturn { panicking = true; } - const stderr = getStderrStream() %% os.abort(); - stderr.print(format ++ "\n", args) %% os.abort(); - writeStackTrace(stderr, global_allocator, stderr_file.isTty(), 1) %% os.abort(); + const stderr = getStderrStream() catch os.abort(); + stderr.print(format ++ "\n", args) catch os.abort(); + writeStackTrace(stderr, global_allocator, stderr_file.isTty(), 1) catch os.abort(); os.abort(); } @@ -118,18 +118,18 @@ pub fn writeStackTrace(out_stream: &io.OutStream, allocator: &mem.Allocator, tty .compile_unit_list = ArrayList(CompileUnit).init(allocator), }; const st = &stack_trace; - st.self_exe_file = %return os.openSelfExe(); + st.self_exe_file = try os.openSelfExe(); defer st.self_exe_file.close(); - %return st.elf.openFile(allocator, &st.self_exe_file); + try st.elf.openFile(allocator, &st.self_exe_file); defer st.elf.close(); - st.debug_info = (%return st.elf.findSection(".debug_info")) ?? return error.MissingDebugInfo; - st.debug_abbrev = (%return st.elf.findSection(".debug_abbrev")) ?? return error.MissingDebugInfo; - st.debug_str = (%return st.elf.findSection(".debug_str")) ?? return error.MissingDebugInfo; - st.debug_line = (%return st.elf.findSection(".debug_line")) ?? return error.MissingDebugInfo; - st.debug_ranges = (%return st.elf.findSection(".debug_ranges")); - %return scanAllCompileUnits(st); + st.debug_info = (try st.elf.findSection(".debug_info")) ?? return error.MissingDebugInfo; + st.debug_abbrev = (try st.elf.findSection(".debug_abbrev")) ?? return error.MissingDebugInfo; + st.debug_str = (try st.elf.findSection(".debug_str")) ?? return error.MissingDebugInfo; + st.debug_line = (try st.elf.findSection(".debug_line")) ?? return error.MissingDebugInfo; + st.debug_ranges = (try st.elf.findSection(".debug_ranges")); + try scanAllCompileUnits(st); var ignored_count: usize = 0; @@ -146,26 +146,26 @@ pub fn writeStackTrace(out_stream: &io.OutStream, allocator: &mem.Allocator, tty // at compile time. I'll call it issue #313 const ptr_hex = if (@sizeOf(usize) == 4) "0x{x8}" else "0x{x16}"; - const compile_unit = findCompileUnit(st, return_address) %% { - %return out_stream.print("???:?:?: " ++ DIM ++ ptr_hex ++ " in ??? (???)" ++ RESET ++ "\n ???\n\n", + const compile_unit = findCompileUnit(st, return_address) catch { + try out_stream.print("???:?:?: " ++ DIM ++ ptr_hex ++ " in ??? (???)" ++ RESET ++ "\n ???\n\n", return_address); continue; }; - const compile_unit_name = %return compile_unit.die.getAttrString(st, DW.AT_name); + const compile_unit_name = try compile_unit.die.getAttrString(st, DW.AT_name); if (getLineNumberInfo(st, compile_unit, usize(return_address) - 1)) |line_info| { defer line_info.deinit(); - %return out_stream.print(WHITE ++ "{}:{}:{}" ++ RESET ++ ": " ++ + try out_stream.print(WHITE ++ "{}:{}:{}" ++ RESET ++ ": " ++ DIM ++ ptr_hex ++ " in ??? ({})" ++ RESET ++ "\n", line_info.file_name, line_info.line, line_info.column, return_address, compile_unit_name); if (printLineFromFile(st.allocator(), out_stream, line_info)) { if (line_info.column == 0) { - %return out_stream.write("\n"); + try out_stream.write("\n"); } else { {var col_i: usize = 1; while (col_i < line_info.column) : (col_i += 1) { - %return out_stream.writeByte(' '); + try out_stream.writeByte(' '); }} - %return out_stream.write(GREEN ++ "^" ++ RESET ++ "\n"); + try out_stream.write(GREEN ++ "^" ++ RESET ++ "\n"); } } else |err| switch (err) { error.EndOfFile, error.PathNotFound => {}, @@ -173,7 +173,7 @@ pub fn writeStackTrace(out_stream: &io.OutStream, allocator: &mem.Allocator, tty } } else |err| switch (err) { error.MissingDebugInfo, error.InvalidDebugInfo => { - %return out_stream.print(ptr_hex ++ " in ??? ({})\n", + try out_stream.print(ptr_hex ++ " in ??? ({})\n", return_address, compile_unit_name); }, else => return err, @@ -181,22 +181,22 @@ pub fn writeStackTrace(out_stream: &io.OutStream, allocator: &mem.Allocator, tty } }, builtin.ObjectFormat.coff => { - %return out_stream.write("(stack trace unavailable for COFF object format)\n"); + try out_stream.write("(stack trace unavailable for COFF object format)\n"); }, builtin.ObjectFormat.macho => { - %return out_stream.write("(stack trace unavailable for Mach-O object format)\n"); + try out_stream.write("(stack trace unavailable for Mach-O object format)\n"); }, builtin.ObjectFormat.wasm => { - %return out_stream.write("(stack trace unavailable for WASM object format)\n"); + try out_stream.write("(stack trace unavailable for WASM object format)\n"); }, builtin.ObjectFormat.unknown => { - %return out_stream.write("(stack trace unavailable for unknown object format)\n"); + try out_stream.write("(stack trace unavailable for unknown object format)\n"); }, } } fn printLineFromFile(allocator: &mem.Allocator, out_stream: &io.OutStream, line_info: &const LineInfo) -> %void { - var f = %return io.File.openRead(line_info.file_name, allocator); + var f = try io.File.openRead(line_info.file_name, allocator); defer f.close(); // TODO fstat and make sure that the file has the correct size @@ -205,12 +205,12 @@ fn printLineFromFile(allocator: &mem.Allocator, out_stream: &io.OutStream, line_ var column: usize = 1; var abs_index: usize = 0; while (true) { - const amt_read = %return f.read(buf[0..]); + const amt_read = try f.read(buf[0..]); const slice = buf[0..amt_read]; for (slice) |byte| { if (line == line_info.line) { - %return out_stream.writeByte(byte); + try out_stream.writeByte(byte); if (byte == '\n') { return; } @@ -437,7 +437,7 @@ const LineNumberProgram = struct { const dir_name = if (file_entry.dir_index >= self.include_dirs.len) { return error.InvalidDebugInfo; } else self.include_dirs[file_entry.dir_index]; - const file_name = %return os.path.join(self.file_entries.allocator, dir_name, file_entry.file_name); + const file_name = try os.path.join(self.file_entries.allocator, dir_name, file_entry.file_name); %defer self.file_entries.allocator.free(file_name); return LineInfo { .line = if (self.prev_line >= 0) usize(self.prev_line) else 0, @@ -461,73 +461,73 @@ const LineNumberProgram = struct { fn readStringRaw(allocator: &mem.Allocator, in_stream: &io.InStream) -> %[]u8 { var buf = ArrayList(u8).init(allocator); while (true) { - const byte = %return in_stream.readByte(); + const byte = try in_stream.readByte(); if (byte == 0) break; - %return buf.append(byte); + try buf.append(byte); } return buf.toSlice(); } fn getString(st: &ElfStackTrace, offset: u64) -> %[]u8 { const pos = st.debug_str.offset + offset; - %return st.self_exe_file.seekTo(pos); + try st.self_exe_file.seekTo(pos); return st.readString(); } fn readAllocBytes(allocator: &mem.Allocator, in_stream: &io.InStream, size: usize) -> %[]u8 { - const buf = %return global_allocator.alloc(u8, size); + const buf = try global_allocator.alloc(u8, size); %defer global_allocator.free(buf); - if ((%return in_stream.read(buf)) < size) return error.EndOfFile; + if ((try in_stream.read(buf)) < size) return error.EndOfFile; return buf; } fn parseFormValueBlockLen(allocator: &mem.Allocator, in_stream: &io.InStream, size: usize) -> %FormValue { - const buf = %return readAllocBytes(allocator, in_stream, size); + const buf = try readAllocBytes(allocator, in_stream, size); return FormValue { .Block = buf }; } fn parseFormValueBlock(allocator: &mem.Allocator, in_stream: &io.InStream, size: usize) -> %FormValue { - const block_len = %return in_stream.readVarInt(builtin.Endian.Little, usize, size); + const block_len = try in_stream.readVarInt(builtin.Endian.Little, usize, size); return parseFormValueBlockLen(allocator, in_stream, block_len); } fn parseFormValueConstant(allocator: &mem.Allocator, in_stream: &io.InStream, signed: bool, size: usize) -> %FormValue { return FormValue { .Const = Constant { .signed = signed, - .payload = %return readAllocBytes(allocator, in_stream, size), + .payload = try readAllocBytes(allocator, in_stream, size), }}; } fn parseFormValueDwarfOffsetSize(in_stream: &io.InStream, is_64: bool) -> %u64 { - return if (is_64) %return in_stream.readIntLe(u64) - else u64(%return in_stream.readIntLe(u32)) ; + return if (is_64) try in_stream.readIntLe(u64) + else u64(try in_stream.readIntLe(u32)) ; } fn parseFormValueTargetAddrSize(in_stream: &io.InStream) -> %u64 { - return if (@sizeOf(usize) == 4) u64(%return in_stream.readIntLe(u32)) - else if (@sizeOf(usize) == 8) %return in_stream.readIntLe(u64) + return if (@sizeOf(usize) == 4) u64(try in_stream.readIntLe(u32)) + else if (@sizeOf(usize) == 8) try in_stream.readIntLe(u64) else unreachable; } fn parseFormValueRefLen(allocator: &mem.Allocator, in_stream: &io.InStream, size: usize) -> %FormValue { - const buf = %return readAllocBytes(allocator, in_stream, size); + const buf = try readAllocBytes(allocator, in_stream, size); return FormValue { .Ref = buf }; } fn parseFormValueRef(allocator: &mem.Allocator, in_stream: &io.InStream, comptime T: type) -> %FormValue { - const block_len = %return in_stream.readIntLe(T); + const block_len = try in_stream.readIntLe(T); return parseFormValueRefLen(allocator, in_stream, block_len); } fn parseFormValue(allocator: &mem.Allocator, in_stream: &io.InStream, form_id: u64, is_64: bool) -> %FormValue { return switch (form_id) { - DW.FORM_addr => FormValue { .Address = %return parseFormValueTargetAddrSize(in_stream) }, + DW.FORM_addr => FormValue { .Address = try parseFormValueTargetAddrSize(in_stream) }, DW.FORM_block1 => parseFormValueBlock(allocator, in_stream, 1), DW.FORM_block2 => parseFormValueBlock(allocator, in_stream, 2), DW.FORM_block4 => parseFormValueBlock(allocator, in_stream, 4), DW.FORM_block => x: { - const block_len = %return readULeb128(in_stream); + const block_len = try readULeb128(in_stream); return parseFormValueBlockLen(allocator, in_stream, block_len); }, DW.FORM_data1 => parseFormValueConstant(allocator, in_stream, false, 1), @@ -535,35 +535,35 @@ fn parseFormValue(allocator: &mem.Allocator, in_stream: &io.InStream, form_id: u DW.FORM_data4 => parseFormValueConstant(allocator, in_stream, false, 4), DW.FORM_data8 => parseFormValueConstant(allocator, in_stream, false, 8), DW.FORM_udata, DW.FORM_sdata => { - const block_len = %return readULeb128(in_stream); + const block_len = try readULeb128(in_stream); const signed = form_id == DW.FORM_sdata; return parseFormValueConstant(allocator, in_stream, signed, block_len); }, DW.FORM_exprloc => { - const size = %return readULeb128(in_stream); - const buf = %return readAllocBytes(allocator, in_stream, size); + const size = try readULeb128(in_stream); + const buf = try readAllocBytes(allocator, in_stream, size); return FormValue { .ExprLoc = buf }; }, - DW.FORM_flag => FormValue { .Flag = (%return in_stream.readByte()) != 0 }, + DW.FORM_flag => FormValue { .Flag = (try in_stream.readByte()) != 0 }, DW.FORM_flag_present => FormValue { .Flag = true }, - DW.FORM_sec_offset => FormValue { .SecOffset = %return parseFormValueDwarfOffsetSize(in_stream, is_64) }, + DW.FORM_sec_offset => FormValue { .SecOffset = try parseFormValueDwarfOffsetSize(in_stream, is_64) }, DW.FORM_ref1 => parseFormValueRef(allocator, in_stream, u8), DW.FORM_ref2 => parseFormValueRef(allocator, in_stream, u16), DW.FORM_ref4 => parseFormValueRef(allocator, in_stream, u32), DW.FORM_ref8 => parseFormValueRef(allocator, in_stream, u64), DW.FORM_ref_udata => { - const ref_len = %return readULeb128(in_stream); + const ref_len = try readULeb128(in_stream); return parseFormValueRefLen(allocator, in_stream, ref_len); }, - DW.FORM_ref_addr => FormValue { .RefAddr = %return parseFormValueDwarfOffsetSize(in_stream, is_64) }, - DW.FORM_ref_sig8 => FormValue { .RefSig8 = %return in_stream.readIntLe(u64) }, + DW.FORM_ref_addr => FormValue { .RefAddr = try parseFormValueDwarfOffsetSize(in_stream, is_64) }, + DW.FORM_ref_sig8 => FormValue { .RefSig8 = try in_stream.readIntLe(u64) }, - DW.FORM_string => FormValue { .String = %return readStringRaw(allocator, in_stream) }, - DW.FORM_strp => FormValue { .StrPtr = %return parseFormValueDwarfOffsetSize(in_stream, is_64) }, + DW.FORM_string => FormValue { .String = try readStringRaw(allocator, in_stream) }, + DW.FORM_strp => FormValue { .StrPtr = try parseFormValueDwarfOffsetSize(in_stream, is_64) }, DW.FORM_indirect => { - const child_form_id = %return readULeb128(in_stream); + const child_form_id = try readULeb128(in_stream); return parseFormValue(allocator, in_stream, child_form_id, is_64); }, else => error.InvalidDebugInfo, @@ -576,23 +576,23 @@ fn parseAbbrevTable(st: &ElfStackTrace) -> %AbbrevTable { const in_stream = &in_file_stream.stream; var result = AbbrevTable.init(st.allocator()); while (true) { - const abbrev_code = %return readULeb128(in_stream); + const abbrev_code = try readULeb128(in_stream); if (abbrev_code == 0) return result; - %return result.append(AbbrevTableEntry { + try result.append(AbbrevTableEntry { .abbrev_code = abbrev_code, - .tag_id = %return readULeb128(in_stream), - .has_children = (%return in_stream.readByte()) == DW.CHILDREN_yes, + .tag_id = try readULeb128(in_stream), + .has_children = (try in_stream.readByte()) == DW.CHILDREN_yes, .attrs = ArrayList(AbbrevAttr).init(st.allocator()), }); const attrs = &result.items[result.len - 1].attrs; while (true) { - const attr_id = %return readULeb128(in_stream); - const form_id = %return readULeb128(in_stream); + const attr_id = try readULeb128(in_stream); + const form_id = try readULeb128(in_stream); if (attr_id == 0 and form_id == 0) break; - %return attrs.append(AbbrevAttr { + try attrs.append(AbbrevAttr { .attr_id = attr_id, .form_id = form_id, }); @@ -608,10 +608,10 @@ fn getAbbrevTable(st: &ElfStackTrace, abbrev_offset: u64) -> %&const AbbrevTable return &header.table; } } - %return st.self_exe_file.seekTo(st.debug_abbrev.offset + abbrev_offset); - %return st.abbrev_table_list.append(AbbrevTableHeader { + try st.self_exe_file.seekTo(st.debug_abbrev.offset + abbrev_offset); + try st.abbrev_table_list.append(AbbrevTableHeader { .offset = abbrev_offset, - .table = %return parseAbbrevTable(st), + .table = try parseAbbrevTable(st), }); return &st.abbrev_table_list.items[st.abbrev_table_list.len - 1].table; } @@ -628,7 +628,7 @@ fn parseDie(st: &ElfStackTrace, abbrev_table: &const AbbrevTable, is_64: bool) - const in_file = &st.self_exe_file; var in_file_stream = io.FileInStream.init(in_file); const in_stream = &in_file_stream.stream; - const abbrev_code = %return readULeb128(in_stream); + const abbrev_code = try readULeb128(in_stream); const table_entry = getAbbrevTableEntry(abbrev_table, abbrev_code) ?? return error.InvalidDebugInfo; var result = Die { @@ -636,18 +636,18 @@ fn parseDie(st: &ElfStackTrace, abbrev_table: &const AbbrevTable, is_64: bool) - .has_children = table_entry.has_children, .attrs = ArrayList(Die.Attr).init(st.allocator()), }; - %return result.attrs.resize(table_entry.attrs.len); + try result.attrs.resize(table_entry.attrs.len); for (table_entry.attrs.toSliceConst()) |attr, i| { result.attrs.items[i] = Die.Attr { .id = attr.attr_id, - .value = %return parseFormValue(st.allocator(), in_stream, attr.form_id, is_64), + .value = try parseFormValue(st.allocator(), in_stream, attr.form_id, is_64), }; } return result; } fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, target_address: usize) -> %LineInfo { - const compile_unit_cwd = %return compile_unit.die.getAttrString(st, DW.AT_comp_dir); + const compile_unit_cwd = try compile_unit.die.getAttrString(st, DW.AT_comp_dir); const in_file = &st.self_exe_file; const debug_line_end = st.debug_line.offset + st.debug_line.size; @@ -658,10 +658,10 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe const in_stream = &in_file_stream.stream; while (this_offset < debug_line_end) : (this_index += 1) { - %return in_file.seekTo(this_offset); + try in_file.seekTo(this_offset); var is_64: bool = undefined; - const unit_length = %return readInitialLength(in_stream, &is_64); + const unit_length = try readInitialLength(in_stream, &is_64); if (unit_length == 0) return error.MissingDebugInfo; const next_offset = unit_length + (if (is_64) usize(12) else usize(4)); @@ -671,37 +671,37 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe continue; } - const version = %return in_stream.readInt(st.elf.endian, u16); + const version = try in_stream.readInt(st.elf.endian, u16); if (version != 2) return error.InvalidDebugInfo; - const prologue_length = %return in_stream.readInt(st.elf.endian, u32); - const prog_start_offset = (%return in_file.getPos()) + prologue_length; + const prologue_length = try in_stream.readInt(st.elf.endian, u32); + const prog_start_offset = (try in_file.getPos()) + prologue_length; - const minimum_instruction_length = %return in_stream.readByte(); + const minimum_instruction_length = try in_stream.readByte(); if (minimum_instruction_length == 0) return error.InvalidDebugInfo; - const default_is_stmt = (%return in_stream.readByte()) != 0; - const line_base = %return in_stream.readByteSigned(); + const default_is_stmt = (try in_stream.readByte()) != 0; + const line_base = try in_stream.readByteSigned(); - const line_range = %return in_stream.readByte(); + const line_range = try in_stream.readByte(); if (line_range == 0) return error.InvalidDebugInfo; - const opcode_base = %return in_stream.readByte(); + const opcode_base = try in_stream.readByte(); - const standard_opcode_lengths = %return st.allocator().alloc(u8, opcode_base - 1); + const standard_opcode_lengths = try st.allocator().alloc(u8, opcode_base - 1); {var i: usize = 0; while (i < opcode_base - 1) : (i += 1) { - standard_opcode_lengths[i] = %return in_stream.readByte(); + standard_opcode_lengths[i] = try in_stream.readByte(); }} var include_directories = ArrayList([]u8).init(st.allocator()); - %return include_directories.append(compile_unit_cwd); + try include_directories.append(compile_unit_cwd); while (true) { - const dir = %return st.readString(); + const dir = try st.readString(); if (dir.len == 0) break; - %return include_directories.append(dir); + try include_directories.append(dir); } var file_entries = ArrayList(FileEntry).init(st.allocator()); @@ -709,13 +709,13 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe &file_entries, target_address); while (true) { - const file_name = %return st.readString(); + const file_name = try st.readString(); if (file_name.len == 0) break; - const dir_index = %return readULeb128(in_stream); - const mtime = %return readULeb128(in_stream); - const len_bytes = %return readULeb128(in_stream); - %return file_entries.append(FileEntry { + const dir_index = try readULeb128(in_stream); + const mtime = try readULeb128(in_stream); + const len_bytes = try readULeb128(in_stream); + try file_entries.append(FileEntry { .file_name = file_name, .dir_index = dir_index, .mtime = mtime, @@ -723,33 +723,33 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe }); } - %return in_file.seekTo(prog_start_offset); + try in_file.seekTo(prog_start_offset); while (true) { - const opcode = %return in_stream.readByte(); + const opcode = try in_stream.readByte(); var sub_op: u8 = undefined; // TODO move this to the correct scope and fix the compiler crash if (opcode == DW.LNS_extended_op) { - const op_size = %return readULeb128(in_stream); + const op_size = try readULeb128(in_stream); if (op_size < 1) return error.InvalidDebugInfo; - sub_op = %return in_stream.readByte(); + sub_op = try in_stream.readByte(); switch (sub_op) { DW.LNE_end_sequence => { prog.end_sequence = true; - if (%return prog.checkLineMatch()) |info| return info; + if (try prog.checkLineMatch()) |info| return info; return error.MissingDebugInfo; }, DW.LNE_set_address => { - const addr = %return in_stream.readInt(st.elf.endian, usize); + const addr = try in_stream.readInt(st.elf.endian, usize); prog.address = addr; }, DW.LNE_define_file => { - const file_name = %return st.readString(); - const dir_index = %return readULeb128(in_stream); - const mtime = %return readULeb128(in_stream); - const len_bytes = %return readULeb128(in_stream); - %return file_entries.append(FileEntry { + const file_name = try st.readString(); + const dir_index = try readULeb128(in_stream); + const mtime = try readULeb128(in_stream); + const len_bytes = try readULeb128(in_stream); + try file_entries.append(FileEntry { .file_name = file_name, .dir_index = dir_index, .mtime = mtime, @@ -757,8 +757,8 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe }); }, else => { - const fwd_amt = math.cast(isize, op_size - 1) %% return error.InvalidDebugInfo; - %return in_file.seekForward(fwd_amt); + const fwd_amt = math.cast(isize, op_size - 1) catch return error.InvalidDebugInfo; + try in_file.seekForward(fwd_amt); }, } } else if (opcode >= opcode_base) { @@ -768,28 +768,28 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe const inc_line = i32(line_base) + i32(adjusted_opcode % line_range); prog.line += inc_line; prog.address += inc_addr; - if (%return prog.checkLineMatch()) |info| return info; + if (try prog.checkLineMatch()) |info| return info; prog.basic_block = false; } else { switch (opcode) { DW.LNS_copy => { - if (%return prog.checkLineMatch()) |info| return info; + if (try prog.checkLineMatch()) |info| return info; prog.basic_block = false; }, DW.LNS_advance_pc => { - const arg = %return readULeb128(in_stream); + const arg = try readULeb128(in_stream); prog.address += arg * minimum_instruction_length; }, DW.LNS_advance_line => { - const arg = %return readILeb128(in_stream); + const arg = try readILeb128(in_stream); prog.line += arg; }, DW.LNS_set_file => { - const arg = %return readULeb128(in_stream); + const arg = try readULeb128(in_stream); prog.file = arg; }, DW.LNS_set_column => { - const arg = %return readULeb128(in_stream); + const arg = try readULeb128(in_stream); prog.column = arg; }, DW.LNS_negate_stmt => { @@ -803,7 +803,7 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe prog.address += inc_addr; }, DW.LNS_fixed_advance_pc => { - const arg = %return in_stream.readInt(st.elf.endian, u16); + const arg = try in_stream.readInt(st.elf.endian, u16); prog.address += arg; }, DW.LNS_set_prologue_end => { @@ -812,7 +812,7 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe if (opcode - 1 >= standard_opcode_lengths.len) return error.InvalidDebugInfo; const len_bytes = standard_opcode_lengths[opcode - 1]; - %return in_file.seekForward(len_bytes); + try in_file.seekForward(len_bytes); }, } } @@ -833,31 +833,31 @@ fn scanAllCompileUnits(st: &ElfStackTrace) -> %void { const in_stream = &in_file_stream.stream; while (this_unit_offset < debug_info_end) { - %return st.self_exe_file.seekTo(this_unit_offset); + try st.self_exe_file.seekTo(this_unit_offset); var is_64: bool = undefined; - const unit_length = %return readInitialLength(in_stream, &is_64); + const unit_length = try readInitialLength(in_stream, &is_64); if (unit_length == 0) return; const next_offset = unit_length + (if (is_64) usize(12) else usize(4)); - const version = %return in_stream.readInt(st.elf.endian, u16); + const version = try in_stream.readInt(st.elf.endian, u16); if (version < 2 or version > 5) return error.InvalidDebugInfo; const debug_abbrev_offset = - if (is_64) %return in_stream.readInt(st.elf.endian, u64) - else %return in_stream.readInt(st.elf.endian, u32); + if (is_64) try in_stream.readInt(st.elf.endian, u64) + else try in_stream.readInt(st.elf.endian, u32); - const address_size = %return in_stream.readByte(); + const address_size = try in_stream.readByte(); if (address_size != @sizeOf(usize)) return error.InvalidDebugInfo; - const compile_unit_pos = %return st.self_exe_file.getPos(); - const abbrev_table = %return getAbbrevTable(st, debug_abbrev_offset); + const compile_unit_pos = try st.self_exe_file.getPos(); + const abbrev_table = try getAbbrevTable(st, debug_abbrev_offset); - %return st.self_exe_file.seekTo(compile_unit_pos); + try st.self_exe_file.seekTo(compile_unit_pos); - const compile_unit_die = %return st.allocator().create(Die); - *compile_unit_die = %return parseDie(st, abbrev_table, is_64); + const compile_unit_die = try st.allocator().create(Die); + *compile_unit_die = try parseDie(st, abbrev_table, is_64); if (compile_unit_die.tag_id != DW.TAG_compile_unit) return error.InvalidDebugInfo; @@ -868,7 +868,7 @@ fn scanAllCompileUnits(st: &ElfStackTrace) -> %void { const pc_end = switch (*high_pc_value) { FormValue.Address => |value| value, FormValue.Const => |value| b: { - const offset = %return value.asUnsignedLe(); + const offset = try value.asUnsignedLe(); break :b (low_pc + offset); }, else => return error.InvalidDebugInfo, @@ -887,7 +887,7 @@ fn scanAllCompileUnits(st: &ElfStackTrace) -> %void { } }; - %return st.compile_unit_list.append(CompileUnit { + try st.compile_unit_list.append(CompileUnit { .version = version, .is_64 = is_64, .pc_range = pc_range, @@ -911,10 +911,10 @@ fn findCompileUnit(st: &ElfStackTrace, target_address: u64) -> %&const CompileUn if (compile_unit.die.getAttrSecOffset(DW.AT_ranges)) |ranges_offset| { var base_address: usize = 0; if (st.debug_ranges) |debug_ranges| { - %return st.self_exe_file.seekTo(debug_ranges.offset + ranges_offset); + try st.self_exe_file.seekTo(debug_ranges.offset + ranges_offset); while (true) { - const begin_addr = %return in_stream.readIntLe(usize); - const end_addr = %return in_stream.readIntLe(usize); + const begin_addr = try in_stream.readIntLe(usize); + const end_addr = try in_stream.readIntLe(usize); if (begin_addr == 0 and end_addr == 0) { break; } @@ -937,7 +937,7 @@ fn findCompileUnit(st: &ElfStackTrace, target_address: u64) -> %&const CompileUn } fn readInitialLength(in_stream: &io.InStream, is_64: &bool) -> %u64 { - const first_32_bits = %return in_stream.readIntLe(u32); + const first_32_bits = try in_stream.readIntLe(u32); *is_64 = (first_32_bits == 0xffffffff); if (*is_64) { return in_stream.readIntLe(u64); @@ -952,7 +952,7 @@ fn readULeb128(in_stream: &io.InStream) -> %u64 { var shift: usize = 0; while (true) { - const byte = %return in_stream.readByte(); + const byte = try in_stream.readByte(); var operand: u64 = undefined; @@ -973,7 +973,7 @@ fn readILeb128(in_stream: &io.InStream) -> %i64 { var shift: usize = 0; while (true) { - const byte = %return in_stream.readByte(); + const byte = try in_stream.readByte(); var operand: i64 = undefined; diff --git a/std/elf.zig b/std/elf.zig index 60b0119894..c91509ff7d 100644 --- a/std/elf.zig +++ b/std/elf.zig @@ -82,8 +82,8 @@ pub const Elf = struct { /// Call close when done. pub fn openPath(elf: &Elf, allocator: &mem.Allocator, path: []const u8) -> %void { - %return elf.prealloc_file.open(path); - %return elf.openFile(allocator, &elf.prealloc_file); + try elf.prealloc_file.open(path); + try elf.openFile(allocator, &elf.prealloc_file); elf.auto_close_stream = true; } @@ -97,28 +97,28 @@ pub const Elf = struct { const in = &file_stream.stream; var magic: [4]u8 = undefined; - %return in.readNoEof(magic[0..]); + try in.readNoEof(magic[0..]); if (!mem.eql(u8, magic, "\x7fELF")) return error.InvalidFormat; - elf.is_64 = switch (%return in.readByte()) { + elf.is_64 = switch (try in.readByte()) { 1 => false, 2 => true, else => return error.InvalidFormat, }; - elf.endian = switch (%return in.readByte()) { + elf.endian = switch (try in.readByte()) { 1 => builtin.Endian.Little, 2 => builtin.Endian.Big, else => return error.InvalidFormat, }; - const version_byte = %return in.readByte(); + const version_byte = try in.readByte(); if (version_byte != 1) return error.InvalidFormat; // skip over padding - %return elf.in_file.seekForward(9); + try elf.in_file.seekForward(9); - elf.file_type = switch (%return in.readInt(elf.endian, u16)) { + elf.file_type = switch (try in.readInt(elf.endian, u16)) { 1 => FileType.Relocatable, 2 => FileType.Executable, 3 => FileType.Shared, @@ -126,7 +126,7 @@ pub const Elf = struct { else => return error.InvalidFormat, }; - elf.arch = switch (%return in.readInt(elf.endian, u16)) { + elf.arch = switch (try in.readInt(elf.endian, u16)) { 0x02 => Arch.Sparc, 0x03 => Arch.x86, 0x08 => Arch.Mips, @@ -139,88 +139,88 @@ pub const Elf = struct { else => return error.InvalidFormat, }; - const elf_version = %return in.readInt(elf.endian, u32); + const elf_version = try in.readInt(elf.endian, u32); if (elf_version != 1) return error.InvalidFormat; if (elf.is_64) { - elf.entry_addr = %return in.readInt(elf.endian, u64); - elf.program_header_offset = %return in.readInt(elf.endian, u64); - elf.section_header_offset = %return in.readInt(elf.endian, u64); + elf.entry_addr = try in.readInt(elf.endian, u64); + elf.program_header_offset = try in.readInt(elf.endian, u64); + elf.section_header_offset = try in.readInt(elf.endian, u64); } else { - elf.entry_addr = u64(%return in.readInt(elf.endian, u32)); - elf.program_header_offset = u64(%return in.readInt(elf.endian, u32)); - elf.section_header_offset = u64(%return in.readInt(elf.endian, u32)); + elf.entry_addr = u64(try in.readInt(elf.endian, u32)); + elf.program_header_offset = u64(try in.readInt(elf.endian, u32)); + elf.section_header_offset = u64(try in.readInt(elf.endian, u32)); } // skip over flags - %return elf.in_file.seekForward(4); + try elf.in_file.seekForward(4); - const header_size = %return in.readInt(elf.endian, u16); + const header_size = try in.readInt(elf.endian, u16); if ((elf.is_64 and header_size != 64) or (!elf.is_64 and header_size != 52)) { return error.InvalidFormat; } - const ph_entry_size = %return in.readInt(elf.endian, u16); - const ph_entry_count = %return in.readInt(elf.endian, u16); - const sh_entry_size = %return in.readInt(elf.endian, u16); - const sh_entry_count = %return in.readInt(elf.endian, u16); - elf.string_section_index = u64(%return in.readInt(elf.endian, u16)); + const ph_entry_size = try in.readInt(elf.endian, u16); + const ph_entry_count = try in.readInt(elf.endian, u16); + const sh_entry_size = try in.readInt(elf.endian, u16); + const sh_entry_count = try in.readInt(elf.endian, u16); + elf.string_section_index = u64(try in.readInt(elf.endian, u16)); if (elf.string_section_index >= sh_entry_count) return error.InvalidFormat; const sh_byte_count = u64(sh_entry_size) * u64(sh_entry_count); - const end_sh = %return math.add(u64, elf.section_header_offset, sh_byte_count); + const end_sh = try math.add(u64, elf.section_header_offset, sh_byte_count); const ph_byte_count = u64(ph_entry_size) * u64(ph_entry_count); - const end_ph = %return math.add(u64, elf.program_header_offset, ph_byte_count); + const end_ph = try math.add(u64, elf.program_header_offset, ph_byte_count); - const stream_end = %return elf.in_file.getEndPos(); + const stream_end = try elf.in_file.getEndPos(); if (stream_end < end_sh or stream_end < end_ph) { return error.InvalidFormat; } - %return elf.in_file.seekTo(elf.section_header_offset); + try elf.in_file.seekTo(elf.section_header_offset); - elf.section_headers = %return elf.allocator.alloc(SectionHeader, sh_entry_count); + elf.section_headers = try elf.allocator.alloc(SectionHeader, sh_entry_count); %defer elf.allocator.free(elf.section_headers); if (elf.is_64) { if (sh_entry_size != 64) return error.InvalidFormat; for (elf.section_headers) |*elf_section| { - elf_section.name = %return in.readInt(elf.endian, u32); - elf_section.sh_type = %return in.readInt(elf.endian, u32); - elf_section.flags = %return in.readInt(elf.endian, u64); - elf_section.addr = %return in.readInt(elf.endian, u64); - elf_section.offset = %return in.readInt(elf.endian, u64); - elf_section.size = %return in.readInt(elf.endian, u64); - elf_section.link = %return in.readInt(elf.endian, u32); - elf_section.info = %return in.readInt(elf.endian, u32); - elf_section.addr_align = %return in.readInt(elf.endian, u64); - elf_section.ent_size = %return in.readInt(elf.endian, u64); + elf_section.name = try in.readInt(elf.endian, u32); + elf_section.sh_type = try in.readInt(elf.endian, u32); + elf_section.flags = try in.readInt(elf.endian, u64); + elf_section.addr = try in.readInt(elf.endian, u64); + elf_section.offset = try in.readInt(elf.endian, u64); + elf_section.size = try in.readInt(elf.endian, u64); + elf_section.link = try in.readInt(elf.endian, u32); + elf_section.info = try in.readInt(elf.endian, u32); + elf_section.addr_align = try in.readInt(elf.endian, u64); + elf_section.ent_size = try in.readInt(elf.endian, u64); } } else { if (sh_entry_size != 40) return error.InvalidFormat; for (elf.section_headers) |*elf_section| { // TODO (multiple occurences) allow implicit cast from %u32 -> %u64 ? - elf_section.name = %return in.readInt(elf.endian, u32); - elf_section.sh_type = %return in.readInt(elf.endian, u32); - elf_section.flags = u64(%return in.readInt(elf.endian, u32)); - elf_section.addr = u64(%return in.readInt(elf.endian, u32)); - elf_section.offset = u64(%return in.readInt(elf.endian, u32)); - elf_section.size = u64(%return in.readInt(elf.endian, u32)); - elf_section.link = %return in.readInt(elf.endian, u32); - elf_section.info = %return in.readInt(elf.endian, u32); - elf_section.addr_align = u64(%return in.readInt(elf.endian, u32)); - elf_section.ent_size = u64(%return in.readInt(elf.endian, u32)); + elf_section.name = try in.readInt(elf.endian, u32); + elf_section.sh_type = try in.readInt(elf.endian, u32); + elf_section.flags = u64(try in.readInt(elf.endian, u32)); + elf_section.addr = u64(try in.readInt(elf.endian, u32)); + elf_section.offset = u64(try in.readInt(elf.endian, u32)); + elf_section.size = u64(try in.readInt(elf.endian, u32)); + elf_section.link = try in.readInt(elf.endian, u32); + elf_section.info = try in.readInt(elf.endian, u32); + elf_section.addr_align = u64(try in.readInt(elf.endian, u32)); + elf_section.ent_size = u64(try in.readInt(elf.endian, u32)); } } for (elf.section_headers) |*elf_section| { if (elf_section.sh_type != SHT_NOBITS) { - const file_end_offset = %return math.add(u64, elf_section.offset, elf_section.size); + const file_end_offset = try math.add(u64, elf_section.offset, elf_section.size); if (stream_end < file_end_offset) return error.InvalidFormat; } } @@ -247,15 +247,15 @@ pub const Elf = struct { if (elf_section.sh_type == SHT_NULL) continue; const name_offset = elf.string_section.offset + elf_section.name; - %return elf.in_file.seekTo(name_offset); + try elf.in_file.seekTo(name_offset); for (name) |expected_c| { - const target_c = %return in.readByte(); + const target_c = try in.readByte(); if (target_c == 0 or expected_c != target_c) continue :section_loop; } { - const null_byte = %return in.readByte(); + const null_byte = try in.readByte(); if (null_byte == 0) return elf_section; } } @@ -264,6 +264,6 @@ pub const Elf = struct { } pub fn seekToSection(elf: &Elf, elf_section: &SectionHeader) -> %void { - %return elf.in_file.seekTo(elf_section.offset); + try elf.in_file.seekTo(elf_section.offset); } }; diff --git a/std/endian.zig b/std/endian.zig index 9f2d2c8dcd..6f5bfff385 100644 --- a/std/endian.zig +++ b/std/endian.zig @@ -16,5 +16,5 @@ pub fn swapIf(endian: builtin.Endian, comptime T: type, x: T) -> T { pub fn swap(comptime T: type, x: T) -> T { var buf: [@sizeOf(T)]u8 = undefined; mem.writeInt(buf[0..], x, false); - return mem.readInt(buf, T, true); + return mem.readInt(buf, T, builtin.Endian.Big); } diff --git a/std/fmt/index.zig b/std/fmt/index.zig index 432b43bfef..0e82555874 100644 --- a/std/fmt/index.zig +++ b/std/fmt/index.zig @@ -40,13 +40,13 @@ pub fn format(context: var, output: fn(@typeOf(context), []const u8)->%void, State.Start => switch (c) { '{' => { if (start_index < i) { - %return output(context, fmt[start_index..i]); + try output(context, fmt[start_index..i]); } state = State.OpenBrace; }, '}' => { if (start_index < i) { - %return output(context, fmt[start_index..i]); + try output(context, fmt[start_index..i]); } state = State.CloseBrace; }, @@ -58,7 +58,7 @@ pub fn format(context: var, output: fn(@typeOf(context), []const u8)->%void, start_index = i; }, '}' => { - %return formatValue(args[next_arg], context, output); + try formatValue(args[next_arg], context, output); next_arg += 1; state = State.Start; start_index = i + 1; @@ -110,7 +110,7 @@ pub fn format(context: var, output: fn(@typeOf(context), []const u8)->%void, }, State.Integer => switch (c) { '}' => { - %return formatInt(args[next_arg], radix, uppercase, width, context, output); + try formatInt(args[next_arg], radix, uppercase, width, context, output); next_arg += 1; state = State.Start; start_index = i + 1; @@ -124,7 +124,7 @@ pub fn format(context: var, output: fn(@typeOf(context), []const u8)->%void, State.IntegerWidth => switch (c) { '}' => { width = comptime %%parseUnsigned(usize, fmt[width_start..i], 10); - %return formatInt(args[next_arg], radix, uppercase, width, context, output); + try formatInt(args[next_arg], radix, uppercase, width, context, output); next_arg += 1; state = State.Start; start_index = i + 1; @@ -134,7 +134,7 @@ pub fn format(context: var, output: fn(@typeOf(context), []const u8)->%void, }, State.Float => switch (c) { '}' => { - %return formatFloatDecimal(args[next_arg], 0, context, output); + try formatFloatDecimal(args[next_arg], 0, context, output); next_arg += 1; state = State.Start; start_index = i + 1; @@ -148,7 +148,7 @@ pub fn format(context: var, output: fn(@typeOf(context), []const u8)->%void, State.FloatWidth => switch (c) { '}' => { width = comptime %%parseUnsigned(usize, fmt[width_start..i], 10); - %return formatFloatDecimal(args[next_arg], width, context, output); + try formatFloatDecimal(args[next_arg], width, context, output); next_arg += 1; state = State.Start; start_index = i + 1; @@ -159,7 +159,7 @@ pub fn format(context: var, output: fn(@typeOf(context), []const u8)->%void, State.BufWidth => switch (c) { '}' => { width = comptime %%parseUnsigned(usize, fmt[width_start..i], 10); - %return formatBuf(args[next_arg], width, context, output); + try formatBuf(args[next_arg], width, context, output); next_arg += 1; state = State.Start; start_index = i + 1; @@ -169,7 +169,7 @@ pub fn format(context: var, output: fn(@typeOf(context), []const u8)->%void, }, State.Character => switch (c) { '}' => { - %return formatAsciiChar(args[next_arg], context, output); + try formatAsciiChar(args[next_arg], context, output); next_arg += 1; state = State.Start; start_index = i + 1; @@ -187,7 +187,7 @@ pub fn format(context: var, output: fn(@typeOf(context), []const u8)->%void, } } if (start_index < fmt.len) { - %return output(context, fmt[start_index..]); + try output(context, fmt[start_index..]); } } @@ -221,7 +221,7 @@ pub fn formatValue(value: var, context: var, output: fn(@typeOf(context), []cons } }, builtin.TypeId.Error => { - %return output(context, "error."); + try output(context, "error."); return output(context, @errorName(value)); }, builtin.TypeId.Pointer => { @@ -247,12 +247,12 @@ pub fn formatAsciiChar(c: u8, context: var, output: fn(@typeOf(context), []const pub fn formatBuf(buf: []const u8, width: usize, context: var, output: fn(@typeOf(context), []const u8)->%void) -> %void { - %return output(context, buf); + try output(context, buf); var leftover_padding = if (width > buf.len) (width - buf.len) else return; const pad_byte: u8 = ' '; while (leftover_padding > 0) : (leftover_padding -= 1) { - %return output(context, (&pad_byte)[0..1]); + try output(context, (&pad_byte)[0..1]); } } @@ -264,7 +264,7 @@ pub fn formatFloat(value: var, context: var, output: fn(@typeOf(context), []cons return output(context, "NaN"); } if (math.signbit(x)) { - %return output(context, "-"); + try output(context, "-"); x = -x; } if (math.isPositiveInf(x)) { @@ -276,21 +276,21 @@ pub fn formatFloat(value: var, context: var, output: fn(@typeOf(context), []cons var buffer: [32]u8 = undefined; const float_decimal = errol3(x, buffer[0..]); - %return output(context, float_decimal.digits[0..1]); - %return output(context, "."); + try output(context, float_decimal.digits[0..1]); + try output(context, "."); if (float_decimal.digits.len > 1) { const num_digits = if (@typeOf(value) == f32) math.min(usize(9), float_decimal.digits.len) else float_decimal.digits.len; - %return output(context, float_decimal.digits[1 .. num_digits]); + try output(context, float_decimal.digits[1 .. num_digits]); } else { - %return output(context, "0"); + try output(context, "0"); } if (float_decimal.exp != 1) { - %return output(context, "e"); - %return formatInt(float_decimal.exp - 1, 10, false, 0, context, output); + try output(context, "e"); + try formatInt(float_decimal.exp - 1, 10, false, 0, context, output); } } @@ -302,7 +302,7 @@ pub fn formatFloatDecimal(value: var, precision: usize, context: var, output: fn return output(context, "NaN"); } if (math.signbit(x)) { - %return output(context, "-"); + try output(context, "-"); x = -x; } if (math.isPositiveInf(x)) { @@ -317,8 +317,8 @@ pub fn formatFloatDecimal(value: var, precision: usize, context: var, output: fn const num_left_digits = if (float_decimal.exp > 0) usize(float_decimal.exp) else 1; - %return output(context, float_decimal.digits[0 .. num_left_digits]); - %return output(context, "."); + try output(context, float_decimal.digits[0 .. num_left_digits]); + try output(context, "."); if (float_decimal.digits.len > 1) { const num_valid_digtis = if (@typeOf(value) == f32) math.min(usize(7), float_decimal.digits.len) else @@ -328,9 +328,9 @@ pub fn formatFloatDecimal(value: var, precision: usize, context: var, output: fn math.min(precision, (num_valid_digtis-num_left_digits)) else num_valid_digtis - num_left_digits; - %return output(context, float_decimal.digits[num_left_digits .. (num_left_digits + num_right_digits)]); + try output(context, float_decimal.digits[num_left_digits .. (num_left_digits + num_right_digits)]); } else { - %return output(context, "0"); + try output(context, "0"); } } @@ -351,7 +351,7 @@ fn formatIntSigned(value: var, base: u8, uppercase: bool, width: usize, const uint = @IntType(false, @typeOf(value).bit_count); if (value < 0) { const minus_sign: u8 = '-'; - %return output(context, (&minus_sign)[0..1]); + try output(context, (&minus_sign)[0..1]); const new_value = uint(-(value + 1)) + 1; const new_width = if (width == 0) 0 else (width - 1); return formatIntUnsigned(new_value, base, uppercase, new_width, context, output); @@ -359,7 +359,7 @@ fn formatIntSigned(value: var, base: u8, uppercase: bool, width: usize, return formatIntUnsigned(uint(value), base, uppercase, width, context, output); } else { const plus_sign: u8 = '+'; - %return output(context, (&plus_sign)[0..1]); + try output(context, (&plus_sign)[0..1]); const new_value = uint(value); const new_width = if (width == 0) 0 else (width - 1); return formatIntUnsigned(new_value, base, uppercase, new_width, context, output); @@ -391,7 +391,7 @@ fn formatIntUnsigned(value: var, base: u8, uppercase: bool, width: usize, const zero_byte: u8 = '0'; var leftover_padding = padding - index; while (true) { - %return output(context, (&zero_byte)[0..1]); + try output(context, (&zero_byte)[0..1]); leftover_padding -= 1; if (leftover_padding == 0) break; @@ -428,7 +428,7 @@ pub fn parseInt(comptime T: type, buf: []const u8, radix: u8) -> %T { if (buf.len == 0) return T(0); if (buf[0] == '-') { - return math.negate(%return parseUnsigned(T, buf[1..], radix)); + return math.negate(try parseUnsigned(T, buf[1..], radix)); } else if (buf[0] == '+') { return parseUnsigned(T, buf[1..], radix); } else { @@ -450,9 +450,9 @@ pub fn parseUnsigned(comptime T: type, buf: []const u8, radix: u8) -> %T { var x: T = 0; for (buf) |c| { - const digit = %return charToDigit(c, radix); - x = %return math.mul(T, x, radix); - x = %return math.add(T, x, digit); + const digit = try charToDigit(c, radix); + x = try math.mul(T, x, radix); + x = try math.add(T, x, digit); } return x; @@ -494,7 +494,7 @@ fn bufPrintWrite(context: &BufPrintContext, bytes: []const u8) -> %void { pub fn bufPrint(buf: []u8, comptime fmt: []const u8, args: ...) -> %[]u8 { var context = BufPrintContext { .remaining = buf, }; - %return format(&context, bufPrintWrite, fmt, args); + try format(&context, bufPrintWrite, fmt, args); return buf[0..buf.len - context.remaining.len]; } @@ -502,7 +502,7 @@ pub fn allocPrint(allocator: &mem.Allocator, comptime fmt: []const u8, args: ... var size: usize = 0; // Cannot fail because `countSize` cannot fail. %%format(&size, countSize, fmt, args); - const buf = %return allocator.alloc(u8, size); + const buf = try allocator.alloc(u8, size); return bufPrint(buf, fmt, args); } @@ -533,7 +533,7 @@ fn bufPrintIntToSlice(buf: []u8, value: var, base: u8, uppercase: bool, width: u } test "parse u64 digit too big" { - _ = parseUnsigned(u64, "123a", 10) %% |err| { + _ = parseUnsigned(u64, "123a", 10) catch |err| { if (err == error.InvalidChar) return; unreachable; }; diff --git a/std/hash_map.zig b/std/hash_map.zig index cf83815798..dda1ecce47 100644 --- a/std/hash_map.zig +++ b/std/hash_map.zig @@ -83,14 +83,14 @@ pub fn HashMap(comptime K: type, comptime V: type, /// Returns the value that was already there. pub fn put(hm: &Self, key: K, value: &const V) -> %?V { if (hm.entries.len == 0) { - %return hm.initCapacity(16); + try hm.initCapacity(16); } hm.incrementModificationCount(); // if we get too full (60%), double the capacity if (hm.size * 5 >= hm.entries.len * 3) { const old_entries = hm.entries; - %return hm.initCapacity(hm.entries.len * 2); + try hm.initCapacity(hm.entries.len * 2); // dump all of the old elements into the new table for (old_entries) |*old_entry| { if (old_entry.used) { @@ -149,7 +149,7 @@ pub fn HashMap(comptime K: type, comptime V: type, } fn initCapacity(hm: &Self, capacity: usize) -> %void { - hm.entries = %return hm.allocator.alloc(Entry, capacity); + hm.entries = try hm.allocator.alloc(Entry, capacity); hm.size = 0; hm.max_distance_from_start_index = 0; for (hm.entries) |*entry| { diff --git a/std/heap.zig b/std/heap.zig index 6a78692ec6..6d7fb258be 100644 --- a/std/heap.zig +++ b/std/heap.zig @@ -49,7 +49,7 @@ pub const IncrementingAllocator = struct { fn init(capacity: usize) -> %IncrementingAllocator { switch (builtin.os) { - Os.linux, Os.darwin, Os.macosx, Os.ios => { + Os.linux, Os.macosx, Os.ios => { const p = os.posix; const addr = p.mmap(null, capacity, p.PROT_READ|p.PROT_WRITE, p.MAP_PRIVATE|p.MAP_ANONYMOUS|p.MAP_NORESERVE, -1, 0); @@ -87,7 +87,7 @@ pub const IncrementingAllocator = struct { fn deinit(self: &IncrementingAllocator) { switch (builtin.os) { - Os.linux, Os.darwin, Os.macosx, Os.ios => { + Os.linux, Os.macosx, Os.ios => { _ = os.posix.munmap(self.bytes.ptr, self.bytes.len); }, Os.windows => { @@ -124,7 +124,7 @@ pub const IncrementingAllocator = struct { if (new_size <= old_mem.len) { return old_mem[0..new_size]; } else { - const result = %return alloc(allocator, new_size, alignment); + const result = try alloc(allocator, new_size, alignment); mem.copy(u8, result, old_mem); return result; } @@ -137,9 +137,9 @@ pub const IncrementingAllocator = struct { test "c_allocator" { if (builtin.link_libc) { - var slice = c_allocator.alloc(u8, 50) %% return; + var slice = c_allocator.alloc(u8, 50) catch return; defer c_allocator.free(slice); - slice = c_allocator.realloc(u8, slice, 100) %% return; + slice = c_allocator.realloc(u8, slice, 100) catch return; } } diff --git a/std/io.zig b/std/io.zig index 9c39a02a8c..c6fd5502a2 100644 --- a/std/io.zig +++ b/std/io.zig @@ -3,7 +3,7 @@ const builtin = @import("builtin"); const Os = builtin.Os; const system = switch(builtin.os) { Os.linux => @import("os/linux.zig"), - Os.darwin, Os.macosx, Os.ios => @import("os/darwin.zig"), + Os.macosx, Os.ios => @import("os/darwin.zig"), Os.windows => @import("os/windows/index.zig"), else => @compileError("Unsupported OS"), }; @@ -51,7 +51,7 @@ error EndOfFile; pub fn getStdErr() -> %File { const handle = if (is_windows) - %return os.windowsGetStdHandle(system.STD_ERROR_HANDLE) + try os.windowsGetStdHandle(system.STD_ERROR_HANDLE) else if (is_posix) system.STDERR_FILENO else @@ -61,7 +61,7 @@ pub fn getStdErr() -> %File { pub fn getStdOut() -> %File { const handle = if (is_windows) - %return os.windowsGetStdHandle(system.STD_OUTPUT_HANDLE) + try os.windowsGetStdHandle(system.STD_OUTPUT_HANDLE) else if (is_posix) system.STDOUT_FILENO else @@ -71,7 +71,7 @@ pub fn getStdOut() -> %File { pub fn getStdIn() -> %File { const handle = if (is_windows) - %return os.windowsGetStdHandle(system.STD_INPUT_HANDLE) + try os.windowsGetStdHandle(system.STD_INPUT_HANDLE) else if (is_posix) system.STDIN_FILENO else @@ -131,10 +131,10 @@ pub const File = struct { pub fn openRead(path: []const u8, allocator: ?&mem.Allocator) -> %File { if (is_posix) { const flags = system.O_LARGEFILE|system.O_RDONLY; - const fd = %return os.posixOpen(path, flags, 0, allocator); + const fd = try os.posixOpen(path, flags, 0, allocator); return openHandle(fd); } else if (is_windows) { - const handle = %return os.windowsOpen(path, system.GENERIC_READ, system.FILE_SHARE_READ, + const handle = try os.windowsOpen(path, system.GENERIC_READ, system.FILE_SHARE_READ, system.OPEN_EXISTING, system.FILE_ATTRIBUTE_NORMAL, allocator); return openHandle(handle); } else { @@ -156,10 +156,10 @@ pub const File = struct { pub fn openWriteMode(path: []const u8, mode: usize, allocator: ?&mem.Allocator) -> %File { if (is_posix) { const flags = system.O_LARGEFILE|system.O_WRONLY|system.O_CREAT|system.O_CLOEXEC|system.O_TRUNC; - const fd = %return os.posixOpen(path, flags, mode, allocator); + const fd = try os.posixOpen(path, flags, mode, allocator); return openHandle(fd); } else if (is_windows) { - const handle = %return os.windowsOpen(path, system.GENERIC_WRITE, + const handle = try os.windowsOpen(path, system.GENERIC_WRITE, system.FILE_SHARE_WRITE|system.FILE_SHARE_READ|system.FILE_SHARE_DELETE, system.CREATE_ALWAYS, system.FILE_ATTRIBUTE_NORMAL, allocator); return openHandle(handle); @@ -190,7 +190,7 @@ pub const File = struct { pub fn seekForward(self: &File, amount: isize) -> %void { switch (builtin.os) { - Os.linux, Os.darwin => { + Os.linux, Os.macosx, Os.ios => { const result = system.lseek(self.handle, amount, system.SEEK_CUR); const err = system.getErrno(result); if (err > 0) { @@ -210,7 +210,7 @@ pub const File = struct { pub fn seekTo(self: &File, pos: usize) -> %void { switch (builtin.os) { - Os.linux, Os.darwin => { + Os.linux, Os.macosx, Os.ios => { const result = system.lseek(self.handle, @bitCast(isize, pos), system.SEEK_SET); const err = system.getErrno(result); if (err > 0) { @@ -230,7 +230,7 @@ pub const File = struct { pub fn getPos(self: &File) -> %usize { switch (builtin.os) { - Os.linux, Os.darwin => { + Os.linux, Os.macosx, Os.ios => { const result = system.lseek(self.handle, 0, system.SEEK_CUR); const err = system.getErrno(result); if (err > 0) { @@ -322,9 +322,9 @@ pub const File = struct { fn write(self: &File, bytes: []const u8) -> %void { if (is_posix) { - %return os.posixWrite(self.handle, bytes); + try os.posixWrite(self.handle, bytes); } else if (is_windows) { - %return os.windowsWrite(self.handle, bytes); + try os.windowsWrite(self.handle, bytes); } else { @compileError("Unsupported OS"); } @@ -344,12 +344,12 @@ pub const InStream = struct { /// If `buffer.len()` would exceed `max_size`, `error.StreamTooLong` is returned and /// the contents read from the stream are lost. pub fn readAllBuffer(self: &InStream, buffer: &Buffer, max_size: usize) -> %void { - %return buffer.resize(0); + try buffer.resize(0); var actual_buf_len: usize = 0; while (true) { const dest_slice = buffer.toSlice()[actual_buf_len..]; - const bytes_read = %return self.readFn(self, dest_slice); + const bytes_read = try self.readFn(self, dest_slice); actual_buf_len += bytes_read; if (bytes_read != dest_slice.len) { @@ -360,7 +360,7 @@ pub const InStream = struct { const new_buf_size = math.min(max_size, actual_buf_len + os.page_size); if (new_buf_size == actual_buf_len) return error.StreamTooLong; - %return buffer.resize(new_buf_size); + try buffer.resize(new_buf_size); } } @@ -372,7 +372,7 @@ pub const InStream = struct { var buf = Buffer.initNull(allocator); defer buf.deinit(); - %return self.readAllBuffer(&buf, max_size); + try self.readAllBuffer(&buf, max_size); return buf.toOwnedSlice(); } @@ -381,10 +381,10 @@ pub const InStream = struct { /// If `buffer.len()` would exceed `max_size`, `error.StreamTooLong` is returned and the contents /// read from the stream so far are lost. pub fn readUntilDelimiterBuffer(self: &InStream, buffer: &Buffer, delimiter: u8, max_size: usize) -> %void { - %return buf.resize(0); + try buf.resize(0); while (true) { - var byte: u8 = %return self.readByte(); + var byte: u8 = try self.readByte(); if (byte == delimiter) { return; @@ -394,7 +394,7 @@ pub const InStream = struct { return error.StreamTooLong; } - %return buf.appendByte(byte); + try buf.appendByte(byte); } } @@ -408,7 +408,7 @@ pub const InStream = struct { var buf = Buffer.initNull(allocator); defer buf.deinit(); - %return self.readUntilDelimiterBuffer(self, &buf, delimiter, max_size); + try self.readUntilDelimiterBuffer(self, &buf, delimiter, max_size); return buf.toOwnedSlice(); } @@ -421,20 +421,20 @@ pub const InStream = struct { /// Same as `read` but end of stream returns `error.EndOfStream`. pub fn readNoEof(self: &InStream, buf: []u8) -> %void { - const amt_read = %return self.read(buf); + const amt_read = try self.read(buf); if (amt_read < buf.len) return error.EndOfStream; } /// Reads 1 byte from the stream or returns `error.EndOfStream`. pub fn readByte(self: &InStream) -> %u8 { var result: [1]u8 = undefined; - %return self.readNoEof(result[0..]); + try self.readNoEof(result[0..]); return result[0]; } /// Same as `readByte` except the returned byte is signed. pub fn readByteSigned(self: &InStream) -> %i8 { - return @bitCast(i8, %return self.readByte()); + return @bitCast(i8, try self.readByte()); } pub fn readIntLe(self: &InStream, comptime T: type) -> %T { @@ -447,7 +447,7 @@ pub const InStream = struct { pub fn readInt(self: &InStream, endian: builtin.Endian, comptime T: type) -> %T { var bytes: [@sizeOf(T)]u8 = undefined; - %return self.readNoEof(bytes[0..]); + try self.readNoEof(bytes[0..]); return mem.readInt(bytes, T, endian); } @@ -456,7 +456,7 @@ pub const InStream = struct { assert(size <= 8); var input_buf: [8]u8 = undefined; const input_slice = input_buf[0..size]; - %return self.readNoEof(input_slice); + try self.readNoEof(input_slice); return mem.readInt(input_slice, T, endian); } @@ -483,7 +483,7 @@ pub const OutStream = struct { const slice = (&byte)[0..1]; var i: usize = 0; while (i < n) : (i += 1) { - %return self.writeFn(self, slice); + try self.writeFn(self, slice); } } }; @@ -493,9 +493,9 @@ pub const OutStream = struct { /// size buffer is too small, and the provided allocator is null, `error.NameTooLong` is returned. /// otherwise if the fixed size buffer is too small, allocator is used to obtain the needed memory. pub fn writeFile(path: []const u8, data: []const u8, allocator: ?&mem.Allocator) -> %void { - var file = %return File.openWrite(path, allocator); + var file = try File.openWrite(path, allocator); defer file.close(); - %return file.write(data); + try file.write(data); } /// On success, caller owns returned buffer. @@ -505,15 +505,15 @@ pub fn readFileAlloc(path: []const u8, allocator: &mem.Allocator) -> %[]u8 { /// On success, caller owns returned buffer. /// Allocates extra_len extra bytes at the end of the file buffer, which are uninitialized. pub fn readFileAllocExtra(path: []const u8, allocator: &mem.Allocator, extra_len: usize) -> %[]u8 { - var file = %return File.openRead(path, allocator); + var file = try File.openRead(path, allocator); defer file.close(); - const size = %return file.getEndPos(); - const buf = %return allocator.alloc(u8, size + extra_len); + const size = try file.getEndPos(); + const buf = try allocator.alloc(u8, size + extra_len); %defer allocator.free(buf); var adapter = FileInStream.init(&file); - %return adapter.stream.readNoEof(buf[0..size]); + try adapter.stream.readNoEof(buf[0..size]); return buf; } @@ -565,11 +565,11 @@ pub fn BufferedInStreamCustom(comptime buffer_size: usize) -> type { // we can read more data from the unbuffered stream if (dest_space < buffer_size) { self.start_index = 0; - self.end_index = %return self.unbuffered_in_stream.read(self.buffer[0..]); + self.end_index = try self.unbuffered_in_stream.read(self.buffer[0..]); } else { // asking for so much data that buffering is actually less efficient. // forward the request directly to the unbuffered stream - const amt_read = %return self.unbuffered_in_stream.read(dest[dest_index..]); + const amt_read = try self.unbuffered_in_stream.read(dest[dest_index..]); return dest_index + amt_read; } } else { @@ -616,7 +616,7 @@ pub fn BufferedOutStreamCustom(comptime buffer_size: usize) -> type { if (self.index == 0) return; - %return self.unbuffered_out_stream.write(self.buffer[0..self.index]); + try self.unbuffered_out_stream.write(self.buffer[0..self.index]); self.index = 0; } @@ -624,7 +624,7 @@ pub fn BufferedOutStreamCustom(comptime buffer_size: usize) -> type { const self = @fieldParentPtr(Self, "stream", out_stream); if (bytes.len >= self.buffer.len) { - %return self.flush(); + try self.flush(); return self.unbuffered_out_stream.write(bytes); } var src_index: usize = 0; @@ -636,7 +636,7 @@ pub fn BufferedOutStreamCustom(comptime buffer_size: usize) -> type { self.index += copy_amt; assert(self.index <= self.buffer.len); if (self.index == self.buffer.len) { - %return self.flush(); + try self.flush(); } src_index += copy_amt; } diff --git a/std/linked_list.zig b/std/linked_list.zig index 3c516dab7d..c33db4424b 100644 --- a/std/linked_list.zig +++ b/std/linked_list.zig @@ -188,7 +188,7 @@ pub fn LinkedList(comptime T: type) -> type { /// Returns: /// A pointer to the new node. pub fn createNode(list: &Self, data: &const T, allocator: &Allocator) -> %&Node { - var node = %return list.allocateNode(allocator); + var node = try list.allocateNode(allocator); *node = Node.init(data); return node; } diff --git a/std/mem.zig b/std/mem.zig index 41a4155b60..bc16abf362 100644 --- a/std/mem.zig +++ b/std/mem.zig @@ -27,7 +27,7 @@ pub const Allocator = struct { freeFn: fn (self: &Allocator, old_mem: []u8), fn create(self: &Allocator, comptime T: type) -> %&T { - const slice = %return self.alloc(T, 1); + const slice = try self.alloc(T, 1); return &slice[0]; } @@ -42,8 +42,8 @@ pub const Allocator = struct { fn alignedAlloc(self: &Allocator, comptime T: type, comptime alignment: u29, n: usize) -> %[]align(alignment) T { - const byte_count = %return math.mul(usize, @sizeOf(T), n); - const byte_slice = %return self.allocFn(self, byte_count, alignment); + const byte_count = try math.mul(usize, @sizeOf(T), n); + const byte_slice = try self.allocFn(self, byte_count, alignment); // This loop should get optimized out in ReleaseFast mode for (byte_slice) |*byte| { *byte = undefined; @@ -63,8 +63,8 @@ pub const Allocator = struct { } const old_byte_slice = ([]u8)(old_mem); - const byte_count = %return math.mul(usize, @sizeOf(T), n); - const byte_slice = %return self.reallocFn(self, old_byte_slice, byte_count, alignment); + const byte_count = try math.mul(usize, @sizeOf(T), n); + const byte_slice = try self.reallocFn(self, old_byte_slice, byte_count, alignment); // This loop should get optimized out in ReleaseFast mode for (byte_slice[old_byte_slice.len..]) |*byte| { *byte = undefined; @@ -142,7 +142,7 @@ pub const FixedBufferAllocator = struct { if (new_size <= old_mem.len) { return old_mem[0..new_size]; } else { - const result = %return alloc(allocator, new_size, alignment); + const result = try alloc(allocator, new_size, alignment); copy(u8, result, old_mem); return result; } @@ -198,7 +198,7 @@ pub fn eql(comptime T: type, a: []const T, b: []const T) -> bool { /// Copies ::m to newly allocated memory. Caller is responsible to free it. pub fn dupe(allocator: &Allocator, comptime T: type, m: []const T) -> %[]T { - const new_buf = %return allocator.alloc(T, m.len); + const new_buf = try allocator.alloc(T, m.len); copy(T, new_buf, m); return new_buf; } @@ -425,7 +425,7 @@ pub fn join(allocator: &Allocator, sep: u8, strings: ...) -> %[]u8 { } } - const buf = %return allocator.alloc(u8, total_strings_len); + const buf = try allocator.alloc(u8, total_strings_len); %defer allocator.free(buf); var buf_index: usize = 0; diff --git a/std/net.zig b/std/net.zig index da078eab3f..a51765c05d 100644 --- a/std/net.zig +++ b/std/net.zig @@ -133,7 +133,7 @@ pub fn connectAddr(addr: &Address, port: u16) -> %Connection { pub fn connect(hostname: []const u8, port: u16) -> %Connection { var addrs_buf: [1]Address = undefined; - const addrs_slice = %return lookup(hostname, addrs_buf[0..]); + const addrs_slice = try lookup(hostname, addrs_buf[0..]); const main_addr = &addrs_slice[0]; return connectAddr(main_addr, port); diff --git a/std/os/child_process.zig b/std/os/child_process.zig index 3a1cd02b56..e112cce66a 100644 --- a/std/os/child_process.zig +++ b/std/os/child_process.zig @@ -75,7 +75,7 @@ pub const ChildProcess = struct { /// First argument in argv is the executable. /// On success must call deinit. pub fn init(argv: []const []const u8, allocator: &mem.Allocator) -> %&ChildProcess { - const child = %return allocator.create(ChildProcess); + const child = try allocator.create(ChildProcess); %defer allocator.destroy(child); *child = ChildProcess { @@ -104,7 +104,7 @@ pub const ChildProcess = struct { } pub fn setUserName(self: &ChildProcess, name: []const u8) -> %void { - const user_info = %return os.getUserInfo(name); + const user_info = try os.getUserInfo(name); self.uid = user_info.uid; self.gid = user_info.gid; } @@ -120,7 +120,7 @@ pub const ChildProcess = struct { } pub fn spawnAndWait(self: &ChildProcess) -> %Term { - %return self.spawn(); + try self.spawn(); return self.wait(); } @@ -200,7 +200,7 @@ pub const ChildProcess = struct { child.cwd = cwd; child.env_map = env_map; - %return child.spawn(); + try child.spawn(); var stdout = Buffer.initNull(allocator); var stderr = Buffer.initNull(allocator); @@ -210,11 +210,11 @@ pub const ChildProcess = struct { var stdout_file_in_stream = io.FileInStream.init(&??child.stdout); var stderr_file_in_stream = io.FileInStream.init(&??child.stderr); - %return stdout_file_in_stream.stream.readAllBuffer(&stdout, max_output_size); - %return stderr_file_in_stream.stream.readAllBuffer(&stderr, max_output_size); + try stdout_file_in_stream.stream.readAllBuffer(&stdout, max_output_size); + try stderr_file_in_stream.stream.readAllBuffer(&stderr, max_output_size); return ExecResult { - .term = %return child.wait(), + .term = try child.wait(), .stdout = stdout.toOwnedSlice(), .stderr = stderr.toOwnedSlice(), }; @@ -226,7 +226,7 @@ pub const ChildProcess = struct { return term; } - %return self.waitUnwrappedWindows(); + try self.waitUnwrappedWindows(); return ??self.term; } @@ -308,8 +308,8 @@ pub const ChildProcess = struct { // pid potentially wrote an error. This way we can do a blocking // read on the error pipe and either get @maxValue(ErrInt) (no error) or // an error code. - %return writeIntFd(self.err_pipe[1], @maxValue(ErrInt)); - const err_int = %return readIntFd(self.err_pipe[0]); + try writeIntFd(self.err_pipe[1], @maxValue(ErrInt)); + const err_int = try readIntFd(self.err_pipe[0]); // Here we potentially return the fork child's error // from the parent pid. if (err_int != @maxValue(ErrInt)) { @@ -335,18 +335,18 @@ pub const ChildProcess = struct { // TODO atomically set a flag saying that we already did this install_SIGCHLD_handler(); - const stdin_pipe = if (self.stdin_behavior == StdIo.Pipe) %return makePipe() else undefined; + const stdin_pipe = if (self.stdin_behavior == StdIo.Pipe) try makePipe() else undefined; %defer if (self.stdin_behavior == StdIo.Pipe) { destroyPipe(stdin_pipe); }; - const stdout_pipe = if (self.stdout_behavior == StdIo.Pipe) %return makePipe() else undefined; + const stdout_pipe = if (self.stdout_behavior == StdIo.Pipe) try makePipe() else undefined; %defer if (self.stdout_behavior == StdIo.Pipe) { destroyPipe(stdout_pipe); }; - const stderr_pipe = if (self.stderr_behavior == StdIo.Pipe) %return makePipe() else undefined; + const stderr_pipe = if (self.stderr_behavior == StdIo.Pipe) try makePipe() else undefined; %defer if (self.stderr_behavior == StdIo.Pipe) { destroyPipe(stderr_pipe); }; const any_ignore = (self.stdin_behavior == StdIo.Ignore or self.stdout_behavior == StdIo.Ignore or self.stderr_behavior == StdIo.Ignore); const dev_null_fd = if (any_ignore) - %return os.posixOpen("/dev/null", posix.O_RDWR, 0, null) + try os.posixOpen("/dev/null", posix.O_RDWR, 0, null) else undefined ; @@ -359,14 +359,14 @@ pub const ChildProcess = struct { break :x env_map; } else x: { we_own_env_map = true; - env_map_owned = %return os.getEnvMap(self.allocator); + env_map_owned = try os.getEnvMap(self.allocator); break :x &env_map_owned; }; defer { if (we_own_env_map) env_map_owned.deinit(); } // This pipe is used to communicate errors between the time of fork // and execve from the child process to the parent process. - const err_pipe = %return makePipe(); + const err_pipe = try makePipe(); %defer destroyPipe(err_pipe); block_SIGCHLD(); @@ -383,27 +383,27 @@ pub const ChildProcess = struct { // we are the child restore_SIGCHLD(); - setUpChildIo(self.stdin_behavior, stdin_pipe[0], posix.STDIN_FILENO, dev_null_fd) %% + setUpChildIo(self.stdin_behavior, stdin_pipe[0], posix.STDIN_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err); - setUpChildIo(self.stdout_behavior, stdout_pipe[1], posix.STDOUT_FILENO, dev_null_fd) %% + setUpChildIo(self.stdout_behavior, stdout_pipe[1], posix.STDOUT_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err); - setUpChildIo(self.stderr_behavior, stderr_pipe[1], posix.STDERR_FILENO, dev_null_fd) %% + setUpChildIo(self.stderr_behavior, stderr_pipe[1], posix.STDERR_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err); if (self.cwd) |cwd| { - os.changeCurDir(self.allocator, cwd) %% + os.changeCurDir(self.allocator, cwd) catch |err| forkChildErrReport(err_pipe[1], err); } if (self.gid) |gid| { - os.posix_setregid(gid, gid) %% |err| forkChildErrReport(err_pipe[1], err); + os.posix_setregid(gid, gid) catch |err| forkChildErrReport(err_pipe[1], err); } if (self.uid) |uid| { - os.posix_setreuid(uid, uid) %% |err| forkChildErrReport(err_pipe[1], err); + os.posix_setreuid(uid, uid) catch |err| forkChildErrReport(err_pipe[1], err); } - os.posixExecve(self.argv, env_map, self.allocator) %% + os.posixExecve(self.argv, env_map, self.allocator) catch |err| forkChildErrReport(err_pipe[1], err); } @@ -452,14 +452,14 @@ pub const ChildProcess = struct { self.stderr_behavior == StdIo.Ignore); const nul_handle = if (any_ignore) - %return os.windowsOpen("NUL", windows.GENERIC_READ, windows.FILE_SHARE_READ, + try os.windowsOpen("NUL", windows.GENERIC_READ, windows.FILE_SHARE_READ, windows.OPEN_EXISTING, windows.FILE_ATTRIBUTE_NORMAL, null) else undefined ; defer { if (any_ignore) os.close(nul_handle); } if (any_ignore) { - %return windowsSetHandleInfo(nul_handle, windows.HANDLE_FLAG_INHERIT, 0); + try windowsSetHandleInfo(nul_handle, windows.HANDLE_FLAG_INHERIT, 0); } @@ -467,7 +467,7 @@ pub const ChildProcess = struct { var g_hChildStd_IN_Wr: ?windows.HANDLE = null; switch (self.stdin_behavior) { StdIo.Pipe => { - %return windowsMakePipeIn(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, saAttr); + try windowsMakePipeIn(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, saAttr); }, StdIo.Ignore => { g_hChildStd_IN_Rd = nul_handle; @@ -485,7 +485,7 @@ pub const ChildProcess = struct { var g_hChildStd_OUT_Wr: ?windows.HANDLE = null; switch (self.stdout_behavior) { StdIo.Pipe => { - %return windowsMakePipeOut(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, saAttr); + try windowsMakePipeOut(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, saAttr); }, StdIo.Ignore => { g_hChildStd_OUT_Wr = nul_handle; @@ -503,7 +503,7 @@ pub const ChildProcess = struct { var g_hChildStd_ERR_Wr: ?windows.HANDLE = null; switch (self.stderr_behavior) { StdIo.Pipe => { - %return windowsMakePipeOut(&g_hChildStd_ERR_Rd, &g_hChildStd_ERR_Wr, saAttr); + try windowsMakePipeOut(&g_hChildStd_ERR_Rd, &g_hChildStd_ERR_Wr, saAttr); }, StdIo.Ignore => { g_hChildStd_ERR_Wr = nul_handle; @@ -517,7 +517,7 @@ pub const ChildProcess = struct { } %defer if (self.stdin_behavior == StdIo.Pipe) { windowsDestroyPipe(g_hChildStd_ERR_Rd, g_hChildStd_ERR_Wr); }; - const cmd_line = %return windowsCreateCommandLine(self.allocator, self.argv); + const cmd_line = try windowsCreateCommandLine(self.allocator, self.argv); defer self.allocator.free(cmd_line); var siStartInfo = windows.STARTUPINFOA { @@ -544,7 +544,7 @@ pub const ChildProcess = struct { var piProcInfo: windows.PROCESS_INFORMATION = undefined; const cwd_slice = if (self.cwd) |cwd| - %return cstr.addNullByte(self.allocator, cwd) + try cstr.addNullByte(self.allocator, cwd) else null ; @@ -552,7 +552,7 @@ pub const ChildProcess = struct { const cwd_ptr = if (cwd_slice) |cwd| cwd.ptr else null; const maybe_envp_buf = if (self.env_map) |env_map| - %return os.createWindowsEnvBlock(self.allocator, env_map) + try os.createWindowsEnvBlock(self.allocator, env_map) else null ; @@ -563,27 +563,27 @@ pub const ChildProcess = struct { // to match posix semantics const app_name = x: { if (self.cwd) |cwd| { - const resolved = %return os.path.resolve(self.allocator, cwd, self.argv[0]); + const resolved = try os.path.resolve(self.allocator, cwd, self.argv[0]); defer self.allocator.free(resolved); - break :x %return cstr.addNullByte(self.allocator, resolved); + break :x try cstr.addNullByte(self.allocator, resolved); } else { - break :x %return cstr.addNullByte(self.allocator, self.argv[0]); + break :x try cstr.addNullByte(self.allocator, self.argv[0]); } }; defer self.allocator.free(app_name); windowsCreateProcess(app_name.ptr, cmd_line.ptr, envp_ptr, cwd_ptr, - &siStartInfo, &piProcInfo) %% |no_path_err| + &siStartInfo, &piProcInfo) catch |no_path_err| { if (no_path_err != error.FileNotFound) return no_path_err; - const PATH = %return os.getEnvVarOwned(self.allocator, "PATH"); + const PATH = try os.getEnvVarOwned(self.allocator, "PATH"); defer self.allocator.free(PATH); var it = mem.split(PATH, ";"); while (it.next()) |search_path| { - const joined_path = %return os.path.join(self.allocator, search_path, app_name); + const joined_path = try os.path.join(self.allocator, search_path, app_name); defer self.allocator.free(joined_path); if (windowsCreateProcess(joined_path.ptr, cmd_line.ptr, envp_ptr, cwd_ptr, @@ -625,10 +625,10 @@ pub const ChildProcess = struct { fn setUpChildIo(stdio: StdIo, pipe_fd: i32, std_fileno: i32, dev_null_fd: i32) -> %void { switch (stdio) { - StdIo.Pipe => %return os.posixDup2(pipe_fd, std_fileno), + StdIo.Pipe => try os.posixDup2(pipe_fd, std_fileno), StdIo.Close => os.close(std_fileno), StdIo.Inherit => {}, - StdIo.Ignore => %return os.posixDup2(dev_null_fd, std_fileno), + StdIo.Ignore => try os.posixDup2(dev_null_fd, std_fileno), } } @@ -656,35 +656,35 @@ fn windowsCreateProcess(app_name: &u8, cmd_line: &u8, envp_ptr: ?&u8, cwd_ptr: ? /// Caller must dealloc. /// Guarantees a null byte at result[result.len]. fn windowsCreateCommandLine(allocator: &mem.Allocator, argv: []const []const u8) -> %[]u8 { - var buf = %return Buffer.initSize(allocator, 0); + var buf = try Buffer.initSize(allocator, 0); defer buf.deinit(); for (argv) |arg, arg_i| { if (arg_i != 0) - %return buf.appendByte(' '); + try buf.appendByte(' '); if (mem.indexOfAny(u8, arg, " \t\n\"") == null) { - %return buf.append(arg); + try buf.append(arg); continue; } - %return buf.appendByte('"'); + try buf.appendByte('"'); var backslash_count: usize = 0; for (arg) |byte| { switch (byte) { '\\' => backslash_count += 1, '"' => { - %return buf.appendByteNTimes('\\', backslash_count * 2 + 1); - %return buf.appendByte('"'); + try buf.appendByteNTimes('\\', backslash_count * 2 + 1); + try buf.appendByte('"'); backslash_count = 0; }, else => { - %return buf.appendByteNTimes('\\', backslash_count); - %return buf.appendByte(byte); + try buf.appendByteNTimes('\\', backslash_count); + try buf.appendByte(byte); backslash_count = 0; }, } } - %return buf.appendByteNTimes('\\', backslash_count * 2); - %return buf.appendByte('"'); + try buf.appendByteNTimes('\\', backslash_count * 2); + try buf.appendByte('"'); } return buf.toOwnedSlice(); @@ -721,9 +721,9 @@ fn windowsSetHandleInfo(h: windows.HANDLE, mask: windows.DWORD, flags: windows.D fn windowsMakePipeIn(rd: &?windows.HANDLE, wr: &?windows.HANDLE, sattr: &const SECURITY_ATTRIBUTES) -> %void { var rd_h: windows.HANDLE = undefined; var wr_h: windows.HANDLE = undefined; - %return windowsMakePipe(&rd_h, &wr_h, sattr); + try windowsMakePipe(&rd_h, &wr_h, sattr); %defer windowsDestroyPipe(rd_h, wr_h); - %return windowsSetHandleInfo(wr_h, windows.HANDLE_FLAG_INHERIT, 0); + try windowsSetHandleInfo(wr_h, windows.HANDLE_FLAG_INHERIT, 0); *rd = rd_h; *wr = wr_h; } @@ -731,9 +731,9 @@ fn windowsMakePipeIn(rd: &?windows.HANDLE, wr: &?windows.HANDLE, sattr: &const S fn windowsMakePipeOut(rd: &?windows.HANDLE, wr: &?windows.HANDLE, sattr: &const SECURITY_ATTRIBUTES) -> %void { var rd_h: windows.HANDLE = undefined; var wr_h: windows.HANDLE = undefined; - %return windowsMakePipe(&rd_h, &wr_h, sattr); + try windowsMakePipe(&rd_h, &wr_h, sattr); %defer windowsDestroyPipe(rd_h, wr_h); - %return windowsSetHandleInfo(rd_h, windows.HANDLE_FLAG_INHERIT, 0); + try windowsSetHandleInfo(rd_h, windows.HANDLE_FLAG_INHERIT, 0); *rd = rd_h; *wr = wr_h; } @@ -767,12 +767,12 @@ const ErrInt = @IntType(false, @sizeOf(error) * 8); fn writeIntFd(fd: i32, value: ErrInt) -> %void { var bytes: [@sizeOf(ErrInt)]u8 = undefined; mem.writeInt(bytes[0..], value, builtin.endian); - os.posixWrite(fd, bytes[0..]) %% return error.SystemResources; + os.posixWrite(fd, bytes[0..]) catch return error.SystemResources; } fn readIntFd(fd: i32) -> %ErrInt { var bytes: [@sizeOf(ErrInt)]u8 = undefined; - os.posixRead(fd, bytes[0..]) %% return error.SystemResources; + os.posixRead(fd, bytes[0..]) catch return error.SystemResources; return mem.readInt(bytes[0..], ErrInt, builtin.endian); } diff --git a/std/os/get_user_id.zig b/std/os/get_user_id.zig index 07974fbe09..7485f788fc 100644 --- a/std/os/get_user_id.zig +++ b/std/os/get_user_id.zig @@ -11,7 +11,7 @@ pub const UserInfo = struct { /// POSIX function which gets a uid from username. pub fn getUserInfo(name: []const u8) -> %UserInfo { return switch (builtin.os) { - Os.linux, Os.darwin, Os.macosx, Os.ios => posixGetUserInfo(name), + Os.linux, Os.macosx, Os.ios => posixGetUserInfo(name), else => @compileError("Unsupported OS"), }; } @@ -31,7 +31,7 @@ error CorruptPasswordFile; // like NIS, AD, etc. See `man nss` or look at an strace for `id myuser`. pub fn posixGetUserInfo(name: []const u8) -> %UserInfo { - var in_stream = %return io.InStream.open("/etc/passwd", null); + var in_stream = try io.InStream.open("/etc/passwd", null); defer in_stream.close(); var buf: [os.page_size]u8 = undefined; @@ -41,7 +41,7 @@ pub fn posixGetUserInfo(name: []const u8) -> %UserInfo { var gid: u32 = 0; while (true) { - const amt_read = %return in_stream.read(buf[0..]); + const amt_read = try in_stream.read(buf[0..]); for (buf[0..amt_read]) |byte| { switch (state) { State.Start => switch (byte) { diff --git a/std/os/index.zig b/std/os/index.zig index 8ebd586475..44d1507f44 100644 --- a/std/os/index.zig +++ b/std/os/index.zig @@ -7,9 +7,11 @@ const os = this; pub const windows = @import("windows/index.zig"); pub const darwin = @import("darwin.zig"); pub const linux = @import("linux.zig"); +pub const zen = @import("zen.zig"); pub const posix = switch(builtin.os) { Os.linux => linux, - Os.darwin, Os.macosx, Os.ios => darwin, + Os.macosx, Os.ios => darwin, + Os.zen => zen, else => @compileError("Unsupported OS"), }; @@ -89,12 +91,12 @@ pub fn getRandomBytes(buf: []u8) -> %void { } return; }, - Os.darwin, Os.macosx, Os.ios => { - const fd = %return posixOpen("/dev/urandom", posix.O_RDONLY|posix.O_CLOEXEC, + Os.macosx, Os.ios => { + const fd = try posixOpen("/dev/urandom", posix.O_RDONLY|posix.O_CLOEXEC, 0, null); defer close(fd); - %return posixRead(fd, buf); + try posixRead(fd, buf); }, Os.windows => { var hCryptProv: windows.HCRYPTPROV = undefined; @@ -130,7 +132,7 @@ pub coldcc fn abort() -> noreturn { c.abort(); } switch (builtin.os) { - Os.linux, Os.darwin, Os.macosx, Os.ios => { + Os.linux, Os.macosx, Os.ios => { _ = posix.raise(posix.SIGABRT); _ = posix.raise(posix.SIGKILL); while (true) {} @@ -151,7 +153,7 @@ pub coldcc fn exit(status: i32) -> noreturn { c.exit(status); } switch (builtin.os) { - Os.linux, Os.darwin, Os.macosx, Os.ios => { + Os.linux, Os.macosx, Os.ios => { posix.exit(status); }, Os.windows => { @@ -254,7 +256,7 @@ pub fn posixOpen(file_path: []const u8, flags: u32, perm: usize, allocator: ?&Al if (file_path.len < stack_buf.len) { path0 = stack_buf[0..file_path.len + 1]; } else if (allocator) |a| { - path0 = %return a.alloc(u8, file_path.len + 1); + path0 = try a.alloc(u8, file_path.len + 1); need_free = true; } else { return error.NameTooLong; @@ -312,14 +314,14 @@ pub fn posixDup2(old_fd: i32, new_fd: i32) -> %void { pub fn createNullDelimitedEnvMap(allocator: &Allocator, env_map: &const BufMap) -> %[]?&u8 { const envp_count = env_map.count(); - const envp_buf = %return allocator.alloc(?&u8, envp_count + 1); + const envp_buf = try allocator.alloc(?&u8, envp_count + 1); mem.set(?&u8, envp_buf, null); %defer freeNullDelimitedEnvMap(allocator, envp_buf); { var it = env_map.iterator(); var i: usize = 0; while (it.next()) |pair| : (i += 1) { - const env_buf = %return allocator.alloc(u8, pair.key.len + pair.value.len + 2); + const env_buf = try allocator.alloc(u8, pair.key.len + pair.value.len + 2); @memcpy(&env_buf[0], pair.key.ptr, pair.key.len); env_buf[pair.key.len] = '='; @memcpy(&env_buf[pair.key.len + 1], pair.value.ptr, pair.value.len); @@ -349,7 +351,7 @@ pub fn freeNullDelimitedEnvMap(allocator: &Allocator, envp_buf: []?&u8) { pub fn posixExecve(argv: []const []const u8, env_map: &const BufMap, allocator: &Allocator) -> %void { - const argv_buf = %return allocator.alloc(?&u8, argv.len + 1); + const argv_buf = try allocator.alloc(?&u8, argv.len + 1); mem.set(?&u8, argv_buf, null); defer { for (argv_buf) |arg| { @@ -359,7 +361,7 @@ pub fn posixExecve(argv: []const []const u8, env_map: &const BufMap, allocator.free(argv_buf); } for (argv) |arg, i| { - const arg_buf = %return allocator.alloc(u8, arg.len + 1); + const arg_buf = try allocator.alloc(u8, arg.len + 1); @memcpy(&arg_buf[0], arg.ptr, arg.len); arg_buf[arg.len] = 0; @@ -367,7 +369,7 @@ pub fn posixExecve(argv: []const []const u8, env_map: &const BufMap, } argv_buf[argv.len] = null; - const envp_buf = %return createNullDelimitedEnvMap(allocator, env_map); + const envp_buf = try createNullDelimitedEnvMap(allocator, env_map); defer freeNullDelimitedEnvMap(allocator, envp_buf); const exe_path = argv[0]; @@ -379,7 +381,7 @@ pub fn posixExecve(argv: []const []const u8, env_map: &const BufMap, // PATH.len because it is >= the largest search_path // +1 for the / to join the search path and exe_path // +1 for the null terminating byte - const path_buf = %return allocator.alloc(u8, PATH.len + exe_path.len + 2); + const path_buf = try allocator.alloc(u8, PATH.len + exe_path.len + 2); defer allocator.free(path_buf); var it = mem.split(PATH, ":"); var seen_eacces = false; @@ -448,7 +450,7 @@ pub fn getEnvMap(allocator: &Allocator) -> %BufMap { i += 1; // skip over null byte - %return result.set(key, value); + try result.set(key, value); } } else { for (posix_environ_raw) |ptr| { @@ -460,7 +462,7 @@ pub fn getEnvMap(allocator: &Allocator) -> %BufMap { while (ptr[end_i] != 0) : (end_i += 1) {} const value = ptr[line_i + 1..end_i]; - %return result.set(key, value); + try result.set(key, value); } return result; } @@ -488,14 +490,14 @@ error EnvironmentVariableNotFound; /// Caller must free returned memory. pub fn getEnvVarOwned(allocator: &mem.Allocator, key: []const u8) -> %[]u8 { if (is_windows) { - const key_with_null = %return cstr.addNullByte(allocator, key); + const key_with_null = try cstr.addNullByte(allocator, key); defer allocator.free(key_with_null); - var buf = %return allocator.alloc(u8, 256); + var buf = try allocator.alloc(u8, 256); %defer allocator.free(buf); while (true) { - const windows_buf_len = %return math.cast(windows.DWORD, buf.len); + const windows_buf_len = try math.cast(windows.DWORD, buf.len); const result = windows.GetEnvironmentVariableA(key_with_null.ptr, buf.ptr, windows_buf_len); if (result == 0) { @@ -507,7 +509,7 @@ pub fn getEnvVarOwned(allocator: &mem.Allocator, key: []const u8) -> %[]u8 { } if (result > buf.len) { - buf = %return allocator.realloc(u8, buf, result); + buf = try allocator.realloc(u8, buf, result); continue; } @@ -523,7 +525,7 @@ pub fn getEnvVarOwned(allocator: &mem.Allocator, key: []const u8) -> %[]u8 { pub fn getCwd(allocator: &Allocator) -> %[]u8 { switch (builtin.os) { Os.windows => { - var buf = %return allocator.alloc(u8, 256); + var buf = try allocator.alloc(u8, 256); %defer allocator.free(buf); while (true) { @@ -537,7 +539,7 @@ pub fn getCwd(allocator: &Allocator) -> %[]u8 { } if (result > buf.len) { - buf = %return allocator.realloc(u8, buf, result); + buf = try allocator.realloc(u8, buf, result); continue; } @@ -545,12 +547,12 @@ pub fn getCwd(allocator: &Allocator) -> %[]u8 { } }, else => { - var buf = %return allocator.alloc(u8, 1024); + var buf = try allocator.alloc(u8, 1024); %defer allocator.free(buf); while (true) { const err = posix.getErrno(posix.getcwd(buf.ptr, buf.len)); if (err == posix.ERANGE) { - buf = %return allocator.realloc(u8, buf, buf.len * 2); + buf = try allocator.realloc(u8, buf, buf.len * 2); continue; } else if (err > 0) { return unexpectedErrorPosix(err); @@ -576,9 +578,9 @@ pub fn symLink(allocator: &Allocator, existing_path: []const u8, new_path: []con } pub fn symLinkWindows(allocator: &Allocator, existing_path: []const u8, new_path: []const u8) -> %void { - const existing_with_null = %return cstr.addNullByte(allocator, existing_path); + const existing_with_null = try cstr.addNullByte(allocator, existing_path); defer allocator.free(existing_with_null); - const new_with_null = %return cstr.addNullByte(allocator, new_path); + const new_with_null = try cstr.addNullByte(allocator, new_path); defer allocator.free(new_with_null); if (windows.CreateSymbolicLinkA(existing_with_null.ptr, new_with_null.ptr, 0) == 0) { @@ -590,7 +592,7 @@ pub fn symLinkWindows(allocator: &Allocator, existing_path: []const u8, new_path } pub fn symLinkPosix(allocator: &Allocator, existing_path: []const u8, new_path: []const u8) -> %void { - const full_buf = %return allocator.alloc(u8, existing_path.len + new_path.len + 2); + const full_buf = try allocator.alloc(u8, existing_path.len + new_path.len + 2); defer allocator.free(full_buf); const existing_buf = full_buf; @@ -636,11 +638,11 @@ pub fn atomicSymLink(allocator: &Allocator, existing_path: []const u8, new_path: } var rand_buf: [12]u8 = undefined; - const tmp_path = %return allocator.alloc(u8, new_path.len + base64.Base64Encoder.calcSize(rand_buf.len)); + const tmp_path = try allocator.alloc(u8, new_path.len + base64.Base64Encoder.calcSize(rand_buf.len)); defer allocator.free(tmp_path); mem.copy(u8, tmp_path[0..], new_path); while (true) { - %return getRandomBytes(rand_buf[0..]); + try getRandomBytes(rand_buf[0..]); b64_fs_encoder.encode(tmp_path[new_path.len..], rand_buf); if (symLink(allocator, existing_path, tmp_path)) { return rename(allocator, tmp_path, new_path); @@ -667,7 +669,7 @@ error FileNotFound; error AccessDenied; pub fn deleteFileWindows(allocator: &Allocator, file_path: []const u8) -> %void { - const buf = %return allocator.alloc(u8, file_path.len + 1); + const buf = try allocator.alloc(u8, file_path.len + 1); defer allocator.free(buf); mem.copy(u8, buf, file_path); @@ -685,7 +687,7 @@ pub fn deleteFileWindows(allocator: &Allocator, file_path: []const u8) -> %void } pub fn deleteFilePosix(allocator: &Allocator, file_path: []const u8) -> %void { - const buf = %return allocator.alloc(u8, file_path.len + 1); + const buf = try allocator.alloc(u8, file_path.len + 1); defer allocator.free(buf); mem.copy(u8, buf, file_path); @@ -719,30 +721,30 @@ pub fn copyFile(allocator: &Allocator, source_path: []const u8, dest_path: []con /// Guaranteed to be atomic. pub fn copyFileMode(allocator: &Allocator, source_path: []const u8, dest_path: []const u8, mode: usize) -> %void { var rand_buf: [12]u8 = undefined; - const tmp_path = %return allocator.alloc(u8, dest_path.len + base64.Base64Encoder.calcSize(rand_buf.len)); + const tmp_path = try allocator.alloc(u8, dest_path.len + base64.Base64Encoder.calcSize(rand_buf.len)); defer allocator.free(tmp_path); mem.copy(u8, tmp_path[0..], dest_path); - %return getRandomBytes(rand_buf[0..]); + try getRandomBytes(rand_buf[0..]); b64_fs_encoder.encode(tmp_path[dest_path.len..], rand_buf); - var out_file = %return io.File.openWriteMode(tmp_path, mode, allocator); + var out_file = try io.File.openWriteMode(tmp_path, mode, allocator); defer out_file.close(); %defer _ = deleteFile(allocator, tmp_path); - var in_file = %return io.File.openRead(source_path, allocator); + var in_file = try io.File.openRead(source_path, allocator); defer in_file.close(); var buf: [page_size]u8 = undefined; while (true) { - const amt = %return in_file.read(buf[0..]); - %return out_file.write(buf[0..amt]); + const amt = try in_file.read(buf[0..]); + try out_file.write(buf[0..amt]); if (amt != buf.len) return rename(allocator, tmp_path, dest_path); } } pub fn rename(allocator: &Allocator, old_path: []const u8, new_path: []const u8) -> %void { - const full_buf = %return allocator.alloc(u8, old_path.len + new_path.len + 2); + const full_buf = try allocator.alloc(u8, old_path.len + new_path.len + 2); defer allocator.free(full_buf); const old_buf = full_buf; @@ -795,7 +797,7 @@ pub fn makeDir(allocator: &Allocator, dir_path: []const u8) -> %void { } pub fn makeDirWindows(allocator: &Allocator, dir_path: []const u8) -> %void { - const path_buf = %return cstr.addNullByte(allocator, dir_path); + const path_buf = try cstr.addNullByte(allocator, dir_path); defer allocator.free(path_buf); if (windows.CreateDirectoryA(path_buf.ptr, null) == 0) { @@ -809,7 +811,7 @@ pub fn makeDirWindows(allocator: &Allocator, dir_path: []const u8) -> %void { } pub fn makeDirPosix(allocator: &Allocator, dir_path: []const u8) -> %void { - const path_buf = %return cstr.addNullByte(allocator, dir_path); + const path_buf = try cstr.addNullByte(allocator, dir_path); defer allocator.free(path_buf); const err = posix.getErrno(posix.mkdir(path_buf.ptr, 0o755)); @@ -835,12 +837,12 @@ pub fn makeDirPosix(allocator: &Allocator, dir_path: []const u8) -> %void { /// Calls makeDir recursively to make an entire path. Returns success if the path /// already exists and is a directory. pub fn makePath(allocator: &Allocator, full_path: []const u8) -> %void { - const resolved_path = %return path.resolve(allocator, full_path); + const resolved_path = try path.resolve(allocator, full_path); defer allocator.free(resolved_path); var end_index: usize = resolved_path.len; while (true) { - makeDir(allocator, resolved_path[0..end_index]) %% |err| { + makeDir(allocator, resolved_path[0..end_index]) catch |err| { if (err == error.PathAlreadyExists) { // TODO stat the file and return an error if it's not a directory // this is important because otherwise a dangling symlink @@ -873,7 +875,7 @@ pub fn makePath(allocator: &Allocator, full_path: []const u8) -> %void { /// Returns ::error.DirNotEmpty if the directory is not empty. /// To delete a directory recursively, see ::deleteTree pub fn deleteDir(allocator: &Allocator, dir_path: []const u8) -> %void { - const path_buf = %return allocator.alloc(u8, dir_path.len + 1); + const path_buf = try allocator.alloc(u8, dir_path.len + 1); defer allocator.free(path_buf); mem.copy(u8, path_buf, dir_path); @@ -913,7 +915,7 @@ pub fn deleteTree(allocator: &Allocator, full_path: []const u8) -> %void { return err; } { - var dir = Dir.open(allocator, full_path) %% |err| { + var dir = Dir.open(allocator, full_path) catch |err| { if (err == error.FileNotFound) return; if (err == error.NotDir) @@ -925,14 +927,14 @@ pub fn deleteTree(allocator: &Allocator, full_path: []const u8) -> %void { var full_entry_buf = ArrayList(u8).init(allocator); defer full_entry_buf.deinit(); - while (%return dir.next()) |entry| { - %return full_entry_buf.resize(full_path.len + entry.name.len + 1); + while (try dir.next()) |entry| { + try full_entry_buf.resize(full_path.len + entry.name.len + 1); const full_entry_path = full_entry_buf.toSlice(); mem.copy(u8, full_entry_path, full_path); full_entry_path[full_path.len] = '/'; mem.copy(u8, full_entry_path[full_path.len + 1..], entry.name); - %return deleteTree(allocator, full_entry_path); + try deleteTree(allocator, full_entry_path); } } return deleteDir(allocator, full_path); @@ -971,7 +973,7 @@ pub const Dir = struct { }; pub fn open(allocator: &Allocator, dir_path: []const u8) -> %Dir { - const fd = %return posixOpen(dir_path, posix.O_RDONLY|posix.O_DIRECTORY|posix.O_CLOEXEC, 0, allocator); + const fd = try posixOpen(dir_path, posix.O_RDONLY|posix.O_DIRECTORY|posix.O_CLOEXEC, 0, allocator); return Dir { .allocator = allocator, .fd = fd, @@ -992,7 +994,7 @@ pub const Dir = struct { start_over: while (true) { if (self.index >= self.end_index) { if (self.buf.len == 0) { - self.buf = %return self.allocator.alloc(u8, page_size); + self.buf = try self.allocator.alloc(u8, page_size); } while (true) { @@ -1002,7 +1004,7 @@ pub const Dir = struct { switch (err) { posix.EBADF, posix.EFAULT, posix.ENOTDIR => unreachable, posix.EINVAL => { - self.buf = %return self.allocator.realloc(u8, self.buf, self.buf.len * 2); + self.buf = try self.allocator.realloc(u8, self.buf, self.buf.len * 2); continue; }, else => return unexpectedErrorPosix(err), @@ -1046,7 +1048,7 @@ pub const Dir = struct { }; pub fn changeCurDir(allocator: &Allocator, dir_path: []const u8) -> %void { - const path_buf = %return allocator.alloc(u8, dir_path.len + 1); + const path_buf = try allocator.alloc(u8, dir_path.len + 1); defer allocator.free(path_buf); mem.copy(u8, path_buf, dir_path); @@ -1070,13 +1072,13 @@ pub fn changeCurDir(allocator: &Allocator, dir_path: []const u8) -> %void { /// Read value of a symbolic link. pub fn readLink(allocator: &Allocator, pathname: []const u8) -> %[]u8 { - const path_buf = %return allocator.alloc(u8, pathname.len + 1); + const path_buf = try allocator.alloc(u8, pathname.len + 1); defer allocator.free(path_buf); mem.copy(u8, path_buf, pathname); path_buf[pathname.len] = 0; - var result_buf = %return allocator.alloc(u8, 1024); + var result_buf = try allocator.alloc(u8, 1024); %defer allocator.free(result_buf); while (true) { const ret_val = posix.readlink(path_buf.ptr, result_buf.ptr, result_buf.len); @@ -1095,7 +1097,7 @@ pub fn readLink(allocator: &Allocator, pathname: []const u8) -> %[]u8 { }; } if (ret_val == result_buf.len) { - result_buf = %return allocator.realloc(u8, result_buf, result_buf.len * 2); + result_buf = try allocator.realloc(u8, result_buf, result_buf.len * 2); continue; } return allocator.shrink(u8, result_buf, ret_val); @@ -1104,7 +1106,7 @@ pub fn readLink(allocator: &Allocator, pathname: []const u8) -> %[]u8 { pub fn sleep(seconds: usize, nanoseconds: usize) { switch(builtin.os) { - Os.linux, Os.darwin, Os.macosx, Os.ios => { + Os.linux, Os.macosx, Os.ios => { posixSleep(u63(seconds), u63(nanoseconds)); }, Os.windows => { @@ -1318,7 +1320,7 @@ pub const ArgIteratorWindows = struct { } fn internalNext(self: &ArgIteratorWindows, allocator: &Allocator) -> %[]u8 { - var buf = %return Buffer.initSize(allocator, 0); + var buf = try Buffer.initSize(allocator, 0); defer buf.deinit(); var backslash_count: usize = 0; @@ -1328,34 +1330,34 @@ pub const ArgIteratorWindows = struct { 0 => return buf.toOwnedSlice(), '"' => { const quote_is_real = backslash_count % 2 == 0; - %return self.emitBackslashes(&buf, backslash_count / 2); + try self.emitBackslashes(&buf, backslash_count / 2); backslash_count = 0; if (quote_is_real) { self.seen_quote_count += 1; if (self.seen_quote_count == self.quote_count and self.seen_quote_count % 2 == 1) { - %return buf.appendByte('"'); + try buf.appendByte('"'); } } else { - %return buf.appendByte('"'); + try buf.appendByte('"'); } }, '\\' => { backslash_count += 1; }, ' ', '\t' => { - %return self.emitBackslashes(&buf, backslash_count); + try self.emitBackslashes(&buf, backslash_count); backslash_count = 0; if (self.seen_quote_count % 2 == 1 and self.seen_quote_count != self.quote_count) { - %return buf.appendByte(byte); + try buf.appendByte(byte); } else { return buf.toOwnedSlice(); } }, else => { - %return self.emitBackslashes(&buf, backslash_count); + try self.emitBackslashes(&buf, backslash_count); backslash_count = 0; - %return buf.appendByte(byte); + try buf.appendByte(byte); }, } } @@ -1364,7 +1366,7 @@ pub const ArgIteratorWindows = struct { fn emitBackslashes(self: &ArgIteratorWindows, buf: &Buffer, emit_count: usize) -> %void { var i: usize = 0; while (i < emit_count) : (i += 1) { - %return buf.appendByte('\\'); + try buf.appendByte('\\'); } } @@ -1428,24 +1430,24 @@ pub fn args() -> ArgIterator { pub fn argsAlloc(allocator: &mem.Allocator) -> %[]const []u8 { // TODO refactor to only make 1 allocation. var it = args(); - var contents = %return Buffer.initSize(allocator, 0); + var contents = try Buffer.initSize(allocator, 0); defer contents.deinit(); var slice_list = ArrayList(usize).init(allocator); defer slice_list.deinit(); while (it.next(allocator)) |arg_or_err| { - const arg = %return arg_or_err; + const arg = try arg_or_err; defer allocator.free(arg); - %return contents.append(arg); - %return slice_list.append(arg.len); + try contents.append(arg); + try slice_list.append(arg.len); } const contents_slice = contents.toSliceConst(); const slice_sizes = slice_list.toSliceConst(); - const slice_list_bytes = %return math.mul(usize, @sizeOf([]u8), slice_sizes.len); - const total_bytes = %return math.add(usize, slice_list_bytes, contents_slice.len); - const buf = %return allocator.alignedAlloc(u8, @alignOf([]u8), total_bytes); + const slice_list_bytes = try math.mul(usize, @sizeOf([]u8), slice_sizes.len); + const total_bytes = try math.add(usize, slice_list_bytes, contents_slice.len); + const buf = try allocator.alignedAlloc(u8, @alignOf([]u8), total_bytes); %defer allocator.free(buf); const result_slice_list = ([][]u8)(buf[0..slice_list_bytes]); @@ -1537,7 +1539,7 @@ pub fn openSelfExe() -> %io.File { Os.linux => { return io.File.openRead("/proc/self/exe", null); }, - Os.darwin => { + Os.macosx, Os.ios => { @panic("TODO: openSelfExe on Darwin"); }, else => @compileError("Unsupported OS"), @@ -1558,10 +1560,10 @@ pub fn selfExePath(allocator: &mem.Allocator) -> %[]u8 { return readLink(allocator, "/proc/self/exe"); }, Os.windows => { - var out_path = %return Buffer.initSize(allocator, 0xff); + var out_path = try Buffer.initSize(allocator, 0xff); %defer out_path.deinit(); while (true) { - const dword_len = %return math.cast(windows.DWORD, out_path.len()); + const dword_len = try math.cast(windows.DWORD, out_path.len()); const copied_amt = windows.GetModuleFileNameA(null, out_path.ptr(), dword_len); if (copied_amt <= 0) { const err = windows.GetLastError(); @@ -1574,14 +1576,14 @@ pub fn selfExePath(allocator: &mem.Allocator) -> %[]u8 { return out_path.toOwnedSlice(); } const new_len = (out_path.len() << 1) | 0b1; - %return out_path.resize(new_len); + try out_path.resize(new_len); } }, - Os.darwin, Os.macosx, Os.ios => { + Os.macosx, Os.ios => { var u32_len: u32 = 0; const ret1 = c._NSGetExecutablePath(undefined, &u32_len); assert(ret1 != 0); - const bytes = %return allocator.alloc(u8, u32_len); + const bytes = try allocator.alloc(u8, u32_len); %defer allocator.free(bytes); const ret2 = c._NSGetExecutablePath(bytes.ptr, &u32_len); assert(ret2 == 0); @@ -1600,13 +1602,13 @@ pub fn selfExeDirPath(allocator: &mem.Allocator) -> %[]u8 { // the file path looks something like `/a/b/c/exe (deleted)` // This path cannot be opened, but it's valid for determining the directory // the executable was in when it was run. - const full_exe_path = %return readLink(allocator, "/proc/self/exe"); + const full_exe_path = try readLink(allocator, "/proc/self/exe"); %defer allocator.free(full_exe_path); const dir = path.dirname(full_exe_path); return allocator.shrink(u8, full_exe_path, dir.len); }, - Os.windows, Os.darwin, Os.macosx, Os.ios => { - const self_exe_path = %return selfExePath(allocator); + Os.windows, Os.macosx, Os.ios => { + const self_exe_path = try selfExePath(allocator); %defer allocator.free(self_exe_path); const dirname = os.path.dirname(self_exe_path); return allocator.shrink(u8, self_exe_path, dirname.len); diff --git a/std/os/path.zig b/std/os/path.zig index fda1f60811..d14cc6ae20 100644 --- a/std/os/path.zig +++ b/std/os/path.zig @@ -412,13 +412,13 @@ pub fn resolveWindows(allocator: &Allocator, paths: []const []const u8) -> %[]u8 if (have_abs_path) { switch (have_drive_kind) { WindowsPath.Kind.Drive => { - result = %return allocator.alloc(u8, max_size); + result = try allocator.alloc(u8, max_size); mem.copy(u8, result, result_disk_designator); result_index += result_disk_designator.len; }, WindowsPath.Kind.NetworkShare => { - result = %return allocator.alloc(u8, max_size); + result = try allocator.alloc(u8, max_size); var it = mem.split(paths[first_index], "/\\"); const server_name = ??it.next(); const other_name = ??it.next(); @@ -438,10 +438,10 @@ pub fn resolveWindows(allocator: &Allocator, paths: []const []const u8) -> %[]u8 }, WindowsPath.Kind.None => { assert(is_windows); // resolveWindows called on non windows can't use getCwd - const cwd = %return os.getCwd(allocator); + const cwd = try os.getCwd(allocator); defer allocator.free(cwd); const parsed_cwd = windowsParsePath(cwd); - result = %return allocator.alloc(u8, max_size + parsed_cwd.disk_designator.len + 1); + result = try allocator.alloc(u8, max_size + parsed_cwd.disk_designator.len + 1); mem.copy(u8, result, parsed_cwd.disk_designator); result_index += parsed_cwd.disk_designator.len; result_disk_designator = result[0..parsed_cwd.disk_designator.len]; @@ -454,10 +454,10 @@ pub fn resolveWindows(allocator: &Allocator, paths: []const []const u8) -> %[]u8 } else { assert(is_windows); // resolveWindows called on non windows can't use getCwd // TODO call get cwd for the result_disk_designator instead of the global one - const cwd = %return os.getCwd(allocator); + const cwd = try os.getCwd(allocator); defer allocator.free(cwd); - result = %return allocator.alloc(u8, max_size + cwd.len + 1); + result = try allocator.alloc(u8, max_size + cwd.len + 1); mem.copy(u8, result, cwd); result_index += cwd.len; @@ -542,12 +542,12 @@ pub fn resolvePosix(allocator: &Allocator, paths: []const []const u8) -> %[]u8 { var result_index: usize = 0; if (have_abs) { - result = %return allocator.alloc(u8, max_size); + result = try allocator.alloc(u8, max_size); } else { assert(!is_windows); // resolvePosix called on windows can't use getCwd - const cwd = %return os.getCwd(allocator); + const cwd = try os.getCwd(allocator); defer allocator.free(cwd); - result = %return allocator.alloc(u8, max_size + cwd.len + 1); + result = try allocator.alloc(u8, max_size + cwd.len + 1); mem.copy(u8, result, cwd); result_index += cwd.len; } @@ -899,11 +899,11 @@ pub fn relative(allocator: &Allocator, from: []const u8, to: []const u8) -> %[]u } pub fn relativeWindows(allocator: &Allocator, from: []const u8, to: []const u8) -> %[]u8 { - const resolved_from = %return resolveWindows(allocator, [][]const u8{from}); + const resolved_from = try resolveWindows(allocator, [][]const u8{from}); defer allocator.free(resolved_from); var clean_up_resolved_to = true; - const resolved_to = %return resolveWindows(allocator, [][]const u8{to}); + const resolved_to = try resolveWindows(allocator, [][]const u8{to}); defer if (clean_up_resolved_to) allocator.free(resolved_to); const parsed_from = windowsParsePath(resolved_from); @@ -942,7 +942,7 @@ pub fn relativeWindows(allocator: &Allocator, from: []const u8, to: []const u8) up_count += 1; } const up_index_end = up_count * "..\\".len; - const result = %return allocator.alloc(u8, up_index_end + to_rest.len); + const result = try allocator.alloc(u8, up_index_end + to_rest.len); %defer allocator.free(result); var result_index: usize = 0; @@ -972,10 +972,10 @@ pub fn relativeWindows(allocator: &Allocator, from: []const u8, to: []const u8) } pub fn relativePosix(allocator: &Allocator, from: []const u8, to: []const u8) -> %[]u8 { - const resolved_from = %return resolvePosix(allocator, [][]const u8{from}); + const resolved_from = try resolvePosix(allocator, [][]const u8{from}); defer allocator.free(resolved_from); - const resolved_to = %return resolvePosix(allocator, [][]const u8{to}); + const resolved_to = try resolvePosix(allocator, [][]const u8{to}); defer allocator.free(resolved_to); var from_it = mem.split(resolved_from, "/"); @@ -992,7 +992,7 @@ pub fn relativePosix(allocator: &Allocator, from: []const u8, to: []const u8) -> up_count += 1; } const up_index_end = up_count * "../".len; - const result = %return allocator.alloc(u8, up_index_end + to_rest.len); + const result = try allocator.alloc(u8, up_index_end + to_rest.len); %defer allocator.free(result); var result_index: usize = 0; @@ -1080,7 +1080,7 @@ error InputOutput; pub fn real(allocator: &Allocator, pathname: []const u8) -> %[]u8 { switch (builtin.os) { Os.windows => { - const pathname_buf = %return allocator.alloc(u8, pathname.len + 1); + const pathname_buf = try allocator.alloc(u8, pathname.len + 1); defer allocator.free(pathname_buf); mem.copy(u8, pathname_buf, pathname); @@ -1099,10 +1099,10 @@ pub fn real(allocator: &Allocator, pathname: []const u8) -> %[]u8 { }; } defer os.close(h_file); - var buf = %return allocator.alloc(u8, 256); + var buf = try allocator.alloc(u8, 256); %defer allocator.free(buf); while (true) { - const buf_len = math.cast(windows.DWORD, buf.len) %% return error.NameTooLong; + const buf_len = math.cast(windows.DWORD, buf.len) catch return error.NameTooLong; const result = windows.GetFinalPathNameByHandleA(h_file, buf.ptr, buf_len, windows.VOLUME_NAME_DOS); if (result == 0) { @@ -1116,7 +1116,7 @@ pub fn real(allocator: &Allocator, pathname: []const u8) -> %[]u8 { } if (result > buf.len) { - buf = %return allocator.realloc(u8, buf, result); + buf = try allocator.realloc(u8, buf, result); continue; } @@ -1137,13 +1137,13 @@ pub fn real(allocator: &Allocator, pathname: []const u8) -> %[]u8 { return allocator.shrink(u8, buf, final_len); } }, - Os.darwin, Os.macosx, Os.ios => { + Os.macosx, Os.ios => { // TODO instead of calling the libc function here, port the implementation // to Zig, and then remove the NameTooLong error possibility. - const pathname_buf = %return allocator.alloc(u8, pathname.len + 1); + const pathname_buf = try allocator.alloc(u8, pathname.len + 1); defer allocator.free(pathname_buf); - const result_buf = %return allocator.alloc(u8, posix.PATH_MAX); + const result_buf = try allocator.alloc(u8, posix.PATH_MAX); %defer allocator.free(result_buf); mem.copy(u8, pathname_buf, pathname); @@ -1168,7 +1168,7 @@ pub fn real(allocator: &Allocator, pathname: []const u8) -> %[]u8 { return allocator.shrink(u8, result_buf, cstr.len(result_buf.ptr)); }, Os.linux => { - const fd = %return os.posixOpen(pathname, posix.O_PATH|posix.O_NONBLOCK|posix.O_CLOEXEC, 0, allocator); + const fd = try os.posixOpen(pathname, posix.O_PATH|posix.O_NONBLOCK|posix.O_CLOEXEC, 0, allocator); defer os.close(fd); var buf: ["/proc/self/fd/-2147483648".len]u8 = undefined; diff --git a/std/os/windows/util.zig b/std/os/windows/util.zig index 5b2dc1efb8..c92575c31d 100644 --- a/std/os/windows/util.zig +++ b/std/os/windows/util.zig @@ -93,7 +93,7 @@ pub fn windowsOpen(file_path: []const u8, desired_access: windows.DWORD, share_m if (file_path.len < stack_buf.len) { path0 = stack_buf[0..file_path.len + 1]; } else if (allocator) |a| { - path0 = %return a.alloc(u8, file_path.len + 1); + path0 = try a.alloc(u8, file_path.len + 1); need_free = true; } else { return error.NameTooLong; @@ -132,7 +132,7 @@ pub fn createWindowsEnvBlock(allocator: &mem.Allocator, env_map: &const BufMap) } break :x bytes_needed; }; - const result = %return allocator.alloc(u8, bytes_needed); + const result = try allocator.alloc(u8, bytes_needed); %defer allocator.free(result); var it = env_map.iterator(); @@ -153,7 +153,7 @@ pub fn createWindowsEnvBlock(allocator: &mem.Allocator, env_map: &const BufMap) error DllNotFound; pub fn windowsLoadDll(allocator: &mem.Allocator, dll_path: []const u8) -> %windows.HMODULE { - const padded_buff = %return cstr.addNullByte(allocator, dll_path); + const padded_buff = try cstr.addNullByte(allocator, dll_path); defer allocator.free(padded_buff); return windows.LoadLibraryA(padded_buff.ptr) ?? error.DllNotFound; } @@ -166,7 +166,7 @@ pub fn windowsUnloadDll(hModule: windows.HMODULE) { test "InvalidDll" { const DllName = "asdf.dll"; const allocator = std.debug.global_allocator; - const handle = os.windowsLoadDll(allocator, DllName) %% |err| { + const handle = os.windowsLoadDll(allocator, DllName) catch |err| { assert(err == error.DllNotFound); return; }; diff --git a/std/os/zen.zig b/std/os/zen.zig new file mode 100644 index 0000000000..ffc491e404 --- /dev/null +++ b/std/os/zen.zig @@ -0,0 +1,94 @@ +////////////////////////////// +//// Reserved mailboxes //// +////////////////////////////// + +pub const MBOX_TERMINAL = 1; + + +/////////////////////////// +//// Syscall numbers //// +/////////////////////////// + +pub const SYS_createMailbox = 0; +pub const SYS_send = 1; +pub const SYS_receive = 2; +pub const SYS_map = 3; + + +//////////////////// +//// Syscalls //// +//////////////////// + +pub fn createMailbox(id: u16) { + _ = syscall1(SYS_createMailbox, id); +} + +pub fn send(mailbox_id: u16, data: usize) { + _ = syscall2(SYS_send, mailbox_id, data); +} + +pub fn receive(mailbox_id: u16) -> usize { + return syscall1(SYS_receive, mailbox_id); +} + +pub fn map(v_addr: usize, p_addr: usize, size: usize, writable: bool) -> bool { + return syscall4(SYS_map, v_addr, p_addr, size, usize(writable)) != 0; +} + + +///////////////////////// +//// Syscall stubs //// +///////////////////////// + +pub inline fn syscall0(number: usize) -> usize { + return asm volatile ("int $0x80" + : [ret] "={eax}" (-> usize) + : [number] "{eax}" (number)); +} + +pub inline fn syscall1(number: usize, arg1: usize) -> usize { + return asm volatile ("int $0x80" + : [ret] "={eax}" (-> usize) + : [number] "{eax}" (number), + [arg1] "{ecx}" (arg1)); +} + +pub inline fn syscall2(number: usize, arg1: usize, arg2: usize) -> usize { + return asm volatile ("int $0x80" + : [ret] "={eax}" (-> usize) + : [number] "{eax}" (number), + [arg1] "{ecx}" (arg1), + [arg2] "{edx}" (arg2)); +} + +pub inline fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) -> usize { + return asm volatile ("int $0x80" + : [ret] "={eax}" (-> usize) + : [number] "{eax}" (number), + [arg1] "{ecx}" (arg1), + [arg2] "{edx}" (arg2), + [arg3] "{ebx}" (arg3)); +} + +pub inline fn syscall4(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize) -> usize { + return asm volatile ("int $0x80" + : [ret] "={eax}" (-> usize) + : [number] "{eax}" (number), + [arg1] "{ecx}" (arg1), + [arg2] "{edx}" (arg2), + [arg3] "{ebx}" (arg3), + [arg4] "{esi}" (arg4)); +} + +pub inline fn syscall5(number: usize, arg1: usize, arg2: usize, arg3: usize, + arg4: usize, arg5: usize) -> usize +{ + return asm volatile ("int $0x80" + : [ret] "={eax}" (-> usize) + : [number] "{eax}" (number), + [arg1] "{ecx}" (arg1), + [arg2] "{edx}" (arg2), + [arg3] "{ebx}" (arg3), + [arg4] "{esi}" (arg4), + [arg5] "{edi}" (arg5)); +} diff --git a/std/rand.zig b/std/rand.zig index f35229ea3c..037cf68cd4 100644 --- a/std/rand.zig +++ b/std/rand.zig @@ -43,7 +43,7 @@ pub const Rand = struct { } else { var result: [@sizeOf(T)]u8 = undefined; r.fillBytes(result[0..]); - return mem.readInt(result, T, false); + return mem.readInt(result, T, builtin.Endian.Little); } } diff --git a/std/special/bootstrap.zig b/std/special/bootstrap.zig index 177e245400..28175de725 100644 --- a/std/special/bootstrap.zig +++ b/std/special/bootstrap.zig @@ -11,6 +11,8 @@ comptime { const strong_linkage = builtin.GlobalLinkage.Strong; if (builtin.link_libc) { @export("main", main, strong_linkage); + } else if (builtin.os == builtin.Os.zen) { + @export("main", zenMain, strong_linkage); } else if (builtin.os == builtin.Os.windows) { @export("WinMainCRTStartup", WinMainCRTStartup, strong_linkage); } else { @@ -18,6 +20,12 @@ comptime { } } +extern fn zenMain() -> noreturn { + // TODO: call exit. + root.main() catch {}; + while (true) {} +} + nakedcc fn _start() -> noreturn { switch (builtin.arch) { builtin.Arch.x86_64 => { @@ -36,7 +44,7 @@ nakedcc fn _start() -> noreturn { extern fn WinMainCRTStartup() -> noreturn { @setAlignStack(16); - root.main() %% std.os.windows.ExitProcess(1); + root.main() catch std.os.windows.ExitProcess(1); std.os.windows.ExitProcess(0); } @@ -44,7 +52,7 @@ fn posixCallMainAndExit() -> noreturn { const argc = *argc_ptr; const argv = @ptrCast(&&u8, &argc_ptr[1]); const envp = @ptrCast(&?&u8, &argv[argc + 1]); - callMain(argc, argv, envp) %% std.os.posix.exit(1); + callMain(argc, argv, envp) catch std.os.posix.exit(1); std.os.posix.exit(0); } @@ -59,6 +67,6 @@ fn callMain(argc: usize, argv: &&u8, envp: &?&u8) -> %void { } extern fn main(c_argc: i32, c_argv: &&u8, c_envp: &?&u8) -> i32 { - callMain(usize(c_argc), c_argv, c_envp) %% return 1; + callMain(usize(c_argc), c_argv, c_envp) catch return 1; return 0; } diff --git a/std/special/build_runner.zig b/std/special/build_runner.zig index 18007538ab..284c04a236 100644 --- a/std/special/build_runner.zig +++ b/std/special/build_runner.zig @@ -23,15 +23,15 @@ pub fn main() -> %void { // skip my own exe name _ = arg_it.skip(); - const zig_exe = %return unwrapArg(arg_it.next(allocator) ?? { + const zig_exe = try unwrapArg(arg_it.next(allocator) ?? { warn("Expected first argument to be path to zig compiler\n"); return error.InvalidArgs; }); - const build_root = %return unwrapArg(arg_it.next(allocator) ?? { + const build_root = try unwrapArg(arg_it.next(allocator) ?? { warn("Expected second argument to be build root directory path\n"); return error.InvalidArgs; }); - const cache_root = %return unwrapArg(arg_it.next(allocator) ?? { + const cache_root = try unwrapArg(arg_it.next(allocator) ?? { warn("Expected third argument to be cache root directory path\n"); return error.InvalidArgs; }); @@ -58,36 +58,36 @@ pub fn main() -> %void { } else |err| err; while (arg_it.next(allocator)) |err_or_arg| { - const arg = %return unwrapArg(err_or_arg); + const arg = try unwrapArg(err_or_arg); if (mem.startsWith(u8, arg, "-D")) { const option_contents = arg[2..]; if (option_contents.len == 0) { warn("Expected option name after '-D'\n\n"); - return usageAndErr(&builder, false, %return stderr_stream); + return usageAndErr(&builder, false, try stderr_stream); } if (mem.indexOfScalar(u8, option_contents, '=')) |name_end| { const option_name = option_contents[0..name_end]; const option_value = option_contents[name_end + 1..]; if (builder.addUserInputOption(option_name, option_value)) - return usageAndErr(&builder, false, %return stderr_stream); + return usageAndErr(&builder, false, try stderr_stream); } else { if (builder.addUserInputFlag(option_contents)) - return usageAndErr(&builder, false, %return stderr_stream); + return usageAndErr(&builder, false, try stderr_stream); } } else if (mem.startsWith(u8, arg, "-")) { if (mem.eql(u8, arg, "--verbose")) { builder.verbose = true; } else if (mem.eql(u8, arg, "--help")) { - return usage(&builder, false, %return stdout_stream); + return usage(&builder, false, try stdout_stream); } else if (mem.eql(u8, arg, "--prefix")) { - prefix = %return unwrapArg(arg_it.next(allocator) ?? { + prefix = try unwrapArg(arg_it.next(allocator) ?? { warn("Expected argument after --prefix\n\n"); - return usageAndErr(&builder, false, %return stderr_stream); + return usageAndErr(&builder, false, try stderr_stream); }); } else if (mem.eql(u8, arg, "--search-prefix")) { - const search_prefix = %return unwrapArg(arg_it.next(allocator) ?? { + const search_prefix = try unwrapArg(arg_it.next(allocator) ?? { warn("Expected argument after --search-prefix\n\n"); - return usageAndErr(&builder, false, %return stderr_stream); + return usageAndErr(&builder, false, try stderr_stream); }); builder.addSearchPrefix(search_prefix); } else if (mem.eql(u8, arg, "--verbose-tokenize")) { @@ -104,7 +104,7 @@ pub fn main() -> %void { builder.verbose_cimport = true; } else { warn("Unrecognized argument: {}\n\n", arg); - return usageAndErr(&builder, false, %return stderr_stream); + return usageAndErr(&builder, false, try stderr_stream); } } else { %%targets.append(arg); @@ -115,11 +115,11 @@ pub fn main() -> %void { root.build(&builder); if (builder.validateUserInputDidItFail()) - return usageAndErr(&builder, true, %return stderr_stream); + return usageAndErr(&builder, true, try stderr_stream); - builder.make(targets.toSliceConst()) %% |err| { + builder.make(targets.toSliceConst()) catch |err| { if (err == error.InvalidStepName) { - return usageAndErr(&builder, true, %return stderr_stream); + return usageAndErr(&builder, true, try stderr_stream); } return err; }; @@ -133,7 +133,7 @@ fn usage(builder: &Builder, already_ran_build: bool, out_stream: &io.OutStream) } // This usage text has to be synchronized with src/main.cpp - %return out_stream.print( + try out_stream.print( \\Usage: {} build [steps] [options] \\ \\Steps: @@ -142,10 +142,10 @@ fn usage(builder: &Builder, already_ran_build: bool, out_stream: &io.OutStream) const allocator = builder.allocator; for (builder.top_level_steps.toSliceConst()) |top_level_step| { - %return out_stream.print(" {s22} {}\n", top_level_step.step.name, top_level_step.description); + try out_stream.print(" {s22} {}\n", top_level_step.step.name, top_level_step.description); } - %return out_stream.write( + try out_stream.write( \\ \\General Options: \\ --help Print this help and exit @@ -158,17 +158,17 @@ fn usage(builder: &Builder, already_ran_build: bool, out_stream: &io.OutStream) ); if (builder.available_options_list.len == 0) { - %return out_stream.print(" (none)\n"); + try out_stream.print(" (none)\n"); } else { for (builder.available_options_list.toSliceConst()) |option| { - const name = %return fmt.allocPrint(allocator, + const name = try fmt.allocPrint(allocator, " -D{}=[{}]", option.name, Builder.typeIdName(option.type_id)); defer allocator.free(name); - %return out_stream.print("{s24} {}\n", name, option.description); + try out_stream.print("{s24} {}\n", name, option.description); } } - %return out_stream.write( + try out_stream.write( \\ \\Advanced Options: \\ --build-file [file] Override path to build.zig @@ -184,12 +184,12 @@ fn usage(builder: &Builder, already_ran_build: bool, out_stream: &io.OutStream) } fn usageAndErr(builder: &Builder, already_ran_build: bool, out_stream: &io.OutStream) -> error { - usage(builder, already_ran_build, out_stream) %% {}; + usage(builder, already_ran_build, out_stream) catch {}; return error.InvalidArgs; } fn unwrapArg(arg: %[]u8) -> %[]u8 { - return arg %% |err| { + return arg catch |err| { warn("Unable to parse command line: {}\n", err); return err; }; diff --git a/std/special/panic.zig b/std/special/panic.zig index 78117a5366..03c2586739 100644 --- a/std/special/panic.zig +++ b/std/special/panic.zig @@ -6,9 +6,13 @@ const builtin = @import("builtin"); pub coldcc fn panic(msg: []const u8) -> noreturn { - if (builtin.os == builtin.Os.freestanding) { - while (true) {} - } else { - @import("std").debug.panic("{}", msg); + switch (builtin.os) { + // TODO: fix panic in zen. + builtin.Os.freestanding, builtin.Os.zen => { + while (true) {} + }, + else => { + @import("std").debug.panic("{}", msg); + }, } } diff --git a/std/unicode.zig b/std/unicode.zig index 6c06eeb73a..c821011558 100644 --- a/std/unicode.zig +++ b/std/unicode.zig @@ -162,7 +162,7 @@ fn testValid(bytes: []const u8, expected_codepoint: u32) { } fn testDecode(bytes: []const u8) -> %u32 { - const length = %return utf8ByteSequenceLength(bytes[0]); + const length = try utf8ByteSequenceLength(bytes[0]); if (bytes.len < length) return error.UnexpectedEof; std.debug.assert(bytes.len == length); return utf8Decode(bytes); |
