Skip to content
Open

fix #61

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion compiler/src/dmd/expressionsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -17561,7 +17561,24 @@ Modifiable checkModifiable(Expression exp, Scope* sc, ModifyFlags flag = ModifyF

//printf("SliceExp::checkModifiable %s\n", sliceExp.toChars());
auto e1 = sliceExp.e1;
if (e1.type.ty == Tsarray || (e1.op == EXP.index && e1.type.ty != Tarray) || e1.op == EXP.slice)

if (e1.op == EXP.arrayLiteral || e1.op == EXP.structLiteral)
{
if (!(flag & ModifyFlags.noError))
{
if (e1.type.ty != Terror)
{
error(exp.loc, "cannot modify the content of %s literal `%s`",
e1.op == EXP.arrayLiteral ? "array".ptr : "struct".ptr,
e1.toChars());
}
}
return Modifiable.no;
}

if (e1.type.ty == Tsarray ||
(e1.op == EXP.index && e1.type.ty != Tarray) ||
e1.op == EXP.slice)
{
return e1.checkModifiable(sc, flag);
}
Expand All @@ -17573,6 +17590,15 @@ Modifiable checkModifiable(Expression exp, Scope* sc, ModifyFlags flag = ModifyF
case EXP.index:
auto indexExp = cast(IndexExp)exp;
auto e1 = indexExp.e1;

if (e1.op == EXP.arrayLiteral || e1.op == EXP.structLiteral)
{
if (!(flag & ModifyFlags.noError))
error(exp.loc, "cannot modify the content of %s literal `%s`",
e1.op == EXP.arrayLiteral ? "array".ptr : "struct".ptr,
e1.toChars());
return Modifiable.no;
}
if (e1.type.ty == Tsarray ||
e1.type.ty == Taarray ||
(e1.op == EXP.index && e1.type.ty != Tarray) ||
Expand Down
1 change: 0 additions & 1 deletion compiler/test/compilable/interpret4.d
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,5 @@ int* find(int[] arr, int needle)
enum int[int] aa = [0: 0];
enum int[] da = [0, 1, 2];
static assert(0 in aa);
static assert(&da[1]);
static assert(find(da, 1));
static assert(!find(da, 3));
15 changes: 11 additions & 4 deletions compiler/test/fail_compilation/array_literal_assign.d
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
/*
TEST_OUTPUT:
---
fail_compilation/array_literal_assign.d(13): Error: discarded assignment to indexed array literal
fail_compilation/array_literal_assign.d(15): Error: discarded assignment to indexed array literal
fail_compilation/array_literal_assign.d(16): Error: discarded assignment to indexed array literal
fail_compilation/array_literal_assign.d(17): Error: discarded assignment to indexed array literal
fail_compilation/array_literal_assign.d(20): Error: cannot modify the content of array literal `[1, 2, 3]`
fail_compilation/array_literal_assign.d(20): Error: cannot modify the content of array literal `[1, 2, 3]`
fail_compilation/array_literal_assign.d(20): Error: discarded assignment to indexed array literal
fail_compilation/array_literal_assign.d(22): Error: cannot modify the content of array literal `[1, 2]`
fail_compilation/array_literal_assign.d(22): Error: discarded assignment to indexed array literal
fail_compilation/array_literal_assign.d(23): Error: cannot modify the content of array literal `[1, 2]`
fail_compilation/array_literal_assign.d(23): Error: discarded assignment to indexed array literal
fail_compilation/array_literal_assign.d(24): Error: cannot modify the content of array literal `[1, 2]`
fail_compilation/array_literal_assign.d(24): Error: discarded assignment to indexed array literal
fail_compilation/array_literal_assign.d(26): Error: cannot modify the content of array literal `[1, 2]`
fail_compilation/array_literal_assign.d(26): Error: cannot modify the content of array literal `[1, 2]`
---
*/

Expand Down
22 changes: 22 additions & 0 deletions compiler/test/fail_compilation/fail22328.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
TEST_OUTPUT:
---
fail_compilation/fail22328.d(16): Error: cannot modify the content of array literal `[1, 2, 3]`
fail_compilation/fail22328.d(16): Error: cannot modify the content of array literal `[1, 2, 3]`
fail_compilation/fail22328.d(16): Error: discarded assignment to indexed array literal
fail_compilation/fail22328.d(17): Error: cannot modify the content of array literal `[10, 20]`
fail_compilation/fail22328.d(17): Error: cannot modify the content of array literal `[10, 20]`
fail_compilation/fail22328.d(17): Error: discarded assignment to indexed array literal
fail_compilation/fail22328.d(19): Error: cannot modify the content of array literal `[1, 2, 3]`
---
*/

void main() {
enum ARR = [1, 2, 3];
ARR[2] = 4;
[10, 20][0] = 30;

ARR[0..2] = [4, 5];

auto p = &ARR[0];
}
14 changes: 8 additions & 6 deletions compiler/test/fail_compilation/fail6795.d
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
/*
TEST_OUTPUT:
---
fail_compilation/fail6795.d(19): Error: cannot modify expression `[0][0]` because it is not an lvalue
fail_compilation/fail6795.d(20): Error: cannot modify expression `[0:0][0]` because it is not an lvalue
fail_compilation/fail6795.d(22): Error: cannot modify expression `[0][0]` because it is not an lvalue
fail_compilation/fail6795.d(23): Error: cannot modify expression `[0:0][0]` because it is not an lvalue
fail_compilation/fail6795.d(25): Error: cannot take address of expression `[0][0]` because it is not an lvalue
fail_compilation/fail6795.d(30): Error: cannot modify expression `Some["zz"]` because it is not an lvalue
fail_compilation/fail6795.d(21): Error: cannot modify the content of array literal `[0]`
fail_compilation/fail6795.d(21): Error: cannot modify expression `[0][0]` because it is not an lvalue
fail_compilation/fail6795.d(22): Error: cannot modify expression `[0:0][0]` because it is not an lvalue
fail_compilation/fail6795.d(24): Error: cannot modify the content of array literal `[0]`
fail_compilation/fail6795.d(24): Error: cannot modify expression `[0][0]` because it is not an lvalue
fail_compilation/fail6795.d(25): Error: cannot modify expression `[0:0][0]` because it is not an lvalue
fail_compilation/fail6795.d(27): Error: cannot take address of expression `[0][0]` because it is not an lvalue
fail_compilation/fail6795.d(32): Error: cannot modify expression `Some["zz"]` because it is not an lvalue
---
*/

Expand Down
11 changes: 0 additions & 11 deletions compiler/test/runnable/nogc.d
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,6 @@ void testIndexedArrayLiteral() @nogc
assert(arr[i] == 400);
}

void testArrayLiteralLvalue()
{
// https://github.com/dlang/dmd/pull/16784
// Test that this array literal is *not* put on the stack because
// it gets its address taken
static int* getPtr(int i) => &[1, 2, 3][i];
int* x = getPtr(1);
int* y = getPtr(1);
assert(x != y);
}

/***********************/

Expand All @@ -98,7 +88,6 @@ int main()
test3032();
test12642();
test12936();
testArrayLiteralLvalue();

printf("Success\n");
return 0;
Expand Down
10 changes: 6 additions & 4 deletions compiler/test/runnable/test6795.d
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
// https://issues.dlang.org/show_bug.cgi?id=6795
void check6795()
{
enum int[] array = [0];
auto array = [0];
// PostExp
assert(array[0]++ == 0);
assert(array[0]-- == 0);
assert(array[0]-- == 1);
// PreExp
assert(++array[0] == 1);
assert(--array[0] == -1);
assert(--array[0] == 0);
// BinAssignExp
assert((array[0] += 3) == 3);
}

// https://issues.dlang.org/show_bug.cgi?id=21312
void check21312()
{
auto p = &[123][0];
int[1] tmp = [123];
auto p = &tmp[0];

assert(*p == 123);
}

Expand Down
Loading