]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/testsuite/gdc.test/compilable/scope.d
7e81028d2781a716711c9baa2ede2c976ec6f826
[thirdparty/gcc.git] / gcc / testsuite / gdc.test / compilable / scope.d
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 scope ref 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 }