1 // PERMUTE_ARGS:
2 
3 extern(C) int printf(const char*, ...);
4 
5 class Foo : Object
6 {
test()7     void test() { }
8 
invariant()9     invariant()
10     {
11         printf("in invariant %p\n", this);
12     }
13 }
14 
testinvariant()15 int testinvariant()
16 {
17     printf("hello\n");
18     Foo f = new Foo();
19     printf("f = %p\n", f);
20     printf("f.sizeof = x%x\n", Foo.sizeof);
21     printf("f.classinfo = %p\n", f.classinfo);
22     printf("f.classinfo._invariant = %p\n", f.classinfo.base);
23     f.test();
24     printf("world\n");
25     return 0;
26 }
27 
28 /***************************************************/
29 // 6453
30 
test6453()31 void test6453()
32 {
33     static class C
34     {
35         static uint called;
36         invariant() { called += 1; }
37         invariant() { called += 4; }
38         invariant() { called += 16; }
39 
40         void publicMember() { assert(called == 21); }
41     }
42 
43     static struct S
44     {
45         static uint called;
46         invariant() { called += 1; }
47         invariant() { called += 4; }
48         invariant() { called += 16; }
49 
50         void publicMember() { assert(called == 21); }
51     }
52 
53     auto c = new C();
54     C.called = 0;
55     c.publicMember();
56     assert(C.called == 42);
57 
58     auto s = new S();
59     S.called = 0;
60     s.publicMember();
61     assert(S.called == 42);
62 
63     // Defined symbols in one invariant cannot be seen from others.
64     static struct S6453
65     {
66         invariant()
67         {
68             struct S {}
69             int x;
70             static assert(!__traits(compiles, y));
71             static assert(!__traits(compiles, z));
72         }
73         invariant()
74         {
75             struct S {}
76             int y;
77             static assert(!__traits(compiles, x));
78             static assert(!__traits(compiles, z));
79         }
80         invariant()
81         {
82             struct S {}
83             int z;
84             static assert(!__traits(compiles, x));
85             static assert(!__traits(compiles, y));
86         }
87     }
88 
89     static struct S6453a
90     {
91         pure    invariant() {}
92         nothrow invariant() {}
93         @safe   invariant() {}
94     }
95     static struct S6453b
96     {
97         pure    shared invariant() {}
98         nothrow shared invariant() {}
99         @safe   shared invariant() {}
100     }
101     static class C6453c
102     {
103         pure    synchronized invariant() {}
104         nothrow synchronized invariant() {}
105         @safe   synchronized invariant() {}
106     }
107 }
108 
109 /***************************************************/
110 // 13113
111 
112 struct S13113
113 {
114     static int count;
invariantS13113115     invariant() // impure, throwable, system, and gc-able
116     {
117         ++count;    // impure
118     }
119 
thisS13113120     this(int) pure nothrow @safe @nogc {}
121     // post invaiant is called directly but doesn't interfere with constructor attributes
122 
~thisS13113123     ~this() pure nothrow @safe @nogc {}
124     // pre invaiant is called directly but doesn't interfere with destructor attributes
125 
fooS13113126     void foo() pure nothrow @safe @nogc {}
127     // pre & post invariant calls don't interfere with method attributes
128 }
129 
test13113()130 void test13113()
131 {
132     assert(S13113.count == 0);
133     {
134         auto s = S13113(1);
135         assert(S13113.count == 1);
136         s.foo();
137         assert(S13113.count == 3);
138     }
139     assert(S13113.count == 4);
140 }
141 
142 /***************************************************/
143 // 13147
144 
145 version (D_InlineAsm_X86)
146     enum x86iasm = true;
147 else version (D_InlineAsm_X86_64)
148     enum x86iasm = true;
149 else
150     enum x86iasm = false;
151 
152 class C13147
153 {
test()154     extern (C++) C13147 test()
155     {
156         static if (x86iasm)
157             asm { naked; ret; }
158         return this;
159     }
160 }
161 
162 struct S13147
163 {
testS13147164     void test()
165     {
166         static if (x86iasm)
167             asm { naked; ret; }
168     }
169 }
170 
test13147()171 void test13147()
172 {
173     auto c = new C13147();
174     c.test();
175     S13147 s;
176     s.test();
177 }
178 
179 
180 /***************************************************/
181 
main()182 void main()
183 {
184     testinvariant();
185     test6453();
186     test13113();
187     test13147();
188 }
189