Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
lilyball opened this issue Mar 8, 2014 · 2 comments
Closed

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

lilyball opened this issue Mar 8, 2014 · 2 comments

Comments

@lilyball
Copy link
Contributor

lilyball commented Mar 8, 2014

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();
                                 ^~~~~~~~~
@sfackler
Copy link
Member

sfackler commented Mar 8, 2014

Dup of #10672

@sfackler sfackler closed this as completed Mar 8, 2014
@lilyball
Copy link
Contributor Author

lilyball commented Mar 8, 2014

@sfackler Thanks. I tried to find a pre-existing issue, and was surprised when I couldn't, but github's search really sucks.

flip1995 pushed a commit to flip1995/rust that referenced this issue Nov 7, 2024
…, r=Centri3

fix incorrect suggestion for `!(a >= b) as i32 == c`

fixes rust-lang#12761

The expression `!(a >= b) as i32 == c` got simplified to `a < b as i32 == c`, but this is a syntax error.
The result we want is `(a < b) as i32 == c`.
This is fixed by adding a parenthesis to the suggestion given in `check_simplify_not` when the boolean expression is casted.

changelog: [`nonminimal_bool`]: fix incorrect suggestion for `!(a >= b) as i32 == c`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants