Debian Codegraph @

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

raw
1 // PERMUTE_ARGS:
2
3 struct Field
4 {
5 this(this) @safe @nogc pure nothrow {}
6 }
7
8 struct Counter
9 {
10 static size_t cnt;
11 this(this) @safe @nogc nothrow { ++cnt; }
12 }
13
14 struct Foo
15 {
16 this(this) @safe @nogc pure nothrow {}
17 Field field;
18 }
19
20 void test1() @safe @nogc pure nothrow
21 {
22 Foo foo;
23 foo.__xpostblit();
24 }
25
26 static assert(__traits(hasMember, Foo, "__xpostblit"));
27
28 //
29
30 struct FieldPostblit
31 {
32 Counter counter;
33 }
34
35 struct AggrPostblit
36 {
37 static size_t cnt;
38 this(this) @safe @nogc nothrow { ++cnt; }
39 }
40
41 struct MixedPostblit
42 {
43 static size_t cnt;
44 Counter counter;
45 this(this) @safe @nogc nothrow { ++cnt; }
46 }
47
48 struct SNoPostblit {}
49 class CNoPostblit {}
50
51 static assert(!__traits(hasMember, SNoPostblit, "__xpostblit"));
52 static assert(!__traits(hasMember, CNoPostblit, "__xpostblit"));
53
54 void test2() @safe @nogc nothrow
55 {
56 FieldPostblit a;
57 assert(Counter.cnt == 0);
58 a.__xpostblit();
59 assert(Counter.cnt == 1);
60 AggrPostblit b;
61 assert(AggrPostblit.cnt == 0);
62 b.__xpostblit();
63 assert(AggrPostblit.cnt == 1);
64 Counter.cnt = 0;
65 MixedPostblit c;
66 assert(MixedPostblit.cnt == 0);
67 assert(Counter.cnt == 0);
68 c.__xpostblit();
69 assert(MixedPostblit.cnt == 1);
70 assert(Counter.cnt == 1);
71 }
72
73 /****************************************************************
74 This test is intended to verify the exception safety of field
75 postblits
76 */
77 string trace = "";
78
79 struct FieldThrow
80 {
81 string name;
82 this(string n)
83 {
84 name = n;
85 }
86
87 bool throwExcept;
88 this(this)
89 {
90 if (throwExcept)
91 {
92 throw new Exception("");
93 }
94 }
95
96 ~this() { trace ~= name ~ ".dtor"; }
97 }
98
99 struct S
100 {
101 auto f1 = FieldThrow("f1");
102 FieldThrow[2] f2f3= [FieldThrow("f2"), FieldThrow("f3")];
103 auto f4 = FieldThrow("f4");
104 }
105
106 void test3()
107 {
108 trace = "";
109
110 S s1;
111
112 // Cause `s1.f4`'s postblit to throw
113 s1.f4.throwExcept = true;
114
115 try
116 {
117 // `s`'s postblit will be a combination of `f1`, `f2f3`, and `f4`'s
118 // postblit in that order. However, `f4`'s postblit will throw,
119 // causing `s1.f2f3` and `s1.f1`'s destructors to execute in that
120 // order
121 S s2 = s1;
122 }
123 catch(Exception ex){ }
124
125 // Confirm the field destructors were called and were called in the
126 // corrrect order
127 assert(trace == "f3.dtor" ~ "f2.dtor" ~ "f1.dtor");
128 }
129 /****************************************************************************/
130
131 void main()
132 {
133 test1();
134 test2();
135 test3();
136 }