Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Merged
merged 1 commit into from

2 participants

Daniel Murphy Hara Kenji
Daniel Murphy
Collaborator

Allow reinterpreting a slice or index expression as a dynamic array, static array, or associative array

http://d.puremagic.com/issues/show_bug.cgi?id=6408

Hara Kenji 9rnsr commented on the diff
src/expression.c
@@ -9509,6 +9509,16 @@ Expression *SliceExp::semantic(Scope *sc)
Lagain:
UnaExp::semantic(sc);
e1 = resolveProperties(sc, e1);
+ if (e1->op == TOKtype)
+ {
+ if (lwr || upr)
+ {
+ error("cannot slice type '%s'", e1->toChars());
+ return new ErrorExp();
Hara Kenji Collaborator
9rnsr added a note

For tuple type, slicing can be acceptable.

Daniel Murphy Collaborator

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

Hara Kenji Collaborator
9rnsr added a note

An example:

template X(T...) {
    pragma(msg, typeof(T[0..2].init));
}
alias X!(int, long) Y;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Daniel Murphy
Collaborator

I guess not, updated. Any idea why the autotester isn't checking recent pull requests? @braddr ?

Hara Kenji
Collaborator

runnable/imports/test35a.d has same problem as Phobos.

runnable/imports/test35a.d(5): Error: argument char[][0u] to typeof is not an expression
runnable/imports/test35a.d(5): Error: argument char[][0u] to typeof is not an expression
runnable/imports/test35a.d(36): Error: template instance imports.test35a.ElemTypeOf!(char[]) error instantiating
Daniel Murphy yebblies 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
064b041
Daniel Murphy
Collaborator

Updated.

Hara Kenji 9rnsr merged commit cd9ef35 into from
Deleted user Unknown referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Deleted user Unknown referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Deleted user Unknown referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Deleted user Unknown referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 18, 2013
  1. Daniel Murphy

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

    yebblies authored
    Allow reinterpreting a slice or index expression as a dynamic array, static array, or associative array
This page is out of date. Refresh to see the latest.
22 src/expression.c
View
@@ -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();
Hara Kenji Collaborator
9rnsr added a note

For tuple type, slicing can be acceptable.

Daniel Murphy Collaborator

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

Hara Kenji Collaborator
9rnsr added a note

An example:

template X(T...) {
    pragma(msg, typeof(T[0..2].init));
}
alias X!(int, long) Y;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ }
+ 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;
2  test/runnable/imports/bug846.d
View
@@ -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 )
2  test/runnable/imports/test35a.d
View
@@ -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 )
20 test/runnable/xtest46.d
View
@@ -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;
Something went wrong with that request. Please try again.