Skip to content
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

Better error message for why ~/@/&Trait doesn't automatically implement Trait #7914

Closed
bblum opened this issue Jul 19, 2013 · 6 comments
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-traits Area: Trait system A-typesystem Area: The type system

Comments

@bblum
Copy link
Contributor

bblum commented Jul 19, 2013

Consider the following:

trait Foo {
    fn foo();
}
impl Foo for () {
    fn foo() { }
}
fn main() {
    (&() as &Foo).foo();
}

The error is:

error: type `&Foo<no-bounds>` does not implement any method in scope named `foo`

The reason trait types do not automatically implement their own traits is that if the trait interface has a Self type variable, it does not make sense for it to be called in a heterogeneous way (e.g., if instantiating fn eq(&self, other: &Self) with int-as-Eq as the first thing, it makes no sense to pass a str-as-Self as the second.

But if the trait has no such problematic interface functions, it should (apparently doesn't yet, but should in the future -- @nikomatsakis) be possible for the user to write an impl with stub methods that do dynamic dispatch.

So, I wish for either (a) a special-case span_note that accompanies this error that instructs the user to write stubs, or (b) recognising a non-problematic trait interface that can be auto-implemented, permitting that, and having a special-case span_note that explains why not in the problematic case.

@Kimundi
Copy link
Member

Kimundi commented Jul 20, 2013

Could also maybe have a deriving mode. #[deriving_self_trait] or so.

@thestinger
Copy link
Contributor

I think auto-implementing it would be much better than deriving. We'll still need to improve the error messages to explain that Self is the issue.

@flaper87
Copy link
Contributor

flaper87 commented Mar 3, 2014

Visiting for triage. The example in the report is still valid.

@japaric
Copy link
Member

japaric commented Nov 2, 2014

Triage: Currently ICEs (see #18490), but the ICE should be fixed by #18527.

Can this be closed after #18527? We now forbid (and diagnose) creating trait objects for traits that are not object-safe.

cc @aturon @bkoropoff

@nrc
Copy link
Member

nrc commented Nov 2, 2014

We can probably close this, the intention is that &Foo will implement Foo, and in the mean time, the error should be prevented by the object safety rules.

@steveklabnik
Copy link
Member

Yes, object safety plus the soon-to-land (if it dind't already) trait object implements the trait makes this moot.

flip1995 pushed a commit to flip1995/rust that referenced this issue Nov 4, 2021
…Frednet

Advise to put a :: prefix inside the ticks

Fixes rust-lang#7914.

changelog: Keep an initial `::` when [`doc_markdown`] suggests to use ticks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-traits Area: Trait system A-typesystem Area: The type system
Projects
None yet
Development

No branches or pull requests

7 participants