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

Activity

Havvy

Havvy commented on Apr 24, 2017

@Havvy
Contributor

My own findings show that this does not happen with if, only with if let.

Playpen

#![allow(dead_code)]
use std::cell::RefCell;

fn passes_compile() {
    let container = RefCell::new(0);
    if container.try_borrow().is_ok() { }
}

fn fails_compile_if_let_true() {
    let container = RefCell::new(0);
    if let true = container.try_borrow().is_ok() { }
}

fn fails_compile_match() {
    let container = RefCell::new(0);
    match container.try_borrow().is_ok() {
        true => (),
        _ => ()
    }
}
rustc 1.16.0 (30cf806ef 2017-03-10)
error: `container` does not live long enough
  --> <anon>:12:1
   |
11 |     if let true = container.try_borrow().is_ok() { }
   |                   --------- borrow occurs here
12 | }
   | ^ `container` dropped here while still borrowed
   |
   = note: values in a scope are dropped in the opposite order they are created

error: `container` does not live long enough
  --> <anon>:20:1
   |
16 |     match container.try_borrow().is_ok() {
   |           --------- borrow occurs here
...
20 | }
   | ^ `container` dropped here while still borrowed
   |
   = note: values in a scope are dropped in the opposite order they are created

error: aborting due to 2 previous errors

Furthermore, the Rust reference says that An if let expression is semantically equivalent to an if expression, so the mismatch between the first and second functions in this comment has to be a bug.

eddyb

eddyb commented on Apr 25, 2017

@eddyb
Member

Closing as duplicate of #21114 (comment). The reference is just wrong, only syntax is similar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

        Participants

        @eddyb@Havvy@MBons

        Issue actions

          if let in block expression position borrow check mismatch · Issue #41495 · rust-lang/rust