Skip to content

Unhelpful error messages for calls of methods defined in an impl with restricted bounds that aren't satisfied #20941

Closed
@huonw

Description

@huonw
struct Foo<T>(T);

impl<T> Foo<T> {
    fn new(x: T) -> Foo<T> { Foo(x) }

    // ... methods without restrictions ...

    fn restricted_good(&self) where T: Clone {}
}

impl<T: Clone> Foo<T> {
    fn restricted_bad(&self) {}
}

struct NotClone;

fn main() {
    let x = Foo::new(NotClone);
    x.restricted_good();
    x.restricted_bad();
}
<anon>:19:7: 19:24 error: the trait `core::clone::Clone` is not implemented for the type `NotClone`
<anon>:19     x.restricted_good();
                ^~~~~~~~~~~~~~~~~
<anon>:20:7: 20:23 error: type `Foo<NotClone>` does not implement any method in scope named `restricted_bad`
<anon>:20     x.restricted_bad();
                ^~~~~~~~~~~~~~~~

The _bad one is completely unhelpful, the _good is much better since it allows one to diagnose the problem. It would be nice for them both to be good.

This comes up relatively often with RefCell::<T, E>::unwrap (which requires E: Show in the impl), and Mutex::<T>::lock (which requires T: Send in the impl).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsE-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions