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