Debian Codegraph @

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

raw
1 /*
2 RUN_OUTPUT:
3 ---
4 Success
5 ---
6 */
7 extern(C) int printf(const char*, ...);
8
9 /**********************************/
10 // https://issues.dlang.org/show_bug.cgi?id=7511
11
12 struct S7511(T)
13 {
14 // this is a pure function for T==int
15 T foo(T x)
16 {
17 return 2 * x;
18 }
19 }
20
21 void test7511a() pure
22 {
23 S7511!int s;
24 s.foo(2); // error -> OK
25 }
26
27 /**********************************/
28 // certain case - wrapper range
29
30 //import std.range;
31 @property bool empty(T)(in T[] a) { return !a.length; }
32 @property ref T front(T)(T[] a) { return a[0]; }
33 void popFront(T)(ref T[] a) { a = a[1 .. $]; }
34
35 struct S(T)
36 {
37 int foo()
38 {
39 auto t = T();
40 return t.bar();
41 }
42 }
43
44 struct Wrap(R)
45 {
46 R original;
47 this(T : R)(T t) { original = t; }
48 this(A...)(A args) { original = R(args); }
49 @property auto empty() { return original.empty; }
50 @property auto front() { return original.front; }
51 void popFront() { original.popFront(); }
52 }
53
54 void test7511b() pure @safe
55 {
56 static struct Iota
57 {
58 size_t curr;
59 size_t max;
60 @property bool empty() pure @safe { return curr == max; }
61 @property size_t front() pure @safe { return curr; }
62 void popFront() pure @safe { ++curr; }
63 }
64 {
65 auto a = Iota(0, 3);
66 size_t i = 0;
67 foreach (e; a) { assert(e == i++); } // OK
68 }
69 {
70 auto a = Wrap!(int[])([0,1,2]);
71 size_t i = 0;
72 foreach (e; a) { assert(e == i++); } // errors!
73 }
74 {
75 auto a = Wrap!Iota(0, 3);
76 size_t i = 0;
77 foreach (e; a) { assert(e == i++); } // errors!
78 }
79 }
80
81 /**********************************/
82 // with attribute inheritance
83
84 struct X
85 {
86 static int bar() pure nothrow @safe
87 {
88 return 1;
89 }
90 }
91
92 class Class(T)
93 {
94 int foo()
95 { // inferred to pure nothrow @safe
96 return T.bar();
97 }
98 }
99
100 alias Class!X C;
101
102 class D : C
103 {
104 override int foo()
105 { // inherits attributes from Class!X.foo
106 return 2;
107 }
108 }
109
110 void test7511c() pure nothrow @safe
111 {
112 // Disabled for Bigzilla 9952
113 /+
114 assert((new C()).foo() == 1);
115 assert((new D()).foo() == 2);
116 static assert(typeof(&C.init.foo).stringof == "int delegate() pure nothrow @safe");
117 static assert(typeof(&D.init.foo).stringof == "int delegate() pure nothrow @safe");
118 +/
119 }
120
121 /**********************************/
122 // curiously recurring template pattern (CRTP)
123
124 class BX(T, bool mutual)
125 {
126 int foo()
127 {
128 static if (mutual)
129 return (cast(T)this).foo();
130 else
131 return 0;
132 }
133 }
134
135 class D1 : BX!(D1, true)
136 {
137 alias typeof(super) B;
138 int val;
139 this(int n) { val = n; }
140 override int foo() { return val; }
141 }
142 class D2 : BX!(D2, false)
143 {
144 alias typeof(super) B;
145 int val;
146 this(int n) { val = n; }
147 override int foo() { return val; }
148 }
149 class D3 : BX!(D3, true)
150 {
151 alias typeof(super) B;
152 int val;
153 this(int n) { val = n; }
154 override int foo() pure nothrow { return val; }
155 }
156 class D4 : BX!(D4, false)
157 {
158 alias typeof(super) B;
159 int val;
160 this(int n) { val = n; }
161 override int foo() pure nothrow { return val; }
162 }
163
164 void test7511d()
165 {
166 // Disabled for Bigzilla 9952
167 /+
168 // mutual dependent and attribute inference impure, un-@safe, and may throw is default.
169 auto d1 = new D1(10);
170 static assert(is(typeof(&d1.B.foo) == int function()));
171 static assert(is(typeof(&d1.foo) == int delegate()));
172 assert(d1.foo() == 10);
173
174 // no mutual dependent.
175 auto d2 = new D2(10);
176 static assert(is(typeof(&d2.B.foo) == int function() pure nothrow @safe));
177 static assert(is(typeof(&d2 .foo) == int delegate() pure nothrow @safe));
178 assert(d2.foo() == 10);
179
180 // mutual dependent with explicit attribute specification.
181 auto d3 = new D3(10);
182 static assert(is(typeof(&d3.B.foo) == int function() pure nothrow));
183 static assert(is(typeof(&d3 .foo) == int delegate() pure nothrow));
184 assert(d3.foo() == 10);
185
186 // no mutual dependent with explicit attribute specification.
187 auto d4 = new D4(10);
188 static assert(is(typeof(&d4.B.foo) == int function() pure nothrow @safe));
189 static assert(is(typeof(&d4 .foo) == int delegate() pure nothrow @safe));
190 assert(d4.foo() == 10);
191 +/
192 }
193
194 /**********************************/
195 // https://issues.dlang.org/show_bug.cgi?id=9952
196
197 @system void writeln9952(int) {} // impure throwable
198
199 class C9952(T)
200 {
201 T foo()
202 {
203 return 2;
204 }
205 }
206
207 class D9952 : C9952!int
208 {
209 override int foo()
210 {
211 writeln9952(super.foo());
212 return 3;
213 }
214 }
215
216 void test9952()
217 {
218 static assert(typeof(&C9952!int.init.foo).stringof == "int delegate()");
219 static assert(typeof(&D9952 .init.foo).stringof == "int delegate()");
220 }
221
222 /**********************************/
223 // https://issues.dlang.org/show_bug.cgi?id=10373
224
225 template isMutable10373(T)
226 {
227 enum isMutable10373 = !is(T == const) && !is(T == immutable) && !is(T == inout);
228 }
229
230 struct Test10373a(T, int N = 0)
231 {
232 static if (N == 0) T[ ] mBuffer;
233 else T[N] mBuffer;
234
235 static if (is(T == class))
236 {}
237 else
238 {
239 T* at_(size_t n) { return &mBuffer[n]; }
240
241 static if (is(typeof(*at_(0) = T.init)))
242 {
243 T opIndexAssign(T v, size_t i)
244 {
245 return (*at_(i) = v);
246 }
247 }
248 }
249 }
250 struct Test10373b(T, int N = 0)
251 {
252 static if (is(T == class))
253 {}
254 else
255 {
256 T* at_(size_t n) { return &mBuffer[n]; }
257
258 static if (is(typeof(*at_(0) = T.init)))
259 {
260 T opIndexAssign(T v, size_t i)
261 {
262 return (*at_(i) = v);
263 }
264 }
265 }
266
267 static if (N == 0) T[ ] mBuffer;
268 else T[N] mBuffer;
269 }
270 struct Test10373c(T, int N = 0)
271 {
272 static if (is(T == class))
273 {}
274 else
275 {
276 T* at_(size_t n) { return &mBuffer[n]; }
277
278 static if (isMutable10373!T)
279 {
280 T opIndexAssign(T v, size_t i)
281 {
282 return (*at_(i) = v);
283 }
284 }
285 }
286
287 static if (N == 0) T[ ] mBuffer;
288 else T[N] mBuffer;
289 }
290
291 void test10373()
292 {
293 static assert(is(Test10373a!(int, 2)));
294 static assert(is(Test10373b!(int, 2)));
295 static assert(is(Test10373c!(int, 2)));
296
297 Test10373a!(int, 2) testa; // dmd2.062:OK dmd2.063:OK
298 Test10373b!(int, 2) testb; // dmd2.062:OK dmd2.063:NG
299 Test10373c!(int, 2) testc; // dmd2.062:OK dmd2.063:OK
300 }
301
302 /**********************************/
303 // https://issues.dlang.org/show_bug.cgi?id=10329
304
305 auto foo10329(T)(T arg)
306 {
307 auto bar()
308 {
309 return arg;
310 }
311 static assert(is(typeof(&bar) == T delegate() pure nothrow @nogc @safe));
312 return bar();
313 }
314
315 auto make10329(T)(T arg)
316 {
317 struct S
318 {
319 auto front() { return T.init; }
320 }
321 S s;
322 static assert(is(typeof(&s.front) == T delegate() pure nothrow @nogc @safe));
323 return s;
324 }
325
326 void test10329() pure nothrow @safe
327 {
328 assert(foo10329(1) == 1);
329
330 auto s = make10329(1);
331 assert(s.front() == 0);
332 }
333
334 /**********************************/
335 // https://issues.dlang.org/show_bug.cgi?id=11896
336
337 class Foo11896a(T = int)
338 {
339 static if (!__traits(isVirtualMethod, zoo)) {} else { Undefined x; }
340
341 static void bar() {}
342 static void bar(Foo11896a foo) {}
343
344 static void zoo()
345 {
346 bar(new Foo11896a);
347 }
348 }
349 Foo11896a!(int) baz11896a;
350
351 // ----
352
353 Frop11896b!(int) frop11896b;
354
355 mixin template baz11896b()
356 {
357 public void bar11896b() {}
358 }
359
360 mixin baz11896b;
361
362 class Foo11896b(T)
363 {
364 static if (! __traits(isVirtualMethod, zoo)) {}
365
366 static void zoo()
367 {
368 bar11896b();
369 }
370 }
371
372 class Frop11896b(T) : Foo11896b!T {}
373
374 // ----
375
376 static bool flag11896c = false;
377
378 class Bar11896c {}
379
380 class Foo11896c(T = Bar11896c)
381 {
382 static if (! __traits(isVirtualMethod, foo)) {}
383 alias Foo11896c!(T) this_type;
384 this()
385 {
386 flag11896c = true;
387 }
388 static public this_type foo()
389 {
390 auto c = new this_type();
391 return flag11896c ? c : null;
392 }
393 }
394
395 void test11896c()
396 {
397 alias Foo11896c!Bar11896c FooBar;
398 assert(FooBar.foo() !is null);
399 }
400
401 /**********************************/
402 // https://issues.dlang.org/show_bug.cgi?id=12392
403
404 void f12392(T)() {}
405 alias fa12392 = f12392;
406
407 void test12392() pure nothrow @safe
408 {
409 fa12392!int();
410 }
411
412 /**********************************/
413
414 int main()
415 {
416 test7511a();
417 test7511b();
418 test7511c();
419 test7511d();
420 test9952();
421 test10373();
422 test10329();
423 test11896c();
424
425 printf("Success\n");
426 return 0;
427 }