http://d.puremagic.com/issues/show_bug.cgi?id=9087

           Summary: Value modified in foreach warning
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nob...@puremagic.com
        ReportedBy: bearophile_h...@eml.cc


--- Comment #0 from bearophile_h...@eml.cc 2012-11-27 18:41:03 PST ---
Currently (DMD 2.061alpha) this code generates a warning:

// program#1
void main() {
    foreach (i; 0 .. 10)
        i++;
}

test.d(4): Warning: variable modified in foreach body requires ref storage
class


This is a rather common and well known source of bugs in D code (C# disallows
such mutation):

// program#2
struct S { int x; }
void main() {
    auto items = [S(1), S(2), S(3)];
    foreach (it; items)
        it.x++;
}


(The bug is that the programmer thinks she has modified the contents of "items"
array, while the changes are just in the "it" copy, and such changes get lost
silently.)

So to both help avoid those common bugs, and improve consistency between the
two cases, I suggest to generate a warning in program#2 too, similar to:

test.d(6): Warning: mutable value modified in foreach body requires ref storage
class

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------

Reply via email to