Skip to content

&mut self to &mut Trait moves self #8813

Closed
@alexcrichton

Description

@alexcrichton

This code shows some odd behavior:

trait A {}
struct B;
impl A for B {}

fn foo(_: &mut A) {}

impl B {
    fn good(&mut self) {
        self.foo();
        foo(&mut *self as &mut A);
        self.foo();
    }

    fn bad(&mut self) {
        self.foo();
        foo(self as &mut A);
        self.foo();
    }

    fn foo(&mut self) {}
}

fn main() {}

Because good works, I would expect bad to work as well.

$ rustc test.rs
test.rs:17:8: 17:13 error: use of moved value: `self`
test.rs:17         self.foo();
                   ^~~~~
test.rs:16:12: 16:19 note: `self` moved here because it has type `&mut B`, 
which is non-copyable (perhaps you meant to use clone()?)
test.rs:16         foo(self as &mut A);
                       ^~~~~~~
error: aborting due to previous error

cc @nikomatsakis

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions