Skip to content

Regression with NLL and RefCell #47224

Closed
Closed
@abusch

Description

@abusch

Hi,

I have some code similar to the one below that compiled fine up until nightly-2018-01-03, but stopped compiling starting with nightly-2018-01-04:

#![feature(nll)]
use std::cell::RefCell;

struct Foo {
    pub v: Vec<u32>,
    pub b: u32,
}

fn f(foo: RefCell<Foo>) {
    let mut foo = foo.borrow_mut();
    
    foo.v.push(foo.b);
    
}

fn main() {
    let foo: RefCell<Foo> = RefCell::new(Foo {v: Vec::new(), b: 0});
    f(foo);
}

The error I now get is

Compiling playground v0.0.1 (file:///playground)
error[E0502]: cannot borrow `foo` as immutable because it is also borrowed as mutable
  --> src/main.rs:12:16
   |
12 |     foo.v.push(foo.b);
   |     ---        ^^^ immutable borrow occurs here
   |     |
   |     mutable borrow occurs here

error: aborting due to previous error

error: Could not compile `playground`.

To learn more, run the command again with --verbose.

I was able to work around it by coercing RefMut<T> to a &mut T using let foo = &mut *foo.borrow_mut(), and normal nll rules work as expected. But since it used to work with RefMut, I suppose it still should?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-NLLArea: Non-lexical lifetimes (NLL)C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions