static assert(0) in template is a disaster

Avrina avrina12309412342 at gmail.com
Wed Jun 17 16:08:41 UTC 2020


On Wednesday, 17 June 2020 at 03:15:52 UTC, Stefan Koch wrote:
> 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.

It's illogical, the size of Foo changes because it is querying it 
for information before the structure is even defined. Yes I 
imagine there is probably a lot of code that compiles that 
shouldn't. There's probably a lot of silent bugs, and from time 
to time when D changes and the order the of the semantic changes, 
code that once did compile won't compile anymore because the code 
never made sense in the first place.


static assert(Foo.tupleof.length == 1);

struct Foo {
  	int a;

     static if(Foo.tupleof.length == 1) {
     	int b;
         static assert(Foo.tupleof.length == 3); // ok
     }

     static if(Foo.tupleof.length == 1) {
     	int c;
     }

     static assert(Foo.tupleof.length == 3); // ok
     static if(Foo.tupleof.length == 3) {
     	int d;
         static assert(0); // never run
     }
}



More information about the Digitalmars-d mailing list