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 {
2929fn err(s: []const u8) void {
3030 testing.expect(!json.validate(s));
3131
32 testNonStreaming(s) catch return;
33 testing.expect(false);
32 testing.expect(std.meta.isError(testNonStreaming(s)));
3433}
3534
3635fn utf8Error(s: []const u8) void {
......@@ -48,8 +47,7 @@ fn any(s: []const u8) void {
4847fn anyStreamingErrNonStreaming(s: []const u8) void {
4948 _ = json.validate(s);
5049
51 testNonStreaming(s) catch return;
52 testing.expect(false);
50 testing.expect(std.meta.isError(testNonStreaming(s)));
5351}
5452
5553fn roundTrip(s: []const u8) !void {
lib/std/meta.zig+10
......@@ -1346,3 +1346,13 @@ test "shuffleVectorIndex" {
13461346 testing.expect(shuffleVectorIndex(6, vector_len) == -3);
13471347 testing.expect(shuffleVectorIndex(7, vector_len) == -4);
13481348}
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 {
206206 return false;
207207 }
208208
209 if (utf8Decode(s[i .. i + cp_len])) |_| {} else |_| {
209 if (std.meta.isError(utf8Decode(s[i .. i + cp_len]))) {
210210 return false;
211211 }
212212 i += cp_len;
src/translate_c.zig+10-9
......@@ -8,6 +8,7 @@ const ctok = std.c.tokenizer;
88const CToken = std.c.Token;
99const mem = std.mem;
1010const math = std.math;
11const meta = std.meta;
1112const ast = @import("translate_c/ast.zig");
1213const Node = ast.Node;
1314const Tag = Node.Tag;
......@@ -1741,7 +1742,7 @@ fn transImplicitCastExpr(
17411742}
17421743
17431744fn isBuiltinDefined(name: []const u8) bool {
1744 inline for (std.meta.declarations(c_builtins)) |decl| {
1745 inline for (meta.declarations(c_builtins)) |decl| {
17451746 if (std.mem.eql(u8, name, decl.name)) return true;
17461747 }
17471748 return false;
......@@ -3157,7 +3158,7 @@ const ClangFunctionType = union(enum) {
31573158 NoProto: *const clang.FunctionType,
31583159
31593160 fn getReturnType(self: @This()) clang.QualType {
3160 switch (@as(std.meta.Tag(@This()), self)) {
3161 switch (@as(meta.Tag(@This()), self)) {
31613162 .Proto => return self.Proto.getReturnType(),
31623163 .NoProto => return self.NoProto.getReturnType(),
31633164 }
......@@ -4106,7 +4107,7 @@ fn transCreateNodeAPInt(c: *Context, int: *const clang.APSInt) !Node {
41064107}
41074108
41084109fn 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}";
41104111 const str = try std.fmt.allocPrint(c.arena, fmt_s, .{num});
41114112 if (num_kind == .float)
41124113 return Tag.float_literal.create(c.arena, str)
......@@ -4861,12 +4862,12 @@ fn parseCNumLit(c: *Context, m: *MacroCtx) ParseError!Node {
48614862 // make the output less noisy by skipping promoteIntLiteral where
48624863 // it's guaranteed to not be required because of C standard type constraints
48634864 const guaranteed_to_fit = switch (suffix) {
4864 .none => if (math.cast(i16, value)) |_| true else |_| false,
4865 .u => if (math.cast(u16, value)) |_| true else |_| false,
4866 .l => if (math.cast(i32, value)) |_| true else |_| false,
4867 .lu => if (math.cast(u32, value)) |_| true else |_| false,
4868 .ll => if (math.cast(i64, value)) |_| true else |_| false,
4869 .llu => if (math.cast(u64, value)) |_| true else |_| false,
4865 .none => !meta.isError(math.cast(i16, value)),
4866 .u => !meta.isError(math.cast(u16, value)),
4867 .l => !meta.isError(math.cast(i32, value)),
4868 .lu => !meta.isError(math.cast(u32, value)),
4869 .ll => !meta.isError(math.cast(i64, value)),
4870 .llu => !meta.isError(math.cast(u64, value)),
48704871 .f => unreachable,
48714872 };
48724873