[Issue 9570] Wrong foreach index implicit conversion error

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Wed May 21 13:40:02 PDT 2014


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

--- Comment #7 from yebblies <yebblies at gmail.com> ---
(In reply to bearophile_hugs from comment #6)
> (In reply to Lionello Lunesu from comment #5)
> 
> > No, the key must be declared const or immutable. Otherwise we need flow
> > analysis to ensure the key doesn't get mutated in the scope.
> 
> I don't understand what's the problem. What kind of bad things do happen if
> the key gets mutated in the scope?
> 
> void main() {
>     ubyte[256] data;
>     foreach (ubyte i, ref x; data) {
>         x = i;
>         i++; // overflow here
>     }
> }

If i is mutated before it's used, the range information from the foreach
aggregate might not be accurate.  Your example is fine, but to tell it apart
from the bad ones requires flow analysis for non-trivial cases.


void main() {
    ubyte[256] data;
    foreach (ubyte i, ref x; data) {
        i = 99999;
        x = i; // information lost
    }
}

--


More information about the Digitalmars-d-bugs mailing list