Debian Codegraph @

gcc/testsuite/gdc.test/compilable/testVRP.d

raw
1 // PERMUTE_ARGS: -O -inline
2
3 // Test value-range propagation.
4 // https://issues.dlang.org/show_bug.cgi?id=3147
5 // https://issues.dlang.org/show_bug.cgi?id=6000
6 // https://issues.dlang.org/show_bug.cgi?id=5225
7 // https://issues.dlang.org/show_bug.cgi?id=24855
8
9 void add()
10 {
11 byte x, y;
12 short a = x + y;
13 }
14
15 void leftShift()
16 {
17 byte x, y;
18 short z = x << 1;
19 }
20
21 void leftShiftFail()
22 {
23 {
24 ubyte x, y;
25 ushort z;
26 static assert(!__traits(compiles, z = x << y));
27 // 1 << 31 surely overflows the range of 'ushort'.
28 }
29 {
30 ulong a, b;
31 int res;
32 static assert(!__traits(compiles, res = a << (b % 65U)));
33 }
34 }
35
36 void rightShiftFail()
37 {
38 {
39 short x;
40 byte y, z;
41 static assert(!__traits(compiles, z = x >> y));
42 // [this passes in 2.053.]
43 }
44 {
45 ulong a, b;
46 int res;
47 static assert(!__traits(compiles, res = a >> (b % 65U)));
48 }
49 }
50
51 void rightShift()
52 {
53 ushort x;
54 ubyte y = x >> 16;
55 }
56
57 void unsignedRightShiftFail()
58 {
59 int x;
60 ubyte y;
61 static assert(!__traits(compiles, y = x >>> 2));
62 // [this passes in 2.053.]
63 }
64
65 void subtract()
66 {
67 ubyte x, y;
68 short z = x - y;
69 }
70
71 void multiply()
72 {
73 byte x, y;
74 short z = x * y;
75 }
76
77 void subMulFail()
78 {
79 ubyte x, y;
80 ubyte z;
81 static assert(!__traits(compiles, z = x - y));
82 static assert(!__traits(compiles, z = x * y));
83 // [these pass in 2.053.]
84 }
85
86 void multiplyNeg1()
87 {
88 byte b;
89 b = -1 + (b * -1);
90 static assert(!__traits(compiles, b = -1 + b * ulong.max));
91 }
92
93 void divide()
94 {
95 short w;
96 byte y = w / 300;
97 }
98
99 void divideFail()
100 {
101 short w;
102 byte y;
103 static assert(!__traits(compiles, y = w / -1));
104 static assert(!__traits(compiles, y = y / w));
105
106 short z;
107 static assert(!__traits(compiles, z = w / z + 1));
108 }
109
110 void plus1Fail()
111 {
112 byte u, v;
113 static assert(!__traits(compiles, v = u + 1));
114 // [these pass in 2.053.]
115 }
116
117 void modulus()
118 {
119 int x;
120 byte u = x % 128;
121 }
122
123 void modulus_bug6000a()
124 {
125 ulong t;
126 uint u = t % 16;
127 }
128
129 void modulus_bug6000b()
130 {
131 long n = 10520;
132 ubyte b;
133 static assert(!__traits(compiles, b = n % 10));
134 }
135
136 void modulus2()
137 {
138 short s;
139 byte b = byte.max;
140 byte c = s % b;
141 }
142
143 void modulus3()
144 {
145 int i;
146 short s = short.max;
147 short t = i % s;
148 }
149
150 void modulus4()
151 {
152 uint i;
153 ushort s;
154 short t;
155 static assert(!__traits(compiles, t = i % s));
156 }
157
158 void modulus5()
159 {
160 short a;
161 byte foo = (a - short.max - 1) % 127;
162 }
163
164 void modulusFail()
165 {
166 int i;
167 short s;
168 byte b;
169 static assert(!__traits(compiles, b = i % s));
170 static assert(!__traits(compiles, b = i % 257));
171 // [these pass in 2.053.]
172 }
173
174 void bitwise()
175 {
176 ubyte a, b, c;
177 uint d;
178 c = a & b;
179 c = a | b;
180 c = a ^ b;
181 c = d & 0xff;
182 // [these pass in 2.053.]
183 }
184
185 void bitAnd()
186 {
187 byte c;
188 int d;
189 c = (0x3ff_ffffU << (0&c)) & (0x4000_0000U << (0&c));
190 // the result of the above is always 0 :).
191 }
192
193 void bitAndTest()
194 {
195 {
196 ushort a, b;
197 byte res = ((a % 7) - 6) & ((b % 7) - 6);
198 }
199 {
200 // rhs[-128..127] outside range of lhs[0..255]
201 // -> calls byte.implicitConvTo(ubyte) => MATCH.convert
202 byte a, b;
203 ubyte res;
204
205 res = cast(byte)(a + 5) & b;
206 res = cast(byte)(a - 5) & b;
207 res = cast(byte)(a / 5) & b;
208 res = cast(byte)(a * 5) & b;
209 res = cast(byte)(a % 5) & b;
210 }
211 }
212
213 void bitOrFail()
214 {
215 {
216 ubyte c;
217 static assert(!__traits(compiles, c = c | 0x100));
218 // [this passes in 2.053.]
219 }
220 {
221 byte a, b;
222 ubyte res;
223
224 static assert(!__traits(compiles, res = (a + 5) | b)); // [-128..255]
225 static assert(!__traits(compiles, res = (a - 5) | b)); // [-133..127]
226 static assert(!__traits(compiles, res = (a / 5) | b)); // [-128..127]
227 static assert(!__traits(compiles, res = (a * 5) | b)); // [-640..639]
228 static assert(!__traits(compiles, res = (a % 5) | b)); // [-128..127]
229 }
230 }
231
232 void bitAndOr()
233 {
234 ubyte c;
235 c = (c | 0x1000) & ~0x1000;
236 }
237
238 void bitOrTest()
239 {
240 {
241 // Tests condition for different signs between min & max
242 // ((imin.negative ^ imax.negative) == 1 && (rhs.imin.negative ^ rhs.imax.negative) == 1
243 ushort a, b;
244 byte res = ((a % 127) - 126) | ((b % 6) - 5);
245 }
246 {
247 // rhs[-128..127] outside range of lhs[0..255]
248 // -> calls byte.implicitConvTo(ubyte) => MATCH.convert
249 byte a, b, c;
250 ubyte res;
251
252 res = cast(byte)(a + 5) | b;
253 res = cast(byte)(a - 5) | b;
254 res = cast(byte)(a / 5) | b;
255 res = cast(byte)(a * 5) | b;
256 res = cast(byte)(a % 5) | b;
257 }
258 }
259
260 void bitAndFail()
261 {
262 {
263 int d;
264 short s;
265 byte c;
266 static assert(!__traits(compiles, c = d & s));
267 static assert(!__traits(compiles, c = d & 256));
268 // [these pass in 2.053.]
269 }
270 {
271 byte a, b;
272 ubyte res;
273
274 static assert(!__traits(compiles, res = (a + 5) & b)); // [-128..132]
275 static assert(!__traits(compiles, res = (a - 5) & b)); // [-256..127]
276 static assert(!__traits(compiles, res = (a / 5) & b)); // [-128..127]
277 static assert(!__traits(compiles, res = (a * 5) & b)); // [-640..635]
278 static assert(!__traits(compiles, res = (a % 5) & b)); // [-128..127]
279 }
280 }
281
282 void bitXor()
283 {
284 {
285 ushort s;
286 ubyte c;
287 c = (0xffff << (s & 0)) ^ 0xff00;
288 }
289 {
290 // rhs[-128..127] outside range of lhs[0..255]
291 // -> calls byte.implicitConvTo(ubyte) => MATCH.convert
292 byte a, b, c;
293 ubyte res;
294
295 res = cast(byte)(a + 5) ^ b;
296 res = cast(byte)(a - 5) ^ b;
297 res = cast(byte)(a / 5) ^ b;
298 res = cast(byte)(a * 5) ^ b;
299 res = cast(byte)(a % 5) ^ b;
300 }
301 }
302
303 void bitXorFail()
304 {
305 {
306 byte a, b;
307 ubyte res;
308
309 static assert(!__traits(compiles, res = (a + 5) ^ b)); // [-256..255]
310 static assert(!__traits(compiles, res = (a - 5) ^ b)); // [-256..255]
311 static assert(!__traits(compiles, res = (a / 5) ^ b)); // [-128..127]
312 static assert(!__traits(compiles, res = (a * 5) ^ b)); // [-640..1023]
313 static assert(!__traits(compiles, res = (a % 5) ^ b)); // [-128..127]
314 }
315 }
316
317 void bitComplement()
318 {
319 int i;
320 ubyte b = ~(i | ~0xff);
321 }
322
323 void bitComplementFail()
324 {
325 ubyte b;
326 static assert(!__traits(compiles, b = ~(b | 1)));
327 // [this passes in 2.053.]
328 }
329
330 void negation()
331 {
332 int x;
333 byte b = -(x & 0x7);
334 }
335
336 void negationFail()
337 {
338 int x;
339 byte b;
340 static assert(!__traits(compiles, b = -(x & 255)));
341 // [this passes in 2.053.]
342 }
343
344 short bug5225(short a) {
345 return a>>1;
346 }
347
348 short bug1977_comment5(byte i) {
349 byte t = 1;
350 short o = t - i;
351 return o;
352 }
353
354 void testDchar()
355 {
356 dchar d;
357 uint i;
358 /+
359 static assert(!__traits(compiles, d = i));
360 static assert(!__traits(compiles, d = i & 0x1fffff));
361 +/
362 d = i % 0x110000;
363 }
364
365 void bug1977_comment11()
366 {
367 uint a;
368 byte b = a & 1;
369 // [this passes in 2.053.]
370 }
371
372 void bug1977_comment20()
373 {
374 long a;
375 int b = a % 1000;
376 }
377
378 /******************************************/
379 // https://issues.dlang.org/show_bug.cgi?id=9617
380
381 void test9617()
382 {
383 void f1(int) {}
384 void f2(short) {}
385 void f3(byte) {}
386
387 // Why these calls are accepted?
388 static assert(!__traits(compiles, f1(ulong.max)));
389 static assert(!__traits(compiles, f2(ulong.max)));
390 static assert(!__traits(compiles, f3(ulong.max)));
391
392 // But, if argument is not constant value, compilation fails.
393 ulong x;
394 static assert(!__traits(compiles, f1(x))); // is not callable using argument types (ulong)
395 static assert(!__traits(compiles, f2(x))); // is not callable using argument types (ulong)
396 static assert(!__traits(compiles, f3(x))); // is not callable using argument types (ulong)
397
398 void f4(uint) {}
399 void f5(ushort) {}
400 void f6(ubyte) {}
401
402 // If parameter type is unsigned, it is collectly rejected
403 static assert(!__traits(compiles, f4(ulong.max))); // is not callable using argument types (ulong)
404 static assert(!__traits(compiles, f5(ulong.max))); // is not callable using argument types (ulong)
405 static assert(!__traits(compiles, f6(ulong.max))); // is not callable using argument types (ulong)
406 }
407
408 //import std.typetuple;
409 template TypeTuple(T...) { alias TypeTuple = T; }
410 template staticIota(size_t end)
411 {
412 static if (0 < end)
413 alias staticIota = TypeTuple!(staticIota!(end - 1), end - 1);
414 else
415 alias staticIota = TypeTuple!();
416 }
417 void test9617a()
418 {
419 alias Repr = TypeTuple!(
420 byte, "127", // T and literal representation of T.max
421 ubyte, "255",
422 short, "32767",
423 ushort, "65535",
424 int, "2147483647",
425 uint, "4294967295",
426 long, "9223372036854775807",
427 ulong, "18446744073709551615" // "" or "L" -> "signed integral overflow"
428 );
429 alias Indices = staticIota!(Repr.length / 2);
430
431 foreach (t; Indices)
432 {
433 alias T = Repr[t * 2];
434 void func(T)(T) {}
435 alias func!T f;
436
437 foreach (r; Indices)
438 {
439 alias S = Repr[r * 2];
440 S src = S.max;
441
442 enum x = Repr[r * 2 + 1];
443 foreach (repr; TypeTuple!(S.stringof~".max", x~"", x~"U", x~"L", x~"LU"))
444 {
445 static if (S.sizeof != T.sizeof)
446 static if (is(typeof(mixin(repr)) R))
447 {
448 // "Compilable" test should be equal, even if
449 // the given argument is either constant or runtime variable.
450 enum ct = __traits(compiles, f( mixin(repr) ));
451 enum rt = __traits(compiles, f( src ));
452
453 static assert(ct == rt);
454 //import std.string;
455 //enum msg = format("%6s.max to %-6s variable/constant = %d/%d, constant_repr = (%s) %s",
456 // S.stringof, T.stringof, rt, ct, R.stringof, repr);
457 //static if (ct != rt) pragma(msg, msg);
458 }
459 }
460 }
461 }
462 }
463
464 void test10018(ubyte value)
465 {
466 const int c = value;
467 ubyte b = c;
468 static assert(!__traits(compiles, b = c - 1));
469 static assert(!__traits(compiles, b = c + 1));
470 immutable int i = value;
471 b = i;
472 static assert(!__traits(compiles, b = i - 1));
473 static assert(!__traits(compiles, b = i + 1));
474 }
475
476 void test13001(bool unknown)
477 {
478 foreach (const i; 0..unknown?2:3)
479 {
480 ubyte b = i;
481 static assert(!__traits(compiles, b = i - 1));
482 b = i + 253;
483 static assert(!__traits(compiles, b = i + 254));
484 }
485 }
486
487 void test10310()
488 {
489 int y;
490 ubyte x = ((y & 252) ^ 2) + 1;
491 }
492
493 // https://issues.dlang.org/show_bug.cgi?id=15289
494 void test15289a()
495 {
496 int [] arr = [1, 2, 3, 4];
497 uint foo = 50 / arr.length;
498 }
499
500 void test15289b()
501 {
502 int [] arr = [1, 2, 3, 4];
503 uint foo = 50 % arr.length;
504 }
505
506 void testShiftRightOnNegative()
507 {
508 int neg = -1;
509 uint[] arr = [1, 2, 3];
510 ubyte b;
511 // Shift with negative value returns value in range [0, ulong.max]
512 static assert(!__traits(compiles, b = arr.length >> neg));
513 static assert(!__traits(compiles, b = arr.length << neg));
514 }