Debian Codegraph @

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

raw
1 /*
2 REQUIRED_ARGS: -preview=dip1000
3 TEST_OUTPUT:
4 ---
5 fail_compilation/retscope2.d(102): Error: assigning scope variable `s` to `ref` variable `p` with longer lifetime is not allowed in a `@safe` function
6 fail_compilation/retscope2.d(107): Error: assigning address of variable `s` to `p` with longer lifetime is not allowed in a `@safe` function
7 ---
8 */
9
10 #line 100
11 @safe foo1(ref char[] p, scope char[] s)
12 {
13 p = s;
14 }
15
16 @safe bar1(ref char* p, char s)
17 {
18 p = &s;
19 }
20
21 /**********************************************/
22
23 // https://issues.dlang.org/show_bug.cgi?id=17123
24
25 void test200()
26 {
27 char[256] buffer;
28
29 char[] delegate() read = () {
30 return buffer[];
31 };
32 }
33
34 /**********************************************/
35
36 /*
37 TEST_OUTPUT:
38 ---
39 fail_compilation/retscope2.d(302): Error: assigning scope variable `a` to return scope `b` is not allowed in a `@safe` function
40 ---
41 */
42
43 #line 300
44 @safe int* test300(scope int* a, return scope int* b)
45 {
46 b = a;
47 return b;
48 }
49
50 /**********************************************/
51
52 /*
53 TEST_OUTPUT:
54 ---
55 fail_compilation/retscope2.d(403): Error: assigning scope variable `a` to return scope `c` is not allowed in a `@safe` function
56 ---
57 */
58
59 #line 400
60 @safe int* test400(scope int* a, return scope int* b)
61 {
62 auto c = b; // infers 'return scope' for 'c'
63 c = a;
64 return c;
65 }
66
67 /**********************************************/
68
69 /*
70 TEST_OUTPUT:
71 ---
72 fail_compilation/retscope2.d(504): Error: returning scope variable `c` is not allowed in a `@safe` function
73 ---
74 */
75
76 #line 500
77 @safe int* test500(scope int* a, return scope int* b)
78 {
79 scope c = b; // does not infer 'return' for 'c'
80 c = a;
81 return c;
82 }
83
84 /**********************************************/
85
86 /*
87 TEST_OUTPUT:
88 ---
89 fail_compilation/retscope2.d(604): Error: assigning scope variable `__param_0` to non-scope anonymous parameter calling `foo600` is not allowed in a `@safe` function
90 fail_compilation/retscope2.d(604): Error: assigning scope variable `__param_1` to non-scope anonymous parameter calling `foo600` is not allowed in a `@safe` function
91 fail_compilation/retscope2.d(614): Error: template instance `retscope2.test600!(int*, int*)` error instantiating
92 ---
93 */
94
95 #line 600
96 @safe test600(A...)(scope A args)
97 {
98 foreach (i, Arg; A)
99 {
100 foo600(args[i]);
101 }
102 }
103
104 @safe void foo600(int*);
105
106 @safe bar600()
107 {
108 scope int* p;
109 scope int* q;
110 test600(p, q);
111 }
112
113 /*************************************************/
114
115 /*
116 TEST_OUTPUT:
117 ---
118 fail_compilation/retscope2.d(719): Error: returning `get2(s)` escapes a reference to local variable `s`
119 fail_compilation/retscope2.d(721): Error: returning `s.get1()` escapes a reference to local variable `s`
120 ---
121 */
122
123 #line 700
124 // https://issues.dlang.org/show_bug.cgi?id=17049
125
126 @safe S700* get2(return ref S700 _this)
127 {
128 return &_this;
129 }
130
131 struct S700
132 {
133 @safe S700* get1() scope return
134 {
135 return &this;
136 }
137 }
138
139 S700* escape700(int i) @safe
140 {
141 S700 s;
142 if (i)
143 return s.get2(); // 719
144 else
145 return s.get1(); // 721
146 }
147
148 /*************************************************/
149
150 /*
151 TEST_OUTPUT:
152 ---
153 fail_compilation/retscope2.d(804): Error: throwing scope variable `e` is not allowed in a `@safe` function
154 ---
155 */
156
157 #line 800
158
159 void foo800() @safe
160 {
161 scope Exception e;
162 throw e;
163 }
164
165 /*************************************************/
166
167 /*
168 TEST_OUTPUT:
169 ---
170 fail_compilation/retscope2.d(907): Error: assigning address of variable `this` to `p17568` with longer lifetime is not allowed in a `@safe` function
171 ---
172 */
173
174 #line 900
175
176 int* p17568;
177 struct T17568
178 {
179 int a;
180 void escape() @safe scope
181 {
182 p17568 = &a;
183 }
184 }
185
186 /*************************************************/
187
188 /*
189 TEST_OUTPUT:
190 ---
191 fail_compilation/retscope2.d(1005): Error: assigning scope variable `p` to non-scope `this._p` is not allowed in a `@safe` function
192 fail_compilation/retscope2.d(1021): Error: assigning scope variable `p` to non-scope `c._p` is not allowed in a `@safe` function
193 fail_compilation/retscope2.d(1024): Error: assigning scope variable `p` to non-scope `d._p` is not allowed in a `@safe` function
194 ---
195 */
196
197 #line 1000
198
199 class C17428
200 {
201 void set(scope int* p) @safe
202 {
203 _p = p;
204 }
205
206 int* _p;
207 }
208
209 class C17428b
210 {
211 int* _p;
212 }
213
214 void test17428() @safe
215 {
216 int x;
217 int* p = &x;
218 scope C17428b c;
219 c._p = p; // bad
220
221 C17428b d;
222 d._p = p; // bad
223 }
224
225
226
227 /*************************************************/
228
229 /*
230 TEST_OUTPUT:
231 ---
232 fail_compilation/retscope2.d(1107): Error: returning scope variable `dg` is not allowed in a `@safe` function
233 ---
234 */
235
236 #line 1100
237
238 struct S17430 { void foo() {} }
239
240 void delegate() test17430() @safe
241 {
242 S17430 s;
243 auto dg = &s.foo; // infer dg as scope
244 return dg;
245 }
246
247 /****************************************************/
248
249 /*
250 TEST_OUTPUT:
251 ---
252 fail_compilation/retscope2.d(1216): Error: returning `s.foo()` escapes a reference to local variable `s`
253 fail_compilation/retscope2.d(1233): Error: returning `t.foo()` escapes a reference to local variable `t`
254 ---
255 */
256
257 #line 1200
258 // https://issues.dlang.org/show_bug.cgi?id=17388
259
260 struct S17388
261 {
262 //int*
263 auto
264 foo() return @safe
265 {
266 return &x;
267 }
268 int x;
269 }
270
271 @safe int* f17388()
272 {
273 S17388 s;
274 return s.foo();
275 }
276
277 struct T17388
278 {
279 //int[]
280 auto
281 foo() return @safe
282 {
283 return x[];
284 }
285 int[4] x;
286 }
287
288 @safe int[] g17388()
289 {
290 T17388 t;
291 return t.foo();
292 }
293
294 /****************************************************/
295
296 /*
297 TEST_OUTPUT:
298 ---
299 fail_compilation/retscope2.d(1306): Error: escaping a reference to local variable `i` by copying `& i` into allocated memory is not allowed in a `@safe` function
300 ---
301 */
302
303 #line 1300
304
305 // https://issues.dlang.org/show_bug.cgi?id=17370
306
307 void test1300() @safe
308 {
309 int i;
310 auto p = new S1300(&i).oops;
311 }
312
313 struct S1300
314 {
315 int* oops;
316 // this(int* p) @safe { oops = p; }
317 }