Skip to content

Commit

Permalink
fix Issue 14207 - [CTFE] ICE on unsupported reinterpret cast in compi…
Browse files Browse the repository at this point in the history
…le time

The problematic code in isSafePointerCast() was introduced in pull 2612 to fix regression 10687.
  • Loading branch information
9rnsr committed Apr 3, 2015
1 parent f234c39 commit 82dca4a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/ctfeexpr.c
Expand Up @@ -652,8 +652,15 @@ bool isSafePointerCast(Type *srcPointee, Type *destPointee)
// It's OK if they are the same size (static array of) integers, eg:
// int* --> uint*
// int[5][] --> uint[5][]
return srcPointee->baseElemOf()->isintegral() &&
destPointee->baseElemOf()->isintegral() &&
if (srcPointee->ty == Tsarray && destPointee->ty == Tsarray)
{
if (srcPointee->size() != destPointee->size())
return false;
srcPointee = srcPointee->baseElemOf();
destPointee = destPointee->baseElemOf();
}
return srcPointee->isintegral() &&
destPointee->isintegral() &&
srcPointee->size() == destPointee->size();
}

Expand Down
8 changes: 8 additions & 0 deletions src/interpret.c
Expand Up @@ -6834,6 +6834,14 @@ void setValueWithoutChecking(VarDeclaration *vd, Expression *newval)

void setValue(VarDeclaration *vd, Expression *newval)
{
#if 0
if (! ((vd->storage_class & (STCout | STCref))
? isCtfeReferenceValid(newval)
: isCtfeValueValid(newval)) )
{
printf("[%s] vd = %s %s, newval = %s\n", vd->loc.toChars(), vd->type->toChars(), vd->toChars(), newval->toChars());
}
#endif
assert((vd->storage_class & (STCout | STCref))
? isCtfeReferenceValid(newval)
: isCtfeValueValid(newval));
Expand Down
22 changes: 22 additions & 0 deletions test/fail_compilation/ctfe14207.d
@@ -0,0 +1,22 @@
/*
TEST_OUTPUT:
---
fail_compilation/ctfe14207.d(13): Error: cannot convert &immutable(ulong) to ubyte[8]* at compile time
fail_compilation/ctfe14207.d(18): called from here: nativeToBigEndian()
fail_compilation/ctfe14207.d(22): called from here: digest()
---
*/

ubyte[8] nativeToBigEndian()
{
immutable ulong res = 1;
return *cast(ubyte[8]*) &res;
}

auto digest()
{
ubyte[8] bits = nativeToBigEndian();
return bits;
}

enum h = digest();

0 comments on commit 82dca4a

Please sign in to comment.