Bug 10424 – array operations accept rvalues on the lhs

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-06-20T08:16:00Z
Last change time
2015-06-09T05:15:17Z
Keywords
accepts-invalid
Assigned to
nobody
Creator
nilsbossung

Comments

Comment #0 by nilsbossung — 2013-06-20T08:16:49Z
int f(); int[] g(); void main() { //f() = 42; // as expected: Error: f() is not an lvalue //g() = [42]; // as expected: Error: g() is not an lvalue g()[] = [42]; // compiles, but shouldn't }
Comment #1 by andrej.mitrovich — 2013-06-20T08:33:18Z
In Expression *AssignExp::semantic(Scope *sc) I could add this check: diff --git a/src/expression.c b/src/expression.c index becbcbb..19e008d 100644 --- a/src/expression.c +++ b/src/expression.c @@ -11153,8 +11153,10 @@ Ltupleassign: else if (e1->op == TOKslice) { Type *tn = e1->type->nextOf(); - if (op == TOKassign && e1->checkModifiable(sc) == 1 && !tn->isMutable()) - { error("slice %s is not mutable", e1->toChars()); + if (op == TOKassign && e1->checkModifiable(sc) == 1 && + (!tn->isMutable() || !((SliceExp *)e1)->e1->isLvalue())) + { + error("slice %s is not mutable", e1->toChars()); return new ErrorExp(); } } But I don't think that's totally correct. Kenji?
Comment #2 by k.hara.pg — 2013-06-20T18:39:14Z
(In reply to comment #0) > int f(); > int[] g(); > void main() > { > //f() = 42; // as expected: Error: f() is not an lvalue > //g() = [42]; // as expected: Error: g() is not an lvalue > g()[] = [42]; // compiles, but shouldn't > } The line is correct D code. g() returns an rvalue int[] array, but the assignment is element-wise, and elements of array are always lvalue. Then, there's no meaningless rvalue modification. void main() { int[] a = [1]; int[] arr = a; int[] g() { return arr; } g()[] = [42]; // element-wise assignment // essentially same as: // int[] x = [42]; arr[] = x[]; assert(arr.ptr == a.ptr); assert(arr == [42]); }
Comment #3 by nilsbossung — 2013-06-21T08:58:48Z
(In reply to comment #2) > The line is correct D code. g() returns an rvalue int[] array, but the > assignment is element-wise, and elements of array are always lvalue. > Then, there's no meaningless rvalue modification. You're right. I over-simplified the test-case. The actual problem involved fixed-sized arrays: struct P { int[2] _data; int[2] data() {return _data;} } void main() { P p; p.data[] = [42, 42]; /* would be neat if this threw a "not an lvalue" error */ p.data[0] = 42; /* ditto */ } So the issue is that the elements of rvalue fixed-sized arrays are treated as lvalues. Should I file a new bug for that?