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

Incorrect Release Code Generation (Works in Debug) #91027

Closed
ghost opened this issue Nov 18, 2021 · 5 comments
Closed

Incorrect Release Code Generation (Works in Debug) #91027

ghost opened this issue Nov 18, 2021 · 5 comments
Labels
C-bug Category: This is a bug. I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ghost
Copy link

ghost commented Nov 18, 2021

I tried this code:

// Note: original reduced to a minimal reproducing sample
fn main() {
    let foos = 1;
    let mut iter = FooIter {foos: &foos, current: Some(Foo::A)};
    match iter.next() {
        None => println!("INCORRECT"), // Occurs in release mode
        Some(_) => println!("CORRECT") // Occurs in debug mode
    }
}

#[derive(Copy, Clone)]
pub enum Foo {
    A,
    B,
    C
}

pub struct FooIter<'a> {
    foos: &'a u32,
    current: Option<Foo>
}

impl<'a> Iterator for FooIter<'a> {
    type Item = Foo;
    
    fn next(&mut self) -> Option<Self::Item> {
        loop {
            let current = self.current?;
            self.current = None;
            
            if *self.foos == 1 {
                return Some(current)
            }
        }
    }
}

I expected to see this happen: In both debug and release builds, I expect to see "CORRECT" be printed.

Instead, this happened: The debug build correctly prints "CORRECT", but the release build incorrectly prints "INCORRECT". Minor perturbations cause the release build to print "CORRECT". Presumably the code is triggering an excessively-aggressive optimization.

Meta

  • I reproduced this on version 1.56.0, target triple x86_64-pc-windows-msvc. As this was on a different system from the one on which I am filing the bug report, I cannot show rustc --version output.
  • As a sanity check, I also reproduced this on the rust playground using 1.56.1 stable: [(https://play.rust-lang.org/?version=stable&mode=release&edition=2021&gist=af0b28cf2eafd458e51a554e8e007332)]
  • On rust playground using beta and nightly, the bug did NOT reproduce. As this does not necessarily mean it was fixed (as opposed to merely hidden), I opted to file the report anyway.

Thank you for your time and attention!

@ghost ghost added the C-bug Category: This is a bug. label Nov 18, 2021
@ehuss
Copy link
Contributor

ehuss commented Nov 19, 2021

Thanks for the report! This appears to have been fixed by #88765. I don't see which change in that update would be related to this, but maybe @rust-lang/wg-llvm can comment.

@nagisa nagisa added the I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness label Nov 19, 2021
@rustbot rustbot added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Nov 19, 2021
@ehuss
Copy link
Contributor

ehuss commented Nov 19, 2021

https://bugs.llvm.org/show_bug.cgi?id=51125 looks suspiciously similar.

@apiraino apiraino added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Nov 22, 2021
@apiraino
Copy link
Contributor

@mattelkinsTC can you report if the LLVM upgrade mentioned in this comment helps (iiuc the above comment)? thanks

@ghost
Copy link
Author

ghost commented Dec 19, 2021

I never tracked it down to the specific code generation problem, so it is hard to say whether the LLVM upgrade was what fixed it. However, the issue does NOT reproduce in 1.57, so it seems likely.

@apiraino
Copy link
Contributor

Discussed in the Zulip thread of the Prioritization Working Group.

I'm going to close this as fixed / not reproducible anymore, but please anyone feel free to reopen if I'm wrong here.

@rustbot label -I-prioritize

@rustbot rustbot removed the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Dec 30, 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. I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness 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

4 participants