authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-10-11 11:51:39 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-10-11 11:51:39 -07:00
log1cbf74a6e96c0591c8979ef0eabf714bdaa6eefd
treed4f157cc731fcd5442f8deead6d8ff3227e295a0
parent94d2ad2f7b187287101947911442d9126927c904

add u.fail for explicit noreturn assert and exit


9 files changed, 18 insertions(+), 19 deletions(-)

src/cmd/aq.zig+1-1
...@@ -39,7 +39,7 @@ pub fn execute(args: [][]u8) !void {...@@ -39,7 +39,7 @@ pub fn execute(args: [][]u8) !void {
39 return;39 return;
40 }40 }
41 }41 }
42 u.assert(false, "unknown command \"{s}\" for \"zigmod aq\"", .{args[0]});42 u.fail("unknown command \"{s}\" for \"zigmod aq\"", .{args[0]});
43}43}
4444
45pub fn server_fetch(url: []const u8) !json.Value {45pub fn server_fetch(url: []const u8) !json.Value {
src/cmd/fetch.zig+1-1
...@@ -322,7 +322,7 @@ fn print_pkg_data_to(w: std.fs.File.Writer, notdone: *std.ArrayList(u.Module), d...@@ -322,7 +322,7 @@ fn print_pkg_data_to(w: std.fs.File.Writer, notdone: *std.ArrayList(u.Module), d
322 }322 }
323 }323 }
324 if (notdone.items.len == len) {324 if (notdone.items.len == len) {
325 u.assert(false, "notdone still has {d} items", .{len});325 u.fail("notdone still has {d} items", .{len});
326 }326 }
327 len = notdone.items.len;327 len = notdone.items.len;
328 }328 }
src/cmd/init.zig+1-2
...@@ -31,8 +31,7 @@ pub fn execute(args: [][]u8) !void {...@@ -31,8 +31,7 @@ pub fn execute(args: [][]u8) !void {
3131
32 const name = try inquirer.forString(stdout, stdin, "package name:", gpa, u.detect_pkgname(u.try_index([]const u8, args, 0, ""), "") catch |err| switch (err) {32 const name = try inquirer.forString(stdout, stdin, "package name:", gpa, u.detect_pkgname(u.try_index([]const u8, args, 0, ""), "") catch |err| switch (err) {
33 error.NoBuildZig => {33 error.NoBuildZig => {
34 u.assert(false, "init requires a build.zig file", .{});34 u.fail("init requires a build.zig file", .{});
35 unreachable;
36 },35 },
37 else => return err,36 else => return err,
38 });37 });
src/cmd/zpm.zig+1-1
...@@ -50,7 +50,7 @@ pub fn execute(args: [][]u8) !void {...@@ -50,7 +50,7 @@ pub fn execute(args: [][]u8) !void {
50 return;50 return;
51 }51 }
52 }52 }
53 u.assert(false, "unknown command \"{s}\" for \"zigmod zpm\"", .{args[0]});53 u.fail("unknown command \"{s}\" for \"zigmod zpm\"", .{args[0]});
54}54}
5555
56pub fn server_fetch(url: []const u8) !json.Value {56pub fn server_fetch(url: []const u8) !json.Value {
src/cmd/zpm/add.zig+1-2
...@@ -28,8 +28,7 @@ pub fn execute(args: [][]u8) !void {...@@ -28,8 +28,7 @@ pub fn execute(args: [][]u8) !void {
28 break :blk pkg;28 break :blk pkg;
29 }29 }
30 }30 }
31 u.assert(false, "no package with name '{s}' found", .{args[0]});31 u.fail("no package with name '{s}' found", .{args[0]});
32 unreachable;
33 };32 };
3433
35 u.assert(found.root_file != null, "package must have an entry point to be able to be added to your dependencies", .{});34 u.assert(found.root_file != null, "package must have an entry point to be able to be added to your dependencies", .{});
src/common.zig+5-7
...@@ -127,8 +127,7 @@ pub fn get_modpath(cachepath: []const u8, d: u.Dep, options: *CollectOptions) ![...@@ -127,8 +127,7 @@ pub fn get_modpath(cachepath: []const u8, d: u.Dep, options: *CollectOptions) ![
127 error.IterEmpty => unreachable,127 error.IterEmpty => unreachable,
128 error.NoMemberFound => {128 error.NoMemberFound => {
129 const vtype = d.version[0..std.mem.indexOf(u8, d.version, "-").?];129 const vtype = d.version[0..std.mem.indexOf(u8, d.version, "-").?];
130 u.assert(false, "fetch: git: version type '{s}' is invalid.", .{vtype});130 u.fail("fetch: git: version type '{s}' is invalid.", .{vtype});
131 unreachable;
132 },131 },
133 };132 };
134 if (try u.does_folder_exist(pv)) {133 if (try u.does_folder_exist(pv)) {
...@@ -141,7 +140,7 @@ pub fn get_modpath(cachepath: []const u8, d: u.Dep, options: *CollectOptions) ![...@@ -141,7 +140,7 @@ pub fn get_modpath(cachepath: []const u8, d: u.Dep, options: *CollectOptions) ![
141 }140 }
142 try d.type.pull(d.path, pv);141 try d.type.pull(d.path, pv);
143 if ((try u.run_cmd(pv, &.{ "git", "checkout", vers.string })) > 0) {142 if ((try u.run_cmd(pv, &.{ "git", "checkout", vers.string })) > 0) {
144 u.assert(false, "fetch: git: {s}: {s} {s} does not exist", .{ d.path, @tagName(vers.id), vers.string });143 u.fail("fetch: git: {s}: {s} {s} does not exist", .{ d.path, @tagName(vers.id), vers.string });
145 }144 }
146 if (builtin.os.tag != .windows and vers.id != .branch) {145 if (builtin.os.tag != .windows and vers.id != .branch) {
147 const pvd = try std.fs.cwd().openDir(pv, .{});146 const pvd = try std.fs.cwd().openDir(pv, .{});
...@@ -184,7 +183,7 @@ pub fn get_modpath(cachepath: []const u8, d: u.Dep, options: *CollectOptions) ![...@@ -184,7 +183,7 @@ pub fn get_modpath(cachepath: []const u8, d: u.Dep, options: *CollectOptions) ![
184 return pv;183 return pv;
185 }184 }
186 try std.fs.cwd().deleteTree(pv);185 try std.fs.cwd().deleteTree(pv);
187 u.assert(false, "{s} does not match hash {s}", .{ d.path, d.version });186 u.fail("{s} does not match hash {s}", .{ d.path, d.version });
188 return p;187 return p;
189 }188 }
190 if (try u.does_folder_exist(p)) {189 if (try u.does_folder_exist(p)) {
...@@ -253,8 +252,7 @@ pub fn get_module_from_dep(d: *u.Dep, cachepath: []const u8, options: *CollectOp...@@ -253,8 +252,7 @@ pub fn get_module_from_dep(d: *u.Dep, cachepath: []const u8, options: *CollectOp
253 if (mod_from.is_for_this()) return mod_from;252 if (mod_from.is_for_this()) return mod_from;
254 return null;253 return null;
255 }254 }
256 u.assert(false, "no zig.mod found and no override props defined. unable to use add this dependency!", .{});255 u.fail("no zig.mod found and no override props defined. unable to use add this dependency!", .{});
257 unreachable;
258 },256 },
259 else => e,257 else => e,
260 };258 };
...@@ -390,7 +388,7 @@ pub fn parse_lockfile(dir: std.fs.Dir) ![]const [4][]const u8 {...@@ -390,7 +388,7 @@ pub fn parse_lockfile(dir: std.fs.Dir) ![]const [4][]const u8 {
390 });388 });
391 },389 },
392 else => {390 else => {
393 u.assert(false, "invalid zigmod.lock version: {d}", .{v});391 u.fail("invalid zigmod.lock version: {d}", .{v});
394 },392 },
395 }393 }
396 }394 }
src/main.zig+1-2
...@@ -63,8 +63,7 @@ pub fn main() !void {...@@ -63,8 +63,7 @@ pub fn main() !void {
63 const result = std.ChildProcess.exec(.{ .allocator = gpa, .argv = sub_cmd_args.items }) catch |e| switch (e) {63 const result = std.ChildProcess.exec(.{ .allocator = gpa, .argv = sub_cmd_args.items }) catch |e| switch (e) {
64 else => return e,64 else => return e,
65 error.FileNotFound => {65 error.FileNotFound => {
66 u.assert(false, "unknown command \"{s}\" for \"zigmod\"", .{args[0]});66 u.fail("unknown command \"{s}\" for \"zigmod\"", .{args[0]});
67 unreachable;
68 },67 },
69 };68 };
70 try std.io.getStdOut().writeAll(result.stdout);69 try std.io.getStdOut().writeAll(result.stdout);
src/util/funcs.zig+6-2
...@@ -25,6 +25,11 @@ pub fn assert(ok: bool, comptime fmt: []const u8, args: anytype) void {...@@ -25,6 +25,11 @@ pub fn assert(ok: bool, comptime fmt: []const u8, args: anytype) void {
25 }25 }
26}26}
2727
28pub fn fail(comptime fmt: []const u8, args: anytype) noreturn {
29 assert(false, fmt, args);
30 unreachable;
31}
32
28pub fn try_index(comptime T: type, array: []T, n: usize, def: T) T {33pub fn try_index(comptime T: type, array: []T, n: usize, def: T) T {
29 if (array.len <= n) {34 if (array.len <= n) {
30 return def;35 return def;
...@@ -160,8 +165,7 @@ pub fn file_list(dpath: []const u8, list: *std.ArrayList([]const u8)) !void {...@@ -160,8 +165,7 @@ pub fn file_list(dpath: []const u8, list: *std.ArrayList([]const u8)) !void {
160pub fn run_cmd_raw(dir: ?[]const u8, args: []const []const u8) !std.ChildProcess.ExecResult {165pub fn run_cmd_raw(dir: ?[]const u8, args: []const []const u8) !std.ChildProcess.ExecResult {
161 return std.ChildProcess.exec(.{ .allocator = gpa, .cwd = dir, .argv = args, .max_output_bytes = std.math.maxInt(usize) }) catch |e| switch (e) {166 return std.ChildProcess.exec(.{ .allocator = gpa, .cwd = dir, .argv = args, .max_output_bytes = std.math.maxInt(usize) }) catch |e| switch (e) {
162 error.FileNotFound => {167 error.FileNotFound => {
163 u.assert(false, "\"{s}\" command not found", .{args[0]});168 u.fail("\"{s}\" command not found", .{args[0]});
164 unreachable;
165 },169 },
166 else => return e,170 else => return e,
167 };171 };
src/util/modfile.zig+1-1
...@@ -48,7 +48,7 @@ pub const ModFile = struct {...@@ -48,7 +48,7 @@ pub const ModFile = struct {
48 const main = mapping.get_string("main");48 const main = mapping.get_string("main");
4949
50 if (std.mem.indexOf(u8, name, "/")) |_| {50 if (std.mem.indexOf(u8, name, "/")) |_| {
51 u.assert(false, "name may not contain any '/'", .{});51 u.fail("name may not contain any '/'", .{});
52 }52 }
5353
54 return Self{54 return Self{