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

Signed integer division can panic even when overflow checks are disabled #86099

Closed
syvb opened this issue Jun 7, 2021 · 2 comments
Closed

Signed integer division can panic even when overflow checks are disabled #86099

syvb opened this issue Jun 7, 2021 · 2 comments
Labels
C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@syvb
Copy link
Contributor

syvb commented Jun 7, 2021

I tried this code with -C overflow-checks=off:

#[allow(arithmetic_overflow)]
fn main() {
    i32::MIN % -1;
    i32::MIN / -1;
}

I expected to see this happen: It does not panic since overflow checks are disabled, and per the reference this doesn't panic when overflow checks are disabled

Instead, this happened: It panics with attempt to calculate the remainder with overflow

Meta

rustc --version --verbose:

$ rustc +local --version --verbose
rustc 1.54.0-dev
binary: rustc
commit-hash: unknown
commit-date: unknown
host: x86_64-unknown-linux-gnu
release: 1.54.0-dev
LLVM version: 12.0.1
Backtrace

thread 'main' panicked at 'attempt to calculate the remainder with overflow', ov.rs:3:5
stack backtrace:
   0: rust_begin_unwind
             at /home/smit/rustc-dev/rust/library/std/src/panicking.rs:515:5
   1: core::panicking::panic_fmt
             at /home/smit/rustc-dev/rust/library/core/src/panicking.rs:92:14
   2: core::panicking::panic
             at /home/smit/rustc-dev/rust/library/core/src/panicking.rs:50:5
   3: ov::main
   4: core::ops::function::FnOnce::call_once
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

I'm not sure if the issue is with rustc or the reference here.

@syvb syvb added the C-bug Category: This is a bug. label Jun 7, 2021
@nagisa nagisa added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jun 7, 2021
@luqmana
Copy link
Member

luqmana commented Jun 17, 2021

RFC 560: Integer overflow says:

The operations /, % for the arguments INT_MIN and -1 will unconditionally panic. This is unconditional for legacy reasons.

Hence, it's by design at the moment. See rust-lang/rfcs#1276

@syvb
Copy link
Contributor Author

syvb commented Jun 17, 2021

See rust-lang/reference#1049 for the PR to the reference.

@syvb syvb closed this as completed Jun 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants