From c95d42fb5f8169f0c5492745ea2b38cb755d504d Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Fri, 20 Mar 2015 23:38:35 -0500 Subject: [PATCH] take RHS by value --- src/libcore/ops.rs | 20 ++++++++-------- src/librustc/middle/expr_use_visitor.rs | 9 +------ src/librustc_trans/trans/expr.rs | 2 +- src/librustc_typeck/check/mod.rs | 2 +- .../compile-fail/feature-gate-op-assign.rs | 2 +- ...{op-assign-borrow.rs => op-assign-move.rs} | 6 ++--- src/test/run-pass/op-assign.rs | 24 +++++++++---------- 7 files changed, 29 insertions(+), 36 deletions(-) rename src/test/compile-fail/{op-assign-borrow.rs => op-assign-move.rs} (80%) diff --git a/src/libcore/ops.rs b/src/libcore/ops.rs index 4fd0c0f1561b0..bea8baeb1d442 100644 --- a/src/libcore/ops.rs +++ b/src/libcore/ops.rs @@ -885,7 +885,7 @@ shr_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize } #[lang = "add_assign"] pub trait AddAssign { /// `+=` - fn add_assign(&mut self, &Rhs); + fn add_assign(&mut self, Rhs); } #[cfg(not(stage0))] @@ -893,7 +893,7 @@ pub trait AddAssign { #[lang = "bitand_assign"] pub trait BitAndAssign { /// `&=` - fn bitand_assign(&mut self, &Rhs); + fn bitand_assign(&mut self, Rhs); } #[cfg(not(stage0))] @@ -901,7 +901,7 @@ pub trait BitAndAssign { #[lang = "bitor_assign"] pub trait BitOrAssign { /// `|=` - fn bitor_assign(&mut self, &Rhs); + fn bitor_assign(&mut self, Rhs); } #[cfg(not(stage0))] @@ -909,7 +909,7 @@ pub trait BitOrAssign { #[lang = "bitxor_assign"] pub trait BitXorAssign { /// `^=` - fn bitxor_assign(&mut self, &Rhs); + fn bitxor_assign(&mut self, Rhs); } #[cfg(not(stage0))] @@ -917,7 +917,7 @@ pub trait BitXorAssign { #[lang = "div_assign"] pub trait DivAssign { /// `/=` - fn div_assign(&mut self, &Rhs); + fn div_assign(&mut self, Rhs); } #[cfg(not(stage0))] @@ -925,7 +925,7 @@ pub trait DivAssign { #[lang = "mul_assign"] pub trait MulAssign { /// `*=` - fn mul_assign(&mut self, &Rhs); + fn mul_assign(&mut self, Rhs); } #[cfg(not(stage0))] @@ -933,7 +933,7 @@ pub trait MulAssign { #[lang = "rem_assign"] pub trait RemAssign { /// `%=` - fn rem_assign(&mut self, &Rhs); + fn rem_assign(&mut self, Rhs); } #[cfg(not(stage0))] @@ -941,7 +941,7 @@ pub trait RemAssign { #[lang = "shl_assign"] pub trait ShlAssign { /// `<<=` - fn shl_assign(&mut self, &Rhs); + fn shl_assign(&mut self, Rhs); } #[cfg(not(stage0))] @@ -949,7 +949,7 @@ pub trait ShlAssign { #[lang = "shr_assign"] pub trait ShrAssign { /// `>>=` - fn shr_assign(&mut self, &Rhs); + fn shr_assign(&mut self, Rhs); } #[cfg(not(stage0))] @@ -957,7 +957,7 @@ pub trait ShrAssign { #[lang = "sub_assign"] pub trait SubAssign { /// `-=` - fn sub_assign(&mut self, &Rhs); + fn sub_assign(&mut self, Rhs); } /// The `Index` trait is used to specify the functionality of indexing operations diff --git a/src/librustc/middle/expr_use_visitor.rs b/src/librustc/middle/expr_use_visitor.rs index d4a3e6c561ea1..7945185da2232 100644 --- a/src/librustc/middle/expr_use_visitor.rs +++ b/src/librustc/middle/expr_use_visitor.rs @@ -585,14 +585,7 @@ impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,'tcx,TYPER> { ast::ExprAssignOp(_, ref lhs, ref rhs) => { self.mutate_expr(expr, &**lhs, WriteAndRead); - - if self.typer.is_method_call(expr.id) { - let r = ty::ReScope(region::CodeExtent::from_node_id(expr.id)); - self.borrow_expr(rhs, r, ty::ImmBorrow, OverloadedOperator); - } else { - // built-in assignment operations consume the RHS - self.consume_expr(&**rhs); - } + self.consume_expr(&**rhs); } ast::ExprRepeat(ref base, ref count) => { diff --git a/src/librustc_trans/trans/expr.rs b/src/librustc_trans/trans/expr.rs index 23212dcab0333..60f1fdbec1dcd 100644 --- a/src/librustc_trans/trans/expr.rs +++ b/src/librustc_trans/trans/expr.rs @@ -1048,7 +1048,7 @@ fn trans_rvalue_stmt_unadjusted<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, let src_datum = unpack_datum!(bcx, trans(bcx, &**src)); trans_overloaded_op(bcx, expr, MethodCall::expr(expr.id), dst, vec![(src_datum, src.id)], None, - true).bcx + false).bcx } } ast::ExprInlineAsm(ref a) => { diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 0e72f3086f50c..de9dc8efb9294 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -3087,7 +3087,7 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>, ast_util::binop_to_string(op.node), actual) }, lhs_resolved_t, None) - }, AutorefArgs::Yes) + }, AutorefArgs::No) } fn check_user_unop<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, diff --git a/src/test/compile-fail/feature-gate-op-assign.rs b/src/test/compile-fail/feature-gate-op-assign.rs index f368198cae871..adec762e42171 100644 --- a/src/test/compile-fail/feature-gate-op-assign.rs +++ b/src/test/compile-fail/feature-gate-op-assign.rs @@ -13,7 +13,7 @@ use std::ops::AddAssign; struct MyInt(i32); impl AddAssign for MyInt { - fn add_assign(&mut self, rhs: &MyInt) { + fn add_assign(&mut self, rhs: MyInt) { self.0 += rhs.0 } } diff --git a/src/test/compile-fail/op-assign-borrow.rs b/src/test/compile-fail/op-assign-move.rs similarity index 80% rename from src/test/compile-fail/op-assign-borrow.rs rename to src/test/compile-fail/op-assign-move.rs index cc8091bd2304c..a6a1b7437bf7a 100644 --- a/src/test/compile-fail/op-assign-borrow.rs +++ b/src/test/compile-fail/op-assign-move.rs @@ -15,16 +15,16 @@ use std::ops::AddAssign; struct Int(i32); impl AddAssign for Int { - fn add_assign(&mut self, rhs: &Int) { + fn add_assign(&mut self, rhs: Int) { self.0 += rhs.0 } } fn main() { let mut x = Int(1); - x //~ error: cannot borrow `x` as mutable because it is also borrowed as immutable + x //~ error: use of moved value: `x` += - x; //~ note: previous borrow of `x` occurs here + x; //~ note: `x` moved here because it has type `Int`, which is non-copyable let y = Int(2); y //~ error: cannot borrow immutable local variable `y` as mutable diff --git a/src/test/run-pass/op-assign.rs b/src/test/run-pass/op-assign.rs index 855d530825158..145027a26423e 100644 --- a/src/test/run-pass/op-assign.rs +++ b/src/test/run-pass/op-assign.rs @@ -65,73 +65,73 @@ fn main() { } impl AddAssign for Int { - fn add_assign(&mut self, rhs: &Int) { + fn add_assign(&mut self, rhs: Int) { self.0 += rhs.0; } } impl BitAndAssign for Int { - fn bitand_assign(&mut self, rhs: &Int) { + fn bitand_assign(&mut self, rhs: Int) { self.0 &= rhs.0; } } impl BitOrAssign for Int { - fn bitor_assign(&mut self, rhs: &Int) { + fn bitor_assign(&mut self, rhs: Int) { self.0 |= rhs.0; } } impl BitXorAssign for Int { - fn bitxor_assign(&mut self, rhs: &Int) { + fn bitxor_assign(&mut self, rhs: Int) { self.0 ^= rhs.0; } } impl DivAssign for Int { - fn div_assign(&mut self, rhs: &Int) { + fn div_assign(&mut self, rhs: Int) { self.0 /= rhs.0; } } impl MulAssign for Int { - fn mul_assign(&mut self, rhs: &Int) { + fn mul_assign(&mut self, rhs: Int) { self.0 *= rhs.0; } } impl RemAssign for Int { - fn rem_assign(&mut self, rhs: &Int) { + fn rem_assign(&mut self, rhs: Int) { self.0 %= rhs.0; } } impl ShlAssign for Int { - fn shl_assign(&mut self, &rhs: &u8) { + fn shl_assign(&mut self, rhs: u8) { self.0 <<= rhs; } } impl ShlAssign for Int { - fn shl_assign(&mut self, &rhs: &u16) { + fn shl_assign(&mut self, rhs: u16) { self.0 <<= rhs; } } impl ShrAssign for Int { - fn shr_assign(&mut self, &rhs: &u8) { + fn shr_assign(&mut self, rhs: u8) { self.0 >>= rhs; } } impl ShrAssign for Int { - fn shr_assign(&mut self, &rhs: &u16) { + fn shr_assign(&mut self, rhs: u16) { self.0 >>= rhs; } } impl SubAssign for Int { - fn sub_assign(&mut self, rhs: &Int) { + fn sub_assign(&mut self, rhs: Int) { self.0 -= rhs.0; } }