authorgravatar for 14938807+xackus@users.noreply.github.comxackus <14938807+xackus@users.noreply.github.com> 2021-04-11 16:26:29+02:00
committergravatar for 14938807+xackus@users.noreply.github.comxackus <14938807+xackus@users.noreply.github.com> 2021-04-11 16:26:29+02:00
loga5007d819a0bd4d247602786a36cabae821f52b9
tree6d43960f769b8f5375f07aa9bdffd4b8e25e0d43
parentcab1d5c1a320239af5be88571d7989ba230b90be

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
......@@ -1334,3 +1334,13 @@ test "shuffleVectorIndex" {
13341334 testing.expect(shuffleVectorIndex(6, vector_len) == -3);
13351335 testing.expect(shuffleVectorIndex(7, vector_len) == -4);
13361336}
1337
1338/// Returns whether `error_union` contains an error.
1339pub fn isError(error_union: anytype) bool {
1340 return if(error_union) |_| false else |_| true;
1341}
1342
1343test "isError" {
1344 std.testing.expect(isError(math.absInt(@as(i8, -128))));
1345 std.testing.expect(!isError(math.absInt(@as(i8, -127))));
1346}
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;
......@@ -1737,7 +1738,7 @@ fn transImplicitCastExpr(
17371738}
17381739
17391740fn isBuiltinDefined(name: []const u8) bool {
1740 inline for (std.meta.declarations(c_builtins)) |decl| {
1741 inline for (meta.declarations(c_builtins)) |decl| {
17411742 if (std.mem.eql(u8, name, decl.name)) return true;
17421743 }
17431744 return false;
......@@ -3136,7 +3137,7 @@ const ClangFunctionType = union(enum) {
31363137 NoProto: *const clang.FunctionType,
31373138
31383139 fn getReturnType(self: @This()) clang.QualType {
3139 switch (@as(std.meta.Tag(@This()), self)) {
3140 switch (@as(meta.Tag(@This()), self)) {
31403141 .Proto => return self.Proto.getReturnType(),
31413142 .NoProto => return self.NoProto.getReturnType(),
31423143 }
......@@ -4072,7 +4073,7 @@ fn transCreateNodeAPInt(c: *Context, int: *const clang.APSInt) !Node {
40724073}
40734074
40744075fn transCreateNodeNumber(c: *Context, num: anytype, num_kind: enum { int, float }) !Node {
4075 const fmt_s = if (comptime std.meta.trait.isNumber(@TypeOf(num))) "{d}" else "{s}";
4076 const fmt_s = if (comptime meta.trait.isNumber(@TypeOf(num))) "{d}" else "{s}";
40764077 const str = try std.fmt.allocPrint(c.arena, fmt_s, .{num});
40774078 if (num_kind == .float)
40784079 return Tag.float_literal.create(c.arena, str)
......@@ -4827,12 +4828,12 @@ fn parseCNumLit(c: *Context, m: *MacroCtx) ParseError!Node {
48274828 // make the output less noisy by skipping promoteIntLiteral where
48284829 // it's guaranteed to not be required because of C standard type constraints
48294830 const guaranteed_to_fit = switch (suffix) {
4830 .none => if (math.cast(i16, value)) |_| true else |_| false,
4831 .u => if (math.cast(u16, value)) |_| true else |_| false,
4832 .l => if (math.cast(i32, value)) |_| true else |_| false,
4833 .lu => if (math.cast(u32, value)) |_| true else |_| false,
4834 .ll => if (math.cast(i64, value)) |_| true else |_| false,
4835 .llu => if (math.cast(u64, value)) |_| true else |_| false,
4831 .none => !meta.isError(math.cast(i16, value)),
4832 .u => !meta.isError(math.cast(u16, value)),
4833 .l => !meta.isError(math.cast(i32, value)),
4834 .lu => !meta.isError(math.cast(u32, value)),
4835 .ll => !meta.isError(math.cast(i64, value)),
4836 .llu => !meta.isError(math.cast(u64, value)),
48364837 .f => unreachable,
48374838 };
48384839