Debian Codegraph @

gcc/testsuite/gdc.test/runnable/implicit.d

raw
1 /*
2 REQUIRED_ARGS: -verrors=simple
3 TEST_OUTPUT:
4 ---
5 runnable/implicit.d(163): Deprecation: slice of static array temporary returned by `pureMaker3c()` assigned to longer lived variable `z1`
6 runnable/implicit.d(164): Deprecation: slice of static array temporary returned by `pureMaker3c()` assigned to longer lived variable `z2`
7 ---
8
9 RUN_OUTPUT:
10 ---
11 Success
12 ---
13 */
14
15 import core.stdc.stdio;
16
17 /***********************************/
18
19 template cat1(T)
20 {
21 T cat1(T i) { return i + 1; }
22 }
23
24 void test1()
25 {
26 auto a = cat1(1);
27 assert(a == 2);
28 }
29
30 /***********************************/
31
32 template cat2(T)
33 {
34 T cat2(T* p) { return *p + 1; }
35 }
36
37 void test2()
38 {
39 int i = 1;
40 auto a = cat2(&i);
41 assert(a == 2);
42 assert(typeid(typeof(a)) == typeid(int));
43 }
44
45 /***********************************/
46
47 struct S3 { }
48
49 template cat3(T)
50 {
51 T cat3(T* p, S3 s) { return *p + 1; }
52 }
53
54 void test3()
55 {
56 S3 s;
57 int i = 1;
58 auto a = cat3(&i, s);
59 assert(a == 2);
60 assert(typeid(typeof(a)) == typeid(int));
61 }
62
63 /***********************************/
64
65 template cat4(T, int N)
66 {
67 T cat4(T[N] p, T[N] q) { return p[0] + N; }
68 }
69
70 void test4()
71 {
72 int[3] i;
73 i[0] = 7;
74 i[1] = 8;
75 i[2] = 9;
76 auto a = cat4(i, i);
77 assert(a == 10);
78 assert(typeid(typeof(a)) == typeid(int));
79 }
80
81 /***********************************/
82
83 template cat5(T, U=T*, int V=7)
84 {
85 T cat5(T x)
86 {
87 U u = &x;
88
89 return x + 3 + *u + V;
90 }
91 }
92
93 void test5()
94 {
95 int x = 2;
96
97 auto a = cat5(x);
98 assert(a == 14);
99 assert(typeid(typeof(a)) == typeid(int));
100
101 auto b = cat5!(int,int*,8)(x);
102 assert(b == 15);
103 assert(typeid(typeof(b)) == typeid(int));
104 }
105
106 /***********************************/
107
108 int* pureMaker() pure
109 {
110 return [1,2,3,4].ptr + 1;
111 }
112
113 void testDIP29_1()
114 {
115 int* p;
116 static assert(!__traits(compiles, { immutable x = p + 3; }));
117 immutable x = pureMaker() + 1;
118 immutable y = pureMaker() - 1;
119 immutable z = 1 + pureMaker();
120 }
121
122 /***********************************/
123
124 int** pureMaker2() pure
125 {
126 int*[] da = [[11,12,13].ptr, [21,22,23].ptr, [31,32,33].ptr, [41,42,43].ptr];
127 return da.ptr + 1;
128 }
129
130 void testDIP29_2()
131 {
132 immutable x2 = pureMaker2() + 1;
133 immutable y2 = pureMaker2() - 1;
134 immutable z2 = 1 + pureMaker2();
135 }
136
137 /***********************************/
138
139 int[] pureMaker3a() pure
140 {
141 return new int[4];
142 }
143
144 int* pureMaker3b() pure
145 {
146 return new int[4].ptr;
147 }
148
149 int[4] pureMaker3c() pure
150 {
151 int[4] buf;
152 return buf;
153 }
154
155 void testDIP29_3()
156 {
157 immutable x1 = pureMaker3a()[];
158 immutable x2 = pureMaker3a()[0..2];
159
160 immutable y2 = pureMaker3b()[0..2];
161
162 // Conversion from *rvalue* of mutable static array to immutable slice
163 immutable z1 = pureMaker3c()[];
164 immutable z2 = pureMaker3c()[0..2];
165
166 // https://issues.dlang.org/show_bug.cgi?id=12467
167 // conversion from lvalue of mutable static array to immutable slice
168 char[3] arr = "foo";
169 static assert(!__traits(compiles, { string str = arr[]; }));
170 }
171
172 /***********************************/
173
174 import core.vararg;
175
176 int* maker() pure { return null; }
177 int* maker1(int *) pure { return null; }
178 int* function(int *) pure makerfp1;
179 int* maker2(int *, ...) pure { return null; }
180 int* maker3(int) pure { return null; }
181 int* maker4(ref int) pure { return null; }
182 int* maker5(ref immutable int) pure { return null; }
183
184 void testDIP29_4()
185 {
186 { immutable x = maker1(maker()); }
187 { immutable x = maker1(null); }
188 static assert(__traits(compiles, { immutable x = (*makerfp1)(maker()); }));
189 { shared x = maker1(null); }
190 { immutable x = maker2(null, 3); }
191 { immutable int g; immutable x = maker2(null, 3, &g); }
192 static assert(!__traits(compiles, { int g; immutable x = maker2(null, 3, &g); }));
193 { immutable x = maker3(1); }
194 static assert(!__traits(compiles, { int g; immutable x = maker4(g); }));
195 { immutable int g; immutable x = maker5(g); }
196 }
197
198 /***********************************/
199 // https://issues.dlang.org/show_bug.cgi?id=14155
200
201 immutable int g14155;
202
203 shared static this() { g14155 = 1; }
204
205 int* make14155m ( int* p) pure { return null; }
206 const(int*) make14155c ( const(int*) p) pure { return &g14155; }
207 immutable(int*) make14155i ( immutable(int*) p) pure { return &g14155; }
208 shared(int*) make14155sm( shared(int*) p) pure { return null; }
209 shared(const int*) make14155sc(shared(const int*) p) pure { return &g14155; }
210
211 void test14155_for_testDIP29_4()
212 {
213 static assert( __traits(compiles, { int* p = make14155m (null); })); // m <- m (normal)
214 static assert( __traits(compiles, { const(int*) p = make14155m (null); })); // c <- m (normal)
215 static assert( __traits(compiles, { shared(int*) p = make14155m (null); })); // sm <- m (unique)
216 static assert( __traits(compiles, { shared(const int*) p = make14155m (null); })); // sc <- m (unique)
217 static assert( __traits(compiles, { immutable(int*) p = make14155m (null); })); // i <- m (unique)
218
219 static assert(!__traits(compiles, { int* p = make14155c (null); })); // m <- c (NG) <bugzilla case>
220 static assert( __traits(compiles, { const(int*) p = make14155c (null); })); // c <- c (normal)
221 static assert(!__traits(compiles, { shared(int*) p = make14155c (null); })); // sm <- c (NG)
222 static assert( __traits(compiles, { shared(const int*) p = make14155c (null); })); // sc <- c (unique or immutable global)
223 static assert( __traits(compiles, { immutable(int*) p = make14155c (null); })); // i <- c (unique or immutable global)
224
225 static assert(!__traits(compiles, { int* p = make14155i (null); })); // m <- i (NG)
226 static assert( __traits(compiles, { const(int*) p = make14155i (null); })); // c <- i (normal)
227 static assert(!__traits(compiles, { shared(int*) p = make14155i (null); })); // sm <- i (NG)
228 static assert( __traits(compiles, { shared(const int*) p = make14155i (null); })); // sc <- i (normal)
229 static assert( __traits(compiles, { immutable(int*) p = make14155i (null); })); // i <- i (normal)
230
231 static assert( __traits(compiles, { int* p = make14155sm(null); })); // m <- sm (unique)
232 static assert( __traits(compiles, { const(int*) p = make14155sm(null); })); // c <- sm (unique)
233 static assert( __traits(compiles, { shared(int*) p = make14155sm(null); })); // sm <- sm (normal)
234 static assert( __traits(compiles, { shared(const int*) p = make14155sm(null); })); // sc <- sm (normal)
235 static assert( __traits(compiles, { immutable(int*) p = make14155sm(null); })); // i <- sm (unique)
236
237 static assert(!__traits(compiles, { int* p = make14155sc(null); })); // m <- sc (NG)
238 static assert( __traits(compiles, { const(int*) p = make14155sc(null); })); // c <- sc (unique or immutable global)
239 static assert(!__traits(compiles, { shared(int*) p = make14155sc(null); })); // sm <- sc (NG)
240 static assert( __traits(compiles, { shared(const int*) p = make14155sc(null); })); // sc <- sc (normal)
241 static assert( __traits(compiles, { immutable(int*) p = make14155sc(null); })); // i <- sc
242
243 int x;
244 int* nestf() pure { return &x; }
245 static assert(!__traits(compiles, { immutable(int*) ip = nestf(); }));
246 }
247
248 /***********************************/
249 // https://issues.dlang.org/show_bug.cgi?id=14141
250
251 struct S14141
252 {
253 Object obj;
254
255 const(Object) getObj() const pure
256 {
257 return obj;
258 }
259 }
260
261 void test14141()
262 {
263 const S14141 s;
264 static assert(is(typeof(s.getObj()) == const Object)); // ok
265 static assert(!__traits(compiles, { Object o = s.getObj(); })); // ok <- fails
266 }
267
268 /***********************************/
269
270 int[] test6(int[] a) pure @safe nothrow
271 {
272 return a.dup;
273 }
274
275 /***********************************/
276
277 int*[] pureFoo() pure { return null; }
278
279
280 void testDIP29_5() pure
281 {
282 { char[] s; immutable x = s.idup; }
283 { immutable x = (cast(int*[])null).dup; }
284 { immutable x = pureFoo(); }
285 { immutable x = pureFoo().dup; }
286 }
287
288 /***********************************/
289
290 void testDIP29_6()
291 {
292 /******* structs ************/
293
294 static assert(__traits(compiles,
295 {
296 static struct S { int *p; }
297 immutable s = new S; // since p is null
298 }));
299
300 static assert(!__traits(compiles,
301 {
302 __gshared int x;
303 static struct S { int *p = &x; }
304 immutable s = new S; // x is mutable
305 }));
306
307 static assert(!__traits(compiles,
308 {
309 int y;
310 struct S { int x; void bar() { y = 3; } }
311 immutable s = new S; // nested struct
312 }));
313
314 static assert(!__traits(compiles,
315 {
316 static struct S { int x; this(int); }
317 immutable s = new S(1);
318 }));
319
320 static assert(__traits(compiles,
321 {
322 static struct S { int x; this(int) pure; }
323 immutable s = new S(1);
324 }));
325
326 static assert(__traits(compiles,
327 {
328 static struct S { int* p = void; this(int) pure; }
329 immutable s = new S(1);
330 }));
331
332 static assert(!__traits(compiles,
333 {
334 static struct S { int* p = void; this(int*) pure; }
335 int x;
336 immutable s = new S(&x);
337 }));
338
339 static assert(__traits(compiles,
340 {
341 static struct S { int* p = void; this(immutable(int)*) pure; }
342 immutable int x;
343 immutable s = new S(&x);
344 }));
345
346 static assert(__traits(compiles,
347 {
348 static struct S { int* p = void; this(int*) pure; }
349 immutable s = new S(null);
350 }));
351
352 /******* classes ************/
353
354 static assert(__traits(compiles,
355 {
356 static class S { int *p; }
357 immutable s = new S; // since p is null
358 }));
359
360 static assert(!__traits(compiles,
361 {
362 __gshared int x;
363 static class S { int *p = &x; }
364 immutable s = new S; // x is mutable
365 }));
366
367 static assert(!__traits(compiles,
368 {
369 int y;
370 class S { int x; void bar() { y = 3; } }
371 immutable s = new S; // nested class
372 }));
373
374 static assert(!__traits(compiles,
375 {
376 static class S { int x; this(int); }
377 immutable s = new S(1);
378 }));
379
380 static assert(__traits(compiles,
381 {
382 static class S { int x; this(int) pure; }
383 immutable s = new S(1);
384 }));
385
386 static assert(__traits(compiles,
387 {
388 static class S { int* p = void; this(int) pure; }
389 immutable s = new S(1);
390 }));
391
392 static assert(!__traits(compiles,
393 {
394 static class S { int* p = void; this(int*) pure; }
395 int x;
396 immutable s = new S(&x);
397 }));
398
399 static assert(__traits(compiles,
400 {
401 static class S { int* p = void; this(immutable(int)*) pure; }
402 immutable int x;
403 immutable s = new S(&x);
404 }));
405
406 static assert(__traits(compiles,
407 {
408 static class S { int* p = void; this(int*) pure; }
409 immutable s = new S(null);
410 }));
411 }
412
413 // https://issues.dlang.org/show_bug.cgi?id=14155
414
415 void test14155_for_testDIP29_6()
416 {
417 static class CI
418 {
419 int* p;
420 this(int) immutable pure { p = &g14155; }
421 }
422
423 static assert(!__traits(compiles, { CI c = new immutable CI(1); }));
424 static assert( __traits(compiles, { const CI c = new immutable CI(1); }));
425 static assert( __traits(compiles, { immutable CI c = new immutable CI(1); }));
426 static assert(!__traits(compiles, { shared CI c = new immutable CI(1); }));
427 static assert(!__traits(compiles, { CI c = new const CI(1); }));
428 static assert( __traits(compiles, { const CI c = new const CI(1); }));
429 static assert( __traits(compiles, { immutable CI c = new const CI(1); }));
430 static assert(!__traits(compiles, { shared CI c = new const CI(1); }));
431 }
432
433 /***********************************/
434 // https://issues.dlang.org/show_bug.cgi?id=13640
435
436 struct S13640
437 {
438 static struct R
439 {
440 int* p;
441 this(inout ref int* p) inout pure { this.p = p; }
442 }
443
444 int* p;
445
446 void foo() inout pure
447 {
448 // Implicit conversion from inout(R) to R should be disallowed.
449 static assert(!__traits(compiles, { R r = inout(R)(p); }));
450 }
451 }
452
453 /***********************************/
454 // https://issues.dlang.org/show_bug.cgi?id=15778
455
456 void test15778()
457 {
458 char[] cs = new char[](3);
459 wchar[] ws = new wchar[](3);
460 dchar[] ds = new dchar[](3);
461
462 cs[] = "abc"; assert(cs == "abc");
463 cs[] = "def"c; assert(cs == "def");
464 ws[] = "abc"; assert(ws == "abc");
465 ws[] = "def"w; assert(ws == "def");
466 ds[] = "abc"; assert(ds == "abc");
467 ds[] = "def"d; assert(ds == "def");
468
469 static assert(!__traits(compiles, (cs[] = "a"w)));
470 static assert(!__traits(compiles, (cs[] = "a"d)));
471 static assert(!__traits(compiles, (ws[] = "a"c)));
472 static assert(!__traits(compiles, (ws[] = "a"d)));
473 static assert(!__traits(compiles, (ds[] = "a"c)));
474 static assert(!__traits(compiles, (ds[] = "a"w)));
475 }
476
477 /***********************************/
478
479 void main()
480 {
481 test1();
482 test2();
483 test3();
484 test4();
485 test5();
486 testDIP29_1();
487 testDIP29_2();
488 testDIP29_3();
489 testDIP29_4();
490 testDIP29_5();
491 testDIP29_6();
492 test15778();
493
494 printf("Success\n");
495 }