From 71a5ff83c7654d277b2d8705a3807e38b8310bd5 Mon Sep 17 00:00:00 2001 From: Christian Kamm Date: Sat, 17 Jan 2009 17:41:03 +0100 Subject: [PATCH] Fix #164. --- dmd/optimize.c | 6 ++++++ tests/mini/compile_bug164_stringinitcast.d | 2 ++ 2 files changed, 8 insertions(+) create mode 100644 tests/mini/compile_bug164_stringinitcast.d diff --git a/dmd/optimize.c b/dmd/optimize.c index 661d69ed..b33f6b0d 100644 --- a/dmd/optimize.c +++ b/dmd/optimize.c @@ -352,6 +352,9 @@ Expression *CastExp::optimize(int result) type->next->equals(e1->type->next) ) { + // make a copy before adjusting type to avoid + // messing up the type of an existing initializer + e1 = e1->syntaxCopy(); e1->type = type; return e1; } @@ -362,6 +365,7 @@ Expression *CastExp::optimize(int result) if (e1->op == TOKnull && (type->ty == Tpointer || type->ty == Tclass)) { + e1 = e1->syntaxCopy(); e1->type = type; return e1; } @@ -377,6 +381,7 @@ Expression *CastExp::optimize(int result) cdto = type->isClassHandle(); if (cdto->isBaseOf(cdfrom, &offset) && offset == 0) { + e1 = e1->syntaxCopy(); e1->type = type; return e1; } @@ -391,6 +396,7 @@ Expression *CastExp::optimize(int result) if (type->size() == e1->type->size() && type->toBasetype()->ty != Tsarray) { + e1 = e1->syntaxCopy(); e1->type = type; return e1; } diff --git a/tests/mini/compile_bug164_stringinitcast.d b/tests/mini/compile_bug164_stringinitcast.d new file mode 100644 index 00000000..eab798c1 --- /dev/null +++ b/tests/mini/compile_bug164_stringinitcast.d @@ -0,0 +1,2 @@ +const C = "foo"; +char[] var = C;