Commit a5007d819a

xackus <14938807+xackus@users.noreply.github.com>
2021-04-11 16:26:29
std.meta: add isError
1 parent cab1d5c
Changed files (4)
lib/std/json/test.zig
@@ -29,8 +29,7 @@ fn ok(s: []const u8) !void {
 fn err(s: []const u8) void {
     testing.expect(!json.validate(s));
 
-    testNonStreaming(s) catch return;
-    testing.expect(false);
+    testing.expect(std.meta.isError(testNonStreaming(s)));
 }
 
 fn utf8Error(s: []const u8) void {
@@ -48,8 +47,7 @@ fn any(s: []const u8) void {
 fn anyStreamingErrNonStreaming(s: []const u8) void {
     _ = json.validate(s);
 
-    testNonStreaming(s) catch return;
-    testing.expect(false);
+    testing.expect(std.meta.isError(testNonStreaming(s)));
 }
 
 fn roundTrip(s: []const u8) !void {
lib/std/meta.zig
@@ -1334,3 +1334,13 @@ test "shuffleVectorIndex" {
     testing.expect(shuffleVectorIndex(6, vector_len) == -3);
     testing.expect(shuffleVectorIndex(7, vector_len) == -4);
 }
+
+/// Returns whether `error_union` contains an error.
+pub fn isError(error_union: anytype) bool {
+    return if(error_union) |_| false else |_| true;
+}
+
+test "isError" {
+    std.testing.expect(isError(math.absInt(@as(i8, -128))));
+    std.testing.expect(!isError(math.absInt(@as(i8, -127))));
+}
lib/std/unicode.zig
@@ -206,7 +206,7 @@ pub fn utf8ValidateSlice(s: []const u8) bool {
                 return false;
             }
 
-            if (utf8Decode(s[i .. i + cp_len])) |_| {} else |_| {
+            if (std.meta.isError(utf8Decode(s[i .. i + cp_len]))) {
                 return false;
             }
             i += cp_len;
src/translate_c.zig
@@ -8,6 +8,7 @@ const ctok = std.c.tokenizer;
 const CToken = std.c.Token;
 const mem = std.mem;
 const math = std.math;
+const meta = std.meta;
 const ast = @import("translate_c/ast.zig");
 const Node = ast.Node;
 const Tag = Node.Tag;
@@ -1737,7 +1738,7 @@ fn transImplicitCastExpr(
 }
 
 fn isBuiltinDefined(name: []const u8) bool {
-    inline for (std.meta.declarations(c_builtins)) |decl| {
+    inline for (meta.declarations(c_builtins)) |decl| {
         if (std.mem.eql(u8, name, decl.name)) return true;
     }
     return false;
@@ -3136,7 +3137,7 @@ const ClangFunctionType = union(enum) {
     NoProto: *const clang.FunctionType,
 
     fn getReturnType(self: @This()) clang.QualType {
-        switch (@as(std.meta.Tag(@This()), self)) {
+        switch (@as(meta.Tag(@This()), self)) {
             .Proto => return self.Proto.getReturnType(),
             .NoProto => return self.NoProto.getReturnType(),
         }
@@ -4072,7 +4073,7 @@ fn transCreateNodeAPInt(c: *Context, int: *const clang.APSInt) !Node {
 }
 
 fn transCreateNodeNumber(c: *Context, num: anytype, num_kind: enum { int, float }) !Node {
-    const fmt_s = if (comptime std.meta.trait.isNumber(@TypeOf(num))) "{d}" else "{s}";
+    const fmt_s = if (comptime meta.trait.isNumber(@TypeOf(num))) "{d}" else "{s}";
     const str = try std.fmt.allocPrint(c.arena, fmt_s, .{num});
     if (num_kind == .float)
         return Tag.float_literal.create(c.arena, str)
@@ -4827,12 +4828,12 @@ fn parseCNumLit(c: *Context, m: *MacroCtx) ParseError!Node {
             // make the output less noisy by skipping promoteIntLiteral where
             // it's guaranteed to not be required because of C standard type constraints
             const guaranteed_to_fit = switch (suffix) {
-                .none => if (math.cast(i16, value)) |_| true else |_| false,
-                .u => if (math.cast(u16, value)) |_| true else |_| false,
-                .l => if (math.cast(i32, value)) |_| true else |_| false,
-                .lu => if (math.cast(u32, value)) |_| true else |_| false,
-                .ll => if (math.cast(i64, value)) |_| true else |_| false,
-                .llu => if (math.cast(u64, value)) |_| true else |_| false,
+                .none => !meta.isError(math.cast(i16, value)),
+                .u => !meta.isError(math.cast(u16, value)),
+                .l => !meta.isError(math.cast(i32, value)),
+                .lu => !meta.isError(math.cast(u32, value)),
+                .ll => !meta.isError(math.cast(i64, value)),
+                .llu => !meta.isError(math.cast(u64, value)),
                 .f => unreachable,
             };