| 1 | const std = @import("std"); |
| 2 | const minInt = std.math.minInt; |
| 3 | const maxInt = std.math.maxInt; |
| 4 | const builtin = @import("builtin"); |
| 5 | |
| 6 | test "int comparison elision" { |
| 7 | if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; |
| 8 | |
| 9 | testIntEdges(u0); |
| 10 | testIntEdges(u1); |
| 11 | testIntEdges(i1); |
| 12 | testIntEdges(u4); |
| 13 | testIntEdges(i4); |
| 14 | |
| 15 | // TODO: support int types > 128 bits wide in other backends |
| 16 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO |
| 17 | if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; |
| 18 | |
| 19 | // TODO: panic: integer overflow with int types > 65528 bits wide |
| 20 | // TODO: LLVM generates too many parameters for wasmtime when splitting up int > 64000 bits wide |
| 21 | testIntEdges(u64000); |
| 22 | testIntEdges(i64000); |
| 23 | } |
| 24 | |
| 25 | // All comparisons in this test have a guaranteed result, |
| 26 | // so one branch of each 'if' should never be analyzed. |
| 27 | fn testIntEdges(comptime T: type) void { |
| 28 | const min = minInt(T); |
| 29 | const max = maxInt(T); |
| 30 | |
| 31 | var runtime_val: T = undefined; |
| 32 | _ = &runtime_val; |
| 33 | |
| 34 | if (min > runtime_val) @compileError("analyzed impossible branch"); |
| 35 | if (min <= runtime_val) {} else @compileError("analyzed impossible branch"); |
| 36 | if (runtime_val < min) @compileError("analyzed impossible branch"); |
| 37 | if (runtime_val >= min) {} else @compileError("analyzed impossible branch"); |
| 38 | |
| 39 | if (min - 1 > runtime_val) @compileError("analyzed impossible branch"); |
| 40 | if (min - 1 >= runtime_val) @compileError("analyzed impossible branch"); |
| 41 | if (min - 1 < runtime_val) {} else @compileError("analyzed impossible branch"); |
| 42 | if (min - 1 <= runtime_val) {} else @compileError("analyzed impossible branch"); |
| 43 | if (min - 1 == runtime_val) @compileError("analyzed impossible branch"); |
| 44 | if (min - 1 != runtime_val) {} else @compileError("analyzed impossible branch"); |
| 45 | if (runtime_val < min - 1) @compileError("analyzed impossible branch"); |
| 46 | if (runtime_val <= min - 1) @compileError("analyzed impossible branch"); |
| 47 | if (runtime_val > min - 1) {} else @compileError("analyzed impossible branch"); |
| 48 | if (runtime_val >= min - 1) {} else @compileError("analyzed impossible branch"); |
| 49 | if (runtime_val == min - 1) @compileError("analyzed impossible branch"); |
| 50 | if (runtime_val != min - 1) {} else @compileError("analyzed impossible branch"); |
| 51 | |
| 52 | if (max >= runtime_val) {} else @compileError("analyzed impossible branch"); |
| 53 | if (max < runtime_val) @compileError("analyzed impossible branch"); |
| 54 | if (runtime_val <= max) {} else @compileError("analyzed impossible branch"); |
| 55 | if (runtime_val > max) @compileError("analyzed impossible branch"); |
| 56 | |
| 57 | if (max + 1 > runtime_val) {} else @compileError("analyzed impossible branch"); |
| 58 | if (max + 1 >= runtime_val) {} else @compileError("analyzed impossible branch"); |
| 59 | if (max + 1 < runtime_val) @compileError("analyzed impossible branch"); |
| 60 | if (max + 1 <= runtime_val) @compileError("analyzed impossible branch"); |
| 61 | if (max + 1 == runtime_val) @compileError("analyzed impossible branch"); |
| 62 | if (max + 1 != runtime_val) {} else @compileError("analyzed impossible branch"); |
| 63 | if (runtime_val < max + 1) {} else @compileError("analyzed impossible branch"); |
| 64 | if (runtime_val <= max + 1) {} else @compileError("analyzed impossible branch"); |
| 65 | if (runtime_val > max + 1) @compileError("analyzed impossible branch"); |
| 66 | if (runtime_val >= max + 1) @compileError("analyzed impossible branch"); |
| 67 | if (runtime_val == max + 1) @compileError("analyzed impossible branch"); |
| 68 | if (runtime_val != max + 1) {} else @compileError("analyzed impossible branch"); |
| 69 | |
| 70 | const undef_const: T = undefined; |
| 71 | |
| 72 | if (min > undef_const) @compileError("analyzed impossible branch"); |
| 73 | if (min <= undef_const) {} else @compileError("analyzed impossible branch"); |
| 74 | if (undef_const < min) @compileError("analyzed impossible branch"); |
| 75 | if (undef_const >= min) {} else @compileError("analyzed impossible branch"); |
| 76 | |
| 77 | if (min - 1 > undef_const) @compileError("analyzed impossible branch"); |
| 78 | if (min - 1 >= undef_const) @compileError("analyzed impossible branch"); |
| 79 | if (min - 1 < undef_const) {} else @compileError("analyzed impossible branch"); |
| 80 | if (min - 1 <= undef_const) {} else @compileError("analyzed impossible branch"); |
| 81 | if (min - 1 == undef_const) @compileError("analyzed impossible branch"); |
| 82 | if (min - 1 != undef_const) {} else @compileError("analyzed impossible branch"); |
| 83 | if (undef_const < min - 1) @compileError("analyzed impossible branch"); |
| 84 | if (undef_const <= min - 1) @compileError("analyzed impossible branch"); |
| 85 | if (undef_const > min - 1) {} else @compileError("analyzed impossible branch"); |
| 86 | if (undef_const >= min - 1) {} else @compileError("analyzed impossible branch"); |
| 87 | if (undef_const == min - 1) @compileError("analyzed impossible branch"); |
| 88 | if (undef_const != min - 1) {} else @compileError("analyzed impossible branch"); |
| 89 | |
| 90 | if (max >= undef_const) {} else @compileError("analyzed impossible branch"); |
| 91 | if (max < undef_const) @compileError("analyzed impossible branch"); |
| 92 | if (undef_const <= max) {} else @compileError("analyzed impossible branch"); |
| 93 | if (undef_const > max) @compileError("analyzed impossible branch"); |
| 94 | |
| 95 | if (max + 1 > undef_const) {} else @compileError("analyzed impossible branch"); |
| 96 | if (max + 1 >= undef_const) {} else @compileError("analyzed impossible branch"); |
| 97 | if (max + 1 < undef_const) @compileError("analyzed impossible branch"); |
| 98 | if (max + 1 <= undef_const) @compileError("analyzed impossible branch"); |
| 99 | if (max + 1 == undef_const) @compileError("analyzed impossible branch"); |
| 100 | if (max + 1 != undef_const) {} else @compileError("analyzed impossible branch"); |
| 101 | if (undef_const < max + 1) {} else @compileError("analyzed impossible branch"); |
| 102 | if (undef_const <= max + 1) {} else @compileError("analyzed impossible branch"); |
| 103 | if (undef_const > max + 1) @compileError("analyzed impossible branch"); |
| 104 | if (undef_const >= max + 1) @compileError("analyzed impossible branch"); |
| 105 | if (undef_const == max + 1) @compileError("analyzed impossible branch"); |
| 106 | if (undef_const != max + 1) {} else @compileError("analyzed impossible branch"); |
| 107 | } |
| 108 | |
| 109 | test "comparison elided on large integer value" { |
| 110 | try std.testing.expect(-1 == @as(i8, -3) >> 2); |
| 111 | try std.testing.expect(-1 == -3 >> 2000); |
| 112 | } |