Skip to content

Error message for non-fully-qualified method lookup does not contain helpful information related to auto traits. #90664

Open
@BGR360

Description

@BGR360

This is spun off from #90601.

Given the following code (playground):

trait Foo {
    fn foo(self);
}

impl<T: Send> Foo for T {
    fn foo(self) {
        println!("foo");
    }
}

struct NotSend(Box<dyn ToString>);

fn main() {
    NotSend(Box::new("hello")).foo();
}

The current output is:

error[E0599]: the method `foo` exists for struct `NotSend`, but its trait bounds were not satisfied
  --> src/main.rs:14:32
   |
11 | struct NotSend(Box<dyn ToString>);
   | ----------------------------------
   | |
   | method `foo` not found for this
   | doesn't satisfy `NotSend: Foo`
   | doesn't satisfy `NotSend: Send`
...
14 |     NotSend(Box::new("hello")).foo();
   |                                ^^^ method cannot be called on `NotSend` due to unsatisfied trait bounds
   |
   = note: the following trait bounds were not satisfied:
           `NotSend: Send`
           which is required by `NotSend: Foo`
           `&NotSend: Send`
           which is required by `&NotSend: Foo`
           `&mut NotSend: Send`
           which is required by `&mut NotSend: Foo`
note: the following trait must be implemented

By contrast, if you fully qualify the method path, you get an explanation for why the auto trait Send does not apply to your type (playground):

error[E0277]: `(dyn ToString + 'static)` cannot be sent between threads safely
  --> src/main.rs:14:14
   |
14 |     Foo::foo(NotSend(Box::new("hello")));
   |     -------- ^^^^^^^^^^^^^^^^^^^^^^^^^^ `(dyn ToString + 'static)` cannot be sent between threads safely
   |     |
   |     required by a bound introduced by this call
   |
   = help: the trait `Send` is not implemented for `(dyn ToString + 'static)`
   = note: required because of the requirements on the impl of `Send` for `Unique<(dyn ToString + 'static)>`
   = note: required because it appears within the type `Box<(dyn ToString + 'static)>`
note: required because it appears within the type `NotSend`
  --> src/main.rs:11:8
   |
11 | struct NotSend(Box<dyn ToString>);
   |        ^^^^^^^
note: required because of the requirements on the impl of `Foo` for `NotSend`
  --> src/main.rs:5:15
   |
5  | impl<T: Send> Foo for T {
   |               ^^^     ^
note: required by `Foo::foo`
  --> src/main.rs:2:5
   |
2  |     fn foo(self);
   |     ^^^^^^^^^^^^^

I don't really have a great plan for how to integrate that extra information into the first error, but here's one proposal:

error[E0599]: the method `foo` exists for struct `NotSend`, but its trait bounds were not satisfied
  --> src/main.rs:14:32
   |
11 | struct NotSend(Box<dyn ToString>);
   | ----------------------------------
   | |
   | method `foo` not found for this
   | doesn't satisfy `NotSend: Foo`
   | doesn't satisfy `NotSend: Send`
...
14 |     NotSend(Box::new("hello")).foo();
   |                                ^^^ method cannot be called on `NotSend` due to unsatisfied trait bounds
   |
   = note: the following trait bounds were not satisfied:
           `NotSend: Send`
           which is required by `NotSend: Foo`
           `&NotSend: Send`
           which is required by `&NotSend: Foo`
           `&mut NotSend: Send`
           which is required by `&mut NotSend: Foo`
note: the trait `Send` is not implemented for `(dyn ToString + 'static)`
note: required because of the requirements on the impl of `Send` for `Unique<(dyn ToString + 'static)>`
note: required because it appears within the type `Box<(dyn ToString + 'static)>`
note: required because it appears within the type `NotSend`
  --> src/main.rs:11:8
   |
11 | struct NotSend(Box<dyn ToString>);
   |        ^^^^^^^

cc #13231

@rustbot label +A-traits +A-typesystem +D-terse +F-auto_traits

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-trait-systemArea: Trait systemA-type-systemArea: Type systemD-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.F-auto_traits`#![feature(auto_traits)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions