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