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

impl dyn Trait on an auto trait produces a confusing diagnostic #85026

Closed
PatchMixolydic opened this issue May 7, 2021 · 1 comment · Fixed by #88525
Closed

impl dyn Trait on an auto trait produces a confusing diagnostic #85026

PatchMixolydic opened this issue May 7, 2021 · 1 comment · Fixed by #88525
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. F-auto_traits `#![feature(auto_traits)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@PatchMixolydic
Copy link
Contributor

PatchMixolydic commented May 7, 2021

Given the following code (playground):

impl dyn Unpin {}

The current output is:

error[E0118]: no nominal type found for inherent implementation
 --> src/lib.rs:1:6
  |
1 | impl dyn Unpin {}
  |      ^^^^^^^^^ impl requires a nominal type
  |
  = note: either implement a trait on it or create a newtype to wrap it instead

This is rather confusing. It's not immediately clear what the difference between impl dyn Unpin and this working code is (playground):

trait A {}
impl dyn A {}

Since impl dyn A is accepted, the user might be confused why dyn A would be considered a nominal type when dyn Unpin isn't. The real reason why impl dyn Unpin is an error is because it is an impl block for a type [edit: a dynamically dispatched auto trait] from another crate, which should be handled by E0116.

Ideally the output should look like:

error[E0116]: cannot define inherent `impl` for a type outside of the crate where the type is defined
 --> src/lib.rs:1:1
  |
1 | impl dyn Unpin {}
  | ^^^^^^^^^^^^^^^^^ impl for type defined outside of crate.
  |
  = note: define and implement a trait or new type instead
@PatchMixolydic PatchMixolydic added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 7, 2021
@PatchMixolydic
Copy link
Contributor Author

PatchMixolydic commented May 7, 2021

It seems like this only happens with auto traits. Regular traits already emit E0116 in this case (playground):

use std::panic::UnwindSafe;
impl dyn Copy {}
impl dyn UnwindSafe {}
error[E0116]: cannot define inherent `impl` for a type outside of the crate where the type is defined
 --> src/lib.rs:3:1
  |
3 | impl dyn Copy {}
  | ^^^^^^^^^^^^^^^^ impl for type defined outside of crate.
  |
  = note: define and implement a trait or new type instead

error[E0118]: no nominal type found for inherent implementation
 --> src/lib.rs:4:6
  |
4 | impl dyn UnwindSafe {}
  |      ^^^^^^^^^^^^^^ impl requires a nominal type
  |
  = note: either implement a trait on it or create a newtype to wrap it instead

Note that this still happens with auto traits in the same crate, but in this case E0116 wouldn't be applicable (playground):

#![feature(auto_traits)]

auto trait X {}
impl dyn X {}
error[E0118]: no nominal type found for inherent implementation
 --> src/lib.rs:4:6
  |
4 | impl dyn X {}
  |      ^^^^^ impl requires a nominal type
  |
  = note: either implement a trait on it or create a newtype to wrap it instead

The diagnostic is still confusing in this case and could be rewritten to explicitly point out that impl dyn AutoTrait is not allowed.

@rustbot modify labels: +D-confusing +F-auto_traits

@rustbot rustbot added D-confusing Diagnostics: Confusing error or lint that should be reworked. F-auto_traits `#![feature(auto_traits)]` labels May 7, 2021
@PatchMixolydic PatchMixolydic changed the title impl dyn Trait on a trait from another crate produces a confusing diagnostic impl dyn Trait on an auto trait from another crate produces a confusing diagnostic May 7, 2021
@PatchMixolydic PatchMixolydic changed the title impl dyn Trait on an auto trait from another crate produces a confusing diagnostic impl dyn Trait on an auto trait produces a confusing diagnostic May 7, 2021
m-ou-se added a commit to m-ou-se/rust that referenced this issue Sep 1, 2021
…uto-trait, r=petrochenkov

fix(rustc_typeck): produce better errors for dyn auto trait

Fixes rust-lang#85026
@bors bors closed this as completed in 026322c Sep 1, 2021
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 D-confusing Diagnostics: Confusing error or lint that should be reworked. F-auto_traits `#![feature(auto_traits)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants