www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 10424] New: array operations accept rvalues on the lhs

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10424

           Summary: array operations accept rvalues on the lhs
           Product: D
           Version: unspecified
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: accepts-invalid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: nilsbossung googlemail.com



---
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
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 20 2013
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10424


Andrej Mitrovic <andrej.mitrovich gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich gmail.com



08:33:18 PDT ---
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?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 20 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10424


Kenji Hara <k.hara.pg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID




 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]); } -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jun 20 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10424




---

 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? -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jun 21 2013