Skip to content

if let in block expression position borrow check mismatch #41495

Closed
@MBons

Description

@MBons

I've ran into a borrowing issue using try_borrow() in an if let statement.

The problem occurs when it's the last expression of the scope and is resolved by making it a statement.
I'm not sure if this is a bug or limitation in the borrow checker, but either way the error message is confusing.

rust play link

fn does_not_compile() {
    let container = RefCell::new(0);
    if let Ok(_) = container.try_borrow() { }
}

fn compiles() {
    let container = RefCell::new(0);
    if let Ok(_) = container.try_borrow() { };
}

rustc 1.16.0 (30cf806 2017-03-10)
error: container does not live long enough
--> :6:1
|
5 | if let Ok(_) = container.try_borrow() { }
| --------- borrow occurs here
6 | }
| ^ container dropped here while still borrowed
|
= note: values in a scope are dropped in the opposite order they are created

error: aborting due to previous error

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions