RFC: Change what assert does on error

Richard (Rikki) Andrew Cattermole richard at cattermole.co.nz
Tue Jul 8 19:18:39 UTC 2025


On 09/07/2025 2:34 AM, Dennis wrote:
> On Tuesday, 8 July 2025 at 10:47:52 UTC, Richard (Rikki) Andrew 
> Cattermole wrote:
>> I've found where the compiler is implementing this, verified it.
>>
>> Its not nothrow specific.
> 
> Whether a function is nothrow affects whether a call expression 'can throw'
> 
> https://github.com/dlang/dmd/ 
> blob/9610da2443ec4ed3aeed060783e07f76287ae397/compiler/src/dmd/ 
> canthrow.d#L131-L139
> 
> Which affects whether a statement 'can throw'
> 
> https://github.com/dlang/dmd/ 
> blob/9610da2443ec4ed3aeed060783e07f76287ae397/compiler/src/dmd/ 
> blockexit.d#L101C23-L101C31
> 
> And when a 'try' statement can only fall through or halt, then a (try A; 
> finally B) gets transformed into (A; B). When the try statement 'can 
> throw' this doesn't happen.
> 
> https://github.com/dlang/dmd/ 
> blob/9610da2443ec4ed3aeed060783e07f76287ae397/compiler/src/dmd/ 
> statementsem.d#L3421-L3432
> 
> Through that path, nothrow produceds better generated code, which you 
> can easily verify by looking at assembler output of:
> 
> ```D
> void f();
> void testA() {try {f();} finally {f();}}
> 
> void g() nothrow;
> void testB() {try {g();} finally {g();}}
> ```

Yeah I've debugged all of this, and you're talking about what I found.

That simplification rewrite, is an optimization that can be removed from 
the frontend.

Right now it is contributing to the belief that Error will not run 
cleanup. Which isn't true. It does.

>> Its Exception specific, not nothrow. Its subtle, but very distinct 
>> difference.
> 
> I have no idea what this distinction is supposed to say, but "there is 
> no nothrow specific optimizations taking place" is either false or 
> pedantic about words.

Right, a nothrow specific optimization to me would mean that a function 
is marked as nothrow and therefore an optimization takes place because 
of it. The attribute comes before the optimization.

That isn't what is happening here. The compiler is going statement by 
statement, looking to see if in the execution of that statement it could 
return via an Exception exception and then when not present simplifying 
the AST. The attribute is coming after the optimization.


More information about the Digitalmars-d mailing list