Debian Codegraph @

gcc/testsuite/gdc.test/compilable/traits.d

raw
1 // REQUIRED_ARGS: -extern-std=c++98
2 // EXTRA_FILES: imports/plainpackage/plainmodule.d imports/pkgmodule/package.d imports/pkgmodule/plainmodule.d
3
4 // This file is intended to contain all compilable traits-related tests in an
5 // effort to keep the number of files in the `compilable` folder to a minimum.
6
7 // https://issues.dlang.org/show_bug.cgi?id=19152
8 module traits;
9
10 class C19152
11 {
12 int OnExecute()
13 {
14 auto name = __traits(getOverloads, this, "OnExecute").stringof;
15 return 0;
16 }
17 }
18
19 static assert(is(typeof(__traits(getTargetInfo, "cppRuntimeLibrary")) == string));
20 version (CppRuntime_Microsoft)
21 {
22 static assert(__traits(getTargetInfo, "cppRuntimeLibrary") == "libcmt" ||
23 __traits(getTargetInfo, "cppRuntimeLibrary")[0..6] == "msvcrt"); // includes mingw import libs
24 }
25
26 version (D_HardFloat)
27 static assert(__traits(getTargetInfo, "floatAbi") == "hard");
28
29 version (Win64)
30 static assert(__traits(getTargetInfo, "objectFormat") == "coff");
31 version (OSX)
32 static assert(__traits(getTargetInfo, "objectFormat") == "macho");
33 version (linux)
34 static assert(__traits(getTargetInfo, "objectFormat") == "elf");
35
36 static assert(__traits(getTargetInfo, "cppStd") == 199711);
37
38 import imports.plainpackage.plainmodule;
39 import imports.pkgmodule.plainmodule;
40
41 #line 40
42 struct MyStruct;
43
44 alias a = imports.plainpackage;
45 alias b = imports.pkgmodule.plainmodule;
46
47 static assert(__traits(isPackage, imports.plainpackage));
48 static assert(__traits(isPackage, a));
49 static assert(!__traits(isPackage, imports.plainpackage.plainmodule));
50 static assert(!__traits(isPackage, b));
51 static assert(__traits(isPackage, imports.pkgmodule));
52 static assert(!__traits(isPackage, MyStruct));
53
54 static assert(!__traits(isModule, imports.plainpackage));
55 static assert(!__traits(isModule, a));
56 static assert(__traits(isModule, imports.plainpackage.plainmodule));
57 static assert(__traits(isModule, b));
58 // This is supposed to work even though we haven't directly imported imports.pkgmodule.
59 static assert(__traits(isModule, imports.pkgmodule));
60 static assert(!__traits(isModule, MyStruct));
61
62 /******************************************/
63 // https://issues.dlang.org/show_bug.cgi?id=19942
64
65 static assert(!__traits(compiles, { a.init; }));
66 static assert(!__traits(compiles, { import m : a; a.init; }));
67
68 version(Windows)
69 static assert(__traits(getLocation, MyStruct)[0] == `compilable\traits.d`);
70 else
71 static assert(__traits(getLocation, MyStruct)[0] == "compilable/traits.d");
72 static assert(__traits(getLocation, MyStruct)[1] == 40);
73 static assert(__traits(getLocation, MyStruct)[2] == 1);
74
75 int foo();
76 int foo(int);
77
78 static assert(__traits(getLocation, __traits(getOverloads, traits, "foo")[1])[1] == 74);
79
80 mixin("int bar;");
81 static assert(__traits(getLocation, bar)[1] == 78);
82
83 struct Outer
84 {
85 struct Nested{}
86
87 void method() {}
88 }
89 static assert(__traits(getLocation, Outer.Nested)[1] == 83);
90 static assert(__traits(getLocation, Outer.method)[1] == 85);
91
92 /******************************************/
93 // https://issues.dlang.org/show_bug.cgi?id=19902
94 // Define hasElaborateCopyConstructor trait
95 // but done as two independent traits per conversation
96 // in https://github.com/dlang/dmd/pull/10265
97
98 struct S
99 {
100 this (ref S rhs) {}
101 }
102
103 struct OuterS
104 {
105 struct S
106 {
107 this (ref S rhs) {}
108 }
109
110 S s;
111 }
112
113 void foo(T)()
114 {
115 struct S(U)
116 {
117 this (ref S rhs) {}
118 }
119 static assert (__traits(hasCopyConstructor, S!int));
120 }
121
122 struct U(T)
123 {
124 this (ref U rhs) {}
125 }
126
127 struct SPostblit
128 {
129 this(this) {}
130 }
131
132 struct DisabledPostblit
133 {
134 @disable this(this);
135 }
136
137 struct NoCpCtor { }
138 class C19902 { }
139
140 struct MoveCtor { this(MoveCtor) { } }
141
142 static assert(__traits(hasCopyConstructor, S));
143 static assert(__traits(hasCopyConstructor, OuterS.S));
144 static assert(__traits(hasCopyConstructor, OuterS));
145 static assert(__traits(compiles, foo!int));
146 static assert(__traits(compiles, foo!S));
147 static assert(__traits(hasCopyConstructor, U!int));
148 static assert(__traits(hasCopyConstructor, U!S));
149 static assert(!__traits(hasPostblit, U!S));
150 static assert(__traits(hasPostblit, SPostblit));
151 static assert(!__traits(hasCopyConstructor, SPostblit));
152 static assert(__traits(hasMoveConstructor, MoveCtor));
153 static assert(!__traits(hasMoveConstructor, NoCpCtor));
154
155 static assert(!__traits(hasCopyConstructor, NoCpCtor));
156 static assert(!__traits(hasCopyConstructor, C19902));
157 static assert(!__traits(hasCopyConstructor, int));
158 static assert(!__traits(hasPostblit, NoCpCtor));
159 static assert(!__traits(hasPostblit, C19902));
160 static assert(!__traits(hasPostblit, int));
161
162 static assert(__traits(isCopyable, int));
163 static assert(!__traits(isCopyable, DisabledPostblit));
164 struct S1 {} // Fine. Can be copied
165 struct S2 { this(this) {} } // Fine. Can be copied
166 struct S3 { @disable this(this); } // Not fine. Copying is disabled.
167 struct S4 { S3 s; } // Not fine. A field has copying disabled.
168 class C1 {}
169 static assert( __traits(isCopyable, S1));
170 static assert( __traits(isCopyable, S2));
171 static assert(!__traits(isCopyable, S3));
172 static assert(!__traits(isCopyable, S4));
173 static assert(__traits(isCopyable, C1));
174 static assert(__traits(isCopyable, int));
175 static assert(__traits(isCopyable, int[]));
176
177 enum E1 : S1 { a = S1(), }
178 enum E2 : S2 { a = S2(), }
179 enum E3 : S3 { a = S3(), }
180 enum E4 : S4 { a = S4(), }
181
182 static assert(__traits(isCopyable, E1));
183 static assert(__traits(isCopyable, E2));
184 static assert(!__traits(isCopyable, E3));
185 static assert(!__traits(isCopyable, E4));
186
187 struct S5
188 {
189 @disable this(ref S5);
190 }
191 static assert(!__traits(isCopyable, S5));
192
193 /******************************************/
194 // https://issues.dlang.org/show_bug.cgi?id=20884
195
196 struct S20884
197 {
198 int x;
199 }
200
201 alias T20884 = immutable(S20884);
202 enum m20884 = "x";
203
204 static assert(is(typeof(__traits(getMember, T20884, m20884)) == immutable(int))); // OK now
205 static assert(is( typeof(mixin("T20884." ~ m20884)) == immutable(int)));
206 static assert(is( typeof(T20884.x) == immutable(int)));
207
208 /******************************************/
209 // https://issues.dlang.org/show_bug.cgi?id=20761
210
211 alias Seq(T...) = T;
212
213 static assert(__traits(isSame, Seq!(1, 2), Seq!(1, 2)));
214 static assert(!__traits(isSame, Seq!(1, 1), Seq!(2, 2)));
215 static assert(!__traits(isSame, Seq!(1, 1, 2), Seq!(1, 1)));
216 static assert(!__traits(isSame, Seq!(1, 1), Seq!(1, 1, 2)));
217
218 static assert(__traits(isSame,
219 Seq!(string, wstring),
220 Seq!(immutable(char)[], immutable(wchar)[]))
221 );
222
223 static assert(__traits(isSame,
224 Seq!(i => i.value, (a, b) => a + b),
225 Seq!(a => a.value, (x, y) => x + y)
226 ));
227
228 static assert(__traits(isSame,
229 Seq!(float, Seq!(double, Seq!real)),
230 Seq!(Seq!(Seq!float, double), real)
231 ));
232
233 static assert(!__traits(isSame,
234 Seq!(int, Seq!(a => a + a)),
235 Seq!(int, Seq!(a => a * a))
236 ));
237
238 // Do these out of order to ensure there are no forward refencing bugs
239
240 extern(C++, __traits(getCppNamespaces,GetNamespaceTest1)) struct GetNamespaceTest4 {}
241 static assert (__traits(getCppNamespaces,GetNamespaceTest1) ==
242 __traits(getCppNamespaces,GetNamespaceTest4));
243
244 extern(C++, "ns") struct GetNamespaceTest1 {}
245 extern(C++, "multiple", "namespaces") struct GetNamespaceTest2 {}
246 extern(C++, mixin("Seq!(`ns`, `nt`)")) struct GetNamespaceTest3 {}
247 static assert(__traits(getCppNamespaces,GetNamespaceTest1)[0] == "ns");
248 static assert(__traits(getCppNamespaces,GetNamespaceTest2) == Seq!("multiple","namespaces"));
249 static assert(__traits(getCppNamespaces,GetNamespaceTest3) == Seq!("ns", "nt"));
250
251 extern(C++, __traits(getCppNamespaces,GetNamespaceTest5)) struct GetNamespaceTest8 {}
252 static assert (__traits(getCppNamespaces,GetNamespaceTest5) ==
253 __traits(getCppNamespaces,GetNamespaceTest8));
254
255 extern(C++, ns) struct GetNamespaceTest5 {}
256 extern(C++, multiple) extern(C++, namespaces) struct GetNamespaceTest6 {}
257 static assert(__traits(getCppNamespaces,GetNamespaceTest5)[0] == "ns");
258 static assert(__traits(getCppNamespaces,GetNamespaceTest6) == Seq!("multiple","namespaces"));
259
260 extern(C++, NS)
261 {
262 struct GetNamespaceTest9 {}
263 extern(C++, nested)
264 {
265 struct GetNamespaceTest10 {}
266 extern(C++,"nested2")
267 struct GetNamespaceTest11 {}
268 }
269 extern (C++, "nested3")
270 {
271 extern(C++, nested4)
272 struct GetNamespaceTest12 {}
273 }
274 }
275 static assert (__traits(getCppNamespaces,NS.GetNamespaceTest9)[0] == "NS");
276 static assert (__traits(getCppNamespaces,NS.GetNamespaceTest10) == Seq!("NS", "nested"));
277 static assert (__traits(getCppNamespaces,NS.GetNamespaceTest11) == Seq!("NS", "nested", "nested2"));
278 static assert (__traits(getCppNamespaces,NS.GetNamespaceTest12) == Seq!("NS", "nested4", "nested3"));
279
280 extern(C++, `ns`) struct GetNamespaceTestTemplated(T) {}
281 extern(C++, `ns`)
282 template GetNamespaceTestTemplated2(T)
283 {
284 struct GetNamespaceTestTemplated2 {}
285 }
286
287 template GetNamespaceTestTemplated3(T)
288 {
289 extern(C++, `ns`)
290 struct GetNamespaceTestTemplated3 {}
291 }
292
293 static assert (__traits(getCppNamespaces,GetNamespaceTestTemplated!int) == Seq!("ns"));
294 static assert (__traits(getCppNamespaces,GetNamespaceTestTemplated2!int) == Seq!("ns"));
295 static assert (__traits(getCppNamespaces,GetNamespaceTestTemplated3!int) == Seq!("ns"));
296 extern(C++, `ns2`)
297 template GetNamespaceTestTemplated4(T)
298 {
299 extern(C++, `ns`)
300 struct GetNamespaceTestTemplated4
301 {
302 struct GetNamespaceTestTemplated5 {}
303 struct GetNamespaceTestTemplated6(T) {}
304 }
305 }
306
307 static assert (__traits(getCppNamespaces,GetNamespaceTestTemplated4!int) == Seq!("ns2","ns"));
308 static assert (__traits(getCppNamespaces,GetNamespaceTestTemplated4!int.GetNamespaceTestTemplated5) == Seq!("ns2","ns"));
309 static assert (__traits(getCppNamespaces,GetNamespaceTestTemplated4!int.GetNamespaceTestTemplated6!int) == Seq!("ns2","ns"));
310
311 // Currently ignored due to https://issues.dlang.org/show_bug.cgi?id=21373
312 extern(C++, `decl`)
313 mixin template GetNamespaceTestTemplatedMixin()
314 {
315 extern(C++, `f`)
316 void foo() {}
317 }
318
319 extern(C++, `inst`)
320 mixin GetNamespaceTestTemplatedMixin!() GNTT;
321
322 static assert (__traits(getCppNamespaces, GNTT.foo) == Seq!(`inst`,/*`decl`,*/ `f`));
323
324 int[1] arr;
325 // test that index assignment parses as an expression, not a type
326 enum _ = __traits(compiles, arr[0] = 0);