aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gdc.test/compilable/testVRP.d
blob: 954b5019b832aa9f2f01b1296e36d6a7e8a3ac2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
// PERMUTE_ARGS: -O -inline

// Test value-range propagation.
// https://issues.dlang.org/show_bug.cgi?id=3147
// https://issues.dlang.org/show_bug.cgi?id=6000
// https://issues.dlang.org/show_bug.cgi?id=5225

void add()
{
    byte x, y;
    short a = x + y;
}

void leftShift()
{
    byte x, y;
    short z = x << 1;
}

void leftShiftFail()
{
    {
        ubyte x, y;
        ushort z;
        static assert(!__traits(compiles, z = x << y));
        // 1 << 31 surely overflows the range of 'ushort'.
    }
    {
        ulong a, b;
        int res;
        static assert(!__traits(compiles, res = a << (b % 65U)));
    }
}

void rightShiftFail()
{
    {
        short x;
        byte y, z;
        static assert(!__traits(compiles, z = x >> y));
        // [this passes in 2.053.]
    }
    {
        ulong a, b;
        int res;
        static assert(!__traits(compiles, res = a >> (b % 65U)));
    }
}

void rightShift()
{
    ushort x;
    ubyte y = x >> 16;
}

void unsignedRightShiftFail()
{
    int x;
    ubyte y;
    static assert(!__traits(compiles, y = x >>> 2));
    // [this passes in 2.053.]
}

void subtract()
{
    ubyte x, y;
    short z = x - y;
}

void multiply()
{
    byte x, y;
    short z = x * y;
}

void subMulFail()
{
    ubyte x, y;
    ubyte z;
    static assert(!__traits(compiles, z = x - y));
    static assert(!__traits(compiles, z = x * y));
    // [these pass in 2.053.]
}

void multiplyNeg1()
{
    byte b;
    b = -1 + (b * -1);
    static assert(!__traits(compiles, b = -1 + b * ulong.max));
}

void divide()
{
    short w;
    byte y = w / 300;
}

void divideFail()
{
    short w;
    byte y;
    static assert(!__traits(compiles, y = w / -1));
}

void plus1Fail()
{
    byte u, v;
    static assert(!__traits(compiles, v = u + 1));
    // [these pass in 2.053.]
}

void modulus()
{
    int x;
    byte u = x % 128;
}

void modulus_bug6000a()
{
    ulong t;
    uint u = t % 16;
}

void modulus_bug6000b()
{
    long n = 10520;
    ubyte b;
    static assert(!__traits(compiles, b = n % 10));
}

void modulus2()
{
    short s;
    byte b = byte.max;
    byte c = s % b;
}

void modulus3()
{
    int i;
    short s = short.max;
    short t = i % s;
}

void modulus4()
{
    uint i;
    ushort s;
    short t;
    static assert(!__traits(compiles, t = i % s));
}

void modulus5()
{
    short a;
    byte foo = (a - short.max - 1) % 127;
}

void modulusFail()
{
    int i;
    short s;
    byte b;
    static assert(!__traits(compiles, b = i % s));
    static assert(!__traits(compiles, b = i % 257));
    // [these pass in 2.053.]
}

void bitwise()
{
    ubyte a, b, c;
    uint d;
    c = a & b;
    c = a | b;
    c = a ^ b;
    c = d & 0xff;
    // [these pass in 2.053.]
}

void bitAnd()
{
    byte c;
    int d;
    c = (0x3ff_ffffU << (0&c)) & (0x4000_0000U << (0&c));
    // the result of the above is always 0 :).
}

void bitAndTest()
{
    {
        ushort a, b;
        byte res = ((a % 7) - 6) & ((b % 7) - 6);
    }
    {
        // rhs[-128..127] outside range of lhs[0..255]
        //   -> calls byte.implicitConvTo(ubyte) => MATCH.convert
        byte a, b;
        ubyte res;

        res = cast(byte)(a + 5) & b;
        res = cast(byte)(a - 5) & b;
        res = cast(byte)(a / 5) & b;
        res = cast(byte)(a * 5) & b;
        res = cast(byte)(a % 5) & b;
    }
}

void bitOrFail()
{
    {
        ubyte c;
        static assert(!__traits(compiles, c = c | 0x100));
        // [this passes in 2.053.]
    }
    {
        byte a, b;
        ubyte res;

        static assert(!__traits(compiles, res = (a + 5) | b)); // [-128..255]
        static assert(!__traits(compiles, res = (a - 5) | b)); // [-133..127]
        static assert(!__traits(compiles, res = (a / 5) | b)); // [-128..127]
        static assert(!__traits(compiles, res = (a * 5) | b)); // [-640..639]
        static assert(!__traits(compiles, res = (a % 5) | b)); // [-128..127]
    }
}

void bitAndOr()
{
    ubyte c;
    c = (c | 0x1000) & ~0x1000;
}

void bitOrTest()
{
    {
        // Tests condition for different signs between min & max
        // ((imin.negative ^ imax.negative) == 1 && (rhs.imin.negative ^ rhs.imax.negative) == 1
        ushort a, b;
        byte res = ((a % 127) - 126) | ((b % 6) - 5);
    }
    {
        // rhs[-128..127] outside range of lhs[0..255]
        //   -> calls byte.implicitConvTo(ubyte) => MATCH.convert
        byte a, b, c;
        ubyte res;

        res = cast(byte)(a + 5) | b;
        res = cast(byte)(a - 5) | b;
        res = cast(byte)(a / 5) | b;
        res = cast(byte)(a * 5) | b;
        res = cast(byte)(a % 5) | b;
    }
}

void bitAndFail()
{
    {
        int d;
        short s;
        byte c;
        static assert(!__traits(compiles, c = d & s));
        static assert(!__traits(compiles, c = d & 256));
        // [these pass in 2.053.]
    }
    {
        byte a, b;
        ubyte res;

        static assert(!__traits(compiles, res = (a + 5) & b)); // [-128..132]
        static assert(!__traits(compiles, res = (a - 5) & b)); // [-256..127]
        static assert(!__traits(compiles, res = (a / 5) & b)); // [-128..127]
        static assert(!__traits(compiles, res = (a * 5) & b)); // [-640..635]
        static assert(!__traits(compiles, res = (a % 5) & b)); // [-128..127]
    }
}

void bitXor()
{
    {
        ushort s;
        ubyte c;
        c = (0xffff << (s & 0)) ^ 0xff00;
    }
    {
        // rhs[-128..127] outside range of lhs[0..255]
        //   -> calls byte.implicitConvTo(ubyte) => MATCH.convert
        byte a, b, c;
        ubyte res;

        res = cast(byte)(a + 5) ^ b;
        res = cast(byte)(a - 5) ^ b;
        res = cast(byte)(a / 5) ^ b;
        res = cast(byte)(a * 5) ^ b;
        res = cast(byte)(a % 5) ^ b;
    }
}

void bitXorFail()
{
    {
        byte a, b;
        ubyte res;

        static assert(!__traits(compiles, res = (a + 5) ^ b)); // [-256..255]
        static assert(!__traits(compiles, res = (a - 5) ^ b)); // [-256..255]
        static assert(!__traits(compiles, res = (a / 5) ^ b)); // [-128..127]
        static assert(!__traits(compiles, res = (a * 5) ^ b)); // [-640..1023]
        static assert(!__traits(compiles, res = (a % 5) ^ b)); // [-128..127]
    }
}

void bitComplement()
{
    int i;
    ubyte b = ~(i | ~0xff);
}

void bitComplementFail()
{
    ubyte b;
    static assert(!__traits(compiles, b = ~(b | 1)));
    // [this passes in 2.053.]
}

void negation()
{
    int x;
    byte b = -(x & 0x7);
}

void negationFail()
{
    int x;
    byte b;
    static assert(!__traits(compiles, b = -(x & 255)));
    // [this passes in 2.053.]
}

short bug5225(short a) {
    return a>>1;
}

short bug1977_comment5(byte i) {
  byte t = 1;
  short o = t - i;
  return o;
}

void testDchar()
{
    dchar d;
    uint i;
    /+
    static assert(!__traits(compiles, d = i));
    static assert(!__traits(compiles, d = i & 0x1fffff));
    +/
    d = i % 0x110000;
}

void bug1977_comment11()
{
    uint a;
    byte b = a & 1;
    // [this passes in 2.053.]
}

void bug1977_comment20()
{
    long a;
    int b = a % 1000;
}

/******************************************/
// 9617

void test9617()
{
    void f1(int) {}
    void f2(short) {}
    void f3(byte) {}

    // Why these calls are accepted?
    static assert(!__traits(compiles, f1(ulong.max)));
    static assert(!__traits(compiles, f2(ulong.max)));
    static assert(!__traits(compiles, f3(ulong.max)));

    // But, if argument is not constant value, compilation fails.
    ulong x;
    static assert(!__traits(compiles, f1(x)));  // is not callable using argument types (ulong)
    static assert(!__traits(compiles, f2(x)));  // is not callable using argument types (ulong)
    static assert(!__traits(compiles, f3(x)));  // is not callable using argument types (ulong)

    void f4(uint) {}
    void f5(ushort) {}
    void f6(ubyte) {}

    // If parameter type is unsigned, it is collectly rejected
    static assert(!__traits(compiles, f4(ulong.max)));  // is not callable using argument types (ulong)
    static assert(!__traits(compiles, f5(ulong.max)));  // is not callable using argument types (ulong)
    static assert(!__traits(compiles, f6(ulong.max)));  // is not callable using argument types (ulong)
}

//import std.typetuple;
template TypeTuple(T...) { alias TypeTuple = T; }
template staticIota(size_t end)
{
    static if (0 < end)
        alias staticIota = TypeTuple!(staticIota!(end - 1), end - 1);
    else
        alias staticIota = TypeTuple!();
}
void test9617a()
{
    alias Repr = TypeTuple!(
        byte,   "127",      // T and literal representation of T.max
        ubyte,  "255",
        short,  "32767",
        ushort, "65535",
        int,    "2147483647",
        uint,   "4294967295",
        long,   "9223372036854775807",
        ulong,  "18446744073709551615"  // "" or "L" -> "signed integral overflow"
    );
    alias Indices = staticIota!(Repr.length / 2);

    foreach (t; Indices)
    {
        alias T = Repr[t * 2];
        void func(T)(T) {}
        alias func!T f;

        foreach (r; Indices)
        {
            alias S = Repr[r * 2];
            S src = S.max;

            enum x = Repr[r * 2 + 1];
            foreach (repr; TypeTuple!(S.stringof~".max", x~"", x~"U", x~"L", x~"LU"))
            {
                static if (S.sizeof != T.sizeof)
                static if (is(typeof(mixin(repr)) R))
                {
                    // "Compilable" test should be equal, even if
                    // the given argument is either constant or runtime variable.
                    enum ct = __traits(compiles, f( mixin(repr) ));
                    enum rt = __traits(compiles, f( src ));

                    static assert(ct == rt);
                    //import std.string;
                    //enum msg = format("%6s.max to %-6s variable/constant = %d/%d, constant_repr = (%s) %s",
                    //                    S.stringof, T.stringof, rt, ct, R.stringof, repr);
                    //static if (ct != rt) pragma(msg, msg);
                }
            }
        }
    }
}

void test10018(ubyte value)
{
    const int c = value;
    ubyte b = c;
    static assert(!__traits(compiles, b = c - 1));
    static assert(!__traits(compiles, b = c + 1));
    immutable int i = value;
    b = i;
    static assert(!__traits(compiles, b = i - 1));
    static assert(!__traits(compiles, b = i + 1));
}

void test13001(bool unknown)
{
    foreach (const i; 0..unknown?2:3)
    {
        ubyte b = i;
        static assert(!__traits(compiles, b = i - 1));
        b = i + 253;
        static assert(!__traits(compiles, b = i + 254));
    }
}

void test10310()
{
    int y;
    ubyte x = ((y & 252) ^ 2) + 1;
}

// https://issues.dlang.org/show_bug.cgi?id=15289
void test15289a()
{
    int [] arr = [1, 2, 3, 4];
    uint foo = 50 / arr.length;
}

void test15289b()
{
    int [] arr = [1, 2, 3, 4];
    uint foo = 50 % arr.length;
}

void testShiftRightOnNegative()
{
    int neg = -1;
    uint[] arr = [1, 2, 3];
    ubyte b;
    // Shift with negative value returns value in range [0, ulong.max]
    static assert(!__traits(compiles, b = arr.length >> neg));
    static assert(!__traits(compiles, b = arr.length << neg));
}