Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Issue 6408 - string[].init gives a wrong type #1495

Merged
merged 1 commit into from Jan 21, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/expression.c
Expand Up @@ -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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For tuple type, slicing can be acceptable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't that evaluate to a TOKtuple? I'll see if I can trigger it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An example:

template X(T...) {
    pragma(msg, typeof(T[0..2].init));
}
alias X!(int, long) Y;

}
e = new TypeExp(loc, e1->type->arrayOf());
return e->semantic(sc);
}

e = this;

Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion test/runnable/imports/bug846.d
Expand Up @@ -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 )
Expand Down
2 changes: 1 addition & 1 deletion test/runnable/imports/test35a.d
Expand Up @@ -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 )
Expand Down
20 changes: 20 additions & 0 deletions test/runnable/xtest46.d
Expand Up @@ -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;
Expand Down