Skip to content

Bug in while let and if let? #30832

@bvssvni

Description

@bvssvni

play.rust-lang.org http://is.gd/FWYYn9

use std::rc::Rc;
use std::cell::RefCell;

fn foo(a: &mut u32) -> Option<()> {
    if *a < 10 { *a += 1; Some(()) } else { None }
}

fn main() {
    let a = Rc::new(RefCell::new(4));
    while let Some(()) = foo(&mut *a.borrow_mut()) {
    // Works:
    // while let Some(()) = { let mut b = a.borrow_mut(); foo(&mut *b) } {
        println!("{}", *a.borrow());
    }
}

The RefMut seems to outlive the while let block, instead of just the right expression.

The same happens if you put it in a block, like this:

while let Some(()) = { foo(&mut *a.borrow_mut()) } {

Same problem with if let.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions