Jump to page: 1 2
Thread overview
[Issue 17226] Exception during the generation of an assert message hides AssertError
Feb 26, 2017
Jack Stouffer
Feb 26, 2017
Jack Stouffer
Mar 10, 2017
RazvanN
Jan 21, 2020
Heromyth
Nov 18, 2022
RazvanN
Nov 18, 2022
hsteoh@qfbox.info
Nov 19, 2022
Paul Backus
Nov 19, 2022
kinke
Nov 19, 2022
Ali Cehreli
Nov 20, 2022
Ali Cehreli
Nov 23, 2022
Bolpat
Dec 17, 2022
Iain Buclaw
Feb 04, 2023
Salih Dincer
February 26, 2017
https://issues.dlang.org/show_bug.cgi?id=17226

Jack Stouffer <jack@jackstouffer.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jack@jackstouffer.com

--- Comment #1 from Jack Stouffer <jack@jackstouffer.com> ---
Full example of catching the exception and still going

import std.string;
import std.stdio;

void foo(int i) {
    // In this case a %s is forgotten but it could be any other trivial error.
    assert(i == 42, format("Bad parameter:", i));
}

void main() {
    try {
        foo(43);
    } catch (Exception) {
        writeln("something threw");
    }

    writeln("This still runs despite being in undefined state");
}

Yeah, this is really, really bad.

--
February 26, 2017
https://issues.dlang.org/show_bug.cgi?id=17226

Jack Stouffer <jack@jackstouffer.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Hardware|x86_64                      |All
                 OS|Linux                       |All

--
February 26, 2017
https://issues.dlang.org/show_bug.cgi?id=17226

jiki@red.email.ne.jp changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |EH
                 CC|                            |jiki@red.email.ne.jp

--- Comment #2 from jiki@red.email.ne.jp ---
Ah, I'd just been wondering why an assert message eats its location
information.
Come on, come on :)

--
March 10, 2017
https://issues.dlang.org/show_bug.cgi?id=17226

RazvanN <razvan.nitu1305@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |razvan.nitu1305@gmail.com

--- Comment #3 from RazvanN <razvan.nitu1305@gmail.com> ---
Not sure if this is a dmd error. I think this might be druntime related :-?

--
January 21, 2020
https://issues.dlang.org/show_bug.cgi?id=17226

Heromyth <bitworld@qq.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bitworld@qq.com

--- Comment #4 from Heromyth <bitworld@qq.com> ---
We also encountered this problem. We can't find out which statement is wrong, just get an error message.

--
November 18, 2022
https://issues.dlang.org/show_bug.cgi?id=17226

--- Comment #5 from RazvanN <razvan.nitu1305@gmail.com> ---
Associated discussion about this: https://forum.dlang.org/thread/qwixdanceeupdefyqwnr@forum.dlang.org

--
November 18, 2022
https://issues.dlang.org/show_bug.cgi?id=17226

hsteoh@qfbox.info changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |hsteoh@qfbox.info

--- Comment #6 from hsteoh@qfbox.info ---
Just use compile-time format string checking:

          assert(myAssumption, format!"%s doesn't work!"(blah));

This produces a compile error:

          string s = format!"blah %d blah"(123.45);

Problem solved.

--
November 19, 2022
https://issues.dlang.org/show_bug.cgi?id=17226

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

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

--- Comment #7 from Paul Backus <snarwin+bugzilla@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.

--
November 19, 2022
https://issues.dlang.org/show_bug.cgi?id=17226

kinke <kinke@gmx.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |kinke@gmx.net

--- Comment #8 from kinke <kinke@gmx.net> ---
(In reply to Paul Backus from comment #7)
> Perhaps the simplest way to fix this is to have assert(condition, message) evaluate the message *first*

No. Changing the evaluation order would have a deep impact:

* Side effects of evaluating the msg first might change the assert condition's
outcome.
* You don't want a potentially expensive msg expression to be evaluated
unconditionally, i.e., including the regular case where the assertion holds.
* The msg expression may depend on the state after a failed condition - e.g.,
`assert(x.trySomething(), x.getLastErrorMsg())` (horrible, I know...).

--
November 19, 2022
https://issues.dlang.org/show_bug.cgi?id=17226

--- Comment #9 from Ali Cehreli <acehreli@yahoo.com> ---
I think the "invalid state" ship has sailed here because a D program by default dumps backtrace to stderr. Can that always work? No, but I think we accept it as best-effort.

I think the same applies to the assert message: A best-effort to show something meaningful. May work but it should not hide the actual issue.

--
« First   ‹ Prev
1 2