Debian Codegraph @

gcc/testsuite/gdc.test/runnable/testrightthis.d

raw
1 /*
2 runnable/traits.d 9091,8972,8971,7027
3 runnable/test4.d test6()
4 RUN_OUTPUT:
5 ---
6 Success
7 ---
8 */
9
10 extern(C) int printf(const char*, ...);
11
12 template TypeTuple(TL...) { alias TypeTuple = TL; }
13
14 /********************************************************/
15
16 mixin("struct S1 {"~aggrDecl1~"}");
17 mixin("class C1 {"~aggrDecl1~"}");
18 enum aggrDecl1 =
19 q{
20 alias Type = typeof(this);
21
22 int x = 2;
23
24 void foo()
25 {
26 static assert( is(typeof(Type.x.offsetof)));
27 static assert( is(typeof(Type.x.mangleof)));
28 static assert( is(typeof(Type.x.sizeof )));
29 static assert( is(typeof(Type.x.alignof )));
30 static assert( is(typeof({ auto n = Type.x.offsetof; })));
31 static assert( is(typeof({ auto n = Type.x.mangleof; })));
32 static assert( is(typeof({ auto n = Type.x.sizeof; })));
33 static assert( is(typeof({ auto n = Type.x.alignof; })));
34 static assert( is(typeof(Type.x)));
35 static assert( is(typeof({ auto n = Type.x; })));
36 static assert( __traits(compiles, Type.x));
37 static assert( __traits(compiles, { auto n = Type.x; }));
38
39 static assert( is(typeof(x.offsetof)));
40 static assert( is(typeof(x.mangleof)));
41 static assert( is(typeof(x.sizeof )));
42 static assert( is(typeof(x.alignof )));
43 static assert( is(typeof({ auto n = x.offsetof; })));
44 static assert( is(typeof({ auto n = x.mangleof; })));
45 static assert( is(typeof({ auto n = x.sizeof; })));
46 static assert( is(typeof({ auto n = x.alignof; })));
47 static assert( is(typeof(x)));
48 static assert( is(typeof({ auto n = x; })));
49 static assert( __traits(compiles, x));
50 static assert( __traits(compiles, { auto n = x; }));
51
52 with (this)
53 {
54 static assert( is(typeof(x.offsetof)));
55 static assert( is(typeof(x.mangleof)));
56 static assert( is(typeof(x.sizeof )));
57 static assert( is(typeof(x.alignof )));
58 static assert( is(typeof({ auto n = x.offsetof; })));
59 static assert( is(typeof({ auto n = x.mangleof; })));
60 static assert( is(typeof({ auto n = x.sizeof; })));
61 static assert( is(typeof({ auto n = x.alignof; })));
62 static assert( is(typeof(x)));
63 static assert( is(typeof({ auto n = x; })));
64 static assert( __traits(compiles, x));
65 static assert( __traits(compiles, { auto n = x; }));
66 }
67 }
68
69 static void bar()
70 {
71 static assert( is(typeof(Type.x.offsetof)));
72 static assert( is(typeof(Type.x.mangleof)));
73 static assert( is(typeof(Type.x.sizeof )));
74 static assert( is(typeof(Type.x.alignof )));
75 static assert( is(typeof({ auto n = Type.x.offsetof; })));
76 static assert( is(typeof({ auto n = Type.x.mangleof; })));
77 static assert( is(typeof({ auto n = Type.x.sizeof; })));
78 static assert( is(typeof({ auto n = Type.x.alignof; })));
79 static assert( is(typeof(Type.x)));
80 static assert(!is(typeof({ auto n = Type.x; })));
81 static assert( __traits(compiles, Type.x));
82 static assert(!__traits(compiles, { auto n = Type.x; }));
83
84 static assert( is(typeof(x.offsetof)));
85 static assert( is(typeof(x.mangleof)));
86 static assert( is(typeof(x.sizeof )));
87 static assert( is(typeof(x.alignof )));
88 static assert( is(typeof({ auto n = x.offsetof; })));
89 static assert( is(typeof({ auto n = x.mangleof; })));
90 static assert( is(typeof({ auto n = x.sizeof; })));
91 static assert( is(typeof({ auto n = x.alignof; })));
92 static assert( is(typeof(x)));
93 static assert(!is(typeof({ auto n = x; })));
94 static assert( __traits(compiles, x));
95 static assert(!__traits(compiles, { auto n = x; }));
96
97 Type t;
98 with (t)
99 {
100 static assert( is(typeof(x.offsetof)));
101 static assert( is(typeof(x.mangleof)));
102 static assert( is(typeof(x.sizeof )));
103 static assert( is(typeof(x.alignof )));
104 static assert( is(typeof({ auto n = x.offsetof; })));
105 static assert( is(typeof({ auto n = x.mangleof; })));
106 static assert( is(typeof({ auto n = x.sizeof; })));
107 static assert( is(typeof({ auto n = x.alignof; })));
108 static assert( is(typeof(x)));
109 static assert( is(typeof({ auto n = x; })));
110 static assert( __traits(compiles, x));
111 static assert( __traits(compiles, { auto n = x; }));
112 }
113 }
114 };
115 void test1()
116 {
117 foreach (Type; TypeTuple!(S1, C1))
118 {
119 static assert( is(typeof(Type.x.offsetof)));
120 static assert( is(typeof(Type.x.mangleof)));
121 static assert( is(typeof(Type.x.sizeof )));
122 static assert( is(typeof(Type.x.alignof )));
123 static assert( is(typeof({ auto n = Type.x.offsetof; })));
124 static assert( is(typeof({ auto n = Type.x.mangleof; })));
125 static assert( is(typeof({ auto n = Type.x.sizeof; })));
126 static assert( is(typeof({ auto n = Type.x.alignof; })));
127 static assert( is(typeof(Type.x)));
128 static assert(!is(typeof({ auto n = Type.x; })));
129 static assert( __traits(compiles, Type.x));
130 static assert(!__traits(compiles, { auto n = Type.x; }));
131
132 Type t;
133 static assert( is(typeof(t.x.offsetof)));
134 static assert( is(typeof(t.x.mangleof)));
135 static assert( is(typeof(t.x.sizeof )));
136 static assert( is(typeof(t.x.alignof )));
137 static assert( is(typeof({ auto n = t.x.offsetof; })));
138 static assert( is(typeof({ auto n = t.x.mangleof; })));
139 static assert( is(typeof({ auto n = t.x.sizeof; })));
140 static assert( is(typeof({ auto n = t.x.alignof; })));
141 static assert( is(typeof(t.x)));
142 static assert( is(typeof({ auto n = t.x; })));
143 static assert( __traits(compiles, t.x));
144 static assert( __traits(compiles, { auto n = t.x; }));
145
146 with (t)
147 {
148 static assert( is(typeof(x.offsetof)));
149 static assert( is(typeof(x.mangleof)));
150 static assert( is(typeof(x.sizeof )));
151 static assert( is(typeof(x.alignof )));
152 static assert( is(typeof({ auto n = x.offsetof; })));
153 static assert( is(typeof({ auto n = x.mangleof; })));
154 static assert( is(typeof({ auto n = x.sizeof; })));
155 static assert( is(typeof({ auto n = x.alignof; })));
156 static assert( is(typeof(x)));
157 static assert( is(typeof({ auto n = x; })));
158 static assert( __traits(compiles, x));
159 static assert( __traits(compiles, { auto n = x; }));
160 }
161 }
162 }
163
164 /********************************************************/
165
166 void test2()
167 {
168 struct S
169 {
170 int val;
171 int[] arr;
172 int[int] aar;
173
174 void foo() {}
175 void boo()() {}
176
177 static void test()
178 {
179 static assert(!__traits(compiles, S.foo()));
180 static assert(!__traits(compiles, S.boo()));
181 static assert(!__traits(compiles, foo()));
182 static assert(!__traits(compiles, boo()));
183 }
184 }
185 int v;
186 int[] a;
187 void f(int n) {}
188
189 static assert( __traits(compiles, S.val)); // 'S.val' is treated just a symbol
190 static assert(!__traits(compiles, { int n = S.val; }));
191 static assert(!__traits(compiles, f(S.val)));
192
193 static assert(!__traits(compiles, v = S.val) && !__traits(compiles, S.val = v));
194
195 static assert(!__traits(compiles, 1 + S.val) && !__traits(compiles, S.val + 1));
196 static assert(!__traits(compiles, 1 - S.val) && !__traits(compiles, S.val - 1));
197 static assert(!__traits(compiles, 1 * S.val) && !__traits(compiles, S.val * 1));
198 static assert(!__traits(compiles, 1 / S.val) && !__traits(compiles, S.val / 1));
199 static assert(!__traits(compiles, 1 % S.val) && !__traits(compiles, S.val % 1));
200 static assert(!__traits(compiles, 1 ~ S.arr) && !__traits(compiles, S.arr ~ 1));
201
202 static assert(!__traits(compiles, 1 & S.val) && !__traits(compiles, S.val & 1));
203 static assert(!__traits(compiles, 1 | S.val) && !__traits(compiles, S.val | 1));
204 static assert(!__traits(compiles, 1 ^ S.val) && !__traits(compiles, S.val ^ 1));
205 static assert(!__traits(compiles, 1 ~ S.val) && !__traits(compiles, S.val ~ 1));
206
207 static assert(!__traits(compiles, 1 ^^ S.val) && !__traits(compiles, S.val ^^ 1));
208 static assert(!__traits(compiles, 1 << S.val) && !__traits(compiles, S.val << 1));
209 static assert(!__traits(compiles, 1 >> S.val) && !__traits(compiles, S.val >> 1));
210 static assert(!__traits(compiles, 1 >>>S.val) && !__traits(compiles, S.val >>>1));
211 static assert(!__traits(compiles, 1 && S.val) && !__traits(compiles, S.val && 1));
212 static assert(!__traits(compiles, 1 || S.val) && !__traits(compiles, S.val || 1));
213 static assert(!__traits(compiles, 1 in S.aar) && !__traits(compiles, S.val || [1:1]));
214
215 static assert(!__traits(compiles, 1 <= S.val) && !__traits(compiles, S.val <= 1));
216 static assert(!__traits(compiles, 1 == S.val) && !__traits(compiles, S.val == 1));
217 static assert(!__traits(compiles, 1 is S.val) && !__traits(compiles, S.val is 1));
218
219 static assert(!__traits(compiles, 1? 1:S.val) && !__traits(compiles, 1? S.val:1));
220 static assert(!__traits(compiles, (1, S.val)) && !__traits(compiles, (S.val, 1)));
221
222 static assert(!__traits(compiles, &S.val));
223 static assert(!__traits(compiles, S.arr[0]) && !__traits(compiles, [1,2][S.val]));
224 static assert(!__traits(compiles, S.val++) && !__traits(compiles, S.val--));
225 static assert(!__traits(compiles, ++S.val) && !__traits(compiles, --S.val));
226
227 static assert(!__traits(compiles, v += S.val) && !__traits(compiles, S.val += 1));
228 static assert(!__traits(compiles, v -= S.val) && !__traits(compiles, S.val -= 1));
229 static assert(!__traits(compiles, v *= S.val) && !__traits(compiles, S.val *= 1));
230 static assert(!__traits(compiles, v /= S.val) && !__traits(compiles, S.val /= 1));
231 static assert(!__traits(compiles, v %= S.val) && !__traits(compiles, S.val %= 1));
232 static assert(!__traits(compiles, v &= S.val) && !__traits(compiles, S.val &= 1));
233 static assert(!__traits(compiles, v |= S.val) && !__traits(compiles, S.val |= 1));
234 static assert(!__traits(compiles, v ^= S.val) && !__traits(compiles, S.val ^= 1));
235 static assert(!__traits(compiles, a ~= S.val) && !__traits(compiles, S.arr ~= 1));
236
237 static assert(!__traits(compiles, v ^^= S.val) && !__traits(compiles, S.val ^^= 1));
238 static assert(!__traits(compiles, v <<= S.val) && !__traits(compiles, S.val <<= 1));
239 static assert(!__traits(compiles, v >>= S.val) && !__traits(compiles, S.val >>= 1));
240 static assert(!__traits(compiles, v >>>=S.val) && !__traits(compiles, S.val >>>=1));
241
242 static assert(!__traits(compiles, { auto x = 1 + S.val; }) && !__traits(compiles, { auto x = S.val + 1; }));
243 static assert(!__traits(compiles, { auto x = 1 - S.val; }) && !__traits(compiles, { auto x = S.val - 1; }));
244 static assert(!__traits(compiles, { auto x = S.arr ~ 1; }) && !__traits(compiles, { auto x = 1 ~ S.arr; }));
245
246 static assert(!__traits(compiles, S.foo()));
247 static assert(!__traits(compiles, S.boo()));
248 S.test();
249 alias foo = S.foo;
250 alias boo = S.boo;
251 static assert(!__traits(compiles, foo()));
252 static assert(!__traits(compiles, boo()));
253
254 // static assert(S.val);
255
256 struct SW { int a; }
257 class CW { int a; }
258 static assert(!__traits(compiles, { with (SW) { int n = a; } }));
259 static assert(!__traits(compiles, { with (CW) { int n = a; } }));
260 }
261
262 /********************************************************/
263
264 struct S3
265 {
266 struct T3 { int val; void foo() {} }
267 T3 member;
268 alias member this;
269
270 static void test()
271 {
272 static assert(!__traits(compiles, S3.val = 1 ));
273 static assert(!__traits(compiles, { S3.val = 1; }));
274 static assert(!__traits(compiles, T3.val = 1 ));
275 static assert(!__traits(compiles, { T3.val = 1; }));
276 static assert(!__traits(compiles, __traits(getMember, S3, "val") = 1 ));
277 static assert(!__traits(compiles, { __traits(getMember, S3, "val") = 1; }));
278 static assert(!__traits(compiles, __traits(getMember, T3, "val") = 1 ));
279 static assert(!__traits(compiles, { __traits(getMember, T3, "val") = 1; }));
280
281 static assert(!__traits(compiles, S3.foo() ));
282 static assert(!__traits(compiles, { S3.foo(); }));
283 static assert(!__traits(compiles, T3.foo() ));
284 static assert(!__traits(compiles, { T3.foo(); }));
285 static assert(!__traits(compiles, __traits(getMember, S3, "foo")() ));
286 static assert(!__traits(compiles, { __traits(getMember, S3, "foo")(); }));
287 static assert(!__traits(compiles, __traits(getMember, T3, "foo")() ));
288 static assert(!__traits(compiles, { __traits(getMember, T3, "foo")(); }));
289 static assert(!__traits(compiles, __traits(getOverloads, S3, "foo")[0]() ));
290 static assert(!__traits(compiles, { __traits(getOverloads, S3, "foo")[0](); }));
291 static assert(!__traits(compiles, __traits(getOverloads, T3, "foo")[0]() ));
292 static assert(!__traits(compiles, { __traits(getOverloads, T3, "foo")[0](); }));
293 }
294 }
295
296 void test3()
297 {
298 }
299
300 /********************************************************/
301
302 void test4()
303 {
304 static struct R
305 {
306 void opIndex(int) {}
307 void opSlice() {}
308 void opSlice(int, int) {}
309 int opDollar() { return 1; }
310 alias length = opDollar;
311 }
312
313 R val;
314 static struct S
315 {
316 R val;
317 void foo()
318 {
319 static assert(__traits(compiles, val[1])); // TypeSArray
320 static assert(__traits(compiles, val[])); // TypeDArray
321 static assert(__traits(compiles, val[0..val.length])); // TypeSlice
322 }
323 }
324 }
325
326 /********************************************************/
327
328 template Test5(string name, bool result)
329 {
330 mixin(`static assert(__traits(compiles, `~name~`.add!"months"(1)) == result);`);
331 }
332
333 static struct Begin5
334 {
335 void add(string s)(int n) {}
336 }
337
338 struct IntervalX5(TP)
339 {
340 Begin5 begin;
341
342 static assert(__traits(compiles, begin.add!"months"(1)) == true);
343 mixin Test5!("begin", true);
344
345 void foo()
346 {
347 static assert(__traits(compiles, begin.add!"months"(1)) == true);
348 mixin Test5!("begin", true);
349 }
350 static test()
351 {
352 static assert(__traits(compiles, begin.add!"months"(1)) == false);
353 mixin Test5!("begin", false);
354 }
355 }
356
357 alias IX5 = IntervalX5!int;
358 alias beginX5 = IX5.begin;
359 static assert(__traits(compiles, beginX5.add!"months"(1)) == false);
360 mixin Test5!("beginG5", false);
361
362 void test5()
363 {
364 static struct IntervalY5(TP)
365 {
366 Begin5 begin;
367
368 static assert(__traits(compiles, begin.add!"months"(1)) == true);
369 mixin Test5!("begin", true);
370
371 void foo()
372 {
373 static assert(__traits(compiles, begin.add!"months"(1)) == true);
374 mixin Test5!("begin", true);
375 }
376 static test()
377 {
378 static assert(__traits(compiles, begin.add!"months"(1)) == false);
379 mixin Test5!("begin", false);
380 }
381 }
382
383 alias IX = IntervalX5!int;
384 alias beginX = IX.begin;
385 static assert(__traits(compiles, beginX.add!"months"(1)) == false);
386 mixin Test5!("beginX", false);
387
388 alias IY = IntervalY5!int;
389 alias beginY = IY.begin;
390 static assert(__traits(compiles, beginY.add!"months"(1)) == false);
391 mixin Test5!("beginY", false);
392 }
393
394 /********************************************************/
395
396 void test6()
397 {
398 static struct Foo
399 {
400 static struct Bar
401 {
402 static int get() { return 0; }
403 static int val;
404 void set() { assert(0); }
405 int num;
406 }
407 static class Baz
408 {
409 static int get() { return 0; }
410 static int val;
411 void set() { assert(0); }
412 int num;
413 }
414 Bar bar;
415 Baz baz;
416 }
417
418 // allowed cases that do 'use' Foo.bar without this
419 assert(Foo.bar.get() == 0); // Foo.bar.get()
420 assert(Foo.baz.get() == 0); // Foo.bar.get()
421 static assert(!__traits(compiles, Foo.bar.set()));
422 static assert(!__traits(compiles, Foo.baz.set()));
423
424 assert(Foo.bar.val == 0); // Foo.bar.val
425 assert(Foo.baz.val == 0); // Foo.baz.val
426 static assert(!__traits(compiles, Foo.bar.num = 1));
427 static assert(!__traits(compiles, Foo.baz.num = 1));
428 }
429
430 /********************************************************/
431
432 struct Tuple7(T...)
433 {
434 T field;
435
436 enum check1 = is(typeof(field[0] = 1));
437 enum check2 = is(typeof({ field[0] = 1; }));
438
439 this(U, size_t n)(U[n] values)
440 if (is(typeof({ foreach (i, _; T) field[0] = values[0]; })))
441 {}
442 }
443
444 void test7()
445 {
446 alias Tuple7!(int, int) Tup7;
447 static assert(Tup7.check1);
448 static assert(Tup7.check2);
449 int[2] ints = [ 1, 2 ];
450 Tup7 t = ints;
451
452 struct S7
453 {
454 int value;
455
456 enum check1 = is(typeof(value = 1));
457 enum check2 = is(typeof({ value = 1; }));
458
459 void foo()(int v)
460 if (is(typeof({
461 value = v; // valid
462 }))) {}
463
464 static void bar()(int v)
465 if (is(typeof({
466 value = v; // always invalid
467 }))) {}
468 }
469 static assert(S7.check1);
470 static assert(S7.check2);
471 S7 s;
472 s.foo(1);
473 static assert(!__traits(compiles, S7.bar(1)));
474 }
475
476 /********************************************************/
477 // https://issues.dlang.org/show_bug.cgi?id=4350
478
479 template Mix4350() { int b; }
480
481 struct S4350
482 {
483 int a;
484
485 mixin Mix4350 mix;
486
487 int c;
488 template Func() { void call(int n) { c = n; } }
489 alias func = Func!();
490 }
491
492 void test4350()
493 {
494 S4350 s;
495
496 s.a = 1;
497 s.mix.b = 2;
498 s.func.call(3);
499 assert(s.a == 1);
500 assert(s.b == 2);
501 assert(s.c == 3);
502
503 with (s) { a = 2; }
504 with (s) { mix.b = 3; }
505 with (s) { func.call(4); }
506 assert(s.a == 2);
507 assert(s.b == 3);
508 assert(s.c == 4);
509 }
510
511 /********************************************************/
512 // https://issues.dlang.org/show_bug.cgi?id=6430
513
514 auto bug6430(int a)
515 {
516 static struct Result2 {}
517 return 4;
518 }
519 auto bug6430(int a, int b)
520 {
521 static struct Result2
522 {
523 int z;
524 int y() { return z; }
525 }
526 auto t = Result2(1);
527 return 5;
528 }
529
530 /********************************************************/
531 // https://issues.dlang.org/show_bug.cgi?id=9619
532
533 struct Foo9619 { int x; }
534 void test9619()
535 {
536 void bar()
537 {
538 typeof(Foo9619.x) y;
539 }
540 }
541
542 /********************************************************/
543 // https://issues.dlang.org/show_bug.cgi?id=9633
544
545 class Foo9633
546 {
547 void baz() {}
548 void bar()
549 {
550 // CallExp::e1->op == TOKvar
551 static assert(!compilesWithoutThis9633!baz);
552 }
553 void vaz()()
554 {
555 static class C
556 {
557 // CallExp::e1->op == TOKtemplate
558 static assert(!__traits(compiles, vaz()));
559 }
560 }
561 }
562
563 template compilesWithoutThis9633(alias F)
564 {
565 enum bool compilesWithoutThis9633 = __traits(compiles, F());
566 }
567
568 void test9633()
569 {
570 auto foo = new Foo9633;
571 foo.bar();
572 foo.vaz();
573 }
574
575 /********************************************************/
576 // https://issues.dlang.org/show_bug.cgi?id=11245
577
578 struct Vec11245
579 {
580 float[2] f;
581 }
582
583 class Bar11245
584 {
585 void func()
586 {
587 float[Vec11245.f.length] newVal;
588 }
589 }
590
591 /********************************************************/
592 // https://issues.dlang.org/show_bug.cgi?id=11614
593
594 struct Tuple11614(T...)
595 {
596 T field;
597 alias field this;
598 }
599
600 struct Foo11614
601 {
602 alias Tuple11614!(int) NEW_ARGS;
603
604 NEW_ARGS args;
605
606 void foo()
607 {
608 static if (NEW_ARGS.length == 1)
609 {}
610 else
611 static assert(0);
612 }
613 }
614
615 /********************************************************/
616 // https://issues.dlang.org/show_bug.cgi?id=11993
617
618 struct S11993
619 {
620 void foo()() const
621 if (is(typeof(this) == const(S11993)))
622 {}
623
624 const void bar()()
625 if (is(typeof(this) == const(S11993)))
626 {}
627 }
628
629 void test11993()
630 {
631 S11993 s;
632 s.foo();
633 s.bar();
634 }
635
636 /********************************************************/
637 // https://issues.dlang.org/show_bug.cgi?id=15934
638
639 class B15934
640 {
641 int foo() { return 1; }
642 int foo() const { return 2; }
643 }
644
645 class C15934 : B15934
646 {
647 override int foo() { return 3; }
648 override int foo() const { return 4; }
649
650 void test1()
651 {
652 assert(this.foo() == 3);
653 assert( foo() == 3);
654 assert(this.B15934.foo() == 1);
655 assert( B15934.foo() == 1);
656 }
657
658 void test2() const
659 {
660 assert(this.foo() == 4);
661 assert( foo() == 4);
662 assert(this.B15934.foo() == 2); // OK <- wrongly returns 1
663 assert( B15934.foo() == 2); // OK <- wrongly returns 1
664 }
665 }
666
667 void test15934()
668 {
669 auto c = new C15934();
670 c.test1();
671 c.test2();
672 }
673
674 /********************************************************/
675
676 int main()
677 {
678 test1();
679 test2();
680 test3();
681 test4();
682 test5();
683 test6();
684 test7();
685 test4350();
686 test9619();
687 test9633();
688 test15934();
689
690 printf("Success\n");
691 return 0;
692 }