Bug 11889 – std.container.Array.opIndex returns by value, resulting in perfect storm

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-01-10T00:20:00Z
Last change time
2014-05-24T05:48:35Z
Assigned to
nobody
Creator
andrei
See also
https://d.puremagic.com/issues/show_bug.cgi?id=11920

Comments

Comment #0 by andrei — 2014-01-10T00:20:37Z
Per http://forum.dlang.org/thread/[email protected]: 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.
Comment #1 by monarchdodra — 2014-01-10T00:50:19Z
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...
Comment #2 by monarchdodra — 2014-01-13T11:47:02Z
@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
Comment #3 by andrei — 2014-01-13T11:48:39Z
But sealing is effectively achieved if we prohibit taking the address of ref results.
Comment #4 by monarchdodra — 2014-01-13T14:54:37Z
Comment #5 by damianday — 2014-05-24T05:48:35Z