From 207862647c00ef824a6dd43be0bfc69d844fc075 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 9 Feb 2016 09:51:25 -0700 Subject: [PATCH] std: fix parse_u64 implementation and add test also codegen implement comparision with pure errors --- src/codegen.cpp | 3 +++ std/std.zig | 11 ++++++++++- test/self_hosted.zig | 2 ++ test/test_std.zig | 1 + 4 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 test/test_std.zig diff --git a/src/codegen.cpp b/src/codegen.cpp index 1a166867a8bb2c8a0d874ea728c70abd010589b2..ee8dcac715978db4cd5b9e2d96ab00f3a5bda44e 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -1269,6 +1269,9 @@ static LLVMValueRef gen_cmp_expr(CodeGen *g, AstNode *node) { } else { zig_unreachable(); } + } else if (op1_type->id == TypeTableEntryIdPureError) { + LLVMIntPredicate pred = cmp_op_to_int_predicate(node->data.bin_op_expr.bin_op, false); + return LLVMBuildICmp(g->builder, pred, val1, val2, ""); } else { zig_unreachable(); } diff --git a/std/std.zig b/std/std.zig index d82d78bf49e6cab45104c8b6de9b48ee8514a820..1333b5be397acc0648fc9c9f5bf9f90cbb0c66a1 100644 --- a/std/std.zig +++ b/std/std.zig @@ -158,7 +158,7 @@ pub fn parse_u64(buf: []u8, radix: u8) -> %u64 { for (buf) |c| { const digit = char_to_digit(c); - if (digit > radix) { + if (digit >= radix) { return error.InvalidChar; } @@ -342,3 +342,12 @@ pub fn buf_print_f64(out_buf: []u8, x: f64, decimals: isize) -> isize { len } + +#attribute("test") +fn parse_u64_digit_too_big() { + parse_u64("123a", 10) %% |err| { + if (err == error.InvalidChar) return; + unreachable{}; + }; + unreachable{}; +} diff --git a/test/self_hosted.zig b/test/self_hosted.zig index dfab9816b08170731f10c21cbd45f9e0bd7a1a14..7a73807743906911ade2371dc1bab8a9f20850b7 100644 --- a/test/self_hosted.zig +++ b/test/self_hosted.zig @@ -1,3 +1,5 @@ +import "test_std.zig"; + #attribute("test") fn empty_function() {} diff --git a/test/test_std.zig b/test/test_std.zig new file mode 100644 index 0000000000000000000000000000000000000000..342d7e15a77373c3266434df9e1e6d230280d9e4 --- /dev/null +++ b/test/test_std.zig @@ -0,0 +1 @@ +import "std.zig"; -- 2.54.0