Unhelpful error message "Attempt to negate with overflow" when calling `i64::abs(i64::MIN)`.
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
Code
fn main() {
println!("Absolute value of `i64::MIN` = {}", i64::MIN.abs());
}
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
rustc 1.80.0 (051478957 2024-07-21)
binary: rustc
commit-hash: 051478957371ee0084a7c0913941d2a8c4757bb9
commit-date: 2024-07-21
host: aarch64-apple-darwin
release: 1.80.0
LLVM version: 18.1.7
Anything else?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the example and start at library/core/src/num/mod.rs:358, the location shown in the panic. Trace the i64::abs overflow path and check the behavior across the signed integer types mentioned in the issue. Done means the overflow panic identifies absolute value rather than negation, with the relevant cases verified.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100