Skip to content

opOp evaluation order difference with DMD (e.g. +=) #1617

@JohanEngelen

Description

@JohanEngelen

Consider the following code:

int sum;

int return1_add9tosum()
{
    sum += 9;
    return 1;
}

void main()
{
    sum = 0;
    sum += return1_add9tosum();

    import std.stdio;
    writeln(sum);
}

DMD 2.071 writes 10. LDC writes 1. It's an evaluation order difference. The spec says that sum should only be evaluated once; left-to-right evaluation is specced, but then the spec also says that DMD does not comply with left-to-right evaluation of AssignExpressions.
Commit a3adb40 of #1562 changed LDC's behavior (before it behaved like DMD).

With fibers in play, the evaluation order is important even if foo() does not modify sum in sum += foo(). When foo() yields, another fiber might modify sum, and thus an older version of sum will be used when the fiber is resumed. I'm pretty sure this is what happened in Weka's unittest failure. In light of that, I think DMD's evaluation order makes more sense (it would make "+=" do the intuitive thing when using fibers).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions