authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-25 13:27:44-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-04-25 13:27:44-04:00
log6f61594692af846226880e5903ad26a922014d55
tree4524089b8c9cc6c69c65a73cf20380a66fcc29be
parent5a94794b73c8f9d042bc7928d071cc1004487983
parent89c41f30c3e62e8b5c03457d1f95dc115a27ef15
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #8496 from xackus/isError

std.meta: add isError

4 files changed, 23 insertions(+), 14 deletions(-)

lib/std/json/test.zig+2-4
...@@ -29,8 +29,7 @@ fn ok(s: []const u8) !void {...@@ -29,8 +29,7 @@ fn ok(s: []const u8) !void {
29fn err(s: []const u8) void {29fn err(s: []const u8) void {
30 testing.expect(!json.validate(s));30 testing.expect(!json.validate(s));
3131
32 testNonStreaming(s) catch return;32 testing.expect(std.meta.isError(testNonStreaming(s)));
33 testing.expect(false);
34}33}
3534
36fn utf8Error(s: []const u8) void {35fn utf8Error(s: []const u8) void {
...@@ -48,8 +47,7 @@ fn any(s: []const u8) void {...@@ -48,8 +47,7 @@ fn any(s: []const u8) void {
48fn anyStreamingErrNonStreaming(s: []const u8) void {47fn anyStreamingErrNonStreaming(s: []const u8) void {
49 _ = json.validate(s);48 _ = json.validate(s);
5049
51 testNonStreaming(s) catch return;50 testing.expect(std.meta.isError(testNonStreaming(s)));
52 testing.expect(false);
53}51}
5452
55fn roundTrip(s: []const u8) !void {53fn roundTrip(s: []const u8) !void {
lib/std/meta.zig+10
...@@ -1346,3 +1346,13 @@ test "shuffleVectorIndex" {...@@ -1346,3 +1346,13 @@ test "shuffleVectorIndex" {
1346 testing.expect(shuffleVectorIndex(6, vector_len) == -3);1346 testing.expect(shuffleVectorIndex(6, vector_len) == -3);
1347 testing.expect(shuffleVectorIndex(7, vector_len) == -4);1347 testing.expect(shuffleVectorIndex(7, vector_len) == -4);
1348}1348}
1349
1350/// Returns whether `error_union` contains an error.
1351pub fn isError(error_union: anytype) bool {
1352 return if (error_union) |_| false else |_| true;
1353}
1354
1355test "isError" {
1356 std.testing.expect(isError(math.absInt(@as(i8, -128))));
1357 std.testing.expect(!isError(math.absInt(@as(i8, -127))));
1358}
lib/std/unicode.zig+1-1
...@@ -206,7 +206,7 @@ pub fn utf8ValidateSlice(s: []const u8) bool {...@@ -206,7 +206,7 @@ pub fn utf8ValidateSlice(s: []const u8) bool {
206 return false;206 return false;
207 }207 }
208208
209 if (utf8Decode(s[i .. i + cp_len])) |_| {} else |_| {209 if (std.meta.isError(utf8Decode(s[i .. i + cp_len]))) {
210 return false;210 return false;
211 }211 }
212 i += cp_len;212 i += cp_len;
src/translate_c.zig+10-9
...@@ -8,6 +8,7 @@ const ctok = std.c.tokenizer;...@@ -8,6 +8,7 @@ const ctok = std.c.tokenizer;
8const CToken = std.c.Token;8const CToken = std.c.Token;
9const mem = std.mem;9const mem = std.mem;
10const math = std.math;10const math = std.math;
11const meta = std.meta;
11const ast = @import("translate_c/ast.zig");12const ast = @import("translate_c/ast.zig");
12const Node = ast.Node;13const Node = ast.Node;
13const Tag = Node.Tag;14const Tag = Node.Tag;
...@@ -1741,7 +1742,7 @@ fn transImplicitCastExpr(...@@ -1741,7 +1742,7 @@ fn transImplicitCastExpr(
1741}1742}
17421743
1743fn isBuiltinDefined(name: []const u8) bool {1744fn isBuiltinDefined(name: []const u8) bool {
1744 inline for (std.meta.declarations(c_builtins)) |decl| {1745 inline for (meta.declarations(c_builtins)) |decl| {
1745 if (std.mem.eql(u8, name, decl.name)) return true;1746 if (std.mem.eql(u8, name, decl.name)) return true;
1746 }1747 }
1747 return false;1748 return false;
...@@ -3157,7 +3158,7 @@ const ClangFunctionType = union(enum) {...@@ -3157,7 +3158,7 @@ const ClangFunctionType = union(enum) {
3157 NoProto: *const clang.FunctionType,3158 NoProto: *const clang.FunctionType,
31583159
3159 fn getReturnType(self: @This()) clang.QualType {3160 fn getReturnType(self: @This()) clang.QualType {
3160 switch (@as(std.meta.Tag(@This()), self)) {3161 switch (@as(meta.Tag(@This()), self)) {
3161 .Proto => return self.Proto.getReturnType(),3162 .Proto => return self.Proto.getReturnType(),
3162 .NoProto => return self.NoProto.getReturnType(),3163 .NoProto => return self.NoProto.getReturnType(),
3163 }3164 }
...@@ -4106,7 +4107,7 @@ fn transCreateNodeAPInt(c: *Context, int: *const clang.APSInt) !Node {...@@ -4106,7 +4107,7 @@ fn transCreateNodeAPInt(c: *Context, int: *const clang.APSInt) !Node {
4106}4107}
41074108
4108fn transCreateNodeNumber(c: *Context, num: anytype, num_kind: enum { int, float }) !Node {4109fn transCreateNodeNumber(c: *Context, num: anytype, num_kind: enum { int, float }) !Node {
4109 const fmt_s = if (comptime std.meta.trait.isNumber(@TypeOf(num))) "{d}" else "{s}";4110 const fmt_s = if (comptime meta.trait.isNumber(@TypeOf(num))) "{d}" else "{s}";
4110 const str = try std.fmt.allocPrint(c.arena, fmt_s, .{num});4111 const str = try std.fmt.allocPrint(c.arena, fmt_s, .{num});
4111 if (num_kind == .float)4112 if (num_kind == .float)
4112 return Tag.float_literal.create(c.arena, str)4113 return Tag.float_literal.create(c.arena, str)
...@@ -4861,12 +4862,12 @@ fn parseCNumLit(c: *Context, m: *MacroCtx) ParseError!Node {...@@ -4861,12 +4862,12 @@ fn parseCNumLit(c: *Context, m: *MacroCtx) ParseError!Node {
4861 // make the output less noisy by skipping promoteIntLiteral where4862 // make the output less noisy by skipping promoteIntLiteral where
4862 // it's guaranteed to not be required because of C standard type constraints4863 // it's guaranteed to not be required because of C standard type constraints
4863 const guaranteed_to_fit = switch (suffix) {4864 const guaranteed_to_fit = switch (suffix) {
4864 .none => if (math.cast(i16, value)) |_| true else |_| false,4865 .none => !meta.isError(math.cast(i16, value)),
4865 .u => if (math.cast(u16, value)) |_| true else |_| false,4866 .u => !meta.isError(math.cast(u16, value)),
4866 .l => if (math.cast(i32, value)) |_| true else |_| false,4867 .l => !meta.isError(math.cast(i32, value)),
4867 .lu => if (math.cast(u32, value)) |_| true else |_| false,4868 .lu => !meta.isError(math.cast(u32, value)),
4868 .ll => if (math.cast(i64, value)) |_| true else |_| false,4869 .ll => !meta.isError(math.cast(i64, value)),
4869 .llu => if (math.cast(u64, value)) |_| true else |_| false,4870 .llu => !meta.isError(math.cast(u64, value)),
4870 .f => unreachable,4871 .f => unreachable,
4871 };4872 };
48724873