Stream: t-compiler/wg-polonius

Topic: multiple borrows for nested mutable method calls


Jake Goulding (Jan 24 2020 at 18:58, on Zulip):
expr.mul_value(expr.signal(1), 6.0);
cannot borrow `expr` as mutable more than once at a time

I know that NLL in general fixed this for outer mutable, inner immutable, but why doesn't it solve it for 2x mutable

Jake Goulding (Jan 24 2020 at 19:02, on Zulip):

repro:

struct Example;

impl Example {
    pub fn inner(&mut self) {}
    pub fn outer(&mut self, _: ()) {}
}

fn main() {
    let mut ex = Example;
    ex.outer(ex.inner());
}
nikomatsakis (Feb 14 2020 at 18:37, on Zulip):

@Jake Goulding the idea of 2PB (two-phase borrow) was that the outer &mut is treated like an & borrow until it is actualy used, more or less

nikomatsakis (Feb 14 2020 at 18:37, on Zulip):

this makes it compatible with an inner &

nikomatsakis (Feb 14 2020 at 18:37, on Zulip):

because two & mix

nikomatsakis (Feb 14 2020 at 18:37, on Zulip):

but it is not compatible with an inner &mut

nikomatsakis (Feb 14 2020 at 18:38, on Zulip):

if we wanted to support that, we'd have had to add a new kind of borrow -- i.e., an "unactivated" &mut wouldn't act like an &, it would act like something else (&const, maybe... "somebody else can mutate")

nikomatsakis (Feb 14 2020 at 18:38, on Zulip):

it's less clear that this is ok

nikomatsakis (Feb 14 2020 at 18:38, on Zulip):

and it seemed to add more concepts

nikomatsakis (Feb 14 2020 at 18:38, on Zulip):

so we opted not to support it

nikomatsakis (Feb 14 2020 at 18:38, on Zulip):

but yeah sometimes it's annoying

Jake Goulding (Feb 14 2020 at 18:39, on Zulip):

You know, I never actually thought about what "2 phase" actually meant, but that's a pretty clear description!

lqd (Feb 17 2020 at 08:58, on Zulip):

(one of the nicest post about 2PB is https://smallcultfollowing.com/babysteps/blog/2017/03/01/nested-method-calls-via-two-phase-borrowing/ -- which is more descriptive than say https://rust-lang.github.io/rustc-guide/borrow_check/two_phase_borrows.html -- even though it's still unclear to me how close/far the actual implementation ended up being in practice, especially related to the stacked borrows model and #59159)

Last update: Jul 02 2020 at 18:25UTC