Debian Codegraph @

gcc/testsuite/gdc.test/compilable/scope.d

raw
1 /*
2 REQUIRED_ARGS: -preview=dip1000
3 */
4
5 struct Cache
6 {
7 ubyte[1] v;
8
9 ubyte[] set(ubyte[1] v) return
10 {
11 return this.v[] = v[];
12 }
13 }
14
15 /*********************************/
16
17 // https://github.com/dlang/dmd/pull/9220
18
19 @safe:
20
21 struct OnlyResult
22 {
23 private this(Values)(scope ref Values values)
24 {
25 this.s = values;
26 }
27
28 string s;
29 }
30
31 auto only(Values)(Values vv)
32 {
33 return OnlyResult(vv);
34 }
35
36
37 void test() @nogc @safe pure
38 {
39 only(null);
40 }
41
42 /************************************/
43
44 // https://github.com/dlang/dmd/pull/9220
45
46 auto callWrappedOops(scope string dArgs) {
47
48 string callWrappedImpl() {
49 return dArgs;
50 }
51 }
52
53 /************************************/
54
55 struct Constant
56 {
57 int* member;
58
59 this(Repeat!(int*) grid) @safe
60 {
61 foreach(ref x; grid)
62 member = x;
63
64 foreach(ref x; grid)
65 x = member;
66 }
67
68 int* foo(return scope Repeat!(int*) grid) @safe
69 {
70 foreach(ref x; grid)
71 x = member;
72
73 foreach(ref x; grid)
74 return x;
75
76 return null;
77 }
78
79 alias Repeat(T...) = T;
80 }
81
82
83 /************************************/
84
85 // https://issues.dlang.org/show_bug.cgi?id=19387
86
87 struct C
88 {
89 void* u;
90 this(this) @safe
91 {
92 }
93 }
94
95 struct S
96 {
97 C c;
98 }
99
100 void foo(scope S s) @safe
101 {
102 }
103
104 void bar(scope S s) @safe
105 {
106 foo(s);
107 }
108
109 /************************************/
110
111 // https://issues.dlang.org/show_bug.cgi?id=20675
112
113 struct D
114 {
115 int pos;
116 char* p;
117 }
118
119 void test(scope ref D d) @safe
120 {
121 D[] da;
122 da ~= D(d.pos, null);
123 }
124
125 /************************************/
126
127 void withEscapes()
128 {
129 static D get() @safe;
130
131 with (get())
132 {
133 }
134 }
135
136 /************************************/
137
138 // https://issues.dlang.org/show_bug.cgi?id=20682
139
140 int f1_20682(return scope ref D d) @safe
141 {
142 return d.pos;
143 }
144
145 ref int f2_20682(return ref scope D d) @safe
146 {
147 return d.pos;
148 }
149
150 void test_20682(scope ref D d) @safe
151 {
152 int[] a;
153 a ~= f1_20682(d);
154 a ~= f2_20682(d);
155 a ~= cast(int) d.p;
156 }
157
158 // Phobos failure
159 void formattedWrite(immutable char[2] args) @safe
160 {
161 scope immutable char[] val = args;
162 }
163
164 void ctfeRead(const ubyte[2] array) @safe
165 {
166 short result;
167
168 foreach_reverse (b; array)
169 result = cast(short) ((result << 8) | b);
170
171 foreach (b; array)
172 result = cast(short) ((result << 8) | b);
173 }
174
175 void demangle() @safe
176 {
177 static struct DotSplitter
178 {
179 const(char)[] s;
180
181 @safe:
182 @property bool empty() const { return !s.length; }
183
184 @property const(char)[] front() const return
185 {
186 immutable i = indexOfDot();
187 return s;
188 }
189
190 void popFront() {}
191
192 private ptrdiff_t indexOfDot() const
193 {
194 return -1;
195 }
196 }
197
198 foreach (comp; DotSplitter(""))
199 {
200 const copy = comp;
201 }
202 }
203
204 void fileCtor() @safe
205 {
206 static struct S
207 {
208 int i;
209 }
210
211 // static S get()
212 // {
213 // return S();
214 // }
215
216 with (S())
217 {
218 int* ptr = &i;
219 }
220 }
221
222 // Missing test coverage
223 int*[4] testArray() @safe
224 {
225 return typeof(return).init;
226 }
227
228 /************************************/
229
230 // https://issues.dlang.org/show_bug.cgi?id=21209
231 void testForeach(T)(const(T)[] ts)
232 {
233 static int g;
234 g++; // force impure for https://issues.dlang.org/show_bug.cgi?id=20150
235 foreach (c; ts)
236 {
237
238 }
239 foreach_reverse(c0; ts)
240 {
241 foreach(c1; ts)
242 {
243
244 }
245 }
246 }
247
248 @safe
249 void main21209()
250 {
251 char[10] cs;
252 float[10] fs;
253 testForeach(cs);
254 testForeach(fs);
255 }
256
257 struct S23669
258 {
259 string[] a;
260 @safe void reserve() scope
261 {
262 a.length += 1;
263 }
264 }
265
266 /************************************/
267
268 // Calling `hasPointers` in escape.d at some point caused
269 // a "circular `typeof` definition" error in std.typecons
270 // https://github.com/dlang/dmd/pull/16719
271 struct Unique
272 {
273 int* p;
274 alias ValueType = typeof({ return p; } ());
275 static if (is(ValueType == int*)) {}
276 }
277
278 // Without handling tuple expansion in escape.d, this error occurs:
279 // Error: returning `(ref Tuple!(int, int) __tup2 = this.tuple.get();) , &__tup2.__expand_field_0` escapes a reference to local variable `__tup2`
280 struct Tuple(Types...)
281 {
282 Types expand;
283 ref Tuple!Types get() { return this; }
284 }
285
286 struct S2
287 {
288 Tuple!(int, int) tuple;
289 int* f() return { return &tuple.get().expand[0]; }
290 }
291
292 /************************************/
293
294 // https://issues.dlang.org/show_bug.cgi?id=23300
295
296 auto array(Range)(Range r)
297 {
298 int[] result;
299 foreach (e; r)
300 result ~= e;
301 return result;
302 }
303
304 struct Range
305 {
306 int* ptr;
307 int front = 3;
308 enum empty = true;
309 void popFront() @safe scope {}
310 }
311
312 auto f() @safe
313 {
314 scope Range r;
315 return r.array;
316 }
317
318 /************************************/
319
320 // Test that there's no endless loop forwarding `__appendtmp1 => __appendtmp1.this(null)`
321 struct SD
322 {
323 int* p;
324 this(int*) return scope {}
325 }
326
327 auto fsd() @safe
328 {
329 SD[] a;
330 a ~= SD(null);
331 }
332
333 /************************************/
334
335 // Test that `return scope` inference from assignment doesn't force the function to be `scope`
336 struct Scape
337 {
338 int* p;
339
340 this()(int* input)
341 {
342 p = input; // return scope inferred here
343 notScope(); // not-scope inferred here
344 }
345
346 void notScope() @safe
347 {
348 static int* g;
349 g = p;
350 }
351 }
352
353 @safe testScape()
354 {
355 Scape(null);
356 }
357
358 /************************************/
359
360 // Test `scope` inference in presence of nested function returning `this`:
361 // `save()` can still be called on a `scope Result`
362 struct Result
363 {
364 int* source;
365 auto save()
366 {
367 int* saveI() { return this.source; }
368 auto saveResult = Result(saveI());
369 }
370 }
371
372 @safe escapeNested()
373 {
374 int s;
375 auto r = Result(&s);
376 r.save();
377 }