Skip to content

E0412 is correct but E0782 is emitted (in trait definitions) #116434

Closed
@Colonial-Dev

Description

@Colonial-Dev

Code

trait Foo {
    type Clone;
    fn foo() -> Clone
}

Current output

error[E0782]: trait objects must include the `dyn` keyword
 --> src/main.rs:4:17
  |
4 |     fn foo() -> Clone;
  |                 ^^^^^
  |
help: add `dyn` keyword before this trait
  |
4 |     fn foo() -> dyn Clone;
  |                 +++

For more information about this error, try `rustc --explain E0782`.

Desired output

Obviously this isn't helpful, as Clone isn't object safe. The correct error is E0412:

error[E0412]: cannot find type `Clone` in this scope
 --> src/main.rs:4:17
  |
4 |     fn foo() -> Clone;
  |                 ^^^
  |
help: you might have meant to use the associated type
  |
4 |     fn foo() -> Self::Clone;
  |                 ++++++

For more information about this error, try `rustc --explain E0412`.

Rationale and extra context

E0412 is correctly emitted if the associated type name is changed to not collide with any traits currently in scope. So:

trait Foo {
    type Bar;
    fn foo() -> Bar
}

Errors as expected.

I've also seen this confuse new users "in the wild" on Rustcord - that's what originally inspired me to file this issue.

Other cases

No response

Anything else?

No response

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`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