| 1 |
/* |
| 2 |
TEST_OUTPUT: |
| 3 |
--- |
| 4 |
fail_compilation/test13537.d(31): Error: modifying field `U.y` which overlaps with fields with other storage classes is not allowed in a `@safe` function |
| 5 |
fail_compilation/test13537.d(32): Error: modifying field `U.y` which overlaps with fields with other storage classes is not allowed in a `@safe` function |
| 6 |
fail_compilation/test13537.d(33): Error: accessing overlapped field `U.z` with pointers is not allowed in a `@safe` function |
| 7 |
fail_compilation/test13537.d(34): Error: modifying field `U.y` which overlaps with fields with other storage classes is not allowed in a `@safe` function |
| 8 |
--- |
| 9 |
*/ |
| 10 |
|
| 11 |
// https://issues.dlang.org/show_bug.cgi?id=13537 |
| 12 |
|
| 13 |
union U |
| 14 |
{ |
| 15 |
immutable int x; |
| 16 |
int y; |
| 17 |
int* z; |
| 18 |
} |
| 19 |
|
| 20 |
union V |
| 21 |
{ |
| 22 |
immutable int x; |
| 23 |
const int y; |
| 24 |
} |
| 25 |
|
| 26 |
void fun() @safe |
| 27 |
{ |
| 28 |
U u; |
| 29 |
|
| 30 |
// errors |
| 31 |
u.y = 1; |
| 32 |
int* p = &u.y; |
| 33 |
int** q = &u.z; |
| 34 |
abc(u.y); |
| 35 |
|
| 36 |
// read access is allowed |
| 37 |
int a = u.x; |
| 38 |
a = u.y; |
| 39 |
def(u.y); |
| 40 |
|
| 41 |
// Overlapping const/immutable is allowed |
| 42 |
auto v = V(1); |
| 43 |
assert(v.y == 1); |
| 44 |
} |
| 45 |
|
| 46 |
void gun() @system |
| 47 |
{ |
| 48 |
U u; |
| 49 |
|
| 50 |
// allowed because system code |
| 51 |
u.y = 1; |
| 52 |
int* p = &u.y; |
| 53 |
int** q = &u.z; |
| 54 |
abc(u.y); |
| 55 |
} |
| 56 |
|
| 57 |
@safe: |
| 58 |
void abc(ref int x) { } |
| 59 |
void def(const ref int x) { } |