www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - CTFE and RTFE results differ (aliasing)

reply kdevel <kdevel vogtner.de> writes:
```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)
May 05
next sibling parent monkyyy <crazymonkyyy gmail.com> writes:
On Monday, 5 May 2025 at 17:15:57 UTC, kdevel wrote:
 
even worse: ```d auto foo(ubyte[4] s){ ubyte[] u = s; ubyte[4] tmp = u; u[0] = tmp[3]; u[3] = tmp[0]; return u; } unittest{ import std; enum ubyte[4] u = [1, 2, 3, 4]; enum r = foo(u); auto s = foo(u); r.writeln; s.writeln; assert(r!=s); } ``` ``` 2.105.3: Success with output: ----- 1 modules passed unittests [1, 2, 3, 4] [4, 2, 3, 1] [216, 88, 171, 59] ----- 2.106.1: Success with output: ----- 1 modules passed unittests [1, 2, 3, 4] [4, 2, 3, 1] [136, 48, 143, 115] ----- Since 2.107.0: Success with output: ----- 1 modules passed unittests [1, 2, 3, 4] [4, 2, 3, 1] [56, 227, 42, 198] ----- ``` what fun
May 05
prev sibling next sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On Monday, 5 May 2025 at 17:15:57 UTC, kdevel wrote:
 ```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]
 ```
This is a bug, please report it. -Steve
May 05
parent kdevel <kdevel vogtner.de> writes:
On Tuesday, 6 May 2025 at 01:29:35 UTC, Steven Schveighoffer 
wrote:
 On Monday, 5 May 2025 at 17:15:57 UTC, kdevel wrote:
 ```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]
 ```
This is a bug, please report it.
I would like to yield to someone with a GH account.
May 06
prev sibling parent kdevel <kdevel vogtner.de> writes:
On Monday, 5 May 2025 at 17:15:57 UTC, kdevel wrote:
 ```
 auto foo (ubyte [4] s)
 {
    auto u = s;
    ubyte [4] tmp = u;
 ```
When I put `shared` in front of that definition the unittest passes.
 ```
    u[0] = tmp [3];
    u[1] = tmp [2];
    u[2] = tmp [1];
    u[3] = tmp [0];
    return u;
 }
 ```
I appreciate if sb. with a GH account could file the issue with the original code as bug.
May 12