1 // PERMUTE_ARGS: -O -inline
3 // Test value-range propagation.
4 // See Bug 3147, Bug 6000, Bug 5225.
16 void leftShiftFail() {
19 static assert(!__traits(compiles, z = x << y));
20 // 1 << 31 surely overflows the range of 'ushort'.
23 void rightShiftFail() {
26 static assert(!__traits(compiles, z = x >> y));
27 // [this passes in 2.053.]
35 void unsignedRightShiftFail() {
38 static assert(!__traits(compiles, y = x >>> 2));
39 // [this passes in 2.053.]
55 static assert(!__traits(compiles, z = x - y));
56 static assert(!__traits(compiles, z = x * y));
57 // [these pass in 2.053.]
63 static assert(!__traits(compiles, b = -1 + b * ulong.max));
74 static assert(!__traits(compiles, y = w / -1));
79 static assert(!__traits(compiles, v = u + 1));
80 // [these pass in 2.053.]
88 void modulus_bug6000a() {
93 void modulus_bug6000b() {
96 static assert(!__traits(compiles, b = n % 10));
115 static assert(!__traits(compiles, t = i % s));
122 static assert(!__traits(compiles, b = i % s));
123 static assert(!__traits(compiles, b = i % 257));
124 // [these pass in 2.053.]
134 // [these pass in 2.053.]
140 c = (0x3ff_ffffU << (0&c)) & (0x4000_0000U << (0&c));
141 // the result of the above is always 0 :).
146 static assert(!__traits(compiles, c = c | 0x100));
147 // [this passes in 2.053.]
152 c = (c | 0x1000) & ~0x1000;
159 static assert(!__traits(compiles, c = d & s));
160 static assert(!__traits(compiles, c = d & 256));
161 // [these pass in 2.053.]
167 c = (0xffff << (s&0)) ^ 0xff00;
170 void bitComplement() {
172 ubyte b = ~(i | ~0xff);
175 void bitComplementFail() {
177 static assert(!__traits(compiles, b = ~(b | 1)));
178 // [this passes in 2.053.]
186 void negationFail() {
189 static assert(!__traits(compiles, b = -(x & 255)));
190 // [this passes in 2.053.]
193 short bug5225(short a) {
197 short bug1977_comment5(byte i) {
207 static assert(!__traits(compiles, d = i));
208 static assert(!__traits(compiles, d = i & 0x1fffff));
213 void bug1977_comment11() {
216 // [this passes in 2.053.]
219 void bug1977_comment20() {
224 /******************************************/
233 // Why these calls are accepted?
234 static assert(!__traits(compiles, f1(ulong.max)));
235 static assert(!__traits(compiles, f2(ulong.max)));
236 static assert(!__traits(compiles, f3(ulong.max)));
238 // But, if argument is not constant value, compilation fails.
240 static assert(!__traits(compiles, f1(x))); // is not callable using argument types (ulong)
241 static assert(!__traits(compiles, f2(x))); // is not callable using argument types (ulong)
242 static assert(!__traits(compiles, f3(x))); // is not callable using argument types (ulong)
248 // If parameter type is unsigned, it is collectly rejected
249 static assert(!__traits(compiles, f4(ulong.max))); // is not callable using argument types (ulong)
250 static assert(!__traits(compiles, f5(ulong.max))); // is not callable using argument types (ulong)
251 static assert(!__traits(compiles, f6(ulong.max))); // is not callable using argument types (ulong)
254 //import std.typetuple;
255 template TypeTuple(T...) { alias TypeTuple = T; }
256 template staticIota(size_t end)
259 alias staticIota = TypeTuple!(staticIota!(end - 1), end - 1);
261 alias staticIota = TypeTuple!();
265 alias Repr = TypeTuple!(
266 byte, "127", // T and literal representation of T.max
272 long, "9223372036854775807",
273 ulong, "18446744073709551615" // "" or "L" -> "signed integral overflow"
275 alias Indices = staticIota!(Repr.length / 2);
279 alias T = Repr[t * 2];
285 alias S = Repr[r * 2];
288 enum x = Repr[r * 2 + 1];
289 foreach (repr; TypeTuple!(S.stringof~".max", x~"", x~"U", x~"L", x~"LU"))
291 static if (S.sizeof != T.sizeof)
292 static if (is(typeof(mixin(repr)) R))
294 // "Compilable" test should be equal, even if
295 // the given argument is either constant or runtime variable.
296 enum ct = __traits(compiles, f( mixin(repr) ));
297 enum rt = __traits(compiles, f( src ));
299 static assert(ct == rt);
301 //enum msg = format("%6s.max to %-6s variable/constant = %d/%d, constant_repr = (%s) %s",
302 // S.stringof, T.stringof, rt, ct, R.stringof, repr);
303 //static if (ct != rt) pragma(msg, msg);
310 void test10018(ubyte value)
314 static assert(!__traits(compiles, b = c - 1));
315 static assert(!__traits(compiles, b = c + 1));
316 immutable int i = value;
318 static assert(!__traits(compiles, b = i - 1));
319 static assert(!__traits(compiles, b = i + 1));
322 void test13001(bool unknown)
324 foreach (const i; 0..unknown?2:3)
327 static assert(!__traits(compiles, b = i - 1));
329 static assert(!__traits(compiles, b = i + 254));