Skip to content

Mutable reference not re-borrowed by binary operator #25753

@Stebalien

Description

@Stebalien

When implementing a binary operator on &mut Something, the binary operator consumes the mutable reference instead of borrowing it:

use std::ops::Shl;

struct Test;

impl<'a> Shl<()> for &'a mut Test {
    type Output = ();
    fn shl(self, rhs: ()) {}
}

// Compiles
fn test_direct() {
    let mut test = Test;
    let test_ref = &mut test;
    test_ref.shl(());
    test_ref.shl(());
}

// Doesn't compile
//
// ```
// error: use of moved value: `test_ref`
// test_ref << ();
// ^~~~~~~~
// note: `test_ref` moved here because it has type `&mut Test`, which is non-copyable
// test_ref << ();
// ```
fn test_shift() {
    let mut test = Test;
    let test_ref = &mut test;
    test_ref << (); // Should be equivalent to test_ref.shl(())
    test_ref << ();
}

fn main() {}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-type-systemArea: Type systemC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions