Skip to content

Loading…

Issue 1064 - Tuples are not expanded in array initializers #690

Merged
merged 1 commit into from

2 participants

@yebblies
D Programming Language member

Expand any tuples in place. expandTuples from expression.c cannot be used because this is an array of ExpInitializers, not Expressions.

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

@yebblies yebblies Issue 1064 - Tuples are not expanded in array initializers
Expand any tuples in place. expandTuples from expression.c cannot be used because this is an array of ExpInitializers, not Expressions.
0e6f621
@9rnsr 9rnsr merged commit 162cf30 into D-Programming-Language:master
@9rnsr
D Programming Language member

Merged.

@9rnsr 9rnsr added a commit to 9rnsr/dmd that referenced this pull request
@9rnsr 9rnsr merge D2 pull #690 55bedf4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 14, 2012
  1. @yebblies

    Issue 1064 - Tuples are not expanded in array initializers

    yebblies committed
    Expand any tuples in place. expandTuples from expression.c cannot be used because this is an array of ExpInitializers, not Expressions.
Showing with 45 additions and 1 deletion.
  1. +32 −1 src/init.c
  2. +1 −0 src/init.h
  3. +12 −0 test/runnable/xtest46.d
View
33 src/init.c
@@ -496,8 +496,33 @@ Initializer *ArrayInitializer::semantic(Scope *sc, Type *t, int needInterpret)
}
Initializer *val = value[i];
+ ExpInitializer *ei = val->isExpInitializer();
+ if (ei && !idx)
+ ei->expandTuples = 1;
val = val->semantic(sc, t->nextOf(), needInterpret);
- value[i] = val;
+
+ ei = val->isExpInitializer();
+ // found a tuple, expand it
+ if (ei && ei->exp->op == TOKtuple)
+ {
+ TupleExp *te = (TupleExp *)ei->exp;
+ index.remove(i);
+ value.remove(i);
+
+ for (size_t j = 0; j < te->exps->dim; ++j)
+ {
+ Expression *e = (*te->exps)[j];
+ index.insert(i + j, (Expression *)NULL);
+ value.insert(i + j, new ExpInitializer(e->loc, e));
+ }
+ i--;
+ continue;
+ }
+ else
+ {
+ value[i] = val;
+ }
+
length++;
if (length == 0)
{ error(loc, "array dimension overflow");
@@ -743,6 +768,7 @@ ExpInitializer::ExpInitializer(Loc loc, Expression *exp)
: Initializer(loc)
{
this->exp = exp;
+ this->expandTuples = 0;
}
Initializer *ExpInitializer::syntaxCopy()
@@ -827,6 +853,11 @@ Initializer *ExpInitializer::semantic(Scope *sc, Type *t, int needInterpret)
Type *tb = t->toBasetype();
+ if (exp->op == TOKtuple &&
+ expandTuples &&
+ !exp->implicitConvTo(t))
+ return new ExpInitializer(loc, exp);
+
/* Look for case of initializing a static array with a too-short
* string literal, such as:
* char[5] foo = "abc";
View
1 src/init.h
@@ -114,6 +114,7 @@ struct ArrayInitializer : Initializer
struct ExpInitializer : Initializer
{
Expression *exp;
+ int expandTuples;
ExpInitializer(Loc loc, Expression *exp);
Initializer *syntaxCopy();
View
12 test/runnable/xtest46.d
@@ -4033,6 +4033,17 @@ void test4647()
}
/***************************************************/
+
+template T1064(E...) { alias E T1064; }
+
+int[] var1064 = [ T1064!(T1064!(T1064!(1, 2), T1064!(), T1064!(3)), T1064!(4, T1064!(T1064!(T1064!(T1064!(5)))), T1064!(T1064!(T1064!(T1064!())))),6) ];
+
+void test1064()
+{
+ assert(var1064 == [1,2,3,4,5,6]);
+}
+
+/***************************************************/
// 5696
template Seq5696(T...){ alias T Seq5696; }
@@ -5111,6 +5122,7 @@ int main()
test1891();
test129();
test130();
+ test1064();
test131();
test132();
test133();
Something went wrong with that request. Please try again.