diff --git a/src/e2ir.c b/src/e2ir.c index 843c38c8bbac..93de35245b01 100644 --- a/src/e2ir.c +++ b/src/e2ir.c @@ -3975,6 +3975,11 @@ elem *toElem(Expression *e, IRState *irs) switch (fty) { + case Tnull: + { + // typeof(null) is same with void* in binary level. + goto Lzero; + } case Tpointer: fty = I64 ? Tuns64 : Tuns32; break; case Tchar: fty = Tuns8; break; case Twchar: fty = Tuns16; break; @@ -4420,11 +4425,6 @@ elem *toElem(Expression *e, IRState *irs) fty = Tcomplex64; goto Lagain; - case X(Tnull, Tarray): - case X(Tnull, Taarray): - case X(Tnull, Tdelegate): - goto Lzero; - /* ============================= */ default: diff --git a/test/runnable/casting.d b/test/runnable/casting.d index a533d8fc0175..6e0978ce66e3 100644 --- a/test/runnable/casting.d +++ b/test/runnable/casting.d @@ -198,6 +198,34 @@ void test11722() shared C11722 sc = cast(shared)c; } +/***************************************************/ +// 14218 + +void test14218() +{ + foreach (To; Seq!( byte, short, int, long, + ubyte, ushort, uint, ulong, + char, wchar, dchar, bool)) + { + auto x = cast(To)null; + assert(x == 0); // false, '0x00' + } + + // Questionable but currently accepted + foreach (To; Seq!( float, double, real, + ifloat, idouble, ireal)) + { + auto x = cast(To)null; + assert(x == 0); // 0i + } + + // Internal error: backend/el.c in el_long() + //foreach (To; Seq!(cfloat, cdouble, creal)) + //{ + // static assert(!__traits(compiles, { auto x = cast(To)null; })); + //} +} + /***************************************************/ int main() @@ -210,6 +238,7 @@ int main() test10834(); test10842(); test11722(); + test14218(); printf("Success\n"); return 0;