From a55ea85869f8dad599bd110a0167a7b6667de0cc Mon Sep 17 00:00:00 2001 From: Daniel Murphy Date: Thu, 2 Feb 2012 01:42:21 +1100 Subject: [PATCH] Issue 783 - Cannot use an array w/ const or variable index as new[] size argument. The length is first parsed as an associate array type, so it needs to be reinterpreted as an expression. --- src/mtype.c | 16 ++++++++++++++++ src/mtype.h | 1 + test/runnable/xtest46.d | 9 +++++++++ 3 files changed, 26 insertions(+) diff --git a/src/mtype.c b/src/mtype.c index 0a5df68cc66c..7f5da9c25fb5 100644 --- a/src/mtype.c +++ b/src/mtype.c @@ -4531,6 +4531,22 @@ int TypeAArray::checkBoolean() return TRUE; } +Expression *TypeAArray::toExpression() +{ + Expression *e = next->toExpression(); + if (e) + { + Expression *ei = index->toExpression(); + if (ei) + { + Expressions *arguments = new Expressions(); + arguments->push(ei); + return new ArrayExp(loc, e, arguments); + } + } + return NULL; +} + int TypeAArray::hasPointers() { return TRUE; diff --git a/src/mtype.h b/src/mtype.h index df318de69f14..780b4bf9537d 100644 --- a/src/mtype.h +++ b/src/mtype.h @@ -525,6 +525,7 @@ struct TypeAArray : TypeArray int isZeroInit(Loc loc); int checkBoolean(); TypeInfoDeclaration *getTypeInfoDeclaration(); + Expression *toExpression(); int hasPointers(); TypeTuple *toArgTypes(); MATCH implicitConvTo(Type *to); diff --git a/test/runnable/xtest46.d b/test/runnable/xtest46.d index d35265f87b22..d4c8650211a1 100644 --- a/test/runnable/xtest46.d +++ b/test/runnable/xtest46.d @@ -3644,6 +3644,15 @@ ref const(Foo6529) func6529(const(Foo6529)[] arr){ return arr[0]; } /***************************************************/ +void test783() +{ + const arr = [ 1,2,3 ]; + const i = 2; + auto jhk = new int[arr[i]]; // "need size of rightmost array, not type arr[i]" +} + +/***************************************************/ + template X157(alias x) { alias x X157;