Debian Codegraph @

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

raw
1 /*
2 TEST_OUTPUT:
3 ---
4 decl: test
5 stmt: test
6 ---
7
8 RUN_OUTPUT:
9 ---
10 0: getter
11 1: setter
12 2: getter
13 3: setter
14 4: setter
15 5: compile error
16 6: compile error
17 7: setter
18 Success
19 ---
20 */
21 extern (C) int printf(const char* fmt, ...);
22
23 /*******************************************/
24
25 template select(alias v1, alias v2)
26 {
27 enum select = v2;
28 }
29
30 struct Test(int N)
31 {
32 int value;
33 int getset;
34
35 static if (N == 0)
36 {
37 ref foo(){ getset = 1; return value; }
38
39 enum result = select!(0, 1);
40 // prints "getter"
41 }
42 static if (N == 1)
43 {
44 ref foo(int x){ getset = 2; value = x; return value; }
45
46 enum result = select!(0, 2);
47 // prints "setter"
48 }
49 static if (N == 2)
50 {
51 @property ref foo(){ getset = 1; return value; }
52
53 enum result = select!(1, 1);
54 // prints "getter"
55 }
56 static if (N == 3)
57 {
58 @property ref foo(int x){ getset = 2; value = x; return value; }
59
60 enum result = select!(2, 2);
61 // prints "setter"
62 }
63
64
65 static if (N == 4)
66 {
67 ref foo() { getset = 1; return value; }
68 ref foo(int x){ getset = 2; value = x; return value; }
69
70 enum result = select!(0, 2);
71 // prints "setter"
72 }
73 static if (N == 5)
74 {
75 @property ref foo() { getset = 1; return value; }
76 ref foo(int x){ getset = 2; value = x; return value; }
77
78 enum result = select!(0, 0);
79 // test.d(xx): Error: cannot overload both property and non-property functions
80 }
81 static if (N == 6)
82 {
83 ref foo() { getset = 1; return value; }
84 @property ref foo(int x){ getset = 2; value = x; return value; }
85
86 enum result = select!(0, 0);
87 // test.d(xx): Error: cannot overload both property and non-property functions
88 }
89 static if (N == 7)
90 {
91 @property ref foo() { getset = 1; return value; }
92 @property ref foo(int x){ getset = 2; value = x; return value; }
93
94 enum result = select!(2, 2);
95 // prints "setter"
96 }
97 }
98
99 template seq(T...)
100 {
101 alias T seq;
102 }
103 template iota(int begin, int end)
104 if (begin <= end)
105 {
106 static if (begin == end)
107 alias seq!() iota;
108 else
109 alias seq!(begin, iota!(begin+1, end)) iota;
110 }
111
112 void test1()
113 {
114 foreach (N; iota!(0, 8))
115 {
116 printf("%d: ", N);
117
118 Test!N s;
119 static if (Test!N.result == 0)
120 {
121 static assert(!is(typeof({ s.foo = 1; })));
122 printf("compile error\n");
123 }
124 else
125 {
126 s.foo = 1;
127 if (s.getset == 1)
128 printf("getter\n");
129 else
130 printf("setter\n");
131 assert(s.getset == Test!N.result);
132 }
133 }
134 }
135
136 /*******************************************/
137 // https://issues.dlang.org/show_bug.cgi?id=7722
138
139 class Foo7722 {}
140 void spam7722(Foo7722 f) {}
141
142 void test7722()
143 {
144 auto f = new Foo7722;
145 f.spam7722;
146 }
147
148 /*******************************************/
149
150 @property void check(alias v1, alias v2, alias dg)()
151 {
152 void checkImpl(alias v)()
153 {
154 static if (v == 0)
155 static assert(!__traits(compiles, dg(0)));
156 else
157 assert(dg(0) == v);
158 }
159
160 checkImpl!(v2)();
161 }
162
163 struct S {}
164
165 int foo(int n) { return 1; }
166 int foo(int n, int m) { return 2; }
167 int goo(int[] a) { return 1; }
168 int goo(int[] a, int m) { return 2; }
169 int bar(S s) { return 1; }
170 int bar(S s, int n) { return 2; }
171
172 int baz(X)(X x) { return 1; }
173 int baz(X)(X x, int n) { return 2; }
174
175 int temp;
176 ref int boo(int n) { return temp; }
177 ref int coo(int[] a) { return temp; }
178 ref int mar(S s) { return temp; }
179
180 ref int maz(X)(X x) { return temp; }
181
182 void test7722a()
183 {
184 int n;
185 int[] a;
186 S s;
187
188 check!(1, 1, x => foo(4) ); check!(1, 1, x => baz(4) );
189 check!(1, 1, x => 4.foo() ); check!(1, 1, x => 4.baz() );
190 check!(0, 1, x => 4.foo ); check!(0, 1, x => 4.baz );
191 check!(2, 2, x => foo(4, 2) ); check!(2, 2, x => baz(4, 2) );
192 check!(2, 2, x => 4.foo(2) ); check!(2, 2, x => 4.baz(2) );
193 check!(0, 2, x => (4.foo = 2) ); check!(0, 2, x => (4.baz = 2) );
194
195 check!(1, 1, x => goo(a) ); check!(1, 1, x => baz(a) );
196 check!(1, 1, x => a.goo() ); check!(1, 1, x => a.baz() );
197 check!(0, 1, x => a.goo ); check!(0, 1, x => a.baz );
198 check!(2, 2, x => goo(a, 2) ); check!(2, 2, x => baz(a, 2) );
199 check!(2, 2, x => a.goo(2) ); check!(2, 2, x => a.baz(2) );
200 check!(0, 2, x => (a.goo = 2) ); check!(0, 2, x => (a.baz = 2) );
201
202 check!(1, 1, x => bar(s) ); check!(1, 1, x => baz(s) );
203 check!(1, 1, x => s.bar() ); check!(1, 1, x => s.baz() );
204 check!(0, 1, x => s.bar ); check!(0, 1, x => s.baz );
205 check!(2, 2, x => bar(s, 2) ); check!(2, 2, x => baz(s, 2) );
206 check!(2, 2, x => s.bar(2) ); check!(2, 2, x => s.baz(2) );
207 check!(0, 2, x => (s.bar = 2) ); check!(0, 2, x => (s.baz = 2) );
208
209 check!(2, 2, x => ( boo(4) = 2)); check!(2, 2, x => ( maz(4) = 2));
210 check!(0, 2, x => (4.boo = 2)); check!(0, 2, x => (4.maz = 2));
211 check!(2, 2, x => ( coo(a) = 2)); check!(2, 2, x => ( maz(a) = 2));
212 check!(0, 2, x => (a.coo = 2)); check!(0, 2, x => (a.maz = 2));
213 check!(2, 2, x => ( mar(s) = 2)); check!(2, 2, x => ( maz(s) = 2));
214 check!(0, 2, x => (s.mar = 2)); check!(0, 2, x => (s.maz = 2));
215 }
216
217 int hoo(T)(int n) { return 1; }
218 int hoo(T)(int n, int m) { return 2; }
219 int koo(T)(int[] a) { return 1; }
220 int koo(T)(int[] a, int m) { return 2; }
221 int var(T)(S s) { return 1; }
222 int var(T)(S s, int n) { return 2; }
223
224 int vaz(T, X)(X x) { return 1; }
225 int vaz(T, X)(X x, int n) { return 2; }
226
227 //int temp;
228 ref int voo(T)(int n) { return temp; }
229 ref int woo(T)(int[] a) { return temp; }
230 ref int nar(T)(S s) { return temp; }
231
232 ref int naz(T, X)(X x) { return temp; }
233
234 void test7722b()
235 {
236 int n;
237 int[] a;
238 S s;
239
240 check!(1, 1, x => hoo!int(4) ); check!(1, 1, x => vaz!int(4) );
241 check!(1, 1, x => 4.hoo!int() ); check!(1, 1, x => 4.vaz!int() );
242 check!(0, 1, x => 4.hoo!int ); check!(0, 1, x => 4.vaz!int );
243 check!(2, 2, x => hoo!int(4, 2) ); check!(2, 2, x => vaz!int(4, 2) );
244 check!(2, 2, x => 4.hoo!int(2) ); check!(2, 2, x => 4.vaz!int(2) );
245 check!(0, 2, x => (4.hoo!int = 2) ); check!(0, 2, x => (4.vaz!int = 2) );
246
247 check!(1, 1, x => koo!int(a) ); check!(1, 1, x => vaz!int(a) );
248 check!(1, 1, x => a.koo!int() ); check!(1, 1, x => a.vaz!int() );
249 check!(0, 1, x => a.koo!int ); check!(0, 1, x => a.vaz!int );
250 check!(2, 2, x => koo!int(a, 2) ); check!(2, 2, x => vaz!int(a, 2) );
251 check!(2, 2, x => a.koo!int(2) ); check!(2, 2, x => a.vaz!int(2) );
252 check!(0, 2, x => (a.koo!int = 2) ); check!(0, 2, x => (a.vaz!int = 2) );
253
254 check!(1, 1, x => var!int(s) ); check!(1, 1, x => vaz!int(s) );
255 check!(1, 1, x => s.var!int() ); check!(1, 1, x => s.vaz!int() );
256 check!(0, 1, x => s.var!int ); check!(0, 1, x => s.vaz!int );
257 check!(2, 2, x => var!int(s, 2) ); check!(2, 2, x => vaz!int(s, 2) );
258 check!(2, 2, x => s.var!int(2) ); check!(2, 2, x => s.vaz!int(2) );
259 check!(0, 2, x => (s.var!int = 2) ); check!(0, 2, x => (s.vaz!int = 2) );
260
261 check!(2, 2, x => ( voo!int(4) = 2)); check!(2, 2, x => ( naz!int(4) = 2));
262 check!(0, 2, x => (4.voo!int = 2)); check!(0, 2, x => (4.naz!int = 2));
263 check!(2, 2, x => ( woo!int(a) = 2)); check!(2, 2, x => ( naz!int(a) = 2));
264 check!(0, 2, x => (a.woo!int = 2)); check!(0, 2, x => (a.naz!int = 2));
265 check!(2, 2, x => ( nar!int(s) = 2)); check!(2, 2, x => ( naz!int(s) = 2));
266 check!(0, 2, x => (s.nar!int = 2)); check!(0, 2, x => (s.naz!int = 2));
267 }
268
269 /*******************************************/
270 // https://issues.dlang.org/show_bug.cgi?id=7174
271
272 void test7174()
273 {
274 @property bool foo7174() { return true; }
275 static if (foo7174) {}
276 }
277
278 /***************************************************/
279 // https://issues.dlang.org/show_bug.cgi?id=7274
280
281 @property foo7274(){ return "test"; }
282 @property bar7274(){ return "kernel32.lib"; }
283
284 pragma(msg, "decl: ", foo7274); // print "decl: foo", not "decl: test"
285 version(Windows) pragma(lib, bar7274); // Error: pragma lib string expected for library name, not 'bar'
286
287 void test7274()
288 {
289 pragma(msg, "stmt: ", foo7274); // print "stmt: foo", not "stmt: test"
290 //pragma(lib, bar); // Error: pragma(lib) not allowed as statement
291 }
292
293 /***************************************************/
294 // https://issues.dlang.org/show_bug.cgi?id=7275
295
296 void test7275()
297 {
298 @property int bar1() { return 0; }
299 @property int bar2() { return 1; }
300 @property int bar3() { return 2; }
301
302 switch (0){
303 case bar1: break;
304 case bar2: ..
305 case bar3: break;
306 default: break;
307 }
308 }
309
310 /*****************************************/
311 // https://issues.dlang.org/show_bug.cgi?id=7538
312
313 void test7538()
314 {
315 struct P
316 {
317 @property long pr() { return 1; }
318 @property void pr(int) {}
319
320 @property long a1()() { return 1; }
321 @property void a1()(int) {}
322 template a2() { @property long a2() { return 1; } }
323 template a2() { @property void a2(int) {} }
324 template a3() { long a3() @property { return 1; } }
325 template a3() { void a3(int) @property {} }
326
327 static
328 {
329 @property long b1()() { return 1; }
330 @property void b1()(int) {}
331 template b2() { @property long b2() { return 1; } }
332 template b2() { @property void b2(int) {} }
333 template b3() { long b3() @property { return 1; } }
334 template b3() { void b3(int) @property {} }
335 }
336
337 @property long c1(T)() { return 1; }
338 @property long c1(T)(int) { return 1; }
339 template c2(T) { @property long c2() { return 1; } }
340 template c2(T) { @property void c2(int) {} }
341 template c3(T) { long c3() @property { return 1; } }
342 template c3(T) { void c3(int) @property {} }
343
344 static
345 {
346 @property long d1(T)() { return 1; }
347 @property void d1(T)(int) {}
348 template d2(T) { @property long d2() { return 1; } }
349 template d2(T) { @property void d2(int) {} }
350 template d3(T) { long d3() @property { return 1; } }
351 template d3(T) { void d3(int) @property {} }
352 }
353
354 void test()
355 {
356 // TOKvar
357 static assert(is(typeof(pr) == long));
358
359 // TOKtemplate
360 static assert(is(typeof(b1) == long));
361 static assert(is(typeof(b2) == long));
362 static assert(is(typeof(b3) == long));
363
364 // TOKimport
365 static assert(is(typeof(d1!int) == long));
366 static assert(is(typeof(d2!int) == long));
367 static assert(is(typeof(d3!int) == long));
368 }
369 }
370 P p;
371 {
372 // TOKdotvar
373 static assert(is(typeof(p.pr) == long));
374
375 // TOKdottd
376 static assert(is(typeof(p.a1) == long));
377 static assert(is(typeof(p.a2) == long));
378 static assert(is(typeof(p.a3) == long));
379
380 // TOKimport
381 static assert(is(typeof(P.b1) == long));
382 static assert(is(typeof(P.b2) == long));
383 static assert(is(typeof(P.b3) == long));
384
385 // TOKdotti;
386 static assert(is(typeof(p.c1!int) == long));
387 static assert(is(typeof(p.c2!int) == long));
388 static assert(is(typeof(p.c3!int) == long));
389 }
390
391 struct F
392 {
393 long fn() { return 1; }
394 void fn(int) {}
395
396 long a1()() { return 1; }
397 void a1()(int) {}
398 template a2() { long a2() { return 1; } }
399 template a2() { void a2(int) {} }
400
401 static
402 {
403 long b1()() { return 1; }
404 void b1()(int) {}
405 template b2() { long b2() { return 1; } }
406 template b2() { void b2(int) {} }
407 }
408
409 long c1(T)() { return 1; }
410 long c1(T)(int) { return 1; }
411 template c2(T) { long c2() { return 1; } }
412 template c2(T) { void c2(int) {} }
413
414 static
415 {
416 long d1(T)() { return 1; }
417 void d1(T)(int) {}
418 template d2(T) { long d2() { return 1; } }
419 template d2(T) { void d2(int) {} }
420 }
421
422 void test()
423 {
424 // TOKvar
425 static assert( is(typeof(fn) == function));
426
427 // TOKtemplate
428 static assert(!is(typeof(b1) == long));
429 static assert(!is(typeof(b2) == long));
430
431 // TOKimport
432 static assert(!is(typeof(d1!int) == long));
433 static assert(!is(typeof(d2!int) == long));
434 }
435 }
436 F f;
437 {
438 // TOKdotvar
439 static assert(is( typeof(f.fn) == function));
440
441 // TOKdottd
442 static assert(!is(typeof(f.a1) == long));
443 static assert(!is(typeof(f.a2) == long));
444
445 // TOKimport
446 static assert(!is(typeof(F.b1) == long));
447 static assert(!is(typeof(F.b2) == long));
448
449 // TOKdotti;
450 static assert(!is(typeof(f.c1!int) == long));
451 static assert(!is(typeof(f.c2!int) == long));
452 }
453 }
454
455 /*****************************************/
456 // https://issues.dlang.org/show_bug.cgi?id=8251
457
458 struct S8251
459 {
460 static @property int min() { return 123; }
461 }
462 @property int T8251_Min() { return 456; }
463
464 template T8251a (int v) { int T8251a = v; }
465
466 template T8251b1(int v = S8251.min) { int T8251b1 = v; }
467 template T8251b2(int v = T8251_Min) { int T8251b2 = v; }
468
469 template T8251c1(int v : S8251.min) { int T8251c1 = v; }
470 template T8251c2(int v : T8251_Min) { int T8251c2 = v; }
471
472 void test8251()
473 {
474 static assert(S8251.min == 123); // OK
475 static assert(T8251_Min == 456); // OK
476 int a0 = T8251a!(S8251.min()); // OK
477 int b0 = T8251a!(T8251_Min()); // OK
478
479 // TemplateValueParameter
480 int a1 = T8251a!(S8251.min); // NG
481 int b1 = T8251a!(T8251_Min); // NG
482
483 // TemplateValueParameterDefault
484 int a2 = T8251b1!(); // NG
485 int b2 = T8251b2!(); // NG
486
487 // TemplateValueParameterSpecialization
488 int a3 = T8251c1!(123); // NG
489 int b3 = T8251c2!(456); // NG
490 }
491
492 /*****************************************/
493 // https://issues.dlang.org/show_bug.cgi?id=9063
494
495 @property bool foo9063(){ return true; }
496 static assert(foo9063);
497
498 /*****************************************/
499 // https://issues.dlang.org/show_bug.cgi?id=9234
500
501 class Fizz9234
502 {
503 void bar() {}
504 Foo9234!bar foobar;
505 }
506
507 struct Foo9234(alias F) {}
508 struct Foo9234(string thunk) {}
509
510 /*****************************************/
511 // https://issues.dlang.org/show_bug.cgi?id=10103
512
513 mixin template Getter10103()
514 {
515 @property auto foo() { return v; }
516 @property auto bar()() { return v; }
517 @property auto baz(T)() { return v; }
518
519 static @property auto goo() { return 1; }
520 }
521
522 mixin template Setter10103()
523 {
524 @property void foo(int x) { v = x; }
525 @property void bar()(int x) { v = x; }
526 @property void baz(T)(int x) { v = x; }
527
528 static @property void goo(int x) {}
529 }
530
531 struct Foo10103
532 {
533 int v;
534 mixin Getter10103!();
535 mixin Setter10103!();
536 }
537
538 void test10103()
539 {
540 auto f = Foo10103(4);
541
542 f.foo;
543 f.foo = 3;
544
545 f.bar;
546 f.bar = 3;
547
548 f.baz!int;
549 f.baz!int = 3;
550
551 Foo10103.goo = 3;
552 }
553
554 /*****************************************/
555 // https://issues.dlang.org/show_bug.cgi?id=10197
556
557 template OriginalType10197(T)
558 {
559 static if (is(T U == enum))
560 alias OriginalType10197 = U;
561 else
562 static assert(0);
563 }
564
565 void test10197()
566 {
567 enum E : int { F = -20 }
568 struct S
569 {
570 int val;
571 @trusted @property T as(T)()
572 if (is(T == int) && !is(T == enum))
573 {
574 return cast(T)(val);
575 }
576 @trusted @property T as(T)()
577 if (is(T == enum))
578 {
579 return cast(T)as!(OriginalType10197!T);
580 }
581 }
582
583 S val = S(-20);
584 assert(val.as!int == -20);
585 assert(val.as!E == E.F);
586 }
587
588 /*****************************************/
589
590 int main()
591 {
592 test1();
593 test7722();
594 test7722a();
595 test7722b();
596 test7174();
597 test7274();
598 test7275();
599 test7538();
600 test8251();
601 test10103();
602 test10197();
603
604 printf("Success\n");
605 return 0;
606 }