Skip to content

Commit

Permalink
Merge pull request #4583 from 9rnsr/fix14440
Browse files Browse the repository at this point in the history
[REG2.067] Issue 14440 - [CTFE] Wrong values set in a matrix constructor
  • Loading branch information
yebblies committed Apr 12, 2015
2 parents 5041bd5 + 7b64c8d commit 444cc4e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/ctfeexpr.c
Expand Up @@ -529,24 +529,23 @@ ArrayLiteralExp *createBlockDuplicatedArrayLiteral(Loc loc, Type *type,
{
Expressions *elements = new Expressions();
elements->setDim(dim);
bool mustCopy = needToCopyLiteral(elem);
if (type->ty == Tsarray && type->nextOf()->ty == Tsarray &&
elem->type->ty != Tsarray)
{
// If it is a multidimensional array literal, do it recursively
elem = createBlockDuplicatedArrayLiteral(loc, type->nextOf(), elem,
(size_t)((TypeSArray *)type->nextOf())->dim->toInteger());
mustCopy = true;
}
bool mustCopy = needToCopyLiteral(elem);
for (size_t i = 0; i < dim; i++)
{
if (mustCopy)
elem = copyLiteral(elem).copy();
(*elements)[i] = elem;
(*elements)[i] = mustCopy ? copyLiteral(elem).copy() : elem;
}
ArrayLiteralExp *ae = new ArrayLiteralExp(loc, elements);
ae->type = type;
ae->ownedByCtfe = 1;
return ae;
ArrayLiteralExp *ale = new ArrayLiteralExp(loc, elements);
ale->type = type;
ale->ownedByCtfe = 1;
return ale;
}

/******************************
Expand Down
41 changes: 41 additions & 0 deletions test/compilable/interpret3.d
Expand Up @@ -4704,6 +4704,47 @@ int bug10198()
}
static assert(bug10198());

/**************************************************
14440 Multidimensional block initialization should create distinct arrays for each elements
**************************************************/

struct Matrix14440(E, size_t row, size_t col)
{
E[col][row] array2D;

@safe pure nothrow
this(E[row * col] numbers...)
{
foreach (r; 0 .. row)
{
foreach (c; 0 .. col)
{
array2D[r][c] = numbers[r * col + c];
}
}
}
}

void test14440()
{
// Replace 'enum' with 'auto' here and it will work fine.
enum matrix = Matrix14440!(int, 3, 3)(
1, 2, 3,
4, 5, 6,
7, 8, 9
);

static assert(matrix.array2D[0][0] == 1);
static assert(matrix.array2D[0][1] == 2);
static assert(matrix.array2D[0][2] == 3);
static assert(matrix.array2D[1][0] == 4);
static assert(matrix.array2D[1][1] == 5);
static assert(matrix.array2D[1][2] == 6);
static assert(matrix.array2D[2][0] == 7);
static assert(matrix.array2D[2][1] == 8);
static assert(matrix.array2D[2][2] == 9);
}

/****************************************************
* Exception chaining tests from xtest46.d
****************************************************/
Expand Down

0 comments on commit 444cc4e

Please sign in to comment.