Skip to content

Trait objects cannot call methods that take self by-value #12761

Closed
@lilyball

Description

@lilyball

As was brought up in this reddit post, Trait objects cannot call methods that take self by-value. Even worse, the error message erroneously claims the desired method does not exist.

It seems to me that trait objects of the form ~Trait should work with self-by-value methods. And trait objects of the form &Trait should produce an appropriate error message (just like calling the same method on a reference does).

Example:

trait Foo {
    fn foo(self) -> int;
}

struct Bar;
impl Foo for Bar {
    fn foo(self) -> int { 42i }
}

fn main() {
    let bar = ~Bar as ~Foo;
    let _x: int = bar.foo();
}

This produces:

Untitled.rs:12:19: 12:28 error: type `~Foo:Send` does not implement any method in scope named `foo`
Untitled.rs:12     let _x: int = bar.foo();
                                 ^~~~~~~~~

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions