Skip to content

Commit ac82683

Browse files
authored
Rollup merge of rust-lang#48197 - bobtwinkles:two_phase_borrow_on_ops, r=nikomatsakis
Allow two-phase borrows of &mut self in ops We need two-phase borrows of ops to be in the initial NLL release since without them lots of existing code will break. Fixes rust-lang#48129. CC @pnkfelix and @nikomatsakis r? @pnkfelix
2 parents 1a4b5fb + 7062955 commit ac82683

File tree

3 files changed

+54
-61
lines changed

3 files changed

+54
-61
lines changed

src/librustc_typeck/check/op.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
201201
let mutbl = match mt.mutbl {
202202
hir::MutImmutable => AutoBorrowMutability::Immutable,
203203
hir::MutMutable => AutoBorrowMutability::Mutable {
204-
// For initial two-phase borrow
205-
// deployment, conservatively omit
206-
// overloaded binary ops.
207-
allow_two_phase_borrow: false,
204+
// Allow two-phase borrows for binops in initial deployment
205+
// since they desugar to methods
206+
allow_two_phase_borrow: true,
208207
}
209208
};
210209
let autoref = Adjustment {
@@ -219,10 +218,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
219218
let mutbl = match mt.mutbl {
220219
hir::MutImmutable => AutoBorrowMutability::Immutable,
221220
hir::MutMutable => AutoBorrowMutability::Mutable {
222-
// For initial two-phase borrow
223-
// deployment, conservatively omit
224-
// overloaded binary ops.
225-
allow_two_phase_borrow: false,
221+
// Allow two-phase borrows for binops in initial deployment
222+
// since they desugar to methods
223+
allow_two_phase_borrow: true,
226224
}
227225
};
228226
let autoref = Adjustment {

src/test/compile-fail/borrowck/two-phase-nonrecv-autoref.rs

-53
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
// #![feature(rustc_attrs)]
3131

3232
use std::ops::{Index, IndexMut};
33-
use std::ops::{AddAssign, SubAssign, MulAssign, DivAssign, RemAssign};
34-
use std::ops::{BitAndAssign, BitOrAssign, BitXorAssign, ShlAssign, ShrAssign};
3533

3634
// This is case outlined by Niko that we want to ensure we reject
3735
// (at least initially).
@@ -186,56 +184,6 @@ fn coerce_index_op() {
186184
//[nll]~^^ ERROR cannot borrow `i` as immutable because it is also borrowed as mutable [E0502]
187185
}
188186

189-
struct A(i32);
190-
191-
macro_rules! trivial_binop {
192-
($Trait:ident, $m:ident) => {
193-
impl $Trait<i32> for A { fn $m(&mut self, rhs: i32) { self.0 = rhs; } }
194-
}
195-
}
196-
197-
trivial_binop!(AddAssign, add_assign);
198-
trivial_binop!(SubAssign, sub_assign);
199-
trivial_binop!(MulAssign, mul_assign);
200-
trivial_binop!(DivAssign, div_assign);
201-
trivial_binop!(RemAssign, rem_assign);
202-
trivial_binop!(BitAndAssign, bitand_assign);
203-
trivial_binop!(BitOrAssign, bitor_assign);
204-
trivial_binop!(BitXorAssign, bitxor_assign);
205-
trivial_binop!(ShlAssign, shl_assign);
206-
trivial_binop!(ShrAssign, shr_assign);
207-
208-
fn overloaded_binops() {
209-
let mut a = A(10);
210-
a += a.0;
211-
//[lxl]~^ ERROR cannot use `a.0` because it was mutably borrowed
212-
//[nll]~^^ ERROR cannot use `a.0` because it was mutably borrowed
213-
a -= a.0;
214-
//[lxl]~^ ERROR cannot use `a.0` because it was mutably borrowed
215-
//[nll]~^^ ERROR cannot use `a.0` because it was mutably borrowed
216-
a *= a.0;
217-
//[lxl]~^ ERROR cannot use `a.0` because it was mutably borrowed
218-
//[nll]~^^ ERROR cannot use `a.0` because it was mutably borrowed
219-
a /= a.0;
220-
//[lxl]~^ ERROR cannot use `a.0` because it was mutably borrowed
221-
//[nll]~^^ ERROR cannot use `a.0` because it was mutably borrowed
222-
a &= a.0;
223-
//[lxl]~^ ERROR cannot use `a.0` because it was mutably borrowed
224-
//[nll]~^^ ERROR cannot use `a.0` because it was mutably borrowed
225-
a |= a.0;
226-
//[lxl]~^ ERROR cannot use `a.0` because it was mutably borrowed
227-
//[nll]~^^ ERROR cannot use `a.0` because it was mutably borrowed
228-
a ^= a.0;
229-
//[lxl]~^ ERROR cannot use `a.0` because it was mutably borrowed
230-
//[nll]~^^ ERROR cannot use `a.0` because it was mutably borrowed
231-
a <<= a.0;
232-
//[lxl]~^ ERROR cannot use `a.0` because it was mutably borrowed
233-
//[nll]~^^ ERROR cannot use `a.0` because it was mutably borrowed
234-
a >>= a.0;
235-
//[lxl]~^ ERROR cannot use `a.0` because it was mutably borrowed
236-
//[nll]~^^ ERROR cannot use `a.0` because it was mutably borrowed
237-
}
238-
239187
fn main() {
240188

241189
// As a reminder, this is the basic case we want to ensure we handle.
@@ -256,5 +204,4 @@ fn main() {
256204

257205
coerce_unsized();
258206
coerce_index_op();
259-
overloaded_binops();
260207
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// revisions: lxl nll
12+
13+
#![cfg_attr(nll, feature(nll))]
14+
15+
use std::ops::{AddAssign, SubAssign, MulAssign, DivAssign, RemAssign};
16+
use std::ops::{BitAndAssign, BitOrAssign, BitXorAssign, ShlAssign, ShrAssign};
17+
18+
struct A(i32);
19+
20+
macro_rules! trivial_binop {
21+
($Trait:ident, $m:ident) => {
22+
impl $Trait<i32> for A { fn $m(&mut self, rhs: i32) { self.0 = rhs; } }
23+
}
24+
}
25+
26+
trivial_binop!(AddAssign, add_assign);
27+
trivial_binop!(SubAssign, sub_assign);
28+
trivial_binop!(MulAssign, mul_assign);
29+
trivial_binop!(DivAssign, div_assign);
30+
trivial_binop!(RemAssign, rem_assign);
31+
trivial_binop!(BitAndAssign, bitand_assign);
32+
trivial_binop!(BitOrAssign, bitor_assign);
33+
trivial_binop!(BitXorAssign, bitxor_assign);
34+
trivial_binop!(ShlAssign, shl_assign);
35+
trivial_binop!(ShrAssign, shr_assign);
36+
37+
fn main() {
38+
let mut a = A(10);
39+
a += a.0;
40+
a -= a.0;
41+
a *= a.0;
42+
a /= a.0;
43+
a &= a.0;
44+
a |= a.0;
45+
a ^= a.0;
46+
a <<= a.0;
47+
a >>= a.0;
48+
}

0 commit comments

Comments
 (0)