Skip to content

Commit

Permalink
take RHS by value
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorge Aparicio committed Mar 21, 2015
1 parent c008919 commit c95d42f
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 36 deletions.
20 changes: 10 additions & 10 deletions src/libcore/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -885,79 +885,79 @@ shr_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
#[lang = "add_assign"]
pub trait AddAssign<Rhs=Self> {
/// `+=`
fn add_assign(&mut self, &Rhs);
fn add_assign(&mut self, Rhs);
}

#[cfg(not(stage0))]
/// `&=`
#[lang = "bitand_assign"]
pub trait BitAndAssign<Rhs=Self> {
/// `&=`
fn bitand_assign(&mut self, &Rhs);
fn bitand_assign(&mut self, Rhs);
}

#[cfg(not(stage0))]
/// `|=`
#[lang = "bitor_assign"]
pub trait BitOrAssign<Rhs=Self> {
/// `|=`
fn bitor_assign(&mut self, &Rhs);
fn bitor_assign(&mut self, Rhs);
}

#[cfg(not(stage0))]
/// `^=`
#[lang = "bitxor_assign"]
pub trait BitXorAssign<Rhs=Self> {
/// `^=`
fn bitxor_assign(&mut self, &Rhs);
fn bitxor_assign(&mut self, Rhs);
}

#[cfg(not(stage0))]
/// `/=`
#[lang = "div_assign"]
pub trait DivAssign<Rhs=Self> {
/// `/=`
fn div_assign(&mut self, &Rhs);
fn div_assign(&mut self, Rhs);
}

#[cfg(not(stage0))]
/// `*=`
#[lang = "mul_assign"]
pub trait MulAssign<Rhs=Self> {
/// `*=`
fn mul_assign(&mut self, &Rhs);
fn mul_assign(&mut self, Rhs);
}

#[cfg(not(stage0))]
/// `%=`
#[lang = "rem_assign"]
pub trait RemAssign<Rhs=Self> {
/// `%=`
fn rem_assign(&mut self, &Rhs);
fn rem_assign(&mut self, Rhs);
}

#[cfg(not(stage0))]
/// `<<=`
#[lang = "shl_assign"]
pub trait ShlAssign<Rhs> {
/// `<<=`
fn shl_assign(&mut self, &Rhs);
fn shl_assign(&mut self, Rhs);
}

#[cfg(not(stage0))]
/// `>>=`
#[lang = "shr_assign"]
pub trait ShrAssign<Rhs> {
/// `>>=`
fn shr_assign(&mut self, &Rhs);
fn shr_assign(&mut self, Rhs);
}

#[cfg(not(stage0))]
/// `-=`
#[lang = "sub_assign"]
pub trait SubAssign<Rhs=Self> {
/// `-=`
fn sub_assign(&mut self, &Rhs);
fn sub_assign(&mut self, Rhs);
}

/// The `Index` trait is used to specify the functionality of indexing operations
Expand Down
9 changes: 1 addition & 8 deletions src/librustc/middle/expr_use_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trans/trans/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/feature-gate-op-assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 12 additions & 12 deletions src/test/run-pass/op-assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8> for Int {
fn shl_assign(&mut self, &rhs: &u8) {
fn shl_assign(&mut self, rhs: u8) {
self.0 <<= rhs;
}
}

impl ShlAssign<u16> for Int {
fn shl_assign(&mut self, &rhs: &u16) {
fn shl_assign(&mut self, rhs: u16) {
self.0 <<= rhs;
}
}

impl ShrAssign<u8> for Int {
fn shr_assign(&mut self, &rhs: &u8) {
fn shr_assign(&mut self, rhs: u8) {
self.0 >>= rhs;
}
}

impl ShrAssign<u16> 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;
}
}

0 comments on commit c95d42f

Please sign in to comment.