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

NLL Regression: Conditional control flow returning from functions no longer works #51545

Open
Osspial opened this issue Jun 14, 2018 · 4 comments
Labels
A-borrow-checker Area: The borrow checker A-NLL Area: Non-lexical lifetimes (NLL) C-bug Category: This is a bug. fixed-by-polonius Compiling with `-Zpolonius` fixes this issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Osspial
Copy link

Osspial commented Jun 14, 2018

If a match expression has one arm that returns a borrowed matched value from its pattern, and another arm that doesn't borrow directly from a matched value but instead borrows the variable the matched value borrows and returns it, compilation fails if the match statement is returning from a function, but not if it's assigning to a variable. This used to work (and iirc one of the goals of NLLs was to make this pattern work), but it broke between nightly-2018-05-17 and nightly-2018-05-19. A nightly for May 18th doesn't appear to exist.

Examples:

This doesn't work now:

// Doesn't compile on nightly-2018-05-19 and later, but does on nightly-2018-05-17.
fn borrow(o: &mut Option<i32>) -> Option<&mut i32> {
    match o.as_mut() {
        Some(i) => Some(i),
        None => o.as_mut()
    }
}

This is similar to the other code, but does work both before and after nightly-2018-05-19

fn main() {
    let mut o: Option<i32> = Some(1i32);

    // Compiles everywhere!
    let x = match o.as_mut() {
        Some(i) => Some(i),
        None => o.as_mut()
    };
}
@Osspial
Copy link
Author

Osspial commented Jun 14, 2018

@matthewjasper matthewjasper added A-NLL Area: Non-lexical lifetimes (NLL) WG-compiler-nll labels Jun 14, 2018
@Osspial
Copy link
Author

Osspial commented Jun 14, 2018

More generally, this means that Problem Case 3 from the NLL RFC no longer works.

https://play.rust-lang.org/?gist=49970ae2c8f31d747f3dbbee90975c70&version=nightly&mode=debug

@Osspial Osspial changed the title NLL Regression: Reborrowing inside of a match breaks when returning borrowed value from function NLL Regression: Conditional control flow returning from functions no longer works Jun 14, 2018
@shepmaster
Copy link
Member

Oh, is this what we were talking about? Yes, this is intentional. Check out Niko's blog post:

We originally intended for NLL to accept examples like this: in the RFC, this was called Problem Case # 3. However, we had to remove that support because it was simply killing compilation times, and there were also cases where it wasn’t as precise as we wanted.

I’m happy to report that this problem is basically solved. Despite the increased precision, the Polonius analysis is now easily as fast as the existing Nightly analysis, thanks some smarter encoding of the rules as well as the move to use datafrog. We’ve not done detailed comparisons, but I consider this problem essentially solved.

If you’d like, you can try Polonius today using the -Zpolonius switch to Nightly. However, keep in mind that this would be a ‘pre-alpha’ state: there are still some known bugs that we have not prioritized fixing and so forth.

@matthewjasper matthewjasper added NLL-polonius Issues related for using Polonius in the borrow checker and removed NLL-deferred labels Dec 1, 2018
@Havvy
Copy link
Contributor

Havvy commented Mar 5, 2019

This looks like a duplicate of #21906.

@crlf0710 crlf0710 added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jun 11, 2020
@fmease fmease added A-borrow-checker Area: The borrow checker fixed-by-polonius Compiling with `-Zpolonius` fixes this issue. C-bug Category: This is a bug. and removed NLL-polonius Issues related for using Polonius in the borrow checker labels Jan 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-borrow-checker Area: The borrow checker A-NLL Area: Non-lexical lifetimes (NLL) C-bug Category: This is a bug. fixed-by-polonius Compiling with `-Zpolonius` fixes this issue. 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

7 participants