| 1 |
/* |
| 2 |
REQUIRED_ARGS: -preview=dip1000 |
| 3 |
TEST_OUTPUT: |
| 4 |
--- |
| 5 |
fail_compilation/test20245.d(21): Error: assigning reference to local variable `x` to non-scope parameter `ptr` calling `escape` is not allowed in a `@safe` function |
| 6 |
fail_compilation/test20245.d(22): Error: escaping a reference to parameter `x` by copying `&x` into allocated memory is not allowed in a `@safe` function |
| 7 |
fail_compilation/test20245.d(23): Error: returning scope variable `a` is not allowed in a `@safe` function |
| 8 |
fail_compilation/test20245.d(27): Error: taking address of `scope` variable `x` with pointers is not allowed in a `@safe` function |
| 9 |
fail_compilation/test20245.d(33): Error: assigning reference to local variable `x` to non-scope parameter `ptr` calling `escape` is not allowed in a `@safe` function |
| 10 |
fail_compilation/test20245.d(34): Error: escaping a reference to parameter `x` by copying `&x` into allocated memory is not allowed in a `@safe` function |
| 11 |
fail_compilation/test20245.d(50): Error: assigning reference to local variable `price` to non-scope `this.minPrice` is not allowed in a `@safe` function |
| 12 |
fail_compilation/test20245.d(69): Error: reference to local variable `this.content[]` calling non-scope member function `Exception.this()` is not allowed in a `@safe` function |
| 13 |
fail_compilation/test20245.d(89): Error: assigning reference to local variable `this` to non-scope parameter `content` calling `listUp` is not allowed in a `@safe` function |
| 14 |
fail_compilation/test20245.d(82): which is not `scope` because of `charPtr = content` |
| 15 |
--- |
| 16 |
*/ |
| 17 |
|
| 18 |
// https://issues.dlang.org/show_bug.cgi?id=20245 |
| 19 |
@safe int* foo(ref int x) { |
| 20 |
int* a = &x; |
| 21 |
escape(&x); |
| 22 |
auto b = [&x]; |
| 23 |
return a; |
| 24 |
} |
| 25 |
|
| 26 |
@safe int** foo(ref scope int* x) { |
| 27 |
int** a = &x; |
| 28 |
return a; |
| 29 |
} |
| 30 |
|
| 31 |
@safe int* foo(return ref int x) { |
| 32 |
int* a = &x; |
| 33 |
escape(&x); |
| 34 |
auto b = [&x]; |
| 35 |
return a; |
| 36 |
} |
| 37 |
|
| 38 |
int* gPtr; |
| 39 |
@safe void escape(int* ptr) |
| 40 |
{ |
| 41 |
gPtr = ptr; |
| 42 |
} |
| 43 |
|
| 44 |
// https://issues.dlang.org/show_bug.cgi?id=21212 |
| 45 |
class MinPointerRecorder |
| 46 |
{ |
| 47 |
int* minPrice; |
| 48 |
void update(ref int price) @safe |
| 49 |
{ |
| 50 |
minPrice = &price; // Should not compile. |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
void main() @safe |
| 55 |
{ |
| 56 |
auto r = new MinPointerRecorder; |
| 57 |
() { int mp = 42; r.update(mp); } (); |
| 58 |
() { ulong[1000] stomp = 13; } (); |
| 59 |
auto x = *r.minPrice; // "13" |
| 60 |
} |
| 61 |
|
| 62 |
// https://issues.dlang.org/show_bug.cgi?id=22782 |
| 63 |
struct DontDoThis |
| 64 |
{ |
| 65 |
immutable char[12] content; |
| 66 |
@safe this(char ch) |
| 67 |
{ |
| 68 |
content[] = ch; |
| 69 |
throw new Exception(content[]); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
void main1() @safe |
| 74 |
{ |
| 75 |
DontDoThis('a'); |
| 76 |
} |
| 77 |
|
| 78 |
// https://issues.dlang.org/show_bug.cgi?id=22783 |
| 79 |
const(char)* charPtr; |
| 80 |
|
| 81 |
// argument is not, or should not be scope |
| 82 |
auto listUp(const(char)* content) {charPtr = content;} |
| 83 |
|
| 84 |
struct DontDoThis2 |
| 85 |
{ |
| 86 |
char content; |
| 87 |
@safe escape() |
| 88 |
{ |
| 89 |
listUp(&content); |
| 90 |
} |
| 91 |
} |