Thread overview
[Issue 10424] New: array operations accept rvalues on the lhs
Jun 20, 2013
Nils
Jun 20, 2013
Andrej Mitrovic
Jun 21, 2013
Kenji Hara
Jun 21, 2013
Nils
June 20, 2013
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


--- Comment #0 from Nils <nilsbossung@googlemail.com> 2013-06-20 08:16:49 PDT ---
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: -------
June 20, 2013
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


--- Comment #1 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-06-20 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: -------
June 21, 2013
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


--- Comment #2 from Kenji Hara <k.hara.pg@gmail.com> 2013-06-20 18:39:14 PDT ---
(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]);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 21, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10424



--- Comment #3 from Nils <nilsbossung@googlemail.com> 2013-06-21 08:58:48 PDT ---
(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?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------