1 // REQUIRED_ARGS: -extern-std=c++98
2 // EXTRA_FILES: imports/plainpackage/plainmodule.d imports/pkgmodule/package.d imports/pkgmodule/plainmodule.d
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.
7 // https://issues.dlang.org/show_bug.cgi?id=19152
14 auto name = __traits(getOverloads, this, "OnExecute").stringof;
19 static assert(is(typeof(__traits(getTargetInfo, "cppRuntimeLibrary")) == string));
20 version (CppRuntime_Microsoft)
22 static assert(__traits(getTargetInfo, "cppRuntimeLibrary") == "libcmt" ||
23 __traits(getTargetInfo, "cppRuntimeLibrary")[0..6] == "msvcrt"); // includes mingw import libs
27 static assert(__traits(getTargetInfo, "floatAbi") == "hard");
30 static assert(__traits(getTargetInfo, "objectFormat") == "coff");
32 static assert(__traits(getTargetInfo, "objectFormat") == "macho");
34 static assert(__traits(getTargetInfo, "objectFormat") == "elf");
36 static assert(__traits(getTargetInfo, "cppStd") == 199711);
38 import imports.plainpackage.plainmodule;
39 import imports.pkgmodule.plainmodule;
44 alias a = imports.plainpackage;
45 alias b = imports.pkgmodule.plainmodule;
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));
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));
62 /******************************************/
63 // https://issues.dlang.org/show_bug.cgi?id=19942
65 static assert(!__traits(compiles, { a.init; }));
66 static assert(!__traits(compiles, { import m : a; a.init; }));
69 static assert(__traits(getLocation, MyStruct)[0] == `compilable\traits.d`);
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);
78 static assert(__traits(getLocation, __traits(getOverloads, traits, "foo")[1])[1] == 74);
81 static assert(__traits(getLocation, bar)[1] == 78);
89 static assert(__traits(getLocation, Outer.Nested)[1] == 83);
90 static assert(__traits(getLocation, Outer.method)[1] == 85);
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
119 static assert (__traits(hasCopyConstructor, S!int));
132 struct DisabledPostblit
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));
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));
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.
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[]));
173 enum E1 : S1 { a = S1(), }
174 enum E2 : S2 { a = S2(), }
175 enum E3 : S3 { a = S3(), }
176 enum E4 : S4 { a = S4(), }
178 static assert(__traits(isCopyable, E1));
179 static assert(__traits(isCopyable, E2));
180 static assert(!__traits(isCopyable, E3));
181 static assert(!__traits(isCopyable, E4));
185 @disable this(ref S5);
187 static assert(!__traits(isCopyable, S5));
189 /******************************************/
190 // https://issues.dlang.org/show_bug.cgi?id=20884
197 alias T20884 = immutable(S20884);
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)));
204 /******************************************/
205 // https://issues.dlang.org/show_bug.cgi?id=20761
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)));
214 static assert(__traits(isSame,
215 Seq!(string, wstring),
216 Seq!(immutable(char)[], immutable(wchar)[]))
219 static assert(__traits(isSame,
220 Seq!(i => i.value, (a, b) => a + b),
221 Seq!(a => a.value, (x, y) => x + y)
224 static assert(__traits(isSame,
225 Seq!(float, Seq!(double, Seq!real)),
226 Seq!(Seq!(Seq!float, double), real)
229 static assert(!__traits(isSame,
230 Seq!(int, Seq!(a => a + a)),
231 Seq!(int, Seq!(a => a * a))
234 // Do these out of order to ensure there are no forward refencing bugs
236 extern(C++, __traits(getCppNamespaces,GetNamespaceTest1)) struct GetNamespaceTest4 {}
237 static assert (__traits(getCppNamespaces,GetNamespaceTest1) ==
238 __traits(getCppNamespaces,GetNamespaceTest4));
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"));
247 extern(C++, __traits(getCppNamespaces,GetNamespaceTest5)) struct GetNamespaceTest8 {}
248 static assert (__traits(getCppNamespaces,GetNamespaceTest5) ==
249 __traits(getCppNamespaces,GetNamespaceTest8));
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"));
258 struct GetNamespaceTest9 {}
261 struct GetNamespaceTest10 {}
262 extern(C++,"nested2")
263 struct GetNamespaceTest11 {}
265 extern (C++, "nested3")
268 struct GetNamespaceTest12 {}
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"));
276 extern(C++, `ns`) struct GetNamespaceTestTemplated(T) {}
278 template GetNamespaceTestTemplated2(T)
280 struct GetNamespaceTestTemplated2 {}
283 template GetNamespaceTestTemplated3(T)
286 struct GetNamespaceTestTemplated3 {}
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"));
293 template GetNamespaceTestTemplated4(T)
296 struct GetNamespaceTestTemplated4
298 struct GetNamespaceTestTemplated5 {}
299 struct GetNamespaceTestTemplated6(T) {}
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"));
307 // Currently ignored due to https://issues.dlang.org/show_bug.cgi?id=21373
309 mixin template GetNamespaceTestTemplatedMixin()
316 mixin GetNamespaceTestTemplatedMixin!() GNTT;
318 static assert (__traits(getCppNamespaces, GNTT.foo) == Seq!(`inst`,/*`decl`,*/ `f`));