www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 13341] New: Wrong optimization for ref parameters and if

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 puremagic.com
          Reporter: sinkuu 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
```

--
Aug 20 2014