Skip to content

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

Closed
@bblum

Description

@bblum

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-trait-systemArea: Trait systemA-type-systemArea: Type system

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions