Skip to content

Commit

Permalink
Merge pull request #5827 from 9rnsr/fix16094
Browse files Browse the repository at this point in the history
Issue 16094 - error: overlapping slice assignment (CTFE)
  • Loading branch information
WalterBright committed Jun 23, 2016
2 parents 1feaa7f + 560f606 commit 20e1c81
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
29 changes: 18 additions & 11 deletions src/dinterpret.d
Expand Up @@ -4198,11 +4198,16 @@ public:

if (newval.op == TOKslice)
{
SliceExp se = cast(SliceExp)newval;
Expression aggr2 = se.e1;
if (aggregate == aggr2)
auto se = cast(SliceExp)newval;
auto aggr2 = se.e1;
const srclower = se.lwr.toInteger();
const srcupper = se.upr.toInteger();

if (aggregate == aggr2 &&
lowerbound < srcupper && srclower < upperbound)
{
e.error("overlapping slice assignment [%d..%d] = [%llu..%llu]", lowerbound, upperbound, se.lwr.toInteger(), se.upr.toInteger());
e.error("overlapping slice assignment [%d..%d] = [%llu..%llu]",
lowerbound, upperbound, srclower, srcupper);
return CTFEExp.cantexp;
}
version (all) // todo: instead we can directly access to each elements of the slice
Expand Down Expand Up @@ -4254,11 +4259,11 @@ public:

if (newval.op == TOKslice && !isBlockAssignment)
{
SliceExp se = cast(SliceExp)newval;
Expression aggr2 = se.e1;
dinteger_t srclower = se.lwr.toInteger();
dinteger_t srcupper = se.upr.toInteger();
bool wantCopy = (newval.type.toBasetype().nextOf().baseElemOf().ty == Tstruct);
auto se = cast(SliceExp)newval;
auto aggr2 = se.e1;
const srclower = se.lwr.toInteger();
const srcupper = se.upr.toInteger();
const wantCopy = (newval.type.toBasetype().nextOf().baseElemOf().ty == Tstruct);

//printf("oldval = %p %s[%d..%u]\nnewval = %p %s[%llu..%llu] wantCopy = %d\n",
// aggregate, aggregate->toChars(), lowerbound, upperbound,
Expand Down Expand Up @@ -4317,9 +4322,11 @@ public:
//assert(0);
return newval; // oldval?
}
if (aggregate == aggr2)
if (aggregate == aggr2 &&
lowerbound < srcupper && srclower < upperbound)
{
e.error("overlapping slice assignment [%d..%d] = [%llu..%llu]", lowerbound, upperbound, se.lwr.toInteger(), se.upr.toInteger());
e.error("overlapping slice assignment [%d..%d] = [%llu..%llu]",
lowerbound, upperbound, srclower, srcupper);
return CTFEExp.cantexp;
}
version (all) // todo: instead we can directly access to each elements of the slice
Expand Down
21 changes: 21 additions & 0 deletions test/compilable/interpret3.d
Expand Up @@ -7710,3 +7710,24 @@ auto baz15998()

static assert(bar15998a == [["", ""]]);
static assert(bar15998b == [["", ""]]);

/**************************************************
16094 - Non-overlapped slice assignment on an aggregate
**************************************************/

char[] f16094a()
{
char[] x = new char[](6);
x[3..6] = x[0..3];
return x;
}

int[] f16094b()
{
int[] x = new int[](6);
x[3..6] = x[0..3];
return x;
}

enum copy16094a = f16094a();
enum copy16094b = f16094b();

0 comments on commit 20e1c81

Please sign in to comment.