www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 14941] New: dmd rejects op assignment to opIndex expression

https://issues.dlang.org/show_bug.cgi?id=14941

          Issue ID: 14941
           Summary: dmd rejects op assignment to opIndex expression
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Keywords: rejects-valid
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: ag0aep6g gmail.com

Found by Ryuichi OHORI, see issue 14935.

----
struct V
{
    void opOpAssign(string op)(int x) {}
}
struct S
{
    V opIndex() {return V();}
}
void main()
{
    S s;

    auto v = s[];
    v += 2; /* no error */

    s.opIndex() += 2; /* no error */

    s[] += 2; /* Error: S cannot be sliced with [] */
}
----

Clearly, S can be sliced with []. And a V can be add-assigned. The compiler
seems to give up when there's opIndexOpAssign for S, but it should try
opIndex+opOpAssign, too.

Also fails in the same manner when S.opIndex returns e.g. `ref int`.

If nothing else, the error message should be improved.

--
Aug 20 2015