| 1 |
/* |
| 2 |
REQUIRED_ARGS: -preview=dip1000 |
| 3 |
TEST_OUTPUT: |
| 4 |
--- |
| 5 |
fail_compilation/test15544.d(20): Error: assigning reference to local `this` to non-scope `_del` is not allowed in a `@safe` function |
| 6 |
fail_compilation/test15544.d(22): Error: assigning reference to local `this` to non-scope `_del` is not allowed in a `@safe` function |
| 7 |
--- |
| 8 |
*/ |
| 9 |
|
| 10 |
// https://issues.dlang.org/show_bug.cgi?id=15544 |
| 11 |
|
| 12 |
void delegate() @safe _del; |
| 13 |
|
| 14 |
struct S { |
| 15 |
int x = 42; |
| 16 |
|
| 17 |
@safe void test() |
| 18 |
{ |
| 19 |
void foo() { assert(x == 42); } |
| 20 |
_del = &foo; |
| 21 |
|
| 22 |
_del = { assert(x == 42); }; |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
/* |
| 27 |
TEST_OUTPUT: |
| 28 |
--- |
| 29 |
fail_compilation/test15544.d(46): Error: assigning reference to local `y` to non-scope `dg` is not allowed in a `@safe` function |
| 30 |
--- |
| 31 |
*/ |
| 32 |
|
| 33 |
int delegate() dg; |
| 34 |
|
| 35 |
void testClosure1() |
| 36 |
{ |
| 37 |
int* x; |
| 38 |
int bar() { return *x; } |
| 39 |
dg = &bar; |
| 40 |
} |
| 41 |
|
| 42 |
@safe void testClosure2() |
| 43 |
{ |
| 44 |
scope int* y; |
| 45 |
int bar() { return *y; } |
| 46 |
dg = &bar; // Error |
| 47 |
auto dg2 = &bar; |
| 48 |
} |