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