Request for Comment assert(__ctfe)

Meta jared771 at gmail.com
Sun Apr 5 20:54:03 UTC 2020


On Sunday, 5 April 2020 at 20:31:15 UTC, Timon Gehr wrote:
> On 05.04.20 14:11, Stefan Koch wrote:
>> Hi Guys,
>> 
>> I am currently working on speeding up dmd. In the presence of 
>> templates and ctfe.
>> and one thing that stood out is that we do codegen for symbols 
>> which are never used.
>> (and hopefully eliminated by a smart linker but eh ...)
>> 
>> I've seen the following pattern in the wild.
>> 
>> 
>> string GenerateMixin(string[] params)
>> {
>>      assert(__ctfe);
>>      .... more code ....
>> }
>> 
>> that means if we see assert(__ctfe) inside the outermost scope 
>> of a function body ( not in a branch) then we know that this 
>> function would assert if called at runtime.
>
> Make sure it works with `in` contracts:
>
> string generateMixin(string[] params)in{
>     assert(__ctfe);
> }do{
>     .... more code ....
> }
>
> (The plain assertion slightly hurts my eyes, because if it 
> fails, that failure would be attributable to the GenerateMixin 
> function, whereas with the `in` contract it is clear that the 
> caller is at fault.)

We likely get the shorthand version for free as well:
string generateMixin(...)
in(__ctfe)
{
    ...
}


More information about the Digitalmars-d mailing list