1 /*
2 REQUIRED_ARGS: -dip1000
3 PERMUTE_ARGS:
4 TEST_OUTPUT:
5 ---
6 fail_compilation/retscope2.d(102): Error: scope variable `s` assigned to `p` with longer lifetime
7 fail_compilation/retscope2.d(107): Error: address of variable `s` assigned to `p` with longer lifetime
8 ---
9 */
10 
11 #line 100
foo1(ref char[]p,scope char[]s)12 @safe foo1(ref char[] p, scope char[] s)
13 {
14     p = s;
15 }
16 
bar1(ref char * p,char s)17 @safe bar1(ref char* p, char s)
18 {
19     p = &s;
20 }
21 
22 /**********************************************/
23 
24 // https://issues.dlang.org/show_bug.cgi?id=17123
25 
test200()26 void test200()
27 {
28     char[256] buffer;
29 
30     char[] delegate() read = () {
31         return buffer[];
32     };
33 }
34 
35 /**********************************************/
36 
37 /*
38 TEST_OUTPUT:
39 ---
40 fail_compilation/retscope2.d(302): Error: scope variable `a` assigned to return scope `b`
41 ---
42 */
43 
44 #line 300
test300(scope int * a,return scope int * b)45 @safe int* test300(scope int* a, return scope int* b)
46 {
47     b = a;
48     return b;
49 }
50 
51 /**********************************************/
52 
53 /*
54 TEST_OUTPUT:
55 ---
56 fail_compilation/retscope2.d(403): Error: scope variable `a` assigned to return scope `c`
57 ---
58 */
59 
60 #line 400
test400(scope int * a,return scope int * b)61 @safe int* test400(scope int* a, return scope int* b)
62 {
63     auto c = b; // infers 'return scope' for 'c'
64     c = a;
65     return c;
66 }
67 
68 /**********************************************/
69 
70 /*
71 TEST_OUTPUT:
72 ---
73 fail_compilation/retscope2.d(504): Error: scope variable `c` may not be returned
74 ---
75 */
76 
77 #line 500
test500(scope int * a,return scope int * b)78 @safe int* test500(scope int* a, return scope int* b)
79 {
80     scope c = b; // does not infer 'return' for 'c'
81     c = a;
82     return c;
83 }
84 
85 /**********************************************/
86 
87 /*
88 TEST_OUTPUT:
89 ---
90 fail_compilation/retscope2.d(604): Error: scope variable `_param_0` assigned to non-scope parameter `unnamed` calling retscope2.foo600
91 fail_compilation/retscope2.d(604): Error: scope variable `_param_1` assigned to non-scope parameter `unnamed` calling retscope2.foo600
92 fail_compilation/retscope2.d(614): Error: template instance retscope2.test600!(int*, int*) error instantiating
93 ---
94 */
95 
96 #line 600
test600(A...)97 @safe test600(A...)(scope A args)
98 {
99     foreach (i, Arg; A)
100     {
101         foo600(args[i]);
102     }
103 }
104 
105 @safe void foo600(int*);
106 
bar600()107 @safe bar600()
108 {
109     scope int* p;
110     scope int* q;
111     test600(p, q);
112 }
113 
114 /*************************************************/
115 
116 /*
117 TEST_OUTPUT:
118 ---
119 fail_compilation/retscope2.d(719): Error: returning `get2(s)` escapes a reference to local variable `s`
120 fail_compilation/retscope2.d(721): Error: returning `s.get1()` escapes a reference to local variable `s`
121 ---
122 */
123 
124 #line 700
125 // https://issues.dlang.org/show_bug.cgi?id=17049
126 
get2(return ref scope S700 _this)127 @safe S700* get2(return ref scope S700 _this)
128 {
129     return &_this;
130 }
131 
132 struct S700
133 {
134     @safe S700* get1() return scope
135     {
136         return &this;
137     }
138 }
139 
140 S700* escape700(int i) @safe
141 {
142     S700 s;
143     if (i)
144         return s.get2(); // 719
145     else
146         return s.get1(); // 721
147 }
148 
149 /*************************************************/
150 
151 /*
152 TEST_OUTPUT:
153 ---
154 fail_compilation/retscope2.d(804): Error: scope variable `e` may not be thrown
155 ---
156 */
157 
158 #line 800
159 
160 void foo800()
161 {
162     scope Exception e;
163     throw e;
164 }
165 
166 /*************************************************/
167 /+
168 /*
169 XEST_OUTPUT:
170 
171 fail_compilation/retscope2.d(907): Error: address of variable `this` assigned to `p17568` with longer lifetime
172 
173 */
174 
175 #line 900
176 
177 int* p17568;
178 struct T17568
179 {
180     int a;
181     void escape() @safe scope
182     {
183         p17568 = &a;
184     }
185 }
186 +/
187 /*************************************************/
188 
189 /*
190 TEST_OUTPUT:
191 ---
192 fail_compilation/retscope2.d(1005): Error: scope variable `p` assigned to `this` with longer lifetime
193 fail_compilation/retscope2.d(1024): Error: scope variable `p` assigned to `d` with longer lifetime
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;   // ok
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: scope variable `dg` may not be returned
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