Skip to content

Unsound mir-opt level 4 with loops and .try_into().unwrap_or() #108360

New issue

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

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

Already on GitHub? # to your account

Closed
lukas-code opened this issue Feb 22, 2023 · 3 comments
Closed

Unsound mir-opt level 4 with loops and .try_into().unwrap_or() #108360

lukas-code opened this issue Feb 22, 2023 · 3 comments
Labels
A-mir-opt Area: MIR optimizations C-bug Category: This is a bug. requires-nightly This issue requires a nightly compiler in some way.

Comments

@lukas-code
Copy link
Member

I tried this code:

fn main() {
    for x in 0..1 {
        let mut value = 0u8;
        value = x.try_into().unwrap_or(42);
        assert_eq!(value, 0);
    }
}

run with

RUSTFLAGS=-Zmir-opt-level=4 cargo run

I expected to see this happen: doesn't panic

Instead, this happened:

thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `42`,
 right: `0`', src/main.rs:5:9

Meta

rustc --version --verbose:

rustc 1.69.0-nightly (246eae2fa 2023-02-21)
binary: rustc
commit-hash: 246eae2fab54a5139365c4e1a08c5724fb385fbf
commit-date: 2023-02-21
host: x86_64-unknown-linux-gnu
release: 1.69.0-nightly
LLVM version: 15.0.7

Bisect

searched nightlies: from nightly-2023-02-16 to nightly-2023-02-22
regressed nightly: nightly-2023-02-17
searched commit range: 2d14db3...9a7cc6c
regressed commit: 639377e -> #107449

bisected with cargo-bisect-rustc v0.6.5

Host triple: x86_64-unknown-linux-gnu
Reproduce with:

cargo bisect-rustc --script=script.sh 

script.sh:

#!/bin/sh
RUSTFLAGS=-Zmir-opt-level=4 cargo run

@rustbot label A-mir-opt requires-nightly

@lukas-code lukas-code added the C-bug Category: This is a bug. label Feb 22, 2023
@rustbot rustbot added A-mir-opt Area: MIR optimizations requires-nightly This issue requires a nightly compiler in some way. labels Feb 22, 2023
@lukas-code
Copy link
Member Author

lukas-code commented Feb 22, 2023

another, but this one is at level 3:

fn bar(x: usize) {
    assert_eq!(x, 42);
}

#[inline(always)]
fn saturating_mul(lhs: usize, rhs: usize) -> usize {
    match lhs.checked_mul(rhs) {
        Some(x) => x,
        None => usize::MAX,
    }
}

fn main() {
    bar(saturating_mul(1, 42));
}
RUSTFLAGS='-Zmir-opt-level=3' cargo run
thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `18446744073709551615`,
 right: `42`', src/main.rs:2:5

@cjgillot
Copy link
Contributor

Does #108208 fix those?

@lukas-code
Copy link
Member Author

Yes, looks like #108208 fixes both of these. Feel free to close this issue or use them as test cases.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-mir-opt Area: MIR optimizations C-bug Category: This is a bug. requires-nightly This issue requires a nightly compiler in some way.
Projects
None yet
Development

No branches or pull requests

3 participants