]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/testsuite/gdc.test/compilable/traits.d
d: Import dmd b8384668f, druntime e6caaab9, phobos 5ab9ad256 (v2.098.0-beta.1)
[thirdparty/gcc.git] / gcc / testsuite / gdc.test / compilable / traits.d
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 static assert(__traits(hasCopyConstructor, S));
141 static assert(__traits(hasCopyConstructor, OuterS.S));
142 static assert(__traits(hasCopyConstructor, OuterS));
143 static assert(__traits(compiles, foo!int));
144 static assert(__traits(compiles, foo!S));
145 static assert(__traits(hasCopyConstructor, U!int));
146 static assert(__traits(hasCopyConstructor, U!S));
147 static assert(!__traits(hasPostblit, U!S));
148 static assert(__traits(hasPostblit, SPostblit));
149 static assert(!__traits(hasCopyConstructor, SPostblit));
150
151 static assert(!__traits(hasCopyConstructor, NoCpCtor));
152 static assert(!__traits(hasCopyConstructor, C19902));
153 static assert(!__traits(hasCopyConstructor, int));
154 static assert(!__traits(hasPostblit, NoCpCtor));
155 static assert(!__traits(hasPostblit, C19902));
156 static assert(!__traits(hasPostblit, int));
157
158 static assert(__traits(isCopyable, int));
159 static assert(!__traits(isCopyable, DisabledPostblit));
160 struct S1 {} // Fine. Can be copied
161 struct S2 { this(this) {} } // Fine. Can be copied
162 struct S3 { @disable this(this); } // Not fine. Copying is disabled.
163 struct S4 { S3 s; } // Not fine. A field has copying disabled.
164 class C1 {}
165 static assert( __traits(isCopyable, S1));
166 static assert( __traits(isCopyable, S2));
167 static assert(!__traits(isCopyable, S3));
168 static assert(!__traits(isCopyable, S4));
169 static assert(__traits(isCopyable, C1));
170 static assert(__traits(isCopyable, int));
171 static assert(__traits(isCopyable, int[]));
172
173 enum E1 : S1 { a = S1(), }
174 enum E2 : S2 { a = S2(), }
175 enum E3 : S3 { a = S3(), }
176 enum E4 : S4 { a = S4(), }
177
178 static assert(__traits(isCopyable, E1));
179 static assert(__traits(isCopyable, E2));
180 static assert(!__traits(isCopyable, E3));
181 static assert(!__traits(isCopyable, E4));
182
183 struct S5
184 {
185 @disable this(ref S5);
186 }
187 static assert(!__traits(isCopyable, S5));
188
189 /******************************************/
190 // https://issues.dlang.org/show_bug.cgi?id=20884
191
192 struct S20884
193 {
194 int x;
195 }
196
197 alias T20884 = immutable(S20884);
198 enum m20884 = "x";
199
200 static assert(is(typeof(__traits(getMember, T20884, m20884)) == immutable(int))); // OK now
201 static assert(is( typeof(mixin("T20884." ~ m20884)) == immutable(int)));
202 static assert(is( typeof(T20884.x) == immutable(int)));
203
204 /******************************************/
205 // https://issues.dlang.org/show_bug.cgi?id=20761
206
207 alias Seq(T...) = T;
208
209 static assert(__traits(isSame, Seq!(1, 2), Seq!(1, 2)));
210 static assert(!__traits(isSame, Seq!(1, 1), Seq!(2, 2)));
211 static assert(!__traits(isSame, Seq!(1, 1, 2), Seq!(1, 1)));
212 static assert(!__traits(isSame, Seq!(1, 1), Seq!(1, 1, 2)));
213
214 static assert(__traits(isSame,
215 Seq!(string, wstring),
216 Seq!(immutable(char)[], immutable(wchar)[]))
217 );
218
219 static assert(__traits(isSame,
220 Seq!(i => i.value, (a, b) => a + b),
221 Seq!(a => a.value, (x, y) => x + y)
222 ));
223
224 static assert(__traits(isSame,
225 Seq!(float, Seq!(double, Seq!real)),
226 Seq!(Seq!(Seq!float, double), real)
227 ));
228
229 static assert(!__traits(isSame,
230 Seq!(int, Seq!(a => a + a)),
231 Seq!(int, Seq!(a => a * a))
232 ));
233
234 // Do these out of order to ensure there are no forward refencing bugs
235
236 extern(C++, __traits(getCppNamespaces,GetNamespaceTest1)) struct GetNamespaceTest4 {}
237 static assert (__traits(getCppNamespaces,GetNamespaceTest1) ==
238 __traits(getCppNamespaces,GetNamespaceTest4));
239
240 extern(C++, "ns") struct GetNamespaceTest1 {}
241 extern(C++, "multiple", "namespaces") struct GetNamespaceTest2 {}
242 extern(C++, mixin("Seq!(`ns`, `nt`)")) struct GetNamespaceTest3 {}
243 static assert(__traits(getCppNamespaces,GetNamespaceTest1)[0] == "ns");
244 static assert(__traits(getCppNamespaces,GetNamespaceTest2) == Seq!("multiple","namespaces"));
245 static assert(__traits(getCppNamespaces,GetNamespaceTest3) == Seq!("ns", "nt"));
246
247 extern(C++, __traits(getCppNamespaces,GetNamespaceTest5)) struct GetNamespaceTest8 {}
248 static assert (__traits(getCppNamespaces,GetNamespaceTest5) ==
249 __traits(getCppNamespaces,GetNamespaceTest8));
250
251 extern(C++, ns) struct GetNamespaceTest5 {}
252 extern(C++, multiple) extern(C++, namespaces) struct GetNamespaceTest6 {}
253 static assert(__traits(getCppNamespaces,GetNamespaceTest5)[0] == "ns");
254 static assert(__traits(getCppNamespaces,GetNamespaceTest6) == Seq!("multiple","namespaces"));
255
256 extern(C++, NS)
257 {
258 struct GetNamespaceTest9 {}
259 extern(C++, nested)
260 {
261 struct GetNamespaceTest10 {}
262 extern(C++,"nested2")
263 struct GetNamespaceTest11 {}
264 }
265 extern (C++, "nested3")
266 {
267 extern(C++, nested4)
268 struct GetNamespaceTest12 {}
269 }
270 }
271 static assert (__traits(getCppNamespaces,NS.GetNamespaceTest9)[0] == "NS");
272 static assert (__traits(getCppNamespaces,NS.GetNamespaceTest10) == Seq!("NS", "nested"));
273 static assert (__traits(getCppNamespaces,NS.GetNamespaceTest11) == Seq!("NS", "nested", "nested2"));
274 static assert (__traits(getCppNamespaces,NS.GetNamespaceTest12) == Seq!("NS", "nested4", "nested3"));
275
276 extern(C++, `ns`) struct GetNamespaceTestTemplated(T) {}
277 extern(C++, `ns`)
278 template GetNamespaceTestTemplated2(T)
279 {
280 struct GetNamespaceTestTemplated2 {}
281 }
282
283 template GetNamespaceTestTemplated3(T)
284 {
285 extern(C++, `ns`)
286 struct GetNamespaceTestTemplated3 {}
287 }
288
289 static assert (__traits(getCppNamespaces,GetNamespaceTestTemplated!int) == Seq!("ns"));
290 static assert (__traits(getCppNamespaces,GetNamespaceTestTemplated2!int) == Seq!("ns"));
291 static assert (__traits(getCppNamespaces,GetNamespaceTestTemplated3!int) == Seq!("ns"));
292 extern(C++, `ns2`)
293 template GetNamespaceTestTemplated4(T)
294 {
295 extern(C++, `ns`)
296 struct GetNamespaceTestTemplated4
297 {
298 struct GetNamespaceTestTemplated5 {}
299 struct GetNamespaceTestTemplated6(T) {}
300 }
301 }
302
303 static assert (__traits(getCppNamespaces,GetNamespaceTestTemplated4!int) == Seq!("ns2","ns"));
304 static assert (__traits(getCppNamespaces,GetNamespaceTestTemplated4!int.GetNamespaceTestTemplated5) == Seq!("ns2","ns"));
305 static assert (__traits(getCppNamespaces,GetNamespaceTestTemplated4!int.GetNamespaceTestTemplated6!int) == Seq!("ns2","ns"));
306
307 // Currently ignored due to https://issues.dlang.org/show_bug.cgi?id=21373
308 extern(C++, `decl`)
309 mixin template GetNamespaceTestTemplatedMixin()
310 {
311 extern(C++, `f`)
312 void foo() {}
313 }
314
315 extern(C++, `inst`)
316 mixin GetNamespaceTestTemplatedMixin!() GNTT;
317
318 static assert (__traits(getCppNamespaces, GNTT.foo) == Seq!(`inst`,/*`decl`,*/ `f`));