www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 15413] New: Foreach over range with disable this doesn't work


          Issue ID: 15413
           Summary: Foreach over range with  disable this doesn't work
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: pro.mathias.lang gmail.com

public struct IRange
    int[] data;

    public this(int[] d) { this.data = d; }
     disable this(this);
    public int front ()  property { return data[0]; }
    public bool empty ()  property { return !!data.length; }
    public void popFront () { data = data[1..$]; }

void main ()
    auto ir = IRange([1,2,3,4]);
    foreach (v; ir)

Obviously it happens because it lowers to:
for (auto __r = ir; !__r.empty; __r.popFront()) {
        auto v = __r.front;

Which means that input ranges which behaves as forward ranges when copied will
have an implicit `.save` here. So fixing this bug could potentially affect a
lot of code.

Related: https://issues.dlang.org/show_bug.cgi?id=4347 (note: I didn't use that
issue because I don't propose we do an implicit `.save`, but rather we get rid
of it in all circumstances).

Dec 06 2015