www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 22186] New: [REG2.096] CTFE pure cast of function no longer

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

          Issue ID: 22186
           Summary: [REG2.096] CTFE pure cast of function no longer
                    allowed
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: regression
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: johanengelen weka.io

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

--
Aug 06 2021