[Issue 17226] Exception during the generation of an assert message hides AssertError

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Nov 19 14:11:39 UTC 2022


https://issues.dlang.org/show_bug.cgi?id=17226

Paul Backus <snarwin+bugzilla at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |snarwin+bugzilla at gmail.com

--- Comment #7 from Paul Backus <snarwin+bugzilla at gmail.com> ---
To be specific, the issue here is that according to the language spec:

> The first AssignExpression must evaluate to true. If it does not, an
> Assert Failure has occurred and the program enters an Invalid State.
>
> [...]
>
> Undefined Behavior: Once in an Invalid State the behavior of the
> continuing execution of the program is undefined.

i.e., once the condition has been evaluated to false, continuing to execute is
undefined *regardless* of what happens in the evaluation of the message.

Perhaps the simplest way to fix this is to have assert(condition, message)
evaluate the message *first*, so that Ali's example has behavior equivalent to
the following code:

---
import std.format;

void foo(int i) {
    auto __msg = format("Bad parameter:", i);
    assert(i == 42, __msg);
}

void main() {
    foo(43);
}
---

This way, if the message expression throws, the assert's condition is never
evaluated, and the program does not enter an invalid state.

--


More information about the Digitalmars-d-bugs mailing list