static assert(0) in template is a disaster

Stefan Koch uplink.coder at googlemail.com
Wed Jun 17 03:15:52 UTC 2020


On Wednesday, 17 June 2020 at 03:04:43 UTC, Avrina wrote:
> On Wednesday, 17 June 2020 at 01:34:18 UTC, Stefan Koch wrote:
>> On Wednesday, 17 June 2020 at 01:26:48 UTC, Andrei 
>> Alexandrescu wrote:
>>> On 6/16/20 7:01 PM, Paul Backus wrote:
>>>> I agree that this is a bug, but it is a bug in the design of 
>>>> the language itself, not the implementation.
>>>
>>> Is that a sort of a compile-time race condition? If that's 
>>> the case, D should deem the program ill-formed with no 
>>> diagnostic required.
>>
>> That is ......
>> I lack the words.
>> I must be misunderstanding.
>>
>> Are you saying that it is fine for D to act differently on 
>> code that used to compile fine,
>> because we don't want to detect order-dependency issues?
>>
>> Please clearify.
>
> It's more than a order-dependency issue.
>
>   pragma(msg, Foo.tupleof.length); // prints 1
>
>   struct Foo {
>  	int a;
>
>     static if(Foo.tupleof.length == 1) {
>     	int b;
>     }
>     static if(Foo.tupleof.length == 2) {
>     	int c;
>     }
>   }
>
>   pragma(msg, Foo.tupleof.length); // prints 1
>
>   void main() {
>     writeln(Foo.tupleof.length); // prints 2
>   }
>
>
>
> If you try to do the same thing with "Foo.sizeof == 4/8" in the 
> `static if`'s you get a compile error. Some attempt was made to 
> prevent this situation, but not much. It doesn't make sense to 
> probe information that isn't known at the time, or cannot be 
> known at any time in that scope. It is ill-formed.

That's because of how pragma(msg) works.

It's evaluated eagerly.
If you put a pragma(msg), inside your static if body it should 
print 2.

static if is supposed to be able to introduce declarations, if 
the code you posted qualifies as legitimately as ill-formed, I am 
afraid there is lots and lots of code. Which does classify as 
ill-formed.


More information about the Digitalmars-d mailing list