This is the **summary issue** for the `mutable_borrow_reservation_conflict` future-compatibility warning and other related errors. The goal of this page is describe why this change was made and how you can fix code that is affected by it. It also provides a place to ask questions or register a complaint if you feel the change should not be made. For more information on the policy around future-compatibility warnings, see our [breaking change policy guidelines][RFC 1589]. [RFC 1589]: https://github.com/rust-lang/rfcs/blob/master/text/1589-rustc-bug-fix-procedure.md #### What is this lint about A two-phase borrow is a mutable-borrow that is initially *reserved* at one point in the program's control-flow, and then subsequently *activated* at a later point in the control-flow. For example, given a vector `v`, `v.push(v.len())` first reserves a borrow of `v` when it evaluates the method receiver, but does not activate that borrow until later when transferring control to the `push` method itself, *after* `v.len()` has been evaluated. This lint detects instances where the reservation itself conflicts with some pre-existing borrow. For example: ```rust let mut v = vec![0, 1, 2]; let shared = &v; // ~~ // a shared borrow of `v` starts here ... v.push(shared.len()); // ~ ~~~~~~ // | ... and that shared borrow is used here... // | // ... but that comes after the reservation of the // mutable borrow of `v` here (the reservation // is subsequently activated when `push` is invoked) ``` The latter code is an example of code that was accepted when two-phased borrows (2PB) initially landed, as part of non-lexical lifetimes (NLL) deployment in the 2018 edition. This lint detects such cases, and warns that this pattern may be rejected by a future version of the compiler. This is much further discussion of this at the following places: * Issue #56254 * RalfJung blog post: https://www.ralfj.de/blog/2018/11/16/stacked-borrows-implementation.html (The history section below attempts to provide a summary of the events that led to this lint being developed and the current status of the lint.) #### How to fix this warning/error Revise the code so that the initial evaluation of the mutable borrow (the "reservation") always comes after all uses of shared borrows it conflicts with. One example revision of the example above: ```rust let mut v = vec![0, 1, 2]; let shared = &v; let len = shared.len(); v.push(len); ``` Now, the last use of `shared` comes *before* the reservation in `v.push(len)`, and thus there is no conflict between the shared borrow and the mutable borrow reservation. #### Historical background At the time NLL was stabilized, this borrowing pattern was not meant to be accepted, because it complicates the abstract model for borrows and thus poses problems for unsafe code authors and for future compiler optimizations. (How *much* complication it introduces is a matter of debate, which is in part why this restriction is being given treatment different from other future compatibility lints.) In other words: at the time that NLL was stabilized, the compiler's acceptance of this borrowing pattern was categorized by the NLL team as a "known bug". The NLL team assumed that, as a bug fix, the compiler would be allowed to start rejecting the pattern in the future. Whether a future version of the compiler rejects the code will depend on an investigation of potential abstract models of the language semantics; we will not convert the lint to a hard error without first performing an evaluation of such abstract models. ##### Timeline: * 2017-03-01 2PB [blog post](http://smallcultfollowing.com/babysteps/blog/2017/03/01/nested-method-calls-via-two-phase-borrowing/) * 2017-06-09 2PB RFC posted: https://github.com/rust-lang/rfcs/pull/2025 * 2017-08-26 2PB RFC accepted and merged https://github.com/rust-lang/rfcs/pull/2025#issuecomment-325153662 * 2017-12-06 2PB alpha implementation PRs: #46537, #47489, #48197 * 2018-02-22 2PB alpha implementation identified as overly general and broken; new beta implementation proposed #48431 * 2018-03-06 2PB beta implementation via PR #48770 * 2018-08-07: @RalfJung proposes "stacked borrows" semantic model for Rust borrowing and aliasing of references: [blog post](https://www.ralfj.de/blog/2018/08/07/stacked-borrows.html) [internals discussion](https://internals.rust-lang.org/t/stacked-borrows-an-aliasing-model-for-rust/8153) * 2018-11-16 stacked borrows implemented [blog post](https://www.ralfj.de/blog/2018/11/16/stacked-borrows-implementation.html) * 2018-11-26 @RalfJung points out that the stacked borrows model does not allow for pre-existing shares to overlap with reservations for a two-phase borrow #56254 * 2018-11-28 The NLL team decides that it should not attempt to rush to make a backportable change implementing the pre-existing share restriction. https://github.com/rust-lang/rust/issues/56254#issuecomment-442829785 * 2018-11-29 The T-lang design team discussed the matter; it did not reach consensus about whether the NLL team was correct in its assumption that it could land the restriction later (by classifying the change as a "soundness fix"). https://github.com/rust-lang/rust/issues/56254#issuecomment-442980457 * For the next four months, debate continued off and on about whether this borrowing pattern should be rejected or not. * 2019-02-25 Pull request implementing the proposed restriction is posted: PR #58739 * 2019-03-06 results from crater run with restriction posted https://github.com/rust-lang/rust/pull/58739#issuecomment-470230949 * 2019-03-07 subset of T-lang design team discusses and @pnkfelix proposed PR #58739 be merged. https://github.com/rust-lang/rust/pull/58739#issuecomment-470676858 * For next three weeks, various people both within and apart from T-lang debate whether the restriction can be landed, given that NLL with a less-restricted 2PB was stabilized. Much of that debate is encoded in comments on PR #58739, such as this summary comment from niko: https://github.com/rust-lang/rust/pull/58739#issuecomment-473078780 * 2019-03-25: compromise reached within T-lang design team: https://github.com/rust-lang/rust/pull/58739#issuecomment-476387184 #### Current status - [x] PR #58739 introduces the `mutable_borrow_reservation_conflict` lint as warn-by-default - [ ] PR #76104 makes the `mutable_borrow_reservation_conflict` lint deny-by-default - [ ] PR ? makes the `mutable_borrow_reservation_conflict` lint a hard error