Bug 22186 – [REG2.096] CTFE pure cast of function no longer allowed

Status
RESOLVED
Resolution
WORKSFORME
Severity
regression
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2021-08-06T14:05:34Z
Last change time
2023-01-14T18:31:30Z
Keywords
CTFE, industry, rejects-valid
Assigned to
No Owner
Creator
johanengelen
See also
https://issues.dlang.org/show_bug.cgi?id=23626

Comments

Comment #0 by johanengelen — 2021-08-06T14:05:34Z
Testcase: ``` //reinterpreting cast from `nothrow @nogc @system void(void delegate() nothrow @nogc @system func)*` to `pure nothrow @nogc @system void(void delegate() nothrow @nogc @system)*` import std.traits; auto as(string addAttrsStr, Func)(scope Func func) { enum addAttrs = FunctionAttribute.pure_; enum oldAttrs = functionAttributes!Func; enum intersection = oldAttrs & addAttrs; static assert(0 == intersection, Func.stringof ~ " already has attrs pure"); enum newAttrs = oldAttrs | addAttrs; static auto callFuncCtfe(Func func) { return func(); } static auto callCasted(Func func) { if(__ctfe) { return (cast(SetFunctionAttributes!(typeof(&callFuncCtfe), functionLinkage!callFuncCtfe, newAttrs))&callFuncCtfe)(func); } return (cast(SetFunctionAttributes!(typeof(func), functionLinkage!func, newAttrs))func)(); } return callCasted(func); } version = BUG_WITH_VOID; version(BUG_WITH_VOID) { auto foo() { void delegate() dg = delegate() { }; as!"pure"(dg); return 1; } } else { auto foo() { int delegate() dg = delegate() { return 1; }; as!"pure"(dg); return 1; } } enum a = foo(); ``` The compile error since 2.096 is: ``` test.d(17): Error: reinterpreting cast from `@system void(void delegate() func)*` to `pure @system void(void delegate())*` is not supported in CTFE test.d(22): called from here: `callCasted(func)` test.d(32): called from here: `as(dg)` test.d(46): called from here: `foo()` ``` Digger says this regression was introduced by: https://github.com/dlang/dmd/pull/12090
Comment #1 by moonlightsentinel — 2021-10-01T12:06:44Z
Tested with current master (2defede4b40c63528ad208b7b6dc0089be6cdcac). Probably fixed by https://github.com/dlang/dmd/pull/12514