Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unhelpful error message "Attempt to negate with overflow" when calling i64::abs(i64::MIN). #128308

Open
NicMcPhee opened this issue Jul 28, 2024 · 0 comments
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.

Comments

@NicMcPhee
Copy link

NicMcPhee commented Jul 28, 2024

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?

Rust Playground example

@NicMcPhee NicMcPhee added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 28, 2024
@fmease fmease added T-libs Relevant to the library team, which will review and decide on the PR/issue. and removed T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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.
Projects
None yet
Development

No branches or pull requests

2 participants