pragma(__ctfe)

Steven Schveighoffer schveiguy at gmail.com
Thu Sep 28 17:04:00 UTC 2023


On 9/27/23 11:30 PM, Nicholas Wilson wrote:
> I have need of the ability to suppress the code generation of functions 
> for using mixin generation functions with dcompute. Below is a link to a 
> PR to implement that functionality.
> 
> https://github.com/dlang/dmd/pull/15636
> 
> What are peoples thoughts on this?

Have you tried immediately-called lambdas?

What I need is for this to work:

```d
void foo() @nogc
{
    int[] buf;
    if (__ctfe) {
       buf = new int[10000];
    }
    else {
       buf = (cast(int*)malloc(int.sizeof * 10000))[0 .. 10000];
    }
    scope(exit) if(!__ctfe) free(buf.ptr);
    ... // use buf
}
```

We need the compiler to back-off when it comes to enforcing runtime 
attributes in compile-time-only blocks. Without it, reasonable backup 
plans don't exist for what seems like obvious allowances. BetterC CTFE 
code becomes impossible, etc.

I'd also like to see `assert(__ctfe)` just suppress code generation, as 
mentioned.

-Steve


More information about the Digitalmars-d mailing list