changeset 1333:6bae1c30480f

Backport D2 CTFE cast behavior to allow cast(char[])char[n] to succeed.
author Christian Kamm <kamm incasoftware de>
date Sun, 10 May 2009 15:42:23 +0200
parents de7fad0ad243
children 7c6dcb98ac35
files dmd/constfold.c
diffstat 1 files changed, 15 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/dmd/constfold.c	Sun May 10 14:37:30 2009 +0200
+++ b/dmd/constfold.c	Sun May 10 15:42:23 2009 +0200
@@ -1049,10 +1049,22 @@
     if (type->equals(e1->type) && to->equals(type))
 	return e1;
 
+    Type *tb = to->toBasetype();
+    Type *typeb = type->toBasetype();
+
+    // LDC: ported from D2 to allow char[] ~ char[n] arguments in CTFE
+    if (e1->op == TOKstring)
+    {
+	if (tb->ty == Tarray && typeb->ty == Tarray &&
+	    tb->nextOf()->size() == typeb->nextOf()->size())
+	{
+	    return expType(to, e1);
+	}
+    }
+
     if (e1->isConst() != 1)
 	return EXP_CANT_INTERPRET;
 
-    Type *tb = to->toBasetype();
     if (tb->ty == Tbool)
 	e = new IntegerExp(loc, e1->toInteger() != 0, type);
     else if (type->isintegral())
@@ -1061,7 +1073,7 @@
 	{   dinteger_t result;
 	    real_t r = e1->toReal();
 
-	    switch (type->toBasetype()->ty)
+	    switch (typeb->ty)
 	    {
 		case Tint8:	result = (d_int8)r;	break;
 		case Tchar:
@@ -1126,7 +1138,7 @@
     else
     {
 	error(loc, "cannot cast %s to %s", e1->type->toChars(), type->toChars());
-	e = new IntegerExp(loc, 0, type);
+	e = new IntegerExp(loc, 0, Type::tint32);
     }
     return e;
 }