assert and static assert and code generatio

FeepingCreature feepingcreature at gmail.com
Thu Jun 15 08:55:49 UTC 2023


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.
>
> -Steve

I think the idea is more that:

```
assert(x < 100);
...
if (x < 200) {
```
And the `if` should be optimized into a constant `if (true)`.
In other words, if DMD does value range propagation based on 
asserts.


More information about the Digitalmars-d mailing list