| 1 |
/* |
| 2 |
REQUIRED_ARGS: -preview=dip1000 |
| 3 |
*/ |
| 4 |
|
| 5 |
/* |
| 6 |
TEST_OUTPUT: |
| 7 |
--- |
| 8 |
fail_compilation/retscope6.d(6007): Error: escaping a reference to local variable `i` by copying `& i` into allocated memory is not allowed in a `@safe` function |
| 9 |
--- |
| 10 |
*/ |
| 11 |
|
| 12 |
#line 6000 |
| 13 |
|
| 14 |
// https://issues.dlang.org/show_bug.cgi?id=17795 |
| 15 |
|
| 16 |
int* test() @safe |
| 17 |
{ |
| 18 |
int i; |
| 19 |
int*[][] arr = new int*[][](1); |
| 20 |
arr[0] ~= &i; |
| 21 |
return arr[0][0]; |
| 22 |
} |
| 23 |
|
| 24 |
/* TEST_OUTPUT: |
| 25 |
--- |
| 26 |
fail_compilation/retscope6.d(7034): Error: assigning address of variable `i` to `s` with longer lifetime is not allowed in a `@safe` function |
| 27 |
fail_compilation/retscope6.d(7035): Error: assigning address of variable `i` to `s` with longer lifetime is not allowed in a `@safe` function |
| 28 |
fail_compilation/retscope6.d(7025): Error: assigning scope variable `__param_2` to `ref` variable `t` with longer lifetime is not allowed in a `@safe` function |
| 29 |
fail_compilation/retscope6.d(7037): Error: template instance `retscope6.S.emplace4!(int*)` error instantiating |
| 30 |
fail_compilation/retscope6.d(7037): Error: assigning address of variable `i` to `s` with longer lifetime is not allowed in a `@safe` function |
| 31 |
--- |
| 32 |
*/ |
| 33 |
|
| 34 |
#line 7000 |
| 35 |
|
| 36 |
alias T = int*; |
| 37 |
|
| 38 |
struct S |
| 39 |
{ |
| 40 |
T payload; |
| 41 |
|
| 42 |
static void emplace(Args...)(ref S s, Args args) @safe |
| 43 |
{ |
| 44 |
s.payload = args[0]; |
| 45 |
} |
| 46 |
|
| 47 |
void emplace2(Args...)(Args args) @safe |
| 48 |
{ |
| 49 |
payload = args[0]; |
| 50 |
} |
| 51 |
|
| 52 |
static void emplace3(Args...)(S s, Args args) @safe |
| 53 |
{ |
| 54 |
s.payload = args[0]; |
| 55 |
} |
| 56 |
|
| 57 |
static void emplace4(Args...)(scope ref S s, scope out S t, scope Args args) @safe |
| 58 |
{ |
| 59 |
s.payload = args[0]; |
| 60 |
t.payload = args[0]; |
| 61 |
} |
| 62 |
|
| 63 |
} |
| 64 |
|
| 65 |
void foo() @safe |
| 66 |
{ |
| 67 |
S s; |
| 68 |
int i; |
| 69 |
s.emplace(s, &i); |
| 70 |
s.emplace2(&i); |
| 71 |
s.emplace3(s, &i); |
| 72 |
s.emplace4(s, s, &i); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
/* TEST_OUTPUT: |
| 77 |
--- |
| 78 |
fail_compilation/retscope6.d(8016): Error: assigning address of variable `i` to `p` with longer lifetime is not allowed in a `@safe` function |
| 79 |
fail_compilation/retscope6.d(8031): Error: assigning reference to local variable `i` to non-scope parameter `p` calling `betty` is not allowed in a `@safe` function |
| 80 |
fail_compilation/retscope6.d(8031): Error: assigning reference to local variable `j` to non-scope parameter `q` calling `betty` is not allowed in a `@safe` function |
| 81 |
fail_compilation/retscope6.d(8023): which is not `scope` because of `p = q` |
| 82 |
fail_compilation/retscope6.d(8048): Error: assigning reference to local variable `i` to non-scope parameter `p` calling `archie` is not allowed in a `@safe` function |
| 83 |
fail_compilation/retscope6.d(8039): which is not `scope` because of `r = p` |
| 84 |
fail_compilation/retscope6.d(8048): Error: assigning reference to local variable `j` to non-scope parameter `q` calling `archie` is not allowed in a `@safe` function |
| 85 |
fail_compilation/retscope6.d(8038): which is not `scope` because of `p = q` |
| 86 |
--- |
| 87 |
*/ |
| 88 |
|
| 89 |
// https://issues.dlang.org/show_bug.cgi?id=19035 |
| 90 |
|
| 91 |
#line 8000 |
| 92 |
@safe |
| 93 |
{ |
| 94 |
|
| 95 |
void escape(int*); |
| 96 |
|
| 97 |
/**********************/ |
| 98 |
|
| 99 |
void frank()(ref scope int* p, int* s) |
| 100 |
{ |
| 101 |
p = s; // should error here |
| 102 |
} |
| 103 |
|
| 104 |
void testfrankly() |
| 105 |
{ |
| 106 |
int* p; |
| 107 |
int i; |
| 108 |
frank(p, &i); |
| 109 |
} |
| 110 |
|
| 111 |
/**********************/ |
| 112 |
|
| 113 |
void betty()(int* p, int* q) |
| 114 |
{ |
| 115 |
p = q; |
| 116 |
escape(p); |
| 117 |
} |
| 118 |
|
| 119 |
void testbetty() |
| 120 |
{ |
| 121 |
int i; |
| 122 |
int j; |
| 123 |
betty(&i, &j); // should error on i and j |
| 124 |
} |
| 125 |
|
| 126 |
/**********************/ |
| 127 |
|
| 128 |
void archie()(int* p, int* q, int* r) |
| 129 |
{ |
| 130 |
p = q; |
| 131 |
r = p; |
| 132 |
escape(q); |
| 133 |
} |
| 134 |
|
| 135 |
void testarchie() |
| 136 |
{ |
| 137 |
int i; |
| 138 |
int j; |
| 139 |
int k; |
| 140 |
archie(&i, &j, &k); // should error on j |
| 141 |
} |
| 142 |
|
| 143 |
} |
| 144 |
|
| 145 |
/* TEST_OUTPUT: |
| 146 |
--- |
| 147 |
fail_compilation/retscope6.d(9023): Error: escaping a reference to local variable `i` by returning `fred(& i)` is not allowed in a `@safe` function |
| 148 |
--- |
| 149 |
*/ |
| 150 |
|
| 151 |
#line 9000 |
| 152 |
|
| 153 |
@safe |
| 154 |
{ |
| 155 |
|
| 156 |
alias T9 = S9!(); struct S9() |
| 157 |
{ |
| 158 |
this(int* q) |
| 159 |
{ |
| 160 |
this.p = q; |
| 161 |
} |
| 162 |
|
| 163 |
int* p; |
| 164 |
} |
| 165 |
|
| 166 |
auto fred(int* r) |
| 167 |
{ |
| 168 |
return T9(r); |
| 169 |
} |
| 170 |
|
| 171 |
T9 testfred() |
| 172 |
{ |
| 173 |
int i; |
| 174 |
auto j = fred(&i); // ok |
| 175 |
return fred(&i); // error |
| 176 |
} |
| 177 |
|
| 178 |
/* TEST_OUTPUT: |
| 179 |
--- |
| 180 |
fail_compilation/retscope6.d(10003): Error: assigning scope variable `values` to non-scope parameter `values` calling `escape` is not allowed in a `@safe` function |
| 181 |
--- |
| 182 |
*/ |
| 183 |
|
| 184 |
#line 10000 |
| 185 |
|
| 186 |
void variadicCaller(int[] values...) |
| 187 |
{ |
| 188 |
escape(values); |
| 189 |
} |
| 190 |
|
| 191 |
void escape(int[] values) {} |
| 192 |
|
| 193 |
/* TEST_OUTPUT: |
| 194 |
--- |
| 195 |
fail_compilation/retscope6.d(11004): Error: assigning address of variable `buffer` to `secret` with longer lifetime is not allowed in a `@safe` function |
| 196 |
--- |
| 197 |
*/ |
| 198 |
|
| 199 |
#line 11000 |
| 200 |
|
| 201 |
void hmac(scope ubyte[] secret) |
| 202 |
{ |
| 203 |
ubyte[10] buffer; |
| 204 |
secret = buffer[]; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
/* TEST_OUTPUT: |
| 209 |
--- |
| 210 |
fail_compilation/retscope6.d(12011): Error: escaping a reference to local variable `x` by returning `escape_m_20150(& x)` is not allowed in a `@safe` function |
| 211 |
fail_compilation/retscope6.d(12022): Error: escaping a reference to local variable `x` by returning `escape_c_20150(& x)` is not allowed in a `@safe` function |
| 212 |
--- |
| 213 |
*/ |
| 214 |
|
| 215 |
#line 12000 |
| 216 |
|
| 217 |
// https://issues.dlang.org/show_bug.cgi?id=20150 |
| 218 |
|
| 219 |
int* escape_m_20150(int* r) @safe pure nothrow |
| 220 |
{ |
| 221 |
return r; |
| 222 |
} |
| 223 |
|
| 224 |
int* f_m_20150() @safe nothrow |
| 225 |
{ |
| 226 |
int x = 42; |
| 227 |
return escape_m_20150(&x); |
| 228 |
} |
| 229 |
|
| 230 |
const(int)* escape_c_20150(const int* r) @safe pure nothrow |
| 231 |
{ |
| 232 |
return r; |
| 233 |
} |
| 234 |
|
| 235 |
const(int)* f_c_20150() @safe nothrow |
| 236 |
{ |
| 237 |
int x = 42; |
| 238 |
return escape_c_20150(&x); |
| 239 |
} |
| 240 |
|
| 241 |
/* TEST_OUTPUT: |
| 242 |
--- |
| 243 |
fail_compilation/retscope6.d(13010): Error: assigning reference to local variable `str` to non-scope parameter `x` calling `f_throw` is not allowed in a `@safe` function |
| 244 |
--- |
| 245 |
*/ |
| 246 |
|
| 247 |
#line 13000 |
| 248 |
// https://issues.dlang.org/show_bug.cgi?id=22221 |
| 249 |
|
| 250 |
void f_throw(string x) @safe pure |
| 251 |
{ |
| 252 |
throw new Exception(x); |
| 253 |
} |
| 254 |
|
| 255 |
void escape_throw_20150() @safe |
| 256 |
{ |
| 257 |
immutable(char)[4] str; |
| 258 |
f_throw(str[]); |
| 259 |
} |
| 260 |
|
| 261 |
/* TEST_OUTPUT: |
| 262 |
--- |
| 263 |
fail_compilation/retscope6.d(14019): Error: assigning scope variable `scopePtr` to non-scope parameter `x` calling `noInfer23021` is not allowed in a `@safe` function |
| 264 |
fail_compilation/retscope6.d(14009): which is not `scope` because of `*escapeHole = cast(const(int)*)x` |
| 265 |
fail_compilation/retscope6.d(14022): Error: returning scope variable `scopePtr` is not allowed in a `@safe` function |
| 266 |
--- |
| 267 |
*/ |
| 268 |
|
| 269 |
#line 14000 |
| 270 |
// https://issues.dlang.org/show_bug.cgi?id=23021 |
| 271 |
|
| 272 |
ref int infer23021(ref int* x) @safe pure nothrow |
| 273 |
{ |
| 274 |
return *x; |
| 275 |
} |
| 276 |
|
| 277 |
ref int noInfer23021(ref int* x, const(int)** escapeHole = null) @safe pure nothrow |
| 278 |
{ |
| 279 |
*escapeHole = x; |
| 280 |
return *x; |
| 281 |
} |
| 282 |
|
| 283 |
ref int escape23021() @safe |
| 284 |
{ |
| 285 |
scope int* scopePtr; |
| 286 |
int* nonScopePtr = null; |
| 287 |
|
| 288 |
// don't infer scope |
| 289 |
cast(void) noInfer23021(scopePtr); // error |
| 290 |
|
| 291 |
// ensure we infer return scope |
| 292 |
return infer23021(scopePtr); // error |
| 293 |
|
| 294 |
// ensure we do not infer return ref |
| 295 |
return infer23021(nonScopePtr); // no error |
| 296 |
} |
| 297 |
|
| 298 |
/******************************/ |
| 299 |
|
| 300 |
/* TEST_OUTPUT: |
| 301 |
--- |
| 302 |
fail_compilation/retscope6.d(14050): Error: assigning scope variable `z` to non-scope parameter `y` calling `f23294` is not allowed in a `@safe` function |
| 303 |
fail_compilation/retscope6.d(14044): which is not `scope` because of `x = y` |
| 304 |
--- |
| 305 |
*/ |
| 306 |
|
| 307 |
// https://issues.dlang.org/show_bug.cgi?id=23294 |
| 308 |
|
| 309 |
@safe: |
| 310 |
int g23294; |
| 311 |
|
| 312 |
auto f23294(int* x, int* y) |
| 313 |
{ |
| 314 |
x = y; |
| 315 |
g23294++; // make sure it's not inferring scope from pure |
| 316 |
} |
| 317 |
|
| 318 |
void escape23294(scope int* z) |
| 319 |
{ |
| 320 |
f23294(z, z); // passes |
| 321 |
} |