]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/testsuite/gdc.test/runnable/sctor.d
d: Import dmd b8384668f, druntime e6caaab9, phobos 5ab9ad256 (v2.098.0-beta.1)
[thirdparty/gcc.git] / gcc / testsuite / gdc.test / runnable / sctor.d
CommitLineData
5fee5ec3
IB
1/*
2REQUIRED_ARGS: -w -de
3PERMUTE_ARGS:
4RUN_OUTPUT:
5---
6Success
7---
8*/
b4c522fa
IB
9
10extern(C) int printf(const char*, ...);
11
12/***************************************************/
13// mutable field
14
15struct S1A
16{
17 int v;
18 this(int)
19 {
20 v = 1;
21 v = 2; // multiple initialization
22 }
23}
24
25struct S1B
26{
27 int v;
28 this(int)
29 {
30 if (true) v = 1; else v = 2;
31 v = 3; // multiple initialization
32 }
33 this(long)
34 {
35 if (true) v = 1;
36 v = 3; // multiple initialization
37 }
38 this(string)
39 {
40 if (true) {} else v = 2;
41 v = 3; // multiple initialization
42 }
43}
44
45struct S1C
46{
47 int v;
48 this(int)
49 {
50 true ? (v = 1) : (v = 2);
51 v = 3; // multiple initialization
52 }
53 this(long)
54 {
55 auto x = true ? (v = 1) : 2;
56 v = 3; // multiple initialization
57 }
58 this(string)
59 {
60 auto x = true ? 1 : (v = 2);
61 v = 3; // multiple initialization
62 }
63}
64
65/***************************************************/
66// with control flow
67
68struct S2
69{
70 immutable int v;
71 immutable int w;
72 int x;
73 this(int)
74 {
75 if (true) v = 1;
76 else v = 2;
77
78 true ? (w = 1) : (w = 2);
79
80 x = 1; // initialization
81 L: x = 2; // assignment after labels
82 }
83 this(long n)
84 {
85 if (n > 0)
86 return;
87 v = 1; // skipped initialization
88
89 // w skipped initialization
90
91 x = 1; // initialization
92 foreach (i; 0..1) x = 2; // assignment in loops
93 }
94}
95
96/***************************************************/
97// with immutable constructor
98
99struct S3
100{
101 int v;
102 int w;
103 this(int) immutable
104 {
105 if (true) v = 1;
106 else v = 2;
107
108 true ? (w = 1) : (w = 2);
109 }
110}
111
112/***************************************************/
113// in typeof
114
115struct S4
116{
117 immutable int v;
118 this(int)
119 {
120 static assert(is(typeof(v = 1)));
121 v = 1;
122 }
123}
124
125/***************************************************/
5fee5ec3 126// https://issues.dlang.org/show_bug.cgi?id=8117
b4c522fa
IB
127
128struct S8117
129{
130 @disable this();
131 this(int) {}
132}
133
134class C8117
135{
136 S8117 s = S8117(1);
137}
138
139void test8117()
140{
141 auto t = new C8117();
142}
143
144/***************************************************/
5fee5ec3 145// https://issues.dlang.org/show_bug.cgi?id=9665
b4c522fa
IB
146
147struct X9665
148{
149 static uint count;
150 ulong payload;
151 this(int n) { payload = n; count += 1; }
152 this(string s) immutable { payload = s.length; count += 10; }
153 void opAssign(X9665 x) { payload = 100; count += 100; }
154}
155
156struct S9665
157{
158 X9665 mval;
159 immutable X9665 ival;
160 this(int n)
161 {
162 X9665.count = 0;
163 mval = X9665(n); // 1st, initializing
164 ival = immutable X9665("hi"); // 1st, initializing
165 mval = X9665(1); // 2nd, assignment
166 static assert(!__traits(compiles, ival = immutable X9665(1))); // 2nd, assignment
167 //printf("X9665.count = %d\n", X9665.count);
168 assert(X9665.count == 112);
169 }
170 this(int[])
171 {
172 X9665.count = 0;
173 mval = 1; // 1st, initializing (implicit constructor call)
174 ival = "hoo"; // ditto
175 assert(X9665.count == 11);
176 }
177}
178
179void test9665()
180{
181 S9665 s1 = S9665(1);
182 assert(s1.mval.payload == 100);
183 assert(s1.ival.payload == 2);
184
185 S9665 s2 = S9665([]);
186 assert(s2.mval.payload == 1);
187 assert(s2.ival.payload == 3);
188}
189
190/***************************************************/
5fee5ec3 191// https://issues.dlang.org/show_bug.cgi?id=11246
b4c522fa
IB
192
193struct Foo11246
194{
195 static int ctor = 0;
196 static int dtor = 0;
197 this(int i)
198 {
199 ++ctor;
200 }
201
202 ~this()
203 {
204 ++dtor;
205 }
206}
207
208struct Bar11246
209{
210 Foo11246 foo;
211
212 this(int)
213 {
214 foo = Foo11246(5);
215 assert(Foo11246.ctor == 1);
216 assert(Foo11246.dtor == 0);
217 }
218}
219
220void test11246()
221{
222 {
223 auto bar = Bar11246(1);
224 assert(Foo11246.ctor == 1);
225 assert(Foo11246.dtor == 0);
226 }
227 assert(Foo11246.ctor == 1);
228 assert(Foo11246.dtor == 1);
229}
230
231/***************************************************/
5fee5ec3 232// https://issues.dlang.org/show_bug.cgi?id=13515
b4c522fa
IB
233
234Object[string][100] aa13515;
235
236static this()
237{
238 aa13515[5]["foo"] = null;
239}
240
241struct S13515
242{
243 Object[string][100] aa;
244
245 this(int n)
246 {
247 aa[5]["foo"] = null;
248 }
249}
250
251void test13515()
252{
253 assert(aa13515[5].length == 1);
254 assert(aa13515[5]["foo"] is null);
255
256 auto s = S13515(1);
257 assert(s.aa[5].length == 1);
258 assert(s.aa[5]["foo"] is null);
259}
260
261/***************************************************/
5fee5ec3 262// https://issues.dlang.org/show_bug.cgi?id=14409
b4c522fa
IB
263
264class B14409 { this(int) {} }
265class C14409 : B14409
266{
267 this(int n)
268 {
269 if (true)
270 super(n);
271 else
272 assert(0);
273 }
274}
275
276/***************************************************/
5fee5ec3 277// https://issues.dlang.org/show_bug.cgi?id=14376
b4c522fa
IB
278
279auto map14376()
280{
281 return MapResult14376!(e => 0)();
282}
283
284struct MapResult14376(alias pred)
285{
286 @property int front() { return pred(0); }
287}
288
289struct S14376
290{
291 typeof(map14376()) x;
292
293 this(int dummy)
294 {
295 if (true)
296 this.x = map14376();
297 else
298 assert(0);
299 }
300}
301
302/***************************************************/
5fee5ec3 303// https://issues.dlang.org/show_bug.cgi?id=14351
b4c522fa
IB
304
305class B14351
306{
307 this(inout int[]) inout { }
308}
309
310class D14351a : B14351
311{
312 this(int[] arr) { super(arr); }
313}
314
315class D14351b : B14351
316{
317 this(const int[] arr) const { super(arr); }
318}
319
320class D14351c : B14351
321{
322 this(inout int[] arr) inout { super(arr); }
323}
324
325/***************************************************/
5fee5ec3 326// https://issues.dlang.org/show_bug.cgi?id=14450
b4c522fa
IB
327
328struct S14450a // non-template struct + non template ctors - OK
329{
330 int x;
331 this(int) { x = 1; }
332 this(int) immutable { x = 2; }
333}
334struct S14450b // non-template struct + template ctors - OK
335{
336 int x;
337 this()(int) { x = 1; }
338 this()(int) immutable { x = 2; }
339}
340struct S14450c() // template struct + non-template ctors - Error -> OK
341{
342 int x;
343 this(int) { x = 1; }
344 this(int) immutable { x = 2; }
345}
346struct S14450d() // template struct + template ctors - OK
347{
348 int x;
349 this()(int) { x = 1; }
350 this()(int) immutable { x = 2; }
351}
352struct S14450e() // template struct + pure template ctors - Error -> OK
353{
354 int x;
355 this()(int) pure { x = 1; }
356 this()(int) pure immutable { x = 2; }
357}
358
359void test14450()
360{
361 { auto m = S14450a(1); assert(m.x == 1); }
362 { auto m = S14450b(1); assert(m.x == 1); }
363 { auto m = S14450c!()(1); assert(m.x == 1); }
364 { auto m = S14450d!()(1); assert(m.x == 1); }
365 { auto m = S14450e!()(1); assert(m.x == 1); }
366
367 { auto i = immutable S14450a(1); assert(i.x == 2); }
368 { auto i = immutable S14450b(1); assert(i.x == 2); }
369 { auto i = immutable S14450c!()(1); assert(i.x == 2); }
370 { auto i = immutable S14450d!()(1); assert(i.x == 2); }
371 { auto i = immutable S14450e!()(1); assert(i.x == 2); }
372}
373
374/***************************************************/
5fee5ec3 375// https://issues.dlang.org/show_bug.cgi?id=14944
b4c522fa
IB
376
377static int[2] tbl14944;
378
379static this()
380{
381 foreach (ref v; tbl14944)
382 {
383 // This is an initialization of referenced memory
384 // rather than the initialization of the reference.
385 v = 1;
386 }
387}
388
389void test14944()
390{
391 assert(tbl14944[0] == 1);
392}
393
394/***************************************************/
5fee5ec3
IB
395// https://issues.dlang.org/show_bug.cgi?id=15258
396// a field initialization affects other overlapped fields
b4c522fa
IB
397
398class C15258
399{
400 this(const char* result)
401 {
402 this.result = result;
403 }
404
405 union
406 {
407 const char** results;
408 const char* result;
409 }
410}
411
412/***************************************************/
5fee5ec3 413// https://issues.dlang.org/show_bug.cgi?id=15869
b4c522fa 414
5fee5ec3
IB
415struct Set {
416 @disable this(this);
417 int value = 0;
418}
419
420Set clobber(ref Set a) {
421 Set ret; // <- This overwrites *a, i.e. &ret is the same as a
422 ret.value = a.value; // <- Now a.value is 0
423 return ret;
424}
425
426struct XX {
427 Set a = Set(1);
428 this(int n) {
429 a = clobber(a); // fix is to make this an assignment, not a construction
430 }
431}
432void test15869()
b4c522fa 433{
5fee5ec3
IB
434 Set a = Set(1);
435 a = clobber(a);
436 assert(a.value == 1);
437
438 XX xx = XX(0);
439 assert(xx.a.value == 1);
b4c522fa
IB
440}
441
5fee5ec3
IB
442/***************************************************/
443// https://issues.dlang.org/show_bug.cgi?id=19389
444
445struct Foo19389 {
446 int x;
447
448 this(int dummy) { x = dummy; }
449}
450
451struct Bar19389 {
452 Foo19389 a;
453 Foo19389 b;
454
455 this(int dummy) {
456 a = (b = Foo19389(dummy));
457 }
458}
459
460
461void test19389()
b4c522fa 462{
5fee5ec3
IB
463 Bar19389 bar = Bar19389(7);
464 assert(bar.a.x == 7);
465 assert(bar.b.x == 7); // fails
b4c522fa
IB
466}
467
5fee5ec3 468
b4c522fa
IB
469/***************************************************/
470
471int main()
472{
473 test8117();
474 test9665();
475 test11246();
476 test13515();
477 test14450();
478 test14944();
5fee5ec3
IB
479 test15869();
480 test19389();
b4c522fa
IB
481
482 printf("Success\n");
483 return 0;
484}