Skip to content

Commit

Permalink
Merge pull request #4446 from 9rnsr/fix14218
Browse files Browse the repository at this point in the history
[REG2.067a] Issue 14218 - casting null to integer type causes error message
  • Loading branch information
braddr authored and MartinNowak committed Mar 1, 2015
1 parent a7db6d6 commit 18f4c44
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/e2ir.c
Expand Up @@ -3993,6 +3993,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;
Expand Down Expand Up @@ -4438,11 +4443,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:
Expand Down
29 changes: 29 additions & 0 deletions test/runnable/casting.d
Expand Up @@ -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()
Expand All @@ -210,6 +238,7 @@ int main()
test10834();
test10842();
test11722();
test14218();

printf("Success\n");
return 0;
Expand Down

0 comments on commit 18f4c44

Please sign in to comment.