Unhelpful error message "Attempt to negate with overflow" when calling i64::abs(i64::MIN)
.
#128308
Labels
A-diagnostics
Area: Messages for errors, warnings, and lints
T-libs
Relevant to the library team, which will review and decide on the PR/issue.
Code
Current output
thread 'main' panicked at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/num/mod.rs:358:5: attempt to negate with overflow
Desired output
It would be nice to have an error that indicates that the problems is coming from
.abs()
and not, e.g.,.neg()
. A possibility:thread 'main' panicked at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/num/mod.rs:358:5: attempt to take absolute value with overflow
Rationale and extra context
The existing error message isn't entirely helpful as it suggests the problem is with negation, which it is but only indirectly. This applies to all signed integer types (
i8
,i16
, etc.).In our project we have an interpreter with both negation and absolute value instructions. When this overflow issue came up with the negation instruction it was reasonably obvious where and how to fix it, but when it came up later with the absolute value instruction it wasn't obvious where to look for the source of the problem. Since the problem was intermittent for us, it wasn't trivial to generate a scenario that consistently surfaced the problem, so a more directed error message would have been a big help.
This is somewhat related to #25378, but that's really about the semantics of
.abs()
(which is fine) and not about the error message.Other cases
No response
Rust Version
Anything else?
Rust Playground example
The text was updated successfully, but these errors were encountered: