Debian Codegraph @

gcc/testsuite/gdc.test/fail_compilation/dtor_attributes.d

raw
1 /*
2 Informative error messages if the compiler generated destructor overrides a user-defined one.
3
4 TEST_OUTPUT:
5 ---
6 fail_compilation/dtor_attributes.d(118): Error: `pure` function `dtor_attributes.test1` cannot call impure destructor `dtor_attributes.Strict.~this`
7 fail_compilation/dtor_attributes.d(113): generated `Strict.~this` is impure because of the following field's destructors:
8 fail_compilation/dtor_attributes.d(111): - HasDtor member
9 fail_compilation/dtor_attributes.d(103): impure `HasDtor.~this` is declared here
10 fail_compilation/dtor_attributes.d(118): Error: `@safe` function `dtor_attributes.test1` cannot call `@system` destructor `dtor_attributes.Strict.~this`
11 fail_compilation/dtor_attributes.d(113): `dtor_attributes.Strict.~this` is declared here
12 fail_compilation/dtor_attributes.d(113): generated `Strict.~this` is @system because of the following field's destructors:
13 fail_compilation/dtor_attributes.d(111): - HasDtor member
14 fail_compilation/dtor_attributes.d(103): @system `HasDtor.~this` is declared here
15 fail_compilation/dtor_attributes.d(118): Error: `@nogc` function `dtor_attributes.test1` cannot call non-@nogc destructor `dtor_attributes.Strict.~this`
16 fail_compilation/dtor_attributes.d(113): generated `Strict.~this` is non-@nogc because of the following field's destructors:
17 fail_compilation/dtor_attributes.d(111): - HasDtor member
18 fail_compilation/dtor_attributes.d(103): non-@nogc `HasDtor.~this` is declared here
19 fail_compilation/dtor_attributes.d(118): Error: destructor `dtor_attributes.Strict.~this` is not `nothrow`
20 fail_compilation/dtor_attributes.d(113): generated `Strict.~this` is not nothrow because of the following field's destructors:
21 fail_compilation/dtor_attributes.d(111): - HasDtor member
22 fail_compilation/dtor_attributes.d(103): not nothrow `HasDtor.~this` is declared here
23 fail_compilation/dtor_attributes.d(116): Error: function `dtor_attributes.test1` may throw but is marked as `nothrow`
24 ---
25 */
26 #line 100
27
28 struct HasDtor
29 {
30 ~this() {}
31 }
32
33 // The user-defined dtor is overridden by a generated dtor calling both
34 // - HasDtor.~this
35 // - Strict.~this
36 struct Strict
37 {
38 HasDtor member;
39
40 ~this() pure nothrow @nogc @safe {}
41 }
42
43 void test1() pure nothrow @nogc @safe
44 {
45 Strict s;
46 }
47
48 /*
49 Works for clases as well.
50
51 TEST_OUTPUT:
52 ---
53 fail_compilation/dtor_attributes.d(209): Error: `pure` function `dtor_attributes.test2` cannot call impure destructor `dtor_attributes.StrictClass.~this`
54 fail_compilation/dtor_attributes.d(204): generated `StrictClass.~this` is impure because of the following field's destructors:
55 fail_compilation/dtor_attributes.d(203): - HasDtor member
56 fail_compilation/dtor_attributes.d(103): impure `HasDtor.~this` is declared here
57 ---
58 */
59 #line 200
60
61 class StrictClass
62 {
63 HasDtor member;
64 ~this() pure {}
65 }
66
67 void test2() pure
68 {
69 scope instance = new StrictClass();
70 }
71
72 /*
73 Ignores members whose destructors are not called.
74
75 TEST_OUTPUT:
76 ---
77 fail_compilation/dtor_attributes.d(321): Error: `pure` function `dtor_attributes.test3` cannot call impure destructor `dtor_attributes.StrictStructRef.~this`
78 fail_compilation/dtor_attributes.d(316): generated `StrictStructRef.~this` is impure because of the following field's destructors:
79 fail_compilation/dtor_attributes.d(310): - HasDtor structMember
80 fail_compilation/dtor_attributes.d(103): impure `HasDtor.~this` is declared here
81 ---
82 */
83 #line 300
84
85 class HasDtorClass
86 {
87 ~this() {}
88 }
89
90 struct Empty {}
91
92 struct StrictStructRef
93 {
94 HasDtor structMember;
95 HasDtorClass classMember;
96 int intMember;
97 int[2] arrayMember;
98 Empty e;
99
100 ~this() pure {}
101 }
102
103 void test3() pure
104 {
105 StrictStructRef structInstance;
106 }
107
108 /*
109 Types from nested types work as well.
110
111 TEST_OUTPUT:
112 ---
113 fail_compilation/dtor_attributes.d(411): Error: `pure` function `dtor_attributes.test4` cannot call impure destructor `dtor_attributes.StrictNested.~this`
114 fail_compilation/dtor_attributes.d(406): generated `StrictNested.~this` is impure because of the following field's destructors:
115 fail_compilation/dtor_attributes.d(403): - HasDtor[4] arrayMember
116 fail_compilation/dtor_attributes.d(103): impure `HasDtor.~this` is declared here
117 ---
118 */
119 #line 400
120
121 struct StrictNested
122 {
123 HasDtor[4] arrayMember;
124 HasDtorClass[4] classMember;
125
126 ~this() pure {}
127 }
128
129 void test4() pure
130 {
131 StrictNested structInstance;
132 }
133
134 /*
135 Ignores member destructors when the user-defined one is permissive enough (e.g. both impure)
136
137 TEST_OUTPUT:
138 ---
139 fail_compilation/dtor_attributes.d(509): Error: `pure` function `dtor_attributes.test5` cannot call impure destructor `dtor_attributes.Permissive.~this`
140 ---
141 */
142 #line 500
143
144 struct Permissive
145 {
146 HasDtor[4] arrayMember;
147 ~this() {}
148 }
149
150 void test5() pure
151 {
152 Permissive structInstance;
153 }
154
155 /*
156 Works with destructors generated through multiple layers
157
158 TEST_OUTPUT:
159 ---
160 fail_compilation/dtor_attributes.d(618): Error: `pure` function `dtor_attributes.test6` cannot call impure destructor `dtor_attributes.HasNestedDtor3.~this`
161 fail_compilation/dtor_attributes.d(611): generated `HasNestedDtor3.~this` is impure because of the following field's destructors:
162 fail_compilation/dtor_attributes.d(613): - HasNestedDtor2 member3
163 fail_compilation/dtor_attributes.d(606): generated `HasNestedDtor2.~this` is impure because of the following field's destructors:
164 fail_compilation/dtor_attributes.d(608): - HasNestedDtor1 member2
165 fail_compilation/dtor_attributes.d(601): generated `HasNestedDtor1.~this` is impure because of the following field's destructors:
166 fail_compilation/dtor_attributes.d(603): - HasDtor member1
167 fail_compilation/dtor_attributes.d(103): impure `HasDtor.~this` is declared here
168 ---
169 */
170 #line 600
171
172 struct HasNestedDtor1
173 {
174 HasDtor member1;
175 }
176
177 struct HasNestedDtor2
178 {
179 HasNestedDtor1 member2;
180 }
181
182 struct HasNestedDtor3
183 {
184 HasNestedDtor2 member3;
185 }
186
187 void test6() pure
188 {
189 HasNestedDtor3 instance;
190 }