[Issue 6074] New: Assert expressions shouldn't have side effects

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun May 29 08:05:56 PDT 2011


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

           Summary: Assert expressions shouldn't have side effects
           Product: D
           Version: D2
          Platform: Other
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: simen.endsjo at pandavre.com


--- Comment #0 from simendsjo <simen.endsjo at pandavre.com> 2011-05-29 08:01:34 PDT ---
http://www.digitalmars.com/d/2.0/expression.html#AssertExpression says "It is
an error if the expression contains any side effects that the program depends
on"

This is not enforced by the compiler though:

module assert_sideeffect;
bool b;
bool f() { b = !b; return b; }
void main() {
    assert(f()); // oops.. changes b in debug mode
    if(!b) { // true only in release
        assert(0);
    }
}

dmd -g -w -wi -debug -run assert_sideeffect
// no output

dmd -g -w -wi -release -run assert_sideeffect
object.Error: assert(0) or HLT instruction

Bearophile suggested:
"In D there are pure functions, so I think it's not too much hard for it to
tell apart when the contents of an assert() are pure or not.
My opinion is that the D compiler has to enforce purity inside assert(), to
avoid bugs.
"

-- 
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