| 1 |
// REQUIRED_ARGS: -preview=dip1021 |
| 2 |
|
| 3 |
/* Should compile successfully |
| 4 |
*/ |
| 5 |
|
| 6 |
|
| 7 |
struct Allocation { |
| 8 |
int* ptr; |
| 9 |
size_t length; |
| 10 |
} |
| 11 |
|
| 12 |
void canFind(scope Allocation); |
| 13 |
|
| 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*); |
| 20 |
|
| 21 |
|
| 22 |
/*****************************/ |
| 23 |
|
| 24 |
@live int* foo1(int* p) |
| 25 |
{ |
| 26 |
return p; // consumes owner |
| 27 |
} |
| 28 |
|
| 29 |
@live int* foo2() |
| 30 |
{ |
| 31 |
int* p = null; |
| 32 |
return p; // consumes owner |
| 33 |
} |
| 34 |
|
| 35 |
@live int* foo3(int* p) |
| 36 |
{ |
| 37 |
scope int* q = p; // borrows from p |
| 38 |
return p; // use of p ends borrow in q |
| 39 |
} |
| 40 |
|
| 41 |
@live int* foo4(int* p) |
| 42 |
{ |
| 43 |
scope int* bq = p; // borrow |
| 44 |
scope const int* cq = p; // const borrow |
| 45 |
return p; // ends both borrows |
| 46 |
} |
| 47 |
|
| 48 |
/*******************************/ |
| 49 |
|
| 50 |
@live void foo5() |
| 51 |
{ |
| 52 |
auto p = malloc(); |
| 53 |
scope(exit) free(p); |
| 54 |
pitcher(); |
| 55 |
} |
| 56 |
|
| 57 |
/*******************************/ |
| 58 |
|
| 59 |
void deallocate(int* ptr, size_t length) @live |
| 60 |
{ |
| 61 |
canFind(Allocation(ptr, length)); // canFind() borrows ptr |
| 62 |
free(ptr); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
/*******************************/ |
| 67 |
|
| 68 |
|
| 69 |
@live int* test1() |
| 70 |
{ |
| 71 |
auto p = malloc(); |
| 72 |
scope b = p; |
| 73 |
return p; |
| 74 |
} |
| 75 |
|
| 76 |
@live int* test2() |
| 77 |
{ |
| 78 |
auto p = malloc(); |
| 79 |
auto q = p; |
| 80 |
return q; |
| 81 |
} |
| 82 |
|
| 83 |
@live void test3() |
| 84 |
{ |
| 85 |
auto p = malloc(); |
| 86 |
free(p); |
| 87 |
} |
| 88 |
|
| 89 |
@live void test4() |
| 90 |
{ |
| 91 |
auto p = malloc(); |
| 92 |
borrow(p); |
| 93 |
free(p); |
| 94 |
} |
| 95 |
|
| 96 |
@live void test5() |
| 97 |
{ |
| 98 |
auto p = malloc(); |
| 99 |
scope q = p; |
| 100 |
borrow2c(p, p); |
| 101 |
free(p); |
| 102 |
} |
| 103 |
|
| 104 |
@live void test6() |
| 105 |
{ |
| 106 |
int* p = void; |
| 107 |
out1(p); // initialize |
| 108 |
free(p); // consume |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
/*******************************/ |
| 113 |
|
| 114 |
void zoo1(int); |
| 115 |
|
| 116 |
@live void zoo2() { |
| 117 |
int* p = malloc(); |
| 118 |
zoo1(*p); // does not consume p |
| 119 |
free(p); |
| 120 |
} |
| 121 |
|
| 122 |
@live void zoo3() { |
| 123 |
int** p = cast(int**)malloc(); |
| 124 |
free(*p); // consumes p |
| 125 |
} |
| 126 |
|
| 127 |
@live void zoo4() { |
| 128 |
int[] a = malloc()[0 .. 1]; |
| 129 |
zoo1(a[0]); // does not consume a |
| 130 |
free(a.ptr); // consumes a |
| 131 |
} |
| 132 |
|
| 133 |
@live void zoo5() { |
| 134 |
int*[] a = (cast(int**)malloc())[0 .. 1]; |
| 135 |
free(a[0]); // consumes a |
| 136 |
} |
| 137 |
|
| 138 |
struct S { int i; int* p; } |
| 139 |
|
| 140 |
@live void zoo6() { |
| 141 |
S* s = cast(S*)malloc(); |
| 142 |
zoo1(s.i); // does not consume s |
| 143 |
free(cast(int*)s); |
| 144 |
} |
| 145 |
|
| 146 |
@live void zoo7() { |
| 147 |
S* s = cast(S*)malloc(); |
| 148 |
free(s.p); // consumes s |
| 149 |
} |
| 150 |
|
| 151 |
/******************************* |
| 152 |
* https://issues.dlang.org/show_bug.cgi?id=21854 |
| 153 |
*/ |
| 154 |
|
| 155 |
@live void test21854() |
| 156 |
{ |
| 157 |
foreach(int tmp; 0..10) { } |
| 158 |
|
| 159 |
int key = 0; |
| 160 |
int limit = 10; |
| 161 |
for (; key < limit; key += 1) |
| 162 |
{ |
| 163 |
int tmp = key; |
| 164 |
} |
| 165 |
} |