[Issue 13341] New: Wrong optimization for ref parameters and if statement
    via Digitalmars-d-bugs 
    digitalmars-d-bugs at puremagic.com
       
    Wed Aug 20 06:58:15 PDT 2014
    
    
  
https://issues.dlang.org/show_bug.cgi?id=13341
          Issue ID: 13341
           Summary: Wrong optimization for ref parameters and if statement
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Keywords: wrong-code
          Severity: normal
          Priority: P1
         Component: DMD
          Assignee: nobody at puremagic.com
          Reporter: sinkuu at aol.jp
This code produces unexpected AssertError when it is compiled with -O and
-inline.
```
// core.checkedint.addu
uint addu(uint x, uint y, ref bool overflow)
{
    uint r = x + y;
    if (r < x || r < y)
        overflow = true;
    return r;
}
void main()
{
    bool of;
    foreach (i; 0 .. 1)
    {
        addu(0, i, of);
        assert(!of); // fails
    }
}
```
Reduced a bit:
```
void addu(uint y, ref bool overflow)
{
    if (0 < y) overflow = true;
}
void main()
{
    bool of;
    foreach (i; 0 .. 1)
    {
        addu(i, of);
        assert(!of); // fails
    }
}
```
Oddly, following code doesn't write anything to stdout, though the assert still
fails.
```
import std.stdio;
void addu(uint y, ref bool overflow)
{
    if (0 < y)
    {
        writeln("if (0 < y)");
        overflow = true;
    }
}
// omitted
```
--
    
    
More information about the Digitalmars-d-bugs
mailing list