| 1 |
/* REQUIRED_ARGS: -preview=dip1000 |
| 2 |
* TEST_OUTPUT: |
| 3 |
--- |
| 4 |
fail_compilation/test19097.d(44): Error: returning scope variable `s` is not allowed in a `@safe` function |
| 5 |
fail_compilation/test19097.d(48): Error: returning scope variable `s1` is not allowed in a `@safe` function |
| 6 |
fail_compilation/test19097.d(77): Error: assigning scope variable `z` to `ref` variable `refPtr` with longer lifetime is not allowed in a `@safe` function |
| 7 |
fail_compilation/test19097.d(108): Error: returning scope variable `s4` is not allowed in a `@safe` function |
| 8 |
fail_compilation/test19097.d(126): Error: returning scope variable `s5c` is not allowed in a `@safe` function |
| 9 |
fail_compilation/test19097.d(130): Error: returning scope variable `s5m` is not allowed in a `@safe` function |
| 10 |
fail_compilation/test19097.d(147): Error: returning scope variable `s6c` is not allowed in a `@safe` function |
| 11 |
fail_compilation/test19097.d(151): Error: returning scope variable `s6m` is not allowed in a `@safe` function |
| 12 |
--- |
| 13 |
*/ |
| 14 |
|
| 15 |
// Test extended return-scope / return-ref semantics, e.g. assigning to `this` or the first parameter |
| 16 |
|
| 17 |
// https://issues.dlang.org/show_bug.cgi?id=19097 |
| 18 |
|
| 19 |
@safe: |
| 20 |
|
| 21 |
void betty(ref scope int* r, return scope int* p) |
| 22 |
{ |
| 23 |
r = p; |
| 24 |
} |
| 25 |
|
| 26 |
void freddy(out scope int* r, return scope int* p) |
| 27 |
{ |
| 28 |
r = p; |
| 29 |
} |
| 30 |
|
| 31 |
struct S |
| 32 |
{ |
| 33 |
int* a; |
| 34 |
this(return scope int* b) scope { a = b; } |
| 35 |
|
| 36 |
int* c; |
| 37 |
void mem(return scope int* d) scope { c = d; } |
| 38 |
} |
| 39 |
|
| 40 |
S thorin() |
| 41 |
{ |
| 42 |
int i; |
| 43 |
S s = S(&i); // should infer scope for s |
| 44 |
return s; // so this should error |
| 45 |
|
| 46 |
S s1; |
| 47 |
s1.mem(&i); |
| 48 |
return s1; |
| 49 |
} |
| 50 |
|
| 51 |
/************************/ |
| 52 |
|
| 53 |
struct S2(T) |
| 54 |
{ |
| 55 |
int* p; |
| 56 |
|
| 57 |
void silent(lazy void dg); |
| 58 |
|
| 59 |
void foo() |
| 60 |
{ |
| 61 |
char[] name; |
| 62 |
silent(name = parseType()); |
| 63 |
} |
| 64 |
|
| 65 |
char[] parseType(char[] name = null); |
| 66 |
} |
| 67 |
|
| 68 |
S2!int s2; |
| 69 |
|
| 70 |
/************************/ |
| 71 |
struct S3 |
| 72 |
{ |
| 73 |
int* ptr; |
| 74 |
void assign(ref int* refPtr, return scope int* z) scope @safe |
| 75 |
{ |
| 76 |
this.ptr = z; // allowed, first ref |
| 77 |
refPtr = z; // should not be allowed |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
int* escape() @safe |
| 82 |
{ |
| 83 |
int local; |
| 84 |
|
| 85 |
S3 escapeThis; |
| 86 |
int* escapeRef; |
| 87 |
|
| 88 |
escapeThis.assign(escapeRef, &local); |
| 89 |
|
| 90 |
return escapeRef; |
| 91 |
} |
| 92 |
|
| 93 |
/************************/ |
| 94 |
// https://issues.dlang.org/show_bug.cgi?id=22837 |
| 95 |
struct S4 |
| 96 |
{ |
| 97 |
int* p; |
| 98 |
this(int dummy, return scope int* p) @safe |
| 99 |
{ |
| 100 |
this.p = p; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
int* escape2() |
| 105 |
{ |
| 106 |
int x; |
| 107 |
auto s4 = S4(0, &x); |
| 108 |
return s4.p; |
| 109 |
} |
| 110 |
|
| 111 |
/************************/ |
| 112 |
// https://issues.dlang.org/show_bug.cgi?id=22801 |
| 113 |
struct S5 |
| 114 |
{ |
| 115 |
int* a; |
| 116 |
this(return ref int b) { a = &b; } |
| 117 |
|
| 118 |
int* c; |
| 119 |
void mem(return ref int d) scope { c = &d; } |
| 120 |
} |
| 121 |
|
| 122 |
S5 frerin() |
| 123 |
{ |
| 124 |
int i; |
| 125 |
S5 s5c = S5(i); // should infer scope for s |
| 126 |
return s5c; // so this should error |
| 127 |
|
| 128 |
S5 s5m; |
| 129 |
s5m.mem(i); |
| 130 |
return s5m; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
struct S6 |
| 135 |
{ |
| 136 |
int** a; |
| 137 |
this(return ref int* b) { a = &b; } |
| 138 |
|
| 139 |
int** c; |
| 140 |
void mem(return ref int* d) scope { c = &d; } |
| 141 |
} |
| 142 |
|
| 143 |
S6 dis() |
| 144 |
{ |
| 145 |
int* i = null; |
| 146 |
S6 s6c = S6(i); // should infer scope for s |
| 147 |
return s6c; // so this should error |
| 148 |
|
| 149 |
S6 s6m; |
| 150 |
s6m.mem(i); |
| 151 |
return s6m; |
| 152 |
} |