| 1 |
/* |
| 2 |
RUN_OUTPUT: |
| 3 |
--- |
| 4 |
Success |
| 5 |
--- |
| 6 |
*/ |
| 7 |
extern(C) int printf(const char*, ...); |
| 8 |
template TypeTuple(TL...) { alias TypeTuple = TL; } |
| 9 |
|
| 10 |
import core.stdc.math : isnan; |
| 11 |
|
| 12 |
/********************************************/ |
| 13 |
// https://issues.dlang.org/show_bug.cgi?id=9112 |
| 14 |
|
| 15 |
void test9112a() // T() and T(v) |
| 16 |
{ |
| 17 |
void test(T)(T v) |
| 18 |
{ |
| 19 |
foreach (string qual; TypeTuple!("", "const ", "immutable ")) |
| 20 |
{ |
| 21 |
mixin("alias U = "~qual~T.stringof~";"); |
| 22 |
//pragma(msg, U); |
| 23 |
|
| 24 |
mixin("auto x1 = "~qual~T.stringof~"();"); // U() default construction syntax |
| 25 |
mixin("auto x2 = "~qual~T.stringof~"(v);"); // U(v) |
| 26 |
static assert(!__traits(compiles, mixin(qual~T.stringof~"(v, v)"))); // U(v, v) |
| 27 |
static assert(is(typeof(x1) == U)); |
| 28 |
static assert(is(typeof(x2) == U)); |
| 29 |
static if ( is(typeof(U.nan) : real)) assert( isnan(x1.re) && !isnan(x1.im), U.stringof); |
| 30 |
static if ( is(typeof(U.nan) : ireal)) assert(!isnan(x1.re) && isnan(x1.im), U.stringof); |
| 31 |
static if ( is(typeof(U.nan) : creal)) assert( isnan(x1.re) && isnan(x1.im), U.stringof); |
| 32 |
static if (!is(typeof(U.nan))) assert( x1 == U.init, U.stringof); |
| 33 |
assert(x2 == v, U.stringof); |
| 34 |
} |
| 35 |
} |
| 36 |
static assert(!__traits(compiles, { auto x1 = void(); })); |
| 37 |
static assert(!__traits(compiles, { auto x2 = void(1); })); |
| 38 |
test!( byte )(10); |
| 39 |
test!(ubyte )(10); |
| 40 |
test!( short )(10); |
| 41 |
test!(ushort )(10); |
| 42 |
test!( int )(10); |
| 43 |
test!(uint )(10); |
| 44 |
test!( long )(10); |
| 45 |
test!(ulong )(10); |
| 46 |
test!( float )(3.14); |
| 47 |
test!( double)(3.14); |
| 48 |
test!( real )(3.14); |
| 49 |
test!( char )('A'); |
| 50 |
test!(wchar )('A'); |
| 51 |
test!(dchar )('A'); |
| 52 |
test!(bool )(true); |
| 53 |
|
| 54 |
static assert(!__traits(compiles, int(1.42))); // in curre,t this is disallowed |
| 55 |
|
| 56 |
{ |
| 57 |
int x; |
| 58 |
alias T = int*; |
| 59 |
//auto p = int*(&x); // Error: found '*' when expecting '.' following int |
| 60 |
//auto p = (int*)(&x); // Error: C style cast illegal, use cast(int*)&x |
| 61 |
auto p = T(&x); |
| 62 |
assert( p == &x); |
| 63 |
assert(*p == x); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
enum Enum : long { a = 10, b = 20 } |
| 68 |
|
| 69 |
void test9112b() // new T(v) |
| 70 |
{ |
| 71 |
void test(T)(T v) |
| 72 |
{ |
| 73 |
foreach (string qual; TypeTuple!("", "const ", "immutable ")) |
| 74 |
{ |
| 75 |
mixin("alias U = "~qual~T.stringof~";"); |
| 76 |
//pragma(msg, U); |
| 77 |
|
| 78 |
mixin("auto p1 = new "~qual~T.stringof~"();"); // U() default construction syntax |
| 79 |
mixin("auto p2 = new "~qual~T.stringof~"(v);"); // U(v) |
| 80 |
static assert(!__traits(compiles, mixin("new "~qual~T.stringof~"(v, v)"))); // U(v, v) |
| 81 |
static assert(is(typeof(p1) == U*)); |
| 82 |
static assert(is(typeof(p2) == U*)); |
| 83 |
assert( p1 !is null); |
| 84 |
assert( p2 !is null); |
| 85 |
auto x1 = *p1; |
| 86 |
auto x2 = *p2; |
| 87 |
static if ( is(typeof(U.nan) : real)) assert( isnan(x1.re) && !isnan(x1.im), U.stringof); |
| 88 |
static if ( is(typeof(U.nan) : ireal)) assert(!isnan(x1.re) && isnan(x1.im), U.stringof); |
| 89 |
static if ( is(typeof(U.nan) : creal)) assert( isnan(x1.re) && isnan(x1.im), U.stringof); |
| 90 |
static if (!is(typeof(U.nan))) assert( x1 == U.init, U.stringof); |
| 91 |
assert(x2 == v, U.stringof); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
static assert(!__traits(compiles, { auto x1 = new void(); })); |
| 96 |
static assert(!__traits(compiles, { auto x2 = new void(1); })); |
| 97 |
static assert(!__traits(compiles, { auto x2 = new void(1, 2); })); |
| 98 |
test!( byte )(10); |
| 99 |
test!(ubyte )(10); |
| 100 |
test!( short )(10); |
| 101 |
test!(ushort )(10); |
| 102 |
test!( int )(10); |
| 103 |
test!(uint )(10); |
| 104 |
test!( long )(10); |
| 105 |
test!(ulong )(10); |
| 106 |
test!( float )(3.14); |
| 107 |
test!( double)(3.14); |
| 108 |
test!( real )(3.14); |
| 109 |
test!( char )('A'); |
| 110 |
test!(wchar )('A'); |
| 111 |
test!(dchar )('A'); |
| 112 |
test!(bool )(true); |
| 113 |
test!(Enum )(Enum.a); |
| 114 |
|
| 115 |
void testPtr(T)(T v) |
| 116 |
{ |
| 117 |
T* pv = &v; |
| 118 |
T** ppv = new T*(pv); |
| 119 |
assert( *ppv == pv); |
| 120 |
assert(**ppv == v); |
| 121 |
} |
| 122 |
foreach (T; TypeTuple!(int, const long, immutable double)) |
| 123 |
{ |
| 124 |
testPtr!T(10); |
| 125 |
} |
| 126 |
foreach (T; TypeTuple!(Enum, const Enum, immutable Enum)) |
| 127 |
{ |
| 128 |
testPtr!T(Enum.a); |
| 129 |
} |
| 130 |
|
| 131 |
static assert(!__traits(compiles, new const int(1, 2))); |
| 132 |
|
| 133 |
static assert(!__traits(compiles, new int(1.42))); // in curre,t this is disallowed |
| 134 |
|
| 135 |
// int(1) in directly on statement scope should be parsed as an expression, but |
| 136 |
// would fail to compile because of "has no effect" error. |
| 137 |
static assert(!__traits(compiles, { int(1); })); |
| 138 |
} |
| 139 |
|
| 140 |
/********************************************/ |
| 141 |
|
| 142 |
int main() |
| 143 |
{ |
| 144 |
test9112a(); |
| 145 |
test9112b(); |
| 146 |
|
| 147 |
printf("Success\n"); |
| 148 |
return 0; |
| 149 |
} |