Skip to content

consider expanding two-phase borrows *just a bit more* #48598

Closed
@nikomatsakis

Description

@nikomatsakis

At first, I thought this was a bug in two-phase borrows, but I realize now that this example does not fall into the subset that @pnkfelix and I identified. That said, I wonder if it should? Try it on play:

#![feature(nll)]

struct Foo<'a> {
    x: &'a u32,
}

impl<'a> Foo<'a> {
    fn method(&mut self, value: &u32) {
        
    }
}

fn main() {
    let mut f = &mut Foo { x: &22 };
    Foo::method(f, f.x)
}

This gives:

error[E0502]: cannot borrow `*f.x` as immutable because it is also borrowed as mutable
  --> src/main.rs:15:20
   |
15 |     Foo::method(f, f.x)
   |                 -  ^^^ immutable borrow occurs here
   |                 |
   |                 mutable borrow occurs here

I wonder if we should expand to auto-refs on any method argument? I think they all have the same character as self.foo(), and those would preserve the self.foo(..) => Foo::foo(self, ..) transformation more faithfully.

cc @rust-lang/wg-compiler-nll

Metadata

Metadata

Labels

A-NLLArea: Non-lexical lifetimes (NLL)I-needs-decisionIssue: In need of a decision.NLL-completeWorking towards the "valid code works" goal

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions