Skip to content

Supertrait methods are called on inner type of object type rather than any explicit impl for the object type #10692

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
huonw opened this issue Nov 27, 2013 · 3 comments
Labels
A-trait-system Area: Trait system A-type-system Area: Type system

Comments

@huonw
Copy link
Member

huonw commented Nov 27, 2013

trait Foo: Clone {
    fn clone_as_obj(&self) -> ~Foo {
        ~(self.clone()) as ~Foo
    }
}

impl Clone for ~Foo {
    fn clone(&self) -> ~Foo {
        self.clone_as_obj();
    }
}

fn test(x: ~Foo) {
    x.clone();
}
clone-abort-3.rs:14:4: 14:14 error: cannot call a method whose type contains a self-type through an object
clone-abort-3.rs:14     x.clone();
                        ^~~~~~~~~~
error: aborting due to previous error
task 'rustc' failed at 'explicit failure', /home/huon/rust/src/libsyntax/diagnostic.rs:102
task '<main>' failed at 'explicit failure', /home/huon/rust/src/librustc/lib.rs:395

It seems like that should prefer the (~Foo).clone() method rather than trying to call the .clone that the compiler knows that the inner type must have.

@huonw
Copy link
Member Author

huonw commented Nov 27, 2013

cc @nikomatsakis (I guess).

@nikomatsakis
Copy link
Contributor

That is not how method resolution works. Object methods are preferred (for various reasons, among them that otherwise you cannot impl Trait for ~Trait). Eventually universal method call syntax should allow you to be explicit for now you can write:

fn Clone<T:Clone>(x: T) { x.clone() }

and then

Clone(x)

instead of x.clone()

@nikomatsakis
Copy link
Contributor

Closing because it is behaving as expected.

flip1995 pushed a commit to flip1995/rust that referenced this issue Sep 7, 2023
new lint: `missing_asserts_for_indexing`

Fixes rust-lang#8296

This lint looks for repeated slice indexing and suggests adding an `assert!` beforehand that helps LLVM elide bounds checks. The lint documentation has an example.

I'm not really sure what category this should be in. It seems like a nice lint for the `perf` category but I suspect this has a pretty high FP rate, so it might have to be a pedantic lint or something.
I'm also not sure about the name. If someone knows a better name for this lint, I'd be fine with changing it.

changelog: new lint [`missing_asserts_for_indexing`]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-trait-system Area: Trait system A-type-system Area: Type system
Projects
None yet
Development

No branches or pull requests

2 participants