[Issue 6206] New: Pure function not called in comma expression in for loop increment

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Jun 24 02:26:22 PDT 2011


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

           Summary: Pure function not called in comma expression in for
                    loop increment
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Keywords: wrong-code
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: bugzilla at kyllingen.net


--- Comment #0 from Lars T. Kyllingstad <bugzilla at kyllingen.net> 2011-06-24 02:21:32 PDT ---
Test case:

    struct S
    {
        int i = 0;
        void incr() pure { ++i; }
    }

    void main()
    {
        S s;
        for (int j=0; j<10; s.incr(), ++j) { }
        assert (s.i == 10); // fails
    }

The assert should pass, but it fails iff S.incr() is marked as pure.  (In fact,
S.i is never increased at all in this case.)  Note that moving '++j' into the
loop body makes the bug disappear.  It seems the call to s.incr() must be part
of a comma expression for the bug to manifest.

While seemingly obscure, this has some very real consequences.  For example, it
means std.algorithm.equal() doesn't work with ranges that have a pure
popFront().

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


More information about the Digitalmars-d-bugs mailing list