Open ghost opened 7 years ago
Regarding your patch: rustc's const evaluator has nothing whatsoever to do with the runtime panic message: Instead, when in debug mode, rustc will insert overflow checks for every multiplication / addition / subtraction.
Thanks @TimNN ! Any idea where to look for the code that inserts overflow checks?
I agree that this would be desirable. Would love to see a PR with an implementation!
I found the code that codegens the panic into the executable: https://github.com/rust-lang/rust/blob/ceb2512144d1fc26330e85fb9d41c22ba1866259/src/librustc_codegen_ssa/mir/block.rs#L354-L396
and this very line(from the above):
https://github.com/rust-lang/rust/blob/ceb2512144d1fc26330e85fb9d41c22ba1866259/src/librustc_codegen_ssa/mir/block.rs#L396
is the one that gets the value of the error message string attempt to multiply with overflow
which can be seen at:
https://github.com/rust-lang/rust/blob/ceb2512144d1fc26330e85fb9d41c22ba1866259/src/librustc/mir/interpret/error.rs#L416
and is part of the panic message that happens at runtime.
So if I'm interpreting this correctly:
https://github.com/rust-lang/rust/blob/ceb2512144d1fc26330e85fb9d41c22ba1866259/src/librustc_codegen_ssa/mir/block.rs#L331
then the condition whether or not it overflowed is already a bool(or possibly something else?) there, so the operands of the mul
or even the mul
operation itself cannot be accessed from here.
And since I basically don't understand 95% of what's going on or what most code does here, and I'm still only at chapter 10(is next) in my rustbook reading(altho many months ago I once I went as far as(but not including) that chapter on how to build your own grep tool - oh it's chapter 12), I'm going to conclude that it's basically impossible(if at least, for me) to show the lhs,op and rhs in the panic message, simply because they aren't accessible from the context where the if cond then panic
block is being codegenned into the executable. (I've only some vague understanding of this, based mostly on deductions - could someone please recommend a link to read about this? uuuu, I found something and this)
EDIT: Ha! I might actually be (gladly!) wrong! due to my interpretation of this comment // Don't codegen the panic block if success if known.
which to me means that the mul
operation is also codegen-ned(a few lines later) thus the lhs,op and rhs would be accessible maybe they're somehow inside cond
(whatever type/struct(?) that is it)
EDIT2: progressing here: https://gist.github.com/xftroxgpx/c34b67bb43ef3ca6fbd75971c460f541
Looks like this is already in stable rust 1.76.0 (at least), as I'm getting this output:
Compiling playground v0.0.1 (/playground)
error: this arithmetic operation will overflow
--> src/main.rs:7:20
|
7 | println!("{}", 2*innum);
| ^^^^^^^ attempt to compute `2_i8 * 122_i8`, which would overflow
|
= note: `#[deny(arithmetic_overflow)]` on by default
error: could not compile `playground` (bin "playground") due to 1 previous error
Therefore the operands are shown, so this issue can be closed. Thanks all!
Is it possible to show the two operands at runtime when this panic occurs ? thread 'main' panicked at 'attempt to multiply with overflow', src/main.rs:7 So maybe we can see
2*122
in the panic message?example code
code with backtrace
output
```rust Compiling playground v0.0.1 (file:///playground) Finished dev [unoptimized + debuginfo] target(s) in 0.62 secs Running `target/debug/playground` thread 'main' panicked at 'attempt to multiply with overflow', src/main.rs:9:20 stack backtrace: 0: std::sys::imp::backtrace::tracing::imp::unwind_backtrace at /checkout/src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:49 1: std::sys_common::backtrace::_print at /checkout/src/libstd/sys_common/backtrace.rs:71 2: std::panicking::default_hook::{{closure}} at /checkout/src/libstd/sys_common/backtrace.rs:60 at /checkout/src/libstd/panicking.rs:380 3: std::panicking::default_hook at /checkout/src/libstd/panicking.rs:396 4: std::panicking::rust_panic_with_hook at /checkout/src/libstd/panicking.rs:610 5: std::panicking::begin_panic at /checkout/src/libstd/panicking.rs:571 6: std::panicking::begin_panic_fmt at /checkout/src/libstd/panicking.rs:521 7: rust_begin_unwind at /checkout/src/libstd/panicking.rs:497 8: core::panicking::panic_fmt at /checkout/src/libcore/panicking.rs:71 9: core::panicking::panic at /checkout/src/libcore/panicking.rs:51 10: playground::main at src/main.rs:9 11: __rust_maybe_catch_panic at /checkout/src/libpanic_unwind/lib.rs:98 12: std::rt::lang_start at /checkout/src/libstd/panicking.rs:458 at /checkout/src/libstd/panic.rs:361 at /checkout/src/libstd/rt.rs:61 13: main 14: __libc_start_main 15: _start ```EDIT: what I tried and had no effect whatsoever: