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