Debian Codegraph @

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

raw
1 /*
2 REQUIRED_ARGS: -preview=dip1000
3 */
4
5 /*
6 TEST_OUTPUT:
7 ---
8 fail_compilation/retscope3.d(2008): Error: escaping a reference to local variable `i` by copying `& i` into allocated memory is not allowed in a `@safe` function
9 fail_compilation/retscope3.d(2017): Error: escaping a reference to local variable `i` by copying `S2000(& i)` into allocated memory is not allowed in a `@safe` function
10 ---
11 */
12
13 #line 2000
14
15 // https://issues.dlang.org/show_bug.cgi?id=17790
16
17 @safe:
18
19 int* bar1()
20 {
21 int i;
22 int*[] arr = [ &i ];
23 return arr[0];
24 }
25
26 struct S2000 { int* p; }
27
28 S2000 bar2()
29 {
30 int i;
31 S2000[] arr = [ S2000(&i) ];
32 return arr[0];
33 }
34
35 void bar3(string[] u...) @safe pure nothrow @nogc
36 {
37 foreach (str; u)
38 {
39 }
40 }
41
42 void bar4()
43 {
44 static struct S { int* p; }
45 S[2][10] pairs;
46 foreach (ref pair; pairs)
47 {
48 }
49 }
50
51 /**********************************************/
52
53 /*
54 TEST_OUTPUT:
55 ---
56 fail_compilation/retscope3.d(4003): Error: escaping a reference to parameter `u` by copying `u[]` into allocated memory is not allowed in a `@safe` function
57 fail_compilation/retscope3.d(4016): Error: storing reference to outer local variable `i` into allocated memory causes it to escape
58 fail_compilation/retscope3.d(4025): Error: escaping reference to stack allocated value returned by `makeSA()` into allocated memory
59 ---
60 */
61
62 #line 4000
63
64 void bar4000(int[1] u...) @safe
65 {
66 int[][] n = [u[]];
67 }
68
69 void bar4001() @safe
70 {
71 static int i;
72 int*[] n = [&i];
73 }
74
75 ref int bar4002(return ref int i) @safe
76 {
77 void nested()
78 {
79 int*[] n = [&i];
80 }
81 return i;
82 }
83
84 int[3] makeSA() @safe;
85
86 void bar4003() @safe
87 {
88 int[][] a = [makeSA()[]];
89 }