Debian Codegraph @

gcc/testsuite/gdc.test/compilable/testfptr.d

raw
1 // PERMUTE_ARGS:
2
3 ref int frvv();
4 class A {}
5 class B : A {}
6
7 B restrictedfunc(in const(int)) @safe pure nothrow;
8 A relaxedfunc(in int);
9
10 void bug3797()
11 {
12 // Cannot convert if the return type or parameters are different
13
14 void function() vv;
15 void function(int) vi;
16 int function() iv;
17 const(int) function() cv;
18 immutable(int) function() xv;
19
20 static assert( is(typeof( vv = vv )));
21 static assert(!is(typeof( vv = vi )));
22 static assert(!is(typeof( vv = iv )));
23 static assert(!is(typeof( vv = cv )));
24 static assert(!is(typeof( vv = xv )));
25
26 static assert(!is(typeof( vi = vv )));
27 static assert( is(typeof( vi = vi )));
28 static assert(!is(typeof( vi = iv )));
29 static assert(!is(typeof( vi = cv )));
30 static assert(!is(typeof( vi = cx )));
31
32 static assert(!is(typeof( iv = vv )));
33 static assert(!is(typeof( iv = vi )));
34 static assert( is(typeof( iv = iv )));
35 static assert( is(typeof( iv = cv )));
36 static assert( is(typeof( iv = xv )));
37
38 static assert(!is(typeof( cv = vv )));
39 static assert( is(typeof( cv = iv )));
40 static assert(!is(typeof( cv = vi )));
41 static assert( is(typeof( cv = cv )));
42 static assert( is(typeof( cv = xv )));
43
44 static assert(!is(typeof( xv = vv )));
45 static assert( is(typeof( xv = iv )));
46 static assert(!is(typeof( xv = vi )));
47 static assert( is(typeof( xv = cv )));
48 static assert( is(typeof( xv = xv )));
49
50 int* function() ipfunc;
51 const(int*) function() cipfunc;
52
53 static assert( is(typeof( cipfunc = ipfunc )) );
54 static assert(!is(typeof( ipfunc = cipfunc )) );
55
56 // functions with different linkages can't convert
57
58 extern(C) void function() cfunc;
59 extern(D) void function() dfunc;
60
61 static assert(!is(typeof( cfunc = dfunc )));
62 static assert(!is(typeof( dfunc = cfunc )));
63
64 // ref return can't convert to non-ref return
65
66 typeof(&frvv) rvv;
67
68 static assert(!is(typeof( rvv = iv )));
69 static assert(!is(typeof( rvv = cv )));
70
71 static assert(!is(typeof( iv = rvv )));
72 static assert(!is(typeof( cv = rvv )));
73
74 // variadic functions don't mix
75
76 void function(...) vf;
77
78 static assert(!is(typeof( vf = vv )));
79 static assert(!is(typeof( vv = vf )));
80
81 // non-nothrow -> nothrow
82
83 void function() nothrow ntf;
84
85 static assert(!is(typeof( ntf = vv )));
86 static assert( is(typeof( vv = ntf )));
87
88 // @safe <-> @trusted -> @system
89
90 void function() @system systemfunc;
91 void function() @trusted trustedfunc;
92 void function() @safe safefunc;
93
94 static assert( is(typeof( trustedfunc = safefunc )));
95 static assert( is(typeof( systemfunc = trustedfunc )));
96 static assert( is(typeof( systemfunc = safefunc )));
97 static assert( is(typeof( safefunc = trustedfunc )));
98
99 static assert(!is(typeof( trustedfunc = systemfunc )));
100 static assert(!is(typeof( safefunc = systemfunc )));
101
102 // pure -> non-pure
103
104 void function() nonpurefunc;
105 void function() pure purefunc;
106
107 static assert(!is(typeof( purefunc = nonpurefunc )));
108 static assert( is(typeof( nonpurefunc = purefunc )));
109
110 // Cannot convert parameter storage classes (except const to in and in to const)
111
112 void function(const(int)) constfunc;
113 void function(in int) infunc;
114 void function(out int) outfunc;
115 void function(ref int) reffunc;
116 void function(lazy int) lazyfunc;
117
118 static assert(is(typeof( infunc = constfunc )));
119 static assert(is(typeof( constfunc = infunc )));
120
121 static assert(!is(typeof( infunc = outfunc )));
122 static assert(!is(typeof( infunc = reffunc )));
123 static assert(!is(typeof( infunc = lazyfunc )));
124
125 static assert(!is(typeof( outfunc = infunc )));
126 static assert(!is(typeof( outfunc = reffunc )));
127 static assert(!is(typeof( outfunc = lazyfunc )));
128
129 static assert(!is(typeof( reffunc = infunc )));
130 static assert(!is(typeof( reffunc = outfunc )));
131 static assert(!is(typeof( reffunc = lazyfunc )));
132
133 static assert(!is(typeof( lazyfunc = infunc )));
134 static assert(!is(typeof( lazyfunc = outfunc )));
135 static assert(!is(typeof( lazyfunc = reffunc )));
136
137 // Test class covariance
138
139 A function() afunc;
140 B function() bfunc;
141
142 static assert( is(typeof( afunc = bfunc )));
143 static assert(!is(typeof( bfunc = afunc )));
144
145 // Test all the conversions at once
146 typeof(&restrictedfunc) prestrictedfunc;
147 typeof(&relaxedfunc) prelaxedfunc = prestrictedfunc;
148 }
149
150 void bug3797dg()
151 {
152 ref int frvv() { return *(new int); }
153 B restrictedfunc(in const(int)) @safe pure nothrow { return null; }
154 A relaxedfunc(in int) { return null; }
155 // Cannot convert if the return type or parameters are different
156
157 void delegate() vv;
158 void delegate(int) vi;
159 int delegate() iv;
160 const(int) delegate() cv;
161 immutable(int) delegate() xv;
162
163 static assert( is(typeof( vv = vv )));
164 static assert(!is(typeof( vv = vi )));
165 static assert(!is(typeof( vv = iv )));
166 static assert(!is(typeof( vv = cv )));
167 static assert(!is(typeof( vv = xv )));
168
169 static assert(!is(typeof( vi = vv )));
170 static assert( is(typeof( vi = vi )));
171 static assert(!is(typeof( vi = iv )));
172 static assert(!is(typeof( vi = cv )));
173 static assert(!is(typeof( vi = cx )));
174
175 static assert(!is(typeof( iv = vv )));
176 static assert(!is(typeof( iv = vi )));
177 static assert( is(typeof( iv = iv )));
178 static assert( is(typeof( iv = cv )));
179 static assert( is(typeof( iv = xv )));
180
181 static assert(!is(typeof( cv = vv )));
182 static assert( is(typeof( cv = iv )));
183 static assert(!is(typeof( cv = vi )));
184 static assert( is(typeof( cv = cv )));
185 static assert( is(typeof( cv = xv )));
186
187 static assert(!is(typeof( xv = vv )));
188 static assert( is(typeof( xv = iv )));
189 static assert(!is(typeof( xv = vi )));
190 static assert( is(typeof( xv = cv )));
191 static assert( is(typeof( xv = xv )));
192
193 int* delegate() ipfunc;
194 const(int*) delegate() cipfunc;
195
196 static assert( is(typeof( cipfunc = ipfunc )) );
197 static assert(!is(typeof( ipfunc = cipfunc )) );
198
199 // delegates with different linkages can't convert
200
201 extern(C) void delegate() cfunc;
202 extern(D) void delegate() dfunc;
203
204 static assert(!is(typeof( cfunc = dfunc )));
205 static assert(!is(typeof( dfunc = cfunc )));
206
207 // ref return can't convert to non-ref return
208
209 typeof(&frvv) rvv;
210
211 static assert(!is(typeof( rvv = iv )));
212 static assert(!is(typeof( rvv = cv )));
213
214 static assert(!is(typeof( iv = rvv )));
215 static assert(!is(typeof( cv = rvv )));
216
217 // variadic delegates don't mix
218
219 void delegate(...) vf;
220
221 static assert(!is(typeof( vf = vv )));
222 static assert(!is(typeof( vv = vf )));
223
224 // non-nothrow -> nothrow
225
226 void delegate() nothrow ntf;
227
228 static assert(!is(typeof( ntf = vv )));
229 static assert( is(typeof( vv = ntf )));
230
231 // @safe <-> @trusted -> @system
232
233 void delegate() @system systemfunc;
234 void delegate() @trusted trustedfunc;
235 void delegate() @safe safefunc;
236
237 static assert( is(typeof( trustedfunc = safefunc )));
238 static assert( is(typeof( systemfunc = trustedfunc )));
239 static assert( is(typeof( systemfunc = safefunc )));
240 static assert( is(typeof( safefunc = trustedfunc )));
241
242 static assert(!is(typeof( trustedfunc = systemfunc )));
243 static assert(!is(typeof( safefunc = systemfunc )));
244
245 // pure -> non-pure
246
247 void delegate() nonpurefunc;
248 void delegate() pure purefunc;
249
250 static assert(!is(typeof( purefunc = nonpurefunc )));
251 static assert( is(typeof( nonpurefunc = purefunc )));
252
253 // Cannot convert parameter storage classes (except const to in and in to const)
254
255 void delegate(const(int)) constfunc;
256 void delegate(in int) infunc;
257 void delegate(out int) outfunc;
258 void delegate(ref int) reffunc;
259 void delegate(lazy int) lazyfunc;
260
261 static assert(is(typeof( infunc = constfunc )));
262 static assert(is(typeof( constfunc = infunc )));
263
264 static assert(!is(typeof( infunc = outfunc )));
265 static assert(!is(typeof( infunc = reffunc )));
266 static assert(!is(typeof( infunc = lazyfunc )));
267
268 static assert(!is(typeof( outfunc = infunc )));
269 static assert(!is(typeof( outfunc = reffunc )));
270 static assert(!is(typeof( outfunc = lazyfunc )));
271
272 static assert(!is(typeof( reffunc = infunc )));
273 static assert(!is(typeof( reffunc = outfunc )));
274 static assert(!is(typeof( reffunc = lazyfunc )));
275
276 static assert(!is(typeof( lazyfunc = infunc )));
277 static assert(!is(typeof( lazyfunc = outfunc )));
278 static assert(!is(typeof( lazyfunc = reffunc )));
279
280 // Test class covariance
281
282 A delegate() afunc;
283 B delegate() bfunc;
284
285 static assert( is(typeof( afunc = bfunc )));
286 static assert(!is(typeof( bfunc = afunc )));
287
288 // Test all the conversions at once
289 typeof(&restrictedfunc) prestrictedfunc;
290 typeof(&relaxedfunc) prelaxedfunc = prestrictedfunc;
291 }
292
293 void bug3268()
294 {
295 auto a = &bug3268;
296 const b = a;
297 assert(a == a);
298 assert(a == b);
299 assert(b == b);
300 immutable c = cast(immutable)a;
301 assert(a == c);
302 assert(b == c);
303 assert(c == c);
304
305 static assert(is(typeof(*a) == typeof(*b)));
306 static assert(is(typeof(*a) == typeof(*c)));
307 }
308
309 void bug3268dg()
310 {
311 void bug3268x() {}
312 auto a = &bug3268x;
313 const b = a;
314 assert(a == a);
315 assert(a == b);
316 assert(b == b);
317 immutable c = cast(immutable)a;
318 assert(a == c);
319 assert(b == c);
320 assert(c == c);
321 }
322
323 void bug3833()
324 {
325 bool b;
326
327 void function() func;
328 void function() pure purefunc;
329 void function() nothrow nothrowfunc;
330 void function() @safe safefunc;
331 void function() @trusted trustedfunc;
332
333 static assert( is(typeof( b ? func : purefunc ) == typeof( func )));
334 static assert( is(typeof( b ? func : nothrowfunc ) == typeof( func )));
335 static assert( is(typeof( b ? func : safefunc ) == typeof( func )));
336 static assert( is(typeof( b ? func : trustedfunc ) == typeof( func )));
337
338 static assert( is(typeof( b ? purefunc : nothrowfunc ) == typeof( func )));
339 static assert( is(typeof( b ? purefunc : safefunc ) == typeof( func )));
340 static assert( is(typeof( b ? purefunc : trustedfunc ) == typeof( func )));
341
342 static assert( is(typeof( b ? nothrowfunc : safefunc ) == typeof( func )));
343 static assert( is(typeof( b ? nothrowfunc : trustedfunc ) == typeof( func )));
344
345 static assert( is(typeof( b ? safefunc : trustedfunc ) == typeof( trustedfunc )));
346
347 auto arr = [func, purefunc, nothrowfunc, safefunc, trustedfunc];
348
349 static assert( is(typeof( arr ) == typeof(func)[]) );
350 }
351
352 void bug3833dg()
353 {
354 bool b;
355
356 void delegate() func;
357 void delegate() pure purefunc;
358 void delegate() nothrow nothrowfunc;
359 void delegate() @safe safefunc;
360 void delegate() @trusted trustedfunc;
361
362 static assert( is(typeof( b ? func : purefunc ) == typeof( func )));
363 static assert( is(typeof( b ? func : nothrowfunc ) == typeof( func )));
364 static assert( is(typeof( b ? func : safefunc ) == typeof( func )));
365 static assert( is(typeof( b ? func : trustedfunc ) == typeof( func )));
366
367 static assert( is(typeof( b ? purefunc : nothrowfunc ) == typeof( func )));
368 static assert( is(typeof( b ? purefunc : safefunc ) == typeof( func )));
369 static assert( is(typeof( b ? purefunc : trustedfunc ) == typeof( func )));
370
371 static assert( is(typeof( b ? nothrowfunc : safefunc ) == typeof( func )));
372 static assert( is(typeof( b ? nothrowfunc : trustedfunc ) == typeof( func )));
373
374 static assert( is(typeof( b ? safefunc : trustedfunc ) == typeof( trustedfunc )));
375
376 auto arr = [func, purefunc, nothrowfunc, safefunc, trustedfunc];
377
378 static assert( is(typeof( arr ) == typeof(func)[]) );
379 }
380
381 void bug4838()
382 {
383 void delegate() const dgc;
384 static assert(typeof(dgc).stringof == "void delegate() const");
385
386 void delegate() immutable dgi;
387 static assert(typeof(dgi).stringof == "void delegate() immutable");
388
389 void delegate() shared dgs;
390 static assert(typeof(dgs).stringof == "void delegate() shared");
391
392 void delegate() shared const dgsc;
393 static assert(typeof(dgsc).stringof == "void delegate() shared const");
394
395 void delegate() inout dgw;
396 static assert(typeof(dgw).stringof == "void delegate() inout");
397
398 void delegate() shared inout dgsw;
399 static assert(typeof(dgsw).stringof == "void delegate() shared inout");
400 }
401
402 void test8822()
403 {
404 struct S { void foo() const {} }
405 S s;
406 void delegate() const dg = &s.foo; // OK
407
408 void foo(void delegate() const dg){} // OK
409
410 struct Foo(T) {}
411 alias Foo!(void delegate() const) X; // NG -> OK
412 }
413
414 void main()
415 {
416 static assert(is(typeof(&main) P : U*, U));
417 auto x = cast(void*)&main;
418
419 const void * p = &main;
420
421 __gshared void function() gp = null;
422 __gshared void delegate() gp2 = null;
423 }