| 1 |
/* REQUIRED_ARGS: -preview=dip1000 |
| 2 |
TEST_OUTPUT: |
| 3 |
--- |
| 4 |
fail_compilation/test14238.d(22): Error: scope parameter `fn` may not be returned |
| 5 |
fail_compilation/test14238.d(25): Error: function `test14238.bar` is `@nogc` yet allocates closure for `bar()` with the GC |
| 6 |
fail_compilation/test14238.d(27): function `baz` closes over variable `x` |
| 7 |
fail_compilation/test14238.d(26): `x` declared here |
| 8 |
--- |
| 9 |
*/ |
| 10 |
// https://issues.dlang.org/show_bug.cgi?id=14238 |
| 11 |
|
| 12 |
@safe: |
| 13 |
|
| 14 |
alias Fn = ref int delegate() return @nogc; |
| 15 |
|
| 16 |
ref int call(return scope Fn fn) @nogc |
| 17 |
{ |
| 18 |
return fn(); // Ok |
| 19 |
} |
| 20 |
|
| 21 |
ref int foo2(scope Fn fn) { |
| 22 |
return fn(); // Error |
| 23 |
} |
| 24 |
|
| 25 |
ref int bar() @nogc { |
| 26 |
int x; |
| 27 |
ref int baz() { |
| 28 |
return x; |
| 29 |
} |
| 30 |
|
| 31 |
if (x == 0) |
| 32 |
return call(&baz); |
| 33 |
else |
| 34 |
return (&baz)(); |
| 35 |
} |