CTFE and RTFE results differ (aliasing)

kdevel kdevel at vogtner.de
Mon May 5 17:15:57 UTC 2025


```d
//
// bs3.d
// 2025-05-05 stvo
//
// $ dmd -g -checkaction=context -unittest -main -run bs3.d
// bs3.d(25): [unittest] [4, 3, 3, 4] != [4, 3, 2, 1]
// 1/1 modules FAILED unittests
//

auto foo (ubyte [4] s)
{
    auto u = s;
    ubyte [4] tmp = u;
    u[0] = tmp [3];
    u[1] = tmp [2];
    u[2] = tmp [1];
    u[3] = tmp [0];
    return u;
}

unittest {
    enum ubyte [4] u = [1,2,3,4];
    enum r = foo (u);
    auto s = foo (u);
    assert (r == s);
}
```

According to the spec [1] the above code conforms to the CTFE 
restrictions and contains no pointers. It also does not contain 
"implementation defined behavior". Thus the result of CTFE and 
RTFE should be identical but they differ.

Suggestions appreciated!

[1] https://dlang.org/spec/function.html#interpretation
     20.22 Compile Time Function Execution (CTFE)


More information about the Digitalmars-d-learn mailing list