www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 11889] New: std.container.Array.opIndex returns by value, resulting in perfect storm

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

           Summary: std.container.Array.opIndex returns by value,
                    resulting in perfect storm
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: andrei erdani.com


--- Comment #0 from Andrei Alexandrescu <andrei erdani.com> 2014-01-10 00:20:37
PST ---
Per
http://forum.dlang.org/thread/mailman.281.1389340467.15871.digitalmars-d puremagic.com:

void main()
{
    import std.container;
    struct S
    {
      int i;
    }
    Array!S array = [ S(0) ];
    array[0].i = 1;
    assert(array[0].i == 1);
}

This fails because opIndex returns by value and then the lax rules of accessing
members for rvalues masquerade the member as a lvalue, which can be
subsequently assigned to.

Current stance of D is that ref results cannot be escaped (not enforced yet
statically but it will) so it's safe to return a ref.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 10 2014
next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11889


monarchdodra gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |monarchdodra gmail.com


--- Comment #1 from monarchdodra gmail.com 2014-01-10 00:50:19 PST ---
Is this really Array that is at fault here?

//----
struct S{int i;}
S s();
int i();

void main()
{
    s().i = 1; //YES.
    i() = 1;   //NO.
}
//----

If you can't assign to an rvalue, I question the ability to assign to an rvalue
member...

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 10 2014
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11889


monarchdodra gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://d.puremagic.com/iss
                   |                            |ues/show_bug.cgi?id=11920


--- Comment #2 from monarchdodra gmail.com 2014-01-13 11:47:02 PST ---
 andrei:

Un-sealing containers is probably a correct solution, but it still feels like
working around the root issue.

I filed this, which I think should also be addressed.
https://d.puremagic.com/issues/show_bug.cgi?id=11920

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 13 2014
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11889



--- Comment #3 from Andrei Alexandrescu <andrei erdani.com> 2014-01-13 11:48:39
PST ---
But sealing is effectively achieved if we prohibit taking the address of ref
results.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 13 2014
prev sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11889



--- Comment #4 from monarchdodra gmail.com 2014-01-13 14:54:37 PST ---
https://github.com/D-Programming-Language/phobos/pull/1857

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