Bug 6206 – Pure function not called in comma expression in for loop increment

Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Linux
Creation time
2011-06-24T02:21:00Z
Last change time
2011-06-24T02:28:28Z
Keywords
wrong-code
Assigned to
nobody
Creator
bugzilla

Comments

Comment #0 by bugzilla — 2011-06-24T02:21:32Z
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().
Comment #1 by kennytm — 2011-06-24T02:28:28Z
Exactly the same as bug 5798, which turns out not completely fixed. *** This issue has been marked as a duplicate of issue 5798 ***