Skip to content

Commit

Permalink
Merge pull request #3478 from 9rnsr/fix12602
Browse files Browse the repository at this point in the history
Issue 12602 - [CTFE] Changes to an array slice wrapped in a struct do not propogate to the original
  • Loading branch information
AndrejMitrovic committed Apr 21, 2014
2 parents 0a95cd4 + a2f919e commit d54b1c7
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/ctfeexpr.c
Expand Up @@ -268,8 +268,6 @@ Expression *copyLiteral(Expression *e)
// If it is a void assignment, use the default initializer
if (!m)
m = voidInitLiteral(v->type, v);
if (m->op == TOKslice)
m = resolveSlice(m);
if ((v->type->ty != m->type->ty) && v->type->ty == Tsarray)
{
// Block assignment from inside struct literals
Expand Down Expand Up @@ -299,8 +297,9 @@ Expression *copyLiteral(Expression *e)
r->type = e->type;
return r;
}
else if ( isPointer(e->type) )
{ // For pointers, we only do a shallow copy.
else if (isPointer(e->type))
{
// For pointers, we only do a shallow copy.
Expression *r;
if (e->op == TOKaddress)
r = new AddrExp(e->loc, ((AddrExp *)e)->e1);
Expand All @@ -317,9 +316,10 @@ Expression *copyLiteral(Expression *e)
return r;
}
else if (e->op == TOKslice)
{ // Array slices only do a shallow copy
{
// Array slices only do a shallow copy
Expression *r = new SliceExp(e->loc, ((SliceExp *)e)->e1,
((SliceExp *)e)->lwr, ((SliceExp *)e)->upr);
((SliceExp *)e)->lwr, ((SliceExp *)e)->upr);
r->type = e->type;
return r;
}
Expand Down
98 changes: 98 additions & 0 deletions test/compilable/interpret3.d
Expand Up @@ -6416,3 +6416,101 @@ auto f12499()
return a[0]; //Error: variable _a_field_0 cannot be read at compile time
}
static assert(f12499() == 5);

/**************************************************
12602 - slice in struct literal members
**************************************************/

struct Result12602
{
uint[] source;
}

auto wrap12602a(uint[] r)
{
return Result12602(r);
}

auto wrap12602b(uint[] r)
{
Result12602 x;
x.source = r;
return x;
}

auto testWrap12602a()
{
uint[] dest = [1, 2, 3, 4];

auto ra = wrap12602a(dest[0..2]);
auto rb = wrap12602a(dest[2..4]);

foreach (i; 0..2)
rb.source[i] = ra.source[i];

assert(ra.source == [1,2]);
assert(rb.source == [1,2]);
assert(&ra.source[0] == &dest[0]);
assert(&rb.source[0] == &dest[2]);
assert(dest == [1,2,1,2]);
return dest;
}

auto testWrap12602b()
{
uint[] dest = [1, 2, 3, 4];

auto ra = wrap12602b(dest[0..2]);
auto rb = wrap12602b(dest[2..4]);

foreach (i; 0..2)
rb.source[i] = ra.source[i];

assert(ra.source == [1,2]);
assert(rb.source == [1,2]);
assert(&ra.source[0] == &dest[0]);
assert(&rb.source[0] == &dest[2]);
assert(dest == [1,2,1,2]);
return dest;
}

auto testWrap12602c()
{
uint[] dest = [1, 2, 3, 4];

auto ra = Result12602(dest[0..2]);
auto rb = Result12602(dest[2..4]);

foreach (i; 0..2)
rb.source[i] = ra.source[i];

assert(ra.source == [1,2]);
assert(rb.source == [1,2]);
assert(&ra.source[0] == &dest[0]);
assert(&rb.source[0] == &dest[2]);
assert(dest == [1,2,1,2]);
return dest;
}

auto testWrap12602d()
{
uint[] dest = [1, 2, 3, 4];

Result12602 ra; ra.source = dest[0..2];
Result12602 rb; rb.source = dest[2..4];

foreach (i; 0..2)
rb.source[i] = ra.source[i];

assert(ra.source == [1,2]);
assert(rb.source == [1,2]);
assert(&ra.source[0] == &dest[0]);
assert(&rb.source[0] == &dest[2]);
assert(dest == [1,2,1,2]);
return dest;
}

static assert(testWrap12602a() == [1,2,1,2]);
static assert(testWrap12602b() == [1,2,1,2]);
static assert(testWrap12602c() == [1,2,1,2]);
static assert(testWrap12602d() == [1,2,1,2]);

0 comments on commit d54b1c7

Please sign in to comment.