MATCH: Improve `A CMP 0 ? A : -A` set of patterns to use bitwise_equal_p.
[official-gcc.git] / gcc / testsuite / gdc.test / fail_compilation / dtor_attributes.d
blob02d95586eaefeab751d9b9a7c984a125e1462af4
1 /*
2 Informative error messages if the compiler generated destructor overrides a user-defined one.
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 ---
26 #line 100
28 struct HasDtor
30 ~this() {}
33 // The user-defined dtor is overridden by a generated dtor calling both
34 // - HasDtor.~this
35 // - Strict.~this
36 struct Strict
38 HasDtor member;
40 ~this() pure nothrow @nogc @safe {}
43 void test1() pure nothrow @nogc @safe
45 Strict s;
49 Works for clases as well.
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 ---
59 #line 200
61 class StrictClass
63 HasDtor member;
64 ~this() pure {}
67 void test2() pure
69 scope instance = new StrictClass();
73 Ignores members whose destructors are not called.
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 ---
83 #line 300
85 class HasDtorClass
87 ~this() {}
90 struct Empty {}
92 struct StrictStructRef
94 HasDtor structMember;
95 HasDtorClass classMember;
96 int intMember;
97 int[2] arrayMember;
98 Empty e;
100 ~this() pure {}
103 void test3() pure
105 StrictStructRef structInstance;
109 Types from nested types work as well.
111 TEST_OUTPUT:
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
119 #line 400
121 struct StrictNested
123 HasDtor[4] arrayMember;
124 HasDtorClass[4] classMember;
126 ~this() pure {}
129 void test4() pure
131 StrictNested structInstance;
135 Ignores member destructors when the user-defined one is permissive enough (e.g. both impure)
137 TEST_OUTPUT:
139 fail_compilation/dtor_attributes.d(509): Error: `pure` function `dtor_attributes.test5` cannot call impure destructor `dtor_attributes.Permissive.~this`
142 #line 500
144 struct Permissive
146 HasDtor[4] arrayMember;
147 ~this() {}
150 void test5() pure
152 Permissive structInstance;
156 Works with destructors generated through multiple layers
158 TEST_OUTPUT:
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
170 #line 600
172 struct HasNestedDtor1
174 HasDtor member1;
177 struct HasNestedDtor2
179 HasNestedDtor1 member2;
182 struct HasNestedDtor3
184 HasNestedDtor2 member3;
187 void test6() pure
189 HasNestedDtor3 instance;