www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 22277] New: removing strongly pure function calls is an

https://issues.dlang.org/show_bug.cgi?id=22277

          Issue ID: 22277
           Summary: removing strongly pure function calls is an incorrect
                    optimization
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: dkorpel live.nl

Currently, with -O -release and strongly pure nothrow functions, DMD likes to
remove calls to it because it determines it has 'no side effects'. This goes
wrong with:
- functions that halt the program with `assert(0)`
- fake pure functions (memory allocation)

```
import core.stdc.stdio;

void free(immutable void* mem) pure nothrow {
    debug printf("memory was freed\n");
}

noreturn panic(string msg) pure nothrow {
    assert(0, msg);
}

void assertPositive(int x) pure nothrow {
    if (x < 0) {
        assert(0);
    }
}

void main()
{
    free(null);
    assertPositive(-3);
    panic("panic!");
}
```
Compile with -O -release -debug and you'll see no output. Remove -release and
it works as intended.

I want to make GC.addRange pure so it can be used in custom allocators (issue
16982) but it's very important dmd doesn't remove calls to it.

--
Sep 04 2021