| 1 |
/* Testing Ownership/Borrowing system |
| 2 |
REQUIRED_ARGS: -preview=dip1021 |
| 3 |
*/ |
| 4 |
|
| 5 |
int* malloc(); |
| 6 |
void free(int*); |
| 7 |
|
| 8 |
/* TEST_OUTPUT: |
| 9 |
--- |
| 10 |
fail_compilation/fob2.d(110): Error: variable `fob2.foo1.b1` has undefined state and cannot be read |
| 11 |
fail_compilation/fob2.d(103): Error: variable `fob2.foo1.p` is not disposed of before return |
| 12 |
--- |
| 13 |
*/ |
| 14 |
|
| 15 |
#line 100 |
| 16 |
|
| 17 |
@live int foo1(int i) |
| 18 |
{ |
| 19 |
int* p = malloc(); |
| 20 |
scope const(int)* b1, b2; |
| 21 |
if (i) |
| 22 |
b1 = p; |
| 23 |
else |
| 24 |
b2 = p; |
| 25 |
*p = 3; |
| 26 |
return *b1; |
| 27 |
} |
| 28 |
|
| 29 |
/* TEST_OUTPUT: |
| 30 |
--- |
| 31 |
fail_compilation/fob2.d(203): Error: more than one mutable reference of `p` in arguments to `fob2.foo2()` |
| 32 |
--- |
| 33 |
*/ |
| 34 |
|
| 35 |
//fail_compilation/fob2.d(203): Error: variable `fob2.zoo2.p` is passed as Owner more than once |
| 36 |
//fail_compilation/fob2.d(202): Error: variable `fob2.zoo2.p` is left dangling at return |
| 37 |
|
| 38 |
#line 200 |
| 39 |
|
| 40 |
@live void zoo2() { |
| 41 |
int* p = malloc(); |
| 42 |
foo2(p, p + 1); |
| 43 |
} |
| 44 |
|
| 45 |
@live void foo2( scope int* p, scope int* q ); |
| 46 |
|
| 47 |
/* TEST_OUTPUT: |
| 48 |
--- |
| 49 |
fail_compilation/fob2.d(303): Error: variable `fob2.foo3.b` is not disposed of before return |
| 50 |
--- |
| 51 |
*/ |
| 52 |
|
| 53 |
#line 300 |
| 54 |
|
| 55 |
@live void foo3() |
| 56 |
{ |
| 57 |
scope int* b = malloc(); |
| 58 |
} |
| 59 |
|
| 60 |
/* TEST_OUTPUT: |
| 61 |
--- |
| 62 |
fail_compilation/fob2.d(427): Error: variable `fob2.test43.p` is both Owner and Undefined |
| 63 |
fail_compilation/fob2.d(429): Error: variable `fob2.test43.p` has undefined state and cannot be read |
| 64 |
fail_compilation/fob2.d(429): Error: variable `fob2.test43.p` is not Owner, cannot consume its value |
| 65 |
fail_compilation/fob2.d(432): Error: variable `fob2.test43.p` has undefined state and cannot be read |
| 66 |
fail_compilation/fob2.d(432): Error: variable `fob2.test43.p` is not Owner, cannot consume its value |
| 67 |
--- |
| 68 |
*/ |
| 69 |
#line 400 |
| 70 |
|
| 71 |
|
| 72 |
bool f(); |
| 73 |
|
| 74 |
@live void test41(int* p, int i) |
| 75 |
{ |
| 76 |
for (; f(); ++i) |
| 77 |
{ |
| 78 |
--i; |
| 79 |
free(p); |
| 80 |
p = null; |
| 81 |
} |
| 82 |
free(p); |
| 83 |
} |
| 84 |
|
| 85 |
@live void test42(int* p, int i) |
| 86 |
{ |
| 87 |
for (; f(); ++i) |
| 88 |
{ |
| 89 |
--i; |
| 90 |
} |
| 91 |
free(p); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
@live void test43(int* p, int i) |
| 96 |
{ |
| 97 |
for (; f(); ++i) |
| 98 |
{ |
| 99 |
free(p); |
| 100 |
--i; |
| 101 |
} |
| 102 |
free(p); |
| 103 |
} |
| 104 |
|
| 105 |
/* TEST_OUTPUT: |
| 106 |
--- |
| 107 |
fail_compilation/fob2.d(506): Error: variable `fob2.test51.p` has undefined state and cannot be read |
| 108 |
fail_compilation/fob2.d(515): Error: variable `fob2.test52.p` has undefined state and cannot be read |
| 109 |
--- |
| 110 |
*/ |
| 111 |
|
| 112 |
// https://issues.dlang.org/show_bug.cgi?id=20747 |
| 113 |
|
| 114 |
#line 500 |
| 115 |
|
| 116 |
@live test51() |
| 117 |
{ |
| 118 |
int x; |
| 119 |
scope p = &x; |
| 120 |
x = 3; |
| 121 |
*p = 4; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
@live void test52() @safe |
| 126 |
{ |
| 127 |
int x = 5; |
| 128 |
auto p = &x; |
| 129 |
auto q = &x; |
| 130 |
*p = 3; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
@live void test53() |
| 135 |
{ |
| 136 |
scope int x; |
| 137 |
scope int y; |
| 138 |
y = x; |
| 139 |
x = 3; |
| 140 |
y = 4; |
| 141 |
} |
| 142 |
|
| 143 |
/* TEST_OUTPUT: |
| 144 |
--- |
| 145 |
fail_compilation/fob2.d(603): Error: variable `fob2.test6.p` is not disposed of before return |
| 146 |
fail_compilation/fob2.d(612): Error: more than one mutable reference of `p` in arguments to `fob2.foo6b()` |
| 147 |
--- |
| 148 |
*/ |
| 149 |
|
| 150 |
#line 600 |
| 151 |
|
| 152 |
@live extern (C) void foo6(int, scope ...); |
| 153 |
|
| 154 |
@live void test6(int* p) |
| 155 |
{ |
| 156 |
foo6(1, p); |
| 157 |
} |
| 158 |
|
| 159 |
@live extern (C) void foo6b(int, scope const ...); |
| 160 |
|
| 161 |
@live int* test6b(return int* p) |
| 162 |
{ |
| 163 |
foo6b(1, p, p); |
| 164 |
return p; |
| 165 |
} |
| 166 |
|
| 167 |
/* TEST_OUTPUT: |
| 168 |
--- |
| 169 |
fail_compilation/fob2.d(705): Error: variable `fob2.test7.p` is not Owner, cannot consume its value |
| 170 |
--- |
| 171 |
*/ |
| 172 |
|
| 173 |
#line 700 |
| 174 |
|
| 175 |
void free7(int*); |
| 176 |
|
| 177 |
@live void test7(scope int* p) |
| 178 |
{ |
| 179 |
free7(p); |
| 180 |
} |
| 181 |
|
| 182 |
/* TEST_OUTPUT: |
| 183 |
--- |
| 184 |
fail_compilation/fob2.d(807): Error: variable `fob2.test8.p` assigning to Owner without disposing of owned value |
| 185 |
--- |
| 186 |
*/ |
| 187 |
|
| 188 |
#line 800 |
| 189 |
|
| 190 |
int* malloc8(); |
| 191 |
void free8(int*); |
| 192 |
|
| 193 |
@live void test8() |
| 194 |
{ |
| 195 |
int* p = malloc8(); |
| 196 |
p = malloc8(); // error here |
| 197 |
free8(p); |
| 198 |
} |