From 064b0419f8956a76f3d7cabeacd0861cff802923 Mon Sep 17 00:00:00 2001 From: Daniel Murphy Date: Sat, 19 Jan 2013 00:22:03 +1100 Subject: [PATCH] Fix Issue 6408 - string[].init gives a wrong type Allow reinterpreting a slice or index expression as a dynamic array, static array, or associative array --- src/expression.c | 22 ++++++++++++++++++++++ test/runnable/imports/bug846.d | 2 +- test/runnable/imports/test35a.d | 2 +- test/runnable/xtest46.d | 20 ++++++++++++++++++++ 4 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/expression.c b/src/expression.c index a88b85588968..c60324ad51bd 100644 --- a/src/expression.c +++ b/src/expression.c @@ -9509,6 +9509,16 @@ Expression *SliceExp::semantic(Scope *sc) Lagain: UnaExp::semantic(sc); e1 = resolveProperties(sc, e1); + if (e1->op == TOKtype && e1->type->ty != Ttuple) + { + if (lwr || upr) + { + error("cannot slice type '%s'", e1->toChars()); + return new ErrorExp(); + } + e = new TypeExp(loc, e1->type->arrayOf()); + return e->semantic(sc); + } e = this; @@ -10041,6 +10051,18 @@ Expression *IndexExp::semantic(Scope *sc) if (!e1->type) e1 = e1->semantic(sc); assert(e1->type); // semantic() should already be run on it + if (e1->op == TOKtype) + { + e2 = e2->semantic(sc); + e2 = resolveProperties(sc, e2); + Type *nt; + if (e2->op == TOKtype) + nt = new TypeAArray(e1->type, e2->type); + else + nt = new TypeSArray(e1->type, e2); + e = new TypeExp(loc, nt); + return e->semantic(sc); + } if (e1->op == TOKerror) goto Lerr; e = this; diff --git a/test/runnable/imports/bug846.d b/test/runnable/imports/bug846.d index 77b2fda77fdd..d83805d84255 100644 --- a/test/runnable/imports/bug846.d +++ b/test/runnable/imports/bug846.d @@ -2,7 +2,7 @@ module imports.bug846; template ElemTypeOf( T ) { - alias typeof(T[0]) ElemTypeOf; + alias typeof(T.init[0]) ElemTypeOf; } template removeIf_( Elem, Pred ) diff --git a/test/runnable/imports/test35a.d b/test/runnable/imports/test35a.d index 682ea91a937a..b0119340761d 100644 --- a/test/runnable/imports/test35a.d +++ b/test/runnable/imports/test35a.d @@ -2,7 +2,7 @@ module imports.test35a; template ElemTypeOf( T ) { - alias typeof(T[0]) ElemTypeOf; + alias typeof(T.init[0]) ElemTypeOf; } template removeIf_( Elem, Pred ) diff --git a/test/runnable/xtest46.d b/test/runnable/xtest46.d index 03cdba0a32c6..939a930f2eb1 100644 --- a/test/runnable/xtest46.d +++ b/test/runnable/xtest46.d @@ -946,6 +946,26 @@ void test48() /***************************************************/ +// 6408 + +static assert(!is(typeof(string[0..1].init))); +static assert(is(typeof(string[].init) == string[])); +static assert(is(typeof(string[][].init) == string[][])); +static assert(is(typeof(string[][][].init) == string[][][])); + +static assert(is(typeof(string[1].init) == string[1])); +static assert(is(typeof(string[1][1].init) == string[1][1])); +static assert(is(typeof(string[1][1][1].init) == string[1][1][1])); + +static assert(is(typeof(string[string].init) == string[string])); +static assert(is(typeof(string[string][string].init) == string[string][string])); +static assert(is(typeof(string[string][string][string].init) == string[string][string][string])); + +template TT6408(T...) { alias T TT6408; } +static assert(is(typeof(TT6408!(int, int)[].init) == TT6408!(int, int))); + +/***************************************************/ + struct S49 { static void* p;