Skip to content

Print better auto-trait diagnostics when opaque return type is involved #67117

Closed

Description

The following code: (playground)

fn is_send<T: Send>(_val: T) {}
fn bar() -> impl Copy { 0 as *const () }

fn main() {
    is_send(bar());
}

gives the following error message:

error[E0277]: `*const ()` cannot be sent between threads safely
 --> src/main.rs:5:5
  |
1 | fn is_send<T: Send>(_val: T) {}
  |    -------    ---- required by this bound in `is_send`
...
5 |     is_send(bar());
  |     ^^^^^^^ `*const ()` cannot be sent between threads safely
  |
  = help: within `impl std::marker::Copy`, the trait `std::marker::Send` is not implemented for `*const ()`
  = note: required because it appears within the type `impl std::marker::Copy`

There are a couple of problems here:

  1. We refer to impl std::marker::Copy within mentioning where it's defined. Since auto traits 'leak through' impl trait, it would probably be a good idea to reference the function where it's defined to help disambiguate between multiple distinct impl SomeTrait types.
  2. We use the generic 'within' terminology, which doesn't really make that make much sense when referring to impl trait types. Since we're already exposing the underlying type in the error message, we might want to say something like:

'note: the anonymous return type impl Copy of function bar (with underlying type *const ()) does not implement std::marker::Send.'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler 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