Request for Comment assert(__ctfe)

Steven Schveighoffer schveiguy at gmail.com
Wed Apr 8 13:26:36 UTC 2020


On 4/8/20 3:09 AM, Johannes Pfau wrote:
> Am Tue, 07 Apr 2020 17:13:54 -0400 schrieb Steven Schveighoffer:
> 
>> On 4/7/20 12:53 PM, Johannes Pfau wrote:
>>> The example above actually already compiles with -betterC, as the check
>>> is done in codegen phase! But some betterC checks seem to be done in
>>> semantic. This doesn't work yet:
>>>
>>> ------------------------
>>> string generateMixin(T)(string b)
>>> {
>>>       assert(__ctfe);
>>>       return typeid(T).stringof ~ " " ~ b ~ ";";
>>> }
>>>
>>> void main() @nogc {
>>>       mixin(generateMixin!string("b"));
>>> }
>>> ------------------------
>>> dmd -betterC test.d -c test.d(4): Error: TypeInfo cannot be used with
>>> -betterC
>>
>> Ironically, ctfe can't deal with TypeInfo today.
>>
>> In the past, the issue was for functions that use TypeInfo is that the
>> non-CTFE branch uses TypeInfo, and that can't be used in betterC, but
>> the CTFE branch doesn't use TypeInfo, but the compiler wants to build
>> the whole function.
>>
>> So while this is still a problem, I don't think this fix will change
>> that.
>>
> 
> I'm not sure what exactly you're referring to, but this code compiles
> perfactly fine:
> --------------------------
>   string generateMixin(T)(string b)
>   {
>        assert(__ctfe);
>        return "auto a = " ~typeid(T).stringof ~ ";";
>   }
>   
>   void main() @nogc
>   {
>        mixin(generateMixin!string("b"));
>        pragma(msg, generateMixin!string("b"));
>   }
> --------------------------
> 

I misread your example! I thought you were doing typeid(T).toString. I 
had to fix an issue very recently that was similar to this.

TypeInfo does not work inside ctfe, but here you aren't actually using 
the TypeInfo object. If you switch to typeid(T).toString, even without 
-betterC you get:

Error: static variable typeid(string) cannot be read at compile time

But your example is indeed a bug with betterC. But even if that was 
fixed, the code would still not work. It seems more like a diagnostic error:

return "auto a = typeid(" ~ T.stringof ~ ");";

This is equivalent to your code and will work in betterC, but the mixin 
of it won't.

> But it does not copile with -betterC. The point here is that the "Error:
> TypeInfo cannot be used with -betterC" message does not apply to CTFE-
> only functions, so this PR should be able to disable the check.

In fact, there is a further error that TypeInfo isn't being used AT ALL, 
and it's still complaining.

> 
> My primary fear here is that the current implementation checks the
> function too late in semantic and therefore such checks (or the "ensure
> no non-ctfe function calls ctfe-only functions" check are much more
> difficult to implement than they should be. Using a pragma or attribute
> would easily solve this.

This is a legitimate concern. Indeed, it might not fix this issue just 
to not emit the code to the binary.

-Steve


More information about the Digitalmars-d mailing list