We make extensive use of the variants of debug_assert in our codebase. Generally, we have the expectation that our software should never panic and all edge cases should be handled gracefully using Result and the likes.
So naturally, we are very concerned when we see a panic during development. It would greatly reduce our anxiety if debug_assert could add a hint to the resulting panic that it came from a debug assert.
Here is an example (that is also available as a playground):
fn main() {
debug_assert!(false, "foobar");
assert!(false, "foobar2");
}
For debug builds the output is the following (with RUST_BACKTRACE=1):
thread 'main' panicked at src/main.rs:2:5:
foobar
stack backtrace:
0: __rustc::rust_begin_unwind
at /rustc/29483883eed69d5fb4db01964cdf2af4d86e9cb2/library/std/src/panicking.rs:697:5
1: core::panicking::panic_fmt
at /rustc/29483883eed69d5fb4db01964cdf2af4d86e9cb2/library/core/src/panicking.rs:75:14
2: playground::main
at ./src/main.rs:2:5
3: core::ops::function::FnOnce::call_once
at ./.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
Compared to hitting the regular assert! in debug mode:
thread 'main' panicked at src/main.rs:3:5:
foobar2
stack backtrace:
0: __rustc::rust_begin_unwind
at /rustc/29483883eed69d5fb4db01964cdf2af4d86e9cb2/library/std/src/panicking.rs:697:5
1: core::panicking::panic_fmt
at /rustc/29483883eed69d5fb4db01964cdf2af4d86e9cb2/library/core/src/panicking.rs:75:14
2: playground::main
at ./src/main.rs:3:5
3: core::ops::function::FnOnce::call_once
at ./.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
At no point do we know that the first assert will only occur in debug mode.
We currently fix this by adding [DEBUG ASSERT] to the beginning of each assert message, but that requires discipline / tooling to enforce.
In an ideal world, the output from the panic would indicate that an assert was "just" a debug_assert. I'm imagining something like this:
thread 'main' panicked at src/main.rs:2:5:
triggered `debug_assert`: foobar
In general, this makes me wonder if all asserts (debug and non-debug) should have output that is separate from other panics. Even more ergonomic would be to change the thread 'main' panicked at part of the output, but that is probably difficult to implement, given that assertions are implemented as macros.