Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix Issue 15544 - Escaping fields to a heap delegate must be disallow…
…ed in @safe code
- Loading branch information
1 parent
b487aff
commit e4428f3
Showing
3 changed files
with
137 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
REQUIRED_ARGS: | ||
PERMUTE_ARGS: | ||
TEST_OUTPUT: | ||
--- | ||
fail_compilation/test15544.d(21): Error: reference to local this assigned to non-scope _del in @safe code | ||
fail_compilation/test15544.d(23): Error: reference to local this assigned to non-scope _del in @safe code | ||
--- | ||
*/ | ||
|
||
// https://issues.dlang.org/show_bug.cgi?id=15544 | ||
|
||
void delegate() @safe _del; | ||
|
||
struct S { | ||
int x = 42; | ||
|
||
@safe void test() | ||
{ | ||
void foo() { assert(x == 42); } | ||
_del = &foo; | ||
|
||
_del = { assert(x == 42); }; | ||
} | ||
} | ||
|
||
/* | ||
TEST_OUTPUT: | ||
--- | ||
fail_compilation/test15544.d(47): Error: reference to local y assigned to non-scope dg in @safe code | ||
--- | ||
*/ | ||
|
||
int delegate() dg; | ||
|
||
void testClosure1() | ||
{ | ||
int* x; | ||
int bar() { return *x; } | ||
dg = &bar; | ||
} | ||
|
||
@safe void testClosure2() | ||
{ | ||
scope int* y; | ||
int bar() { return *y; } | ||
dg = &bar; // Error | ||
auto dg2 = &bar; | ||
} | ||
|
||
|