Helping the compiler with assumptions while using exceptions

Timon Gehr timon.gehr at gmx.ch
Mon May 30 15:36:17 PDT 2011


> Timon Gehr:
>
>> The answer is yes, theoretically it could. (It would either have to have some very
>> advanced code analysis caps, or would just have to treat enforce specially.)
>
> Id's not so advanced stuff.
>
> Bye,
> bearophile

You are saying that analyzing a function for thrown exceptions and using the
conditions on which these exceptions are thrown in a meaningful way across
function calls is not advanced? Why don't we have it in that case?

Jonathan M Davis wrote:
> A valid point. But as long as enforce isn't inlineable, it can't be optimized.
> And with assert compiled out, I wouldn't expect it to have any affect on
> optimizations. If it did, depending on what was optimized, it could make bugs
> caused by values that would have made the assertion fail were it compiled in
> that much worse. Generally, assert is supposed to have no effect beyond
> throwing if it's compiled in and fails. Now, maybe Walter has a somewhat
> different take on that and maybe assertions could be used for optimizations,
> but the fact that they can be compiled out makes it a bit iffy.
>
> But sure, as long as it all gets reduced down to if's and flow control, then
> the optimizer can have a field day. But at present, that can't happen with
> enforce (if it ever will), and I question that it can ever be done with assert
> thanks to the guarantees that are associated with it.
>

assert does not give any guarantees whatsoever. That would be reverse. It takes
guarantees from the user.
If an assertion fails, the program is buggy no matter if it has been compiled out
or not. The behavior of the program is undefined if a compiled out assertion would
fail. It is like array bounds. If the check is disabled, the compiler can still
assume that all accesses are valid. (a smart compiler could even optimize based on
that fact ;))

@"It could make bugs so much worse." A bug is a bug, no matter what. Actually, the
optimizations based on assert could also make the bug less bad, that depends on
the case. And if the bug slipped through when testing with assertions enabled, I'd
usually like it to be bad enough to crash the program when it occurs.

Timon


More information about the Digitalmars-d-learn mailing list