d: Merge upstream dmd, druntime 26f049fb26, phobos 330d6a4fd.
[official-gcc.git] / gcc / testsuite / gdc.test / compilable / ob1.d
blob720c765eda3265cfbb2926d55381eb656415753e
1 // REQUIRED_ARGS: -preview=dip1021
3 /* Should compile successfully
4 */
7 struct Allocation {
8 int* ptr;
9 size_t length;
12 void canFind(scope Allocation);
14 int* malloc();
15 void free(int*);
16 void pitcher();
17 void borrow(scope int*);
18 void borrow2c(const scope int*, const scope int*);
19 void out1(out int*);
22 /*****************************/
24 @live int* foo1(int* p)
26 return p; // consumes owner
29 @live int* foo2()
31 int* p = null;
32 return p; // consumes owner
35 @live int* foo3(int* p)
37 scope int* q = p; // borrows from p
38 return p; // use of p ends borrow in q
41 @live int* foo4(int* p)
43 scope int* bq = p; // borrow
44 scope const int* cq = p; // const borrow
45 return p; // ends both borrows
48 /*******************************/
50 @live void foo5()
52 auto p = malloc();
53 scope(exit) free(p);
54 pitcher();
57 /*******************************/
59 void deallocate(int* ptr, size_t length) @live
61 canFind(Allocation(ptr, length)); // canFind() borrows ptr
62 free(ptr);
66 /*******************************/
69 @live int* test1()
71 auto p = malloc();
72 scope b = p;
73 return p;
76 @live int* test2()
78 auto p = malloc();
79 auto q = p;
80 return q;
83 @live void test3()
85 auto p = malloc();
86 free(p);
89 @live void test4()
91 auto p = malloc();
92 borrow(p);
93 free(p);
96 @live void test5()
98 auto p = malloc();
99 scope q = p;
100 borrow2c(p, p);
101 free(p);
104 @live void test6()
106 int* p = void;
107 out1(p); // initialize
108 free(p); // consume
112 /*******************************/
114 void zoo1(int);
116 @live void zoo2() {
117 int* p = malloc();
118 zoo1(*p); // does not consume p
119 free(p);
122 @live void zoo3() {
123 int** p = cast(int**)malloc();
124 free(*p); // consumes p
127 @live void zoo4() {
128 int[] a = malloc()[0 .. 1];
129 zoo1(a[0]); // does not consume a
130 free(a.ptr); // consumes a
133 @live void zoo5() {
134 int*[] a = (cast(int**)malloc())[0 .. 1];
135 free(a[0]); // consumes a
138 struct S { int i; int* p; }
140 @live void zoo6() {
141 S* s = cast(S*)malloc();
142 zoo1(s.i); // does not consume s
143 free(cast(int*)s);
146 @live void zoo7() {
147 S* s = cast(S*)malloc();
148 free(s.p); // consumes s