Debian Codegraph @

gcc/testsuite/gdc.test/runnable/test3449.d

raw
1
2 /******************************************/
3 // https://issues.dlang.org/show_bug.cgi?id=3449
4
5 template TypeTuple(T...) { alias TypeTuple = T; }
6
7 // If module variable has no explicit initializer,
8 // constant folding is not allowed for that.
9 int mg1;
10 const int cg1;
11 immutable int ig1;
12 static this()
13 {
14 mg1 = 10;
15 }
16
17 shared static this()
18 {
19 cg1 = 10;
20 ig1 = 10;
21 }
22 static assert(!__traits(compiles, { static assert(mg1 == 0); }));
23 static assert(!__traits(compiles, { static assert(cg1 == 0); }));
24 static assert(!__traits(compiles, { static assert(ig1 == 0); }));
25
26 // But, if module variable has explicit initializer and
27 // non-mutable type, constant folding is allowed for that..
28 int mg2 = 1;
29 const int cg2 = 1;
30 immutable int ig2 = 1;
31 static this()
32 {
33 mg2 = 11;
34 static assert(!__traits(compiles, cg2 = 11)); // not allowed for constant folding
35 static assert(!__traits(compiles, ig2 = 11)); // not allowed for constant folding
36 }
37 static assert(!__traits(compiles, { static assert(mg2 == 1); }));
38 static assert(cg2 == 1); // possible
39 static assert(ig2 == 1); // possible
40
41 // For aggregate fields, compiler behavior will be changed.
42 void test3449()
43 {
44 static struct S(T)
45 {
46 T field1; // doesn't have explicit initializer
47 T field2 = 1; // has explicit initializer
48
49 this(int n)
50 {
51 field1 = n; // allowed
52 field2 = n; // NEW! re-assigning during construction is allowed for any qualified fields.
53 }
54 }
55
56 foreach (T; TypeTuple!(int, const int, immutable int))
57 {
58 alias S!T ST;
59
60 auto s1 = ST(); // default construction
61 assert(s1.field1 == 0); // == T.init
62 assert(s1.field2 == 1); // == specified initializer
63
64 // Getting address for non-mutable field is allowed.
65 T* s1p1 = &s1.field1;
66 T* s1p2 = &s1.field2;
67 assert(*s1p1 == 0);
68 assert(*s1p2 == 1);
69 static if (is(T == int))
70 { // If T is mutable,
71 // modification through indirection is allowed
72 *s1p1 = 100, *s1p2 = 101;
73 assert(*s1p1 == 100);
74 assert(*s1p2 == 101);
75 }
76 else
77 { // If T is not mutable, modification is not allowed
78 static assert(!__traits(compiles, *s1p1 = 100));
79 static assert(!__traits(compiles, *s1p2 = 100));
80 }
81
82 // Access to non-static non-mutable field is
83 // now correctly rejected by "need this" error.
84 static assert(!__traits(compiles, ST.field1 == 1));
85 static assert(!__traits(compiles, ST.field2 == 0));
86
87 // So, re-assignment of non-mutable fields
88 // during construction is enough acceptable.
89 auto s2 = ST(10);
90 assert(s2.field1 == 10);
91 assert(s2.field2 == 10);
92 }
93 }
94
95 /******************************************/
96 // https://issues.dlang.org/show_bug.cgi?id=10643
97
98 struct S10643
99 {
100 const int[1000] x = void;
101
102 this(int n)
103 {
104 x[] = n;
105 }
106 }
107 static assert(S10643.sizeof == int.sizeof * 1000);
108
109 /******************************************/
110
111 int main()
112 {
113 test3449();
114
115 return 0;
116 }