assert and static assert and code generatio

Steven Schveighoffer schveiguy at gmail.com
Fri Jun 16 00:46:28 UTC 2023


On 6/15/23 5:32 PM, Cecil Ward wrote:
> On Wednesday, 14 June 2023 at 15:53:01 UTC, Steven Schveighoffer wrote:
>> On 6/14/23 11:23 AM, Cecil Ward wrote:
>>
>>> What if I say something like assert( x < 100 ); and let’s say I’m 
>>> giving the compiler information in the maximum conceivable range of 
>>> values, 0…99, so the compiler could generate better code as it now 
>>> knows that the expression x < 200 is false and let’s say that the 
>>> assert is the only source of information about the value range.
>>
>> Yes, err.... if x < 100, then x is surely < 200? But I think that's 
>> just a typo. That's the kind of thing that asserts can do, even if the 
>> assert is not generated into code.
>>
> 
> It wasn’t a typo. I intended to give an second expression that is 
> trivially always true. The point was that later if statements, even if 
> their test is not identical to the test in the assert, can be removed 
> sometimes.

Quote: "it now knows that the expression x < 200 is *false*"

That's what I assumed was the typo.

> 
> To test things out, I wrote an assume() routine which takes an argument 
> that is a bool. It uses a special GCC builtin and can make later if 
> statements go away. It worked well and seems sufficiently valuable that 
> I will try to use it a lot in future. The assume statement is like an 
> assert.

It appears that even ldc doesn't use the assert to make any deductions 
when the assert is not compiled-in. So maybe not implemented, but it 
would be to spec.

-Steve




More information about the Digitalmars-d mailing list