]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/testsuite/gdc.test/compilable/testVRP.d
Add D front-end, libphobos library, and D2 testsuite.
[thirdparty/gcc.git] / gcc / testsuite / gdc.test / compilable / testVRP.d
1 // PERMUTE_ARGS: -O -inline
2
3 // Test value-range propagation.
4 // See Bug 3147, Bug 6000, Bug 5225.
5
6 void add() {
7 byte x, y;
8 short a = x + y;
9 }
10
11 void leftShift() {
12 byte x, y;
13 short z = x << 1;
14 }
15
16 void leftShiftFail() {
17 ubyte x, y;
18 ushort z;
19 static assert(!__traits(compiles, z = x << y));
20 // 1 << 31 surely overflows the range of 'ushort'.
21 }
22
23 void rightShiftFail() {
24 short x;
25 byte y, z;
26 static assert(!__traits(compiles, z = x >> y));
27 // [this passes in 2.053.]
28 }
29
30 void rightShift() {
31 ushort x;
32 ubyte y = x >> 16;
33 }
34
35 void unsignedRightShiftFail() {
36 int x;
37 ubyte y;
38 static assert(!__traits(compiles, y = x >>> 2));
39 // [this passes in 2.053.]
40 }
41
42 void subtract() {
43 ubyte x, y;
44 short z = x - y;
45 }
46
47 void multiply() {
48 byte x, y;
49 short z = x * y;
50 }
51
52 void subMulFail() {
53 ubyte x, y;
54 ubyte z;
55 static assert(!__traits(compiles, z = x - y));
56 static assert(!__traits(compiles, z = x * y));
57 // [these pass in 2.053.]
58 }
59
60 void multiplyNeg1() {
61 byte b;
62 b = -1 + (b * -1);
63 static assert(!__traits(compiles, b = -1 + b * ulong.max));
64 }
65
66 void divide() {
67 short w;
68 byte y = w / 300;
69 }
70
71 void divideFail() {
72 short w;
73 byte y;
74 static assert(!__traits(compiles, y = w / -1));
75 }
76
77 void plus1Fail() {
78 byte u, v;
79 static assert(!__traits(compiles, v = u + 1));
80 // [these pass in 2.053.]
81 }
82
83 void modulus() {
84 int x;
85 byte u = x % 128;
86 }
87
88 void modulus_bug6000a() {
89 ulong t;
90 uint u = t % 16;
91 }
92
93 void modulus_bug6000b() {
94 long n = 10520;
95 ubyte b;
96 static assert(!__traits(compiles, b = n % 10));
97 }
98
99 void modulus2() {
100 short s;
101 byte b = byte.max;
102 byte c = s % b;
103 }
104
105 void modulus3() {
106 int i;
107 short s = short.max;
108 short t = i % s;
109 }
110
111 void modulus4() {
112 uint i;
113 ushort s;
114 short t;
115 static assert(!__traits(compiles, t = i % s));
116 }
117
118 void modulusFail() {
119 int i;
120 short s;
121 byte b;
122 static assert(!__traits(compiles, b = i % s));
123 static assert(!__traits(compiles, b = i % 257));
124 // [these pass in 2.053.]
125 }
126
127 void bitwise() {
128 ubyte a, b, c;
129 uint d;
130 c = a & b;
131 c = a | b;
132 c = a ^ b;
133 c = d & 0xff;
134 // [these pass in 2.053.]
135 }
136
137 void bitAnd() {
138 byte c;
139 int d;
140 c = (0x3ff_ffffU << (0&c)) & (0x4000_0000U << (0&c));
141 // the result of the above is always 0 :).
142 }
143
144 void bitOrFail() {
145 ubyte c;
146 static assert(!__traits(compiles, c = c | 0x100));
147 // [this passes in 2.053.]
148 }
149
150 void bitAndOr() {
151 ubyte c;
152 c = (c | 0x1000) & ~0x1000;
153 }
154
155 void bitAndFail() {
156 int d;
157 short s;
158 byte c;
159 static assert(!__traits(compiles, c = d & s));
160 static assert(!__traits(compiles, c = d & 256));
161 // [these pass in 2.053.]
162 }
163
164 void bitXor() {
165 ushort s;
166 ubyte c;
167 c = (0xffff << (s&0)) ^ 0xff00;
168 }
169
170 void bitComplement() {
171 int i;
172 ubyte b = ~(i | ~0xff);
173 }
174
175 void bitComplementFail() {
176 ubyte b;
177 static assert(!__traits(compiles, b = ~(b | 1)));
178 // [this passes in 2.053.]
179 }
180
181 void negation() {
182 int x;
183 byte b = -(x & 0x7);
184 }
185
186 void negationFail() {
187 int x;
188 byte b;
189 static assert(!__traits(compiles, b = -(x & 255)));
190 // [this passes in 2.053.]
191 }
192
193 short bug5225(short a) {
194 return a>>1;
195 }
196
197 short bug1977_comment5(byte i) {
198 byte t = 1;
199 short o = t - i;
200 return o;
201 }
202
203 void testDchar() {
204 dchar d;
205 uint i;
206 /+
207 static assert(!__traits(compiles, d = i));
208 static assert(!__traits(compiles, d = i & 0x1fffff));
209 +/
210 d = i % 0x110000;
211 }
212
213 void bug1977_comment11() {
214 uint a;
215 byte b = a & 1;
216 // [this passes in 2.053.]
217 }
218
219 void bug1977_comment20() {
220 long a;
221 int b = a % 1000;
222 }
223
224 /******************************************/
225 // 9617
226
227 void test9617()
228 {
229 void f1(int) {}
230 void f2(short) {}
231 void f3(byte) {}
232
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)));
237
238 // But, if argument is not constant value, compilation fails.
239 ulong x;
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)
243
244 void f4(uint) {}
245 void f5(ushort) {}
246 void f6(ubyte) {}
247
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)
252 }
253
254 //import std.typetuple;
255 template TypeTuple(T...) { alias TypeTuple = T; }
256 template staticIota(size_t end)
257 {
258 static if (0 < end)
259 alias staticIota = TypeTuple!(staticIota!(end - 1), end - 1);
260 else
261 alias staticIota = TypeTuple!();
262 }
263 void test9617a()
264 {
265 alias Repr = TypeTuple!(
266 byte, "127", // T and literal representation of T.max
267 ubyte, "255",
268 short, "32767",
269 ushort, "65535",
270 int, "2147483647",
271 uint, "4294967295",
272 long, "9223372036854775807",
273 ulong, "18446744073709551615" // "" or "L" -> "signed integral overflow"
274 );
275 alias Indices = staticIota!(Repr.length / 2);
276
277 foreach (t; Indices)
278 {
279 alias T = Repr[t * 2];
280 void func(T)(T) {}
281 alias func!T f;
282
283 foreach (r; Indices)
284 {
285 alias S = Repr[r * 2];
286 S src = S.max;
287
288 enum x = Repr[r * 2 + 1];
289 foreach (repr; TypeTuple!(S.stringof~".max", x~"", x~"U", x~"L", x~"LU"))
290 {
291 static if (S.sizeof != T.sizeof)
292 static if (is(typeof(mixin(repr)) R))
293 {
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 ));
298
299 static assert(ct == rt);
300 //import std.string;
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);
304 }
305 }
306 }
307 }
308 }
309
310 void test10018(ubyte value)
311 {
312 const int c = value;
313 ubyte b = c;
314 static assert(!__traits(compiles, b = c - 1));
315 static assert(!__traits(compiles, b = c + 1));
316 immutable int i = value;
317 b = i;
318 static assert(!__traits(compiles, b = i - 1));
319 static assert(!__traits(compiles, b = i + 1));
320 }
321
322 void test13001(bool unknown)
323 {
324 foreach (const i; 0..unknown?2:3)
325 {
326 ubyte b = i;
327 static assert(!__traits(compiles, b = i - 1));
328 b = i + 253;
329 static assert(!__traits(compiles, b = i + 254));
330 }
331 }