Debian Codegraph @

gcc/testsuite/gdc.test/fail_compilation/retscope.d

raw
1 /*
2 REQUIRED_ARGS: -preview=dip1000
3 TEST_OUTPUT:
4 ---
5 fail_compilation/retscope.d(22): Error: scope parameter `p` may not be returned
6 fail_compilation/retscope.d(32): Error: escaping a reference to local variable `j` by returning `b ? nested1(& i) : nested2(& j)` is not allowed in a `@safe` function
7 fail_compilation/retscope.d(45): Error: assigning scope variable `p` to global variable `q` is not allowed in a `@safe` function
8 fail_compilation/retscope.d(47): Error: assigning address of variable `i` to `q` with longer lifetime is not allowed in a `@safe` function
9 fail_compilation/retscope.d(48): Error: assigning scope variable `a` to global variable `b` is not allowed in a `@safe` function
10 fail_compilation/retscope.d(49): Error: assigning address of expression temporary returned by `(*fp2)()` to `q` with longer lifetime is not allowed in a `@safe` function
11 ---
12 */
13
14
15
16
17 int* foo1(return scope int* p) @safe { return p; } // ok
18
19 int* foo2()(scope int* p) @safe { return p; } // ok, 'return' is inferred
20 alias foo2a = foo2!();
21
22 int* foo3(scope int* p) @safe { return p; } // error
23
24 int* foo4(bool b) @safe
25 {
26 int i;
27 int j;
28
29 int* nested1(scope int* p) { return null; }
30 int* nested2(return scope int* p) { return p; }
31
32 return b ? nested1(&i) : nested2(&j);
33 }
34
35 /************************************************/
36
37 struct S2 { int a,b,c,d; }
38
39 @safe S2 function() fp2;
40
41 void test2(scope int* p, int[] a ...) @safe
42 {
43 static int* q;
44 static int[] b;
45 q = p;
46 int i;
47 q = &i;
48 b = a;
49 q = &fp2().d;
50 }
51
52 /**************************************************/
53
54 /*
55 TEST_OUTPUT:
56 ---
57 fail_compilation/retscope.d(75): Error: function `retscope.HTTP.Impl.onReceive` is `@nogc` yet allocates closure for `onReceive()` with the GC
58 fail_compilation/retscope.d(77): delegate `() => this.x` closes over variable `this`
59 ---
60 */
61
62
63 struct Curl
64 {
65 int delegate() dg;
66 }
67
68 struct HTTP
69 {
70 struct Impl
71 {
72 Curl curl;
73 int x;
74
75 @nogc void onReceive()
76 {
77 auto dg = ( ) { return x; };
78 curl.dg = dg;
79 }
80 }
81 }
82
83 /***********************************************/
84
85 /*
86 TEST_OUTPUT:
87 ---
88 ---
89 */
90 // https://issues.dlang.org/show_bug.cgi?id=8838
91
92 int[] foo8() @safe
93 {
94 int[5] sa;
95 return bar8(sa);
96 }
97
98 int[] bar8(int[] a) @safe
99 {
100 return a;
101 }
102
103
104 /*************************************************/
105
106 /*
107 TEST_OUTPUT:
108 ---
109 fail_compilation/retscope.d(95): Error: assigning reference to local variable `sa` to non-scope parameter `a` calling `bar8` is not allowed in a `@safe` function
110 fail_compilation/retscope.d(123): Error: escaping a reference to local variable `tmp` by returning `foo9(cast(char[])tmp)` is not allowed in a `@safe` function
111 ---
112 */
113
114 char[] foo9(return char[] a) @safe pure nothrow @nogc
115 {
116 return a;
117 }
118
119 char[] bar9() @safe
120 {
121 char[20] tmp;
122 foo9(tmp); // ok
123 return foo9(tmp); // error
124 }
125
126 /*************************************************/
127
128 /*
129 //
130 //
131 //fail_compilation/retscope.d(143): To enforce `@safe`, the compiler allocates a closure unless `opApply()` uses `scope`
132 //
133 */
134
135 struct S10
136 {
137 static int opApply(int delegate(S10*) dg);
138 }
139
140 S10* test10()
141 {
142 foreach (S10* m; S10)
143 return m;
144 return null;
145 }
146
147 /************************************************/
148
149 /*
150 TEST_OUTPUT:
151 ---
152 fail_compilation/retscope.d(158): Error: scope parameter `this` may not be returned
153 ---
154 */
155
156 class C11
157 {
158 @safe C11 foo() scope { return this; }
159 }
160
161
162 /****************************************************/
163
164 /*
165 TEST_OUTPUT:
166 ---
167 fail_compilation/retscope.d(177): Error: assigning address of variable `i` to `p` with longer lifetime is not allowed in a `@safe` function
168 ---
169 */
170
171
172
173 void foo11() @safe
174 {
175 int[] p;
176 int[3] i;
177 p = i[];
178 }
179
180 /************************************************/
181 /*
182 TEST_OUTPUT:
183 ---
184 fail_compilation/retscope.d(197): Error: returning scope variable `e` is not allowed in a `@safe` function
185 ---
186 */
187
188 struct Escaper
189 {
190 void* DG;
191 }
192
193 void* escapeDg1(scope void* d) @safe
194 {
195 Escaper e;
196 e.DG = d;
197 return e.DG;
198 }
199
200 /*************************************************/
201 /*
202 TEST_OUTPUT:
203 ---
204 fail_compilation/retscope.d(212): Error: assigning scope variable `p` to non-scope `e.e` is not allowed in a `@safe` function
205 ---
206 */
207 struct Escaper3 { void* e; }
208
209 void* escape3 (scope void* p) @safe {
210 Escaper3 e;
211 scope dg = () { return e.e; };
212 e.e = p;
213 return dg();
214 }
215
216 /**************************************************/
217
218 /*
219 TEST_OUTPUT:
220 ---
221 fail_compilation/retscope.d(229): Error: scope parameter `ptr` may not be returned
222 ---
223 */
224
225 alias dg_t = void* delegate () return scope @safe;
226
227 void* funretscope(scope dg_t ptr) @safe
228 {
229 return ptr();
230 }
231
232 /*****************************************************/
233
234 /*
235 TEST_OUTPUT:
236 ---
237 fail_compilation/retscope.d(248): Error: cannot implicitly convert expression `() => & x` of type `void* delegate() pure nothrow @nogc @safe` to `void* delegate() scope @safe`
238 fail_compilation/retscope.d(248): Error: cannot implicitly convert expression `() => & x` of type `void* delegate() pure nothrow @nogc @safe` to `void* delegate() scope @safe`
239 fail_compilation/retscope.d(249): Error: cannot implicitly convert expression `() => & x` of type `void* delegate() pure nothrow @nogc @safe` to `void* delegate() scope @safe`
240 fail_compilation/retscope.d(249): Error: cannot implicitly convert expression `() => & x` of type `void* delegate() pure nothrow @nogc @safe` to `void* delegate() scope @safe`
241 ---
242 */
243
244 void escape4() @safe
245 {
246 alias FunDG = void* delegate () scope @safe;
247 int x = 42;
248 scope FunDG f = () return { return &x; };
249 scope FunDG g = () { return &x; };
250 }
251
252 /**************************************************/
253
254 /*
255 TEST_OUTPUT:
256 ---
257 fail_compilation/retscope.d(266): Error: taking address of `scope` variable `p` with pointers is not allowed in a `@safe` function
258 ---
259 */
260
261 void escape5() @safe
262 {
263 int* q;
264 scope int* p;
265 scope int** pp = &q; // ok
266 pp = &p; // error
267 }
268
269 /***********************************************/
270
271 /*
272 TEST_OUTPUT:
273 ---
274 fail_compilation/retscope.d(286): Error: escaping a reference to local variable `b` by returning `foo6(& b)` is not allowed in a `@safe` function
275 ---
276 */
277
278 @safe int* foo6()(int* arg)
279 {
280 return arg;
281 }
282
283 int* escape6() @safe
284 {
285 int b;
286 return foo6(&b);
287 }
288
289 /***************************************************/
290
291 struct S7
292 {
293 int[10] a;
294 int[3] abc(int i) @safe
295 {
296 return a[0 .. 3]; // should not error
297 }
298 }
299
300 /***************************************************/
301
302 /*
303 TEST_OUTPUT:
304 ---
305 fail_compilation/retscope.d(315): Error: scope parameter `p` may not be returned
306 fail_compilation/retscope.d(316): Error: returning `p[]` escapes a reference to parameter `p`
307 fail_compilation/retscope.d(319): Error: scope parameter `p` may not be returned
308 ---
309 */
310
311 int[3] escape8(scope int[] p) @safe { return p[0 .. 3]; } // should not error
312 char*[3] escape9(scope char*[] p) @safe { return p[0 .. 3]; }
313
314 // https://issues.dlang.org/show_bug.cgi?id=24663
315 int*[3] escape8b(scope int*[3] p) @safe { return p[]; }
316 ref int*[3] escape9b( int*[3] p) @safe { return p[]; }
317
318 ref int[3] asStatic(return scope int[] p) @safe { return p[0 .. 3]; }
319 ref int[3] asStatic2( scope int[] p) @safe { return p[0 .. 3]; }
320
321 /***************************************************/
322
323 /*
324 TEST_OUTPUT:
325 ---
326 fail_compilation/retscope.d(335): Error: assigning reference to local variable `i` to non-scope `f` is not allowed in a `@safe` function
327 ---
328 */
329
330 int* escape10() @safe
331 {
332 int i;
333 int* f;
334 scope int** x = &f;
335 f = &i;
336
337 return bar10(x);
338 }
339
340 int* bar10( scope int** ptr ) @safe
341 {
342 return *ptr;
343 }
344
345 /******************************************/
346
347 /*
348 TEST_OUTPUT:
349 ---
350 fail_compilation/retscope.d(358): Error: taking address of `scope` variable `aa` with pointers is not allowed in a `@safe` function
351 ---
352 */
353
354 int* escape11() @safe
355 {
356 int i;
357 int*[3] aa = [ &i, null, null ];
358 return bar11(&aa[0]);
359 }
360
361 int* bar11(scope int** x) @safe
362 {
363 return foo11(*x);
364 }
365
366 int* foo11(int* x) @safe { return x; }
367
368 /******************************************/
369
370 void escape15() @safe
371 {
372 int arg;
373 const(void)*[1] argsAddresses;
374 argsAddresses[0] = // MUST be an array assignment
375 (ref arg)@trusted{ return cast(const void*) &arg; }(arg);
376 }
377
378 /******************************************/
379 /*
380 TEST_OUTPUT:
381 ---
382 fail_compilation/retscope.d(1003): Error: returning `f.foo()` escapes a reference to local variable `f`
383 ---
384 */
385
386 #line 1000
387 int* escape12() @safe
388 {
389 Foo12 f;
390 return f.foo;
391 }
392
393 struct Foo12
394 {
395 int* foo() return @safe;
396 }
397
398 /******************************************/
399 /*
400 TEST_OUTPUT:
401 ---
402 fail_compilation/retscope.d(1103): Error: returning scope variable `f` is not allowed in a `@safe` function
403 ---
404 */
405
406 #line 1100
407 int* escape13() @safe
408 {
409 scope Foo13 f;
410 return f.foo;
411 }
412
413 class Foo13
414 {
415 int* foo() return @safe;
416 }
417
418 /******************************************/
419 /*
420 TEST_OUTPUT:
421 ---
422 fail_compilation/retscope.d(1205): Error: scope variable `f14` calling non-scope member function `Foo14.foo()` is not allowed in a `@safe` function
423 ---
424 */
425
426 #line 1200
427 int* escape14() @safe
428 {
429 int i;
430 Foo14 f14;
431 f14.v = &i;
432 return f14.foo;
433 }
434
435 struct Foo14
436 {
437 int* v;
438 int* foo () @safe { return this.v; }
439 }
440
441 /******************************************/
442 /*
443 TEST_OUTPUT:
444 ---
445 fail_compilation/retscope.d(1311): Error: assigning scope variable `u2` to `ek` with longer lifetime is not allowed in a `@safe` function
446 ---
447 */
448
449 #line 1300
450 @safe struct U13 {
451 int* k;
452 int* get() return scope { return k; }
453 static int* sget(return scope ref U13 u) { return u.k; }
454 }
455
456 @safe void foo13() {
457 int* ek;
458
459 int i;
460 auto u2 = U13(&i);
461 ek = U13.sget(u2); // Error: scope variable u2 assigned to ek with longer lifetime
462
463 auto u1 = U13(new int);
464 ek = u1.get(); // ok
465 ek = U13.sget(u1); // ok
466 }
467
468 /************************************************/
469
470 /*
471 TEST_OUTPUT:
472 ---
473 fail_compilation/retscope.d(1405): Error: assigning reference to local variable `buf` to non-scope anonymous parameter calling `myprintf` is not allowed in a `@safe` function
474 ---
475 */
476
477 #line 1400
478 @trusted extern(C) int myprintf(const(char)*, ...);
479
480 @safe void foo14()
481 {
482 char[4] buf = [ 'h', 'i', '\n', 0 ];
483 myprintf(&buf[0]);
484 }
485
486 /************************************************/
487
488 /*
489 TEST_OUTPUT:
490 ---
491 fail_compilation/retscope.d(1509): Error: assigning reference to stack allocated value returned by `(*fp15)()` to non-scope anonymous parameter is not allowed in a `@safe` function
492 ---
493 */
494
495 #line 1500
496
497 @safe void bar15(int*);
498
499 struct S15 { int a,b,c,d; }
500
501 @safe S15 function() fp15;
502
503 void test15() @safe
504 {
505 bar15(&fp15().d);
506 }
507
508
509 /*************************************************/
510
511 void foo16() @nogc nothrow
512 {
513 alias dg_t = string delegate(string) @nogc nothrow;
514
515 dg_t dg = (string s) => s;
516 }
517
518 /*************************************************/
519
520 /*
521 TEST_OUTPUT:
522 ---
523 fail_compilation/retscope.d(1701): Error: cannot implicitly convert expression `& func` of type `int* function(int* p)` to `int* function(scope int* p)`
524 fail_compilation/retscope.d(1702): Error: cannot implicitly convert expression `& func` of type `int* function(int* p)` to `int* function(return int* p)`
525 fail_compilation/retscope.d(1703): Error: cannot implicitly convert expression `& func` of type `int* function(int* p)` to `int* function(return scope int* p)`
526 fail_compilation/retscope.d(1711): Error: cannot implicitly convert expression `& funcr` of type `int* function(return int* p)` to `int* function(scope int* p)`
527 fail_compilation/retscope.d(1716): Error: cannot implicitly convert expression `& funcrs` of type `int* function(return scope int* p)` to `int* function(scope int* p)`
528 ---
529 */
530
531 int* func(int* p);
532 int* funcs(scope int* p);
533 int* funcr(return int* p);
534 int* funcrs(return scope int* p);
535
536 void foo17()
537 {
538 #line 1700
539 typeof(func) *fp1 = &func;
540 typeof(funcs) *fp2 = &func; // error
541 typeof(funcr) *fp3 = &func; // error
542 typeof(funcrs) *fp4 = &func; // error
543
544 typeof(func) *fq1 = &funcs;
545 typeof(funcs) *fq2 = &funcs;
546 typeof(funcr) *fq3 = &funcs;
547 typeof(funcrs) *fq4 = &funcs;
548
549 typeof(func) *fr1 = &funcr;
550 typeof(funcs) *fr2 = &funcr; // error
551 typeof(funcr) *fr3 = &funcr;
552 typeof(funcrs) *fr4 = &funcr;
553
554 typeof(func) *fs1 = &funcrs;
555 typeof(funcs) *fs2 = &funcrs; // error
556 typeof(funcr) *fs3 = &funcrs;
557 typeof(funcrs) *fs4 = &funcrs;
558 }
559
560 /*************************************************/
561
562 /*
563 TEST_OUTPUT:
564 ---
565 fail_compilation/retscope.d(1801): Error: cannot implicitly convert expression `&c.func` of type `int* delegate()` to `int* delegate() scope`
566 fail_compilation/retscope.d(1802): Error: cannot implicitly convert expression `&c.func` of type `int* delegate()` to `int* delegate() return scope`
567 fail_compilation/retscope.d(1803): Error: cannot implicitly convert expression `&c.func` of type `int* delegate()` to `int* delegate() return scope`
568 fail_compilation/retscope.d(1811): Error: cannot implicitly convert expression `&c.funcr` of type `int* delegate() return scope` to `int* delegate() scope`
569 fail_compilation/retscope.d(1816): Error: cannot implicitly convert expression `&c.funcrs` of type `int* delegate() return scope` to `int* delegate() scope`
570 ---
571 */
572
573 class C18
574 {
575 int* func();
576 int* funcs() scope;
577 int* funcr() return;
578 int* funcrs() return scope;
579 }
580
581 void foo18()
582 {
583 C18 c;
584
585 #line 1800
586 typeof(&c.func) fp1 = &c.func;
587 typeof(&c.funcs) fp2 = &c.func; // error
588 typeof(&c.funcr) fp3 = &c.func; // error
589 typeof(&c.funcrs) fp4 = &c.func; // error
590
591 typeof(&c.func) fq1 = &c.funcs;
592 typeof(&c.funcs) fq2 = &c.funcs;
593 typeof(&c.funcr) fq3 = &c.funcs;
594 typeof(&c.funcrs) fq4 = &c.funcs;
595
596 typeof(&c.func) fr1 = &c.funcr;
597 typeof(&c.funcs) fr2 = &c.funcr; // error
598 typeof(&c.funcr) fr3 = &c.funcr;
599 typeof(&c.funcrs) fr4 = &c.funcr;
600
601 typeof(&c.func) fs1 = &c.funcrs;
602 typeof(&c.funcs) fs2 = &c.funcrs; // error
603 typeof(&c.funcr) fs3 = &c.funcrs;
604 typeof(&c.funcrs) fs4 = &c.funcrs;
605 }
606
607 /*********************************************/
608
609 @safe void foo19(C)(ref C[] str) // infer 'scope' for 'str'
610 {
611 str = str;
612 str = str[1 .. str.length];
613 }
614
615 @safe void test19()
616 {
617 char[10] s;
618 char[] t = s[];
619 foo19(t);
620 }
621
622 /********************************************/
623
624
625 bool foo20(const string a) @safe pure nothrow @nogc
626 {
627 return !a.length;
628 }
629
630 struct Result(R)
631 {
632 R source;
633
634 bool empty() // infer 'scope' for 'this'
635 { return foo20(source); }
636 }
637
638 @safe void test20()
639 {
640 scope n = Result!string("abc");
641 n.empty();
642 }
643
644 /************************************************/
645
646 // https://issues.dlang.org/show_bug.cgi?id=17117
647
648 ref int foo21(return ref int s)
649 {
650 return s;
651 }
652
653 int fail21()
654 {
655 int s;
656 return foo21(s); // Error: escaping reference to local variable s
657 }
658
659 int test21()
660 {
661 int s;
662 s = foo21(s);
663 return s;
664 }
665
666 /**********************************************/
667
668 @safe void foo22()(ref char[] s)
669 {
670 char[] a = s;
671 }
672
673 @safe void test22(scope char[] s)
674 {
675 foo22(s);
676 }
677
678 /*********************************************
679 TEST_OUTPUT:
680 ---
681 fail_compilation/retscope.d(1907): Error: assigning scope variable `x` to `ref` variable `this` with longer lifetime is not allowed in a `@safe` function
682 fail_compilation/retscope.d(1913): Error: returning scope variable `x` is not allowed in a `@safe` function
683 ---
684 */
685 #line 1900
686 struct Constant
687 {
688 int* member;
689
690 int* foo(scope Repeat!(int*) grid) @safe
691 {
692 foreach(ref x; grid)
693 member = x;
694
695 foreach(ref x; grid)
696 x = member;
697
698 foreach(ref x; grid)
699 return x;
700
701 return null;
702 }
703
704 alias Repeat(T...) = T;
705 }