| 1 |
// REQUIRED_ARGS: -o- -d -m64 |
| 2 |
|
| 3 |
struct S1 { int v; } |
| 4 |
struct S2 { int* p; } |
| 5 |
class C { int v; } |
| 6 |
|
| 7 |
#line 6 |
| 8 |
/* |
| 9 |
TEST_OUTPUT: |
| 10 |
--- |
| 11 |
fail_compilation/fail13902.d(45): Error: using the result of a comma expression is not allowed |
| 12 |
fail_compilation/fail13902.d(32): Error: returning `& x` escapes a reference to local variable `x` |
| 13 |
fail_compilation/fail13902.d(33): Error: returning `&s1.v` escapes a reference to local variable `s1` |
| 14 |
fail_compilation/fail13902.d(38): Error: returning `& sa1` escapes a reference to local variable `sa1` |
| 15 |
fail_compilation/fail13902.d(39): Error: returning `& sa2` escapes a reference to local variable `sa2` |
| 16 |
fail_compilation/fail13902.d(40): Error: returning `& x` escapes a reference to local variable `x` |
| 17 |
fail_compilation/fail13902.d(41): Error: returning `(& x + 4)` escapes a reference to local variable `x` |
| 18 |
fail_compilation/fail13902.d(42): Error: returning `& x + cast(long)x * 4L` escapes a reference to local variable `x` |
| 19 |
fail_compilation/fail13902.d(45): Error: returning `& y` escapes a reference to local variable `y` |
| 20 |
--- |
| 21 |
*/ |
| 22 |
int* testEscape1() |
| 23 |
{ |
| 24 |
int x, y; |
| 25 |
int[] da1; |
| 26 |
int[][] da2; |
| 27 |
int[1] sa1; |
| 28 |
int[1][1] sa2; |
| 29 |
int* ptr; |
| 30 |
S1 s1; |
| 31 |
S2 s2; |
| 32 |
C c; |
| 33 |
|
| 34 |
if (0) return &x; // VarExp |
| 35 |
if (0) return &s1.v; // DotVarExp |
| 36 |
if (0) return s2.p; // no error |
| 37 |
if (0) return &c.v; // no error |
| 38 |
if (0) return &da1[0]; // no error |
| 39 |
if (0) return &da2[0][0]; // no error |
| 40 |
if (0) return &sa1[0]; // IndexExp |
| 41 |
if (0) return &sa2[0][0]; // IndexExp |
| 42 |
if (0) return &x; |
| 43 |
if (0) return &x + 1; // optimized to SymOffExp == (& x+4) |
| 44 |
if (0) return &x + x; |
| 45 |
//if (0) return ptr += &x + 1; // semantic error |
| 46 |
if (0) ptr -= &x - &y; // no error |
| 47 |
if (0) return (&x, &y); // CommaExp |
| 48 |
|
| 49 |
return null; // ok |
| 50 |
} |
| 51 |
|
| 52 |
#line 49 |
| 53 |
/* |
| 54 |
TEST_OUTPUT: |
| 55 |
--- |
| 56 |
fail_compilation/fail13902.d(88): Error: using the result of a comma expression is not allowed |
| 57 |
fail_compilation/fail13902.d(75): Error: returning `& x` escapes a reference to parameter `x` |
| 58 |
fail_compilation/fail13902.d(76): Error: returning `&s1.v` escapes a reference to parameter `s1` |
| 59 |
fail_compilation/fail13902.d(81): Error: returning `& sa1` escapes a reference to parameter `sa1` |
| 60 |
fail_compilation/fail13902.d(82): Error: returning `& sa2` escapes a reference to parameter `sa2` |
| 61 |
fail_compilation/fail13902.d(83): Error: returning `& x` escapes a reference to parameter `x` |
| 62 |
fail_compilation/fail13902.d(84): Error: returning `(& x + 4)` escapes a reference to parameter `x` |
| 63 |
fail_compilation/fail13902.d(85): Error: returning `& x + cast(long)x * 4L` escapes a reference to parameter `x` |
| 64 |
fail_compilation/fail13902.d(88): Error: returning `& y` escapes a reference to parameter `y` |
| 65 |
--- |
| 66 |
*/ |
| 67 |
int* testEscape2( |
| 68 |
int x, int y, |
| 69 |
int[] da1, |
| 70 |
int[][] da2, |
| 71 |
int[1] sa1, |
| 72 |
int[1][1] sa2, |
| 73 |
int* ptr, |
| 74 |
S1 s1, |
| 75 |
S2 s2, |
| 76 |
C c, |
| 77 |
) |
| 78 |
{ |
| 79 |
if (0) return &x; // VarExp |
| 80 |
if (0) return &s1.v; // DotVarExp |
| 81 |
if (0) return s2.p; // no error |
| 82 |
if (0) return &c.v; // no error |
| 83 |
if (0) return &da1[0]; // no error |
| 84 |
if (0) return &da2[0][0]; // no error |
| 85 |
if (0) return &sa1[0]; // IndexExp |
| 86 |
if (0) return &sa2[0][0]; // IndexExp |
| 87 |
if (0) return &x; |
| 88 |
if (0) return &x + 1; // optimized to SymOffExp == (& x+4) |
| 89 |
if (0) return &x + x; |
| 90 |
//if (0) return ptr += &x + 1; // semantic error |
| 91 |
if (0) ptr -= &x - &y; // no error |
| 92 |
if (0) return (&x, &y); // CommaExp |
| 93 |
|
| 94 |
return null; // ok |
| 95 |
} |
| 96 |
|
| 97 |
#line 92 |
| 98 |
/* |
| 99 |
TEST_OUTPUT: |
| 100 |
--- |
| 101 |
fail_compilation/fail13902.d(123): Error: using the result of a comma expression is not allowed |
| 102 |
--- |
| 103 |
*/ |
| 104 |
int* testEscape3( |
| 105 |
return ref int x, return ref int y, |
| 106 |
ref int[] da1, |
| 107 |
ref int[][] da2, |
| 108 |
return ref int[1] sa1, |
| 109 |
return ref int[1][1] sa2, |
| 110 |
ref int* ptr, |
| 111 |
return ref S1 s1, |
| 112 |
ref S2 s2, |
| 113 |
ref C c, |
| 114 |
) |
| 115 |
{ |
| 116 |
if (0) return &x; // VarExp |
| 117 |
if (0) return &s1.v; // DotVarExp |
| 118 |
if (0) return s2.p; // no error |
| 119 |
if (0) return &c.v; // no error |
| 120 |
if (0) return &da1[0]; // no error |
| 121 |
if (0) return &da2[0][0]; // no error |
| 122 |
if (0) return &sa1[0]; // IndexExp |
| 123 |
if (0) return &sa2[0][0]; // IndexExp |
| 124 |
if (0) return ptr = &x; |
| 125 |
if (0) return ptr = &x + 1; // optimized to SymOffExp == (& x+4) |
| 126 |
if (0) return ptr = &x + x; |
| 127 |
//if (0) return ptr += &x + 1; // semantic error |
| 128 |
if (0) return ptr -= &x - &y; // no error |
| 129 |
if (0) return (&x, &y); // CommaExp |
| 130 |
|
| 131 |
return null; // ok |
| 132 |
} |
| 133 |
|
| 134 |
/* |
| 135 |
TEST_OUTPUT: |
| 136 |
--- |
| 137 |
fail_compilation/fail13902.d(150): Error: returning `cast(int[])sa1` escapes a reference to parameter `sa1` |
| 138 |
fail_compilation/fail13902.d(151): Error: returning `cast(int[])sa1` escapes a reference to parameter `sa1` |
| 139 |
fail_compilation/fail13902.d(152): Error: returning `sa1[]` escapes a reference to parameter `sa1` |
| 140 |
fail_compilation/fail13902.d(155): Error: returning `cast(int[])sa2` escapes a reference to local variable `sa2` |
| 141 |
fail_compilation/fail13902.d(156): Error: returning `cast(int[])sa2` escapes a reference to local variable `sa2` |
| 142 |
fail_compilation/fail13902.d(157): Error: returning `sa2[]` escapes a reference to local variable `sa2` |
| 143 |
fail_compilation/fail13902.d(161): Error: returning `cast(int[])s.sa` escapes a reference to local variable `s` |
| 144 |
fail_compilation/fail13902.d(162): Error: returning `cast(int[])s.sa` escapes a reference to local variable `s` |
| 145 |
fail_compilation/fail13902.d(163): Error: returning `s.sa[]` escapes a reference to local variable `s` |
| 146 |
fail_compilation/fail13902.d(166): Error: escaping reference to stack allocated value returned by `makeSA()` |
| 147 |
fail_compilation/fail13902.d(167): Error: escaping reference to stack allocated value returned by `makeSA()` |
| 148 |
fail_compilation/fail13902.d(168): Error: escaping reference to stack allocated value returned by `makeSA()` |
| 149 |
fail_compilation/fail13902.d(171): Error: escaping reference to stack allocated value returned by `makeS()` |
| 150 |
fail_compilation/fail13902.d(172): Error: escaping reference to stack allocated value returned by `makeS()` |
| 151 |
fail_compilation/fail13902.d(173): Error: escaping reference to stack allocated value returned by `makeS()` |
| 152 |
--- |
| 153 |
*/ |
| 154 |
int[] testEscape4(int[3] sa1) // https://issues.dlang.org/show_bug.cgi?id=9279 |
| 155 |
{ |
| 156 |
if (0) return sa1; // error <- no error |
| 157 |
if (0) return cast(int[])sa1; // error <- no error |
| 158 |
if (0) return sa1[]; // error |
| 159 |
|
| 160 |
int[3] sa2; |
| 161 |
if (0) return sa2; // error |
| 162 |
if (0) return cast(int[])sa2; // error |
| 163 |
if (0) return sa2[]; // error |
| 164 |
|
| 165 |
struct S { int[3] sa; } |
| 166 |
S s; |
| 167 |
if (0) return s.sa; // error <- no error |
| 168 |
if (0) return cast(int[])s.sa; // error <- no error |
| 169 |
if (0) return s.sa[]; // error |
| 170 |
|
| 171 |
int[3] makeSA() { int[3] ret; return ret; } |
| 172 |
if (0) return makeSA(); // error <- no error |
| 173 |
if (0) return cast(int[])makeSA(); // error <- no error |
| 174 |
if (0) return makeSA()[]; // error <- no error |
| 175 |
|
| 176 |
S makeS() { S s; return s; } |
| 177 |
if (0) return makeS().sa; // error <- no error |
| 178 |
if (0) return cast(int[])makeS().sa; // error <- no error |
| 179 |
if (0) return makeS().sa[]; // error <- no error |
| 180 |
|
| 181 |
return null; |
| 182 |
} |
| 183 |
|
| 184 |
/* |
| 185 |
TEST_OUTPUT: |
| 186 |
--- |
| 187 |
fail_compilation/fail13902.d(201): Error: returning `x` escapes a reference to local variable `x` |
| 188 |
fail_compilation/fail13902.d(202): Error: returning `s1.v` escapes a reference to local variable `s1` |
| 189 |
fail_compilation/fail13902.d(206): Error: returning `sa1[0]` escapes a reference to local variable `sa1` |
| 190 |
fail_compilation/fail13902.d(207): Error: returning `sa2[0][0]` escapes a reference to local variable `sa2` |
| 191 |
fail_compilation/fail13902.d(208): Error: returning `x = 1` escapes a reference to local variable `x` |
| 192 |
fail_compilation/fail13902.d(209): Error: returning `x += 1` escapes a reference to local variable `x` |
| 193 |
fail_compilation/fail13902.d(210): Error: returning `s1.v = 1` escapes a reference to local variable `s1` |
| 194 |
fail_compilation/fail13902.d(211): Error: returning `s1.v += 1` escapes a reference to local variable `s1` |
| 195 |
--- |
| 196 |
*/ |
| 197 |
ref int testEscapeRef1() |
| 198 |
{ |
| 199 |
int x; |
| 200 |
int[] da1; |
| 201 |
int[][] da2; |
| 202 |
int[1] sa1; |
| 203 |
int[1][1] sa2; |
| 204 |
S1 s1; |
| 205 |
C c; |
| 206 |
|
| 207 |
if (0) return x; // VarExp |
| 208 |
if (0) return s1.v; // DotVarExp |
| 209 |
if (0) return c.v; // no error |
| 210 |
if (0) return da1[0]; // no error |
| 211 |
if (0) return da2[0][0]; // no error |
| 212 |
if (0) return sa1[0]; // IndexExp |
| 213 |
if (0) return sa2[0][0]; // IndexExp |
| 214 |
if (0) return x = 1; // AssignExp |
| 215 |
if (0) return x += 1; // BinAssignExp |
| 216 |
if (0) return s1.v = 1; // AssignExp (e1 is DotVarExp) |
| 217 |
if (0) return s1.v += 1; // BinAssignExp (e1 is DotVarExp) |
| 218 |
|
| 219 |
static int g; |
| 220 |
return g; // ok |
| 221 |
} |
| 222 |
|
| 223 |
/* |
| 224 |
TEST_OUTPUT: |
| 225 |
--- |
| 226 |
fail_compilation/fail13902.d(240): Error: returning `x` escapes a reference to parameter `x` |
| 227 |
fail_compilation/fail13902.d(241): Error: returning `s1.v` escapes a reference to parameter `s1` |
| 228 |
fail_compilation/fail13902.d(245): Error: returning `sa1[0]` escapes a reference to parameter `sa1` |
| 229 |
fail_compilation/fail13902.d(246): Error: returning `sa2[0][0]` escapes a reference to parameter `sa2` |
| 230 |
fail_compilation/fail13902.d(247): Error: returning `x = 1` escapes a reference to parameter `x` |
| 231 |
fail_compilation/fail13902.d(248): Error: returning `x += 1` escapes a reference to parameter `x` |
| 232 |
fail_compilation/fail13902.d(249): Error: returning `s1.v = 1` escapes a reference to parameter `s1` |
| 233 |
fail_compilation/fail13902.d(250): Error: returning `s1.v += 1` escapes a reference to parameter `s1` |
| 234 |
--- |
| 235 |
*/ |
| 236 |
ref int testEscapeRef2( |
| 237 |
int x, |
| 238 |
int[] da1, |
| 239 |
int[][] da2, |
| 240 |
int[1] sa1, |
| 241 |
int[1][1] sa2, |
| 242 |
S1 s1, |
| 243 |
C c, |
| 244 |
) |
| 245 |
{ |
| 246 |
if (0) return x; // VarExp |
| 247 |
if (0) return s1.v; // DotVarExp |
| 248 |
if (0) return c.v; // no error |
| 249 |
if (0) return da1[0]; // no error |
| 250 |
if (0) return da2[0][0]; // no error |
| 251 |
if (0) return sa1[0]; // IndexExp |
| 252 |
if (0) return sa2[0][0]; // IndexExp |
| 253 |
if (0) return x = 1; // AssignExp |
| 254 |
if (0) return x += 1; // BinAssignExp |
| 255 |
if (0) return s1.v = 1; // AssignExp (e1 is DotVarExp) |
| 256 |
if (0) return s1.v += 1; // BinAssignExp (e1 is DotVarExp) |
| 257 |
|
| 258 |
static int g; |
| 259 |
return g; // ok |
| 260 |
} |
| 261 |
|
| 262 |
/* |
| 263 |
TEST_OUTPUT: |
| 264 |
--- |
| 265 |
--- |
| 266 |
*/ |
| 267 |
ref int testEscapeRef2( |
| 268 |
return ref int x, |
| 269 |
ref int[] da1, |
| 270 |
ref int[][] da2, |
| 271 |
return ref int[1] sa1, |
| 272 |
return ref int[1][1] sa2, |
| 273 |
return ref S1 s1, |
| 274 |
ref C c, |
| 275 |
) |
| 276 |
{ |
| 277 |
if (0) return x; // VarExp |
| 278 |
if (0) return s1.v; // DotVarExp |
| 279 |
if (0) return c.v; // no error |
| 280 |
if (0) return da1[0]; // no error |
| 281 |
if (0) return da2[0][0]; // no error |
| 282 |
if (0) return sa1[0]; // IndexExp |
| 283 |
if (0) return sa2[0][0]; // IndexExp |
| 284 |
if (0) return x = 1; // AssignExp |
| 285 |
if (0) return x += 1; // BinAssignExp |
| 286 |
if (0) return s1.v = 1; // AssignExp (e1 is DotVarExp) |
| 287 |
if (0) return s1.v += 1; // BinAssignExp (e1 is DotVarExp) |
| 288 |
|
| 289 |
static int g; |
| 290 |
return g; // ok |
| 291 |
} |
| 292 |
|
| 293 |
/* |
| 294 |
TEST_OUTPUT: |
| 295 |
--- |
| 296 |
fail_compilation/fail13902.d(294): Error: returning `[& x]` escapes a reference to local variable `x` |
| 297 |
fail_compilation/fail13902.d(295): Error: returning `[& x]` escapes a reference to local variable `x` |
| 298 |
--- |
| 299 |
*/ |
| 300 |
int*[] testArrayLiteral1() { int x; return [&x]; } |
| 301 |
int*[1] testArrayLiteral2() { int x; return [&x]; } |
| 302 |
|
| 303 |
/* |
| 304 |
TEST_OUTPUT: |
| 305 |
--- |
| 306 |
fail_compilation/fail13902.d(304): Error: returning `S2(& x)` escapes a reference to local variable `x` |
| 307 |
fail_compilation/fail13902.d(305): Error: returning `new S2(& x)` escapes a reference to local variable `x` |
| 308 |
--- |
| 309 |
*/ |
| 310 |
S2 testStructLiteral1() { int x; return S2(&x); } |
| 311 |
S2* testStructLiteral2() { int x; return new S2(&x); } |
| 312 |
|
| 313 |
/* |
| 314 |
TEST_OUTPUT: |
| 315 |
--- |
| 316 |
fail_compilation/fail13902.d(314): Error: returning `sa[]` escapes a reference to local variable `sa` |
| 317 |
fail_compilation/fail13902.d(315): Error: returning `sa[cast(ulong)n..2][1..2]` escapes a reference to local variable `sa` |
| 318 |
--- |
| 319 |
*/ |
| 320 |
int[] testSlice1() { int[3] sa; return sa[]; } |
| 321 |
int[] testSlice2() { int[3] sa; int n; return sa[n..2][1..2]; } |
| 322 |
|
| 323 |
/* |
| 324 |
TEST_OUTPUT: |
| 325 |
--- |
| 326 |
fail_compilation/fail13902.d(324): Error: returning `vda[0]` escapes a reference to variadic parameter `vda` |
| 327 |
fail_compilation/fail13902.d(325): Error: returning `vda[]` escapes a reference to variadic parameter `vda` |
| 328 |
--- |
| 329 |
*/ |
| 330 |
ref int testDynamicArrayVariadic1(int[] vda...) { return vda[0]; } |
| 331 |
@safe int[] testDynamicArrayVariadic2(int[] vda...) { return vda[]; } |
| 332 |
int[3] testDynamicArrayVariadic3(int[] vda...) { return vda[0..3]; } // no error |
| 333 |
|
| 334 |
/* |
| 335 |
TEST_OUTPUT: |
| 336 |
--- |
| 337 |
fail_compilation/fail13902.d(335): Error: returning `vsa[0]` escapes a reference to parameter `vsa` |
| 338 |
fail_compilation/fail13902.d(336): Error: returning `vsa[]` escapes a reference to parameter `vsa` |
| 339 |
--- |
| 340 |
*/ |
| 341 |
ref int testStaticArrayVariadic1(int[3] vsa...) { return vsa[0]; } |
| 342 |
int[] testStaticArrayVariadic2(int[3] vsa...) { return vsa[]; } |
| 343 |
int[3] testStaticArrayVariadic3(int[3] vsa...) { return vsa[0..3]; } // no error |
| 344 |
|
| 345 |
/* |
| 346 |
TEST_OUTPUT: |
| 347 |
--- |
| 348 |
fail_compilation/fail13902.d(355): Error: returning `match(st)` escapes a reference to local variable `st` |
| 349 |
--- |
| 350 |
*/ |
| 351 |
|
| 352 |
// This was reduced from a `static assert(!__traits(compiles, {...}))` test in `std.sumtype` |
| 353 |
// which was asserting that matchers couldn't escape sumtype members. |
| 354 |
// This should give an error even without `@safe` or `-preview=dip1000` |
| 355 |
|
| 356 |
int* match(return ref int i) { return &i; } |
| 357 |
|
| 358 |
int* escape() |
| 359 |
{ |
| 360 |
int st; |
| 361 |
return match(st); |
| 362 |
} |