Closed
Description
I tried this code:
fn max_turbulence_size(arr: Vec<i32>) -> i32 {
let mut prev = 0;
let mut cnt = 0;
let mut ret = 0;
for w in arr.windows(2) {
let d = w[1] - w[0];
if d == 0 {
cnt = 0;
} else if d > 0 {
if prev < 0 {
cnt += 1;
} else {
cnt = 1;
}
} else {
if prev > 0 {
cnt += 1;
} else {
cnt = 1;
}
}
// Uncomment the follow line will give the right answer
// println!("{} {}", d, prev);
ret = ret.max(cnt);
// The follow line seems optimized out if we don't access `prev`, `d` simultaneously.
prev = d;
}
ret + 1
}
fn main() {
let v = vec![9,4,2,10,7,8,8,1,9];
let ans = max_turbulence_size(v);
// The right answer is 5. But when build with release, the output is 2
println!("{}", ans);
}
I expected to see this happen: 5 is printed.
Instead, this happened: when build with release, the output is 2.
Meta
rustc --version --verbose
:
rustc 1.58.0-nightly (2885c4748 2021-11-20)
binary: rustc
commit-hash: 2885c474823637ae69c5967327327a337aebedb2
commit-date: 2021-11-20
host: x86_64-unknown-linux-gnu
release: 1.58.0-nightly
LLVM version: 13.0.0
I also test all the following rust version at playground, and got wrong answer when build with release.
stable: 1.61.0
beta: 1.62.0-beta.6
nightly: 1.64.0-nightly (2022-06-25 20a6f3a)
Metadata
Metadata
Assignees
Labels
Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.Area: Code generationCategory: This is a bug.Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessCritical priorityRelevant to the compiler team, which will review and decide on the PR/issue.Performance or correctness regression from one stable version to another.