[Issue 15413] New: Foreach over range with @disable this doesn't work
via Digitalmars-d-bugs
digitalmars-d-bugs at puremagic.com
Sun Dec 6 11:11:13 PST 2015
https://issues.dlang.org/show_bug.cgi?id=15413
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 at puremagic.com
Reporter: pro.mathias.lang at 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).
--
More information about the Digitalmars-d-bugs
mailing list