Skip to content

Typo in trait function results in misleading error message and subpar suggestion #135349

Closed
@gmorenz

Description

@gmorenz

Code

fn foo() -> i32 {
    Default::defualt()
}

Current output

error[E0782]: expected a type, found a trait
 --> src/lib.rs:2:5
  |
2 |     Default::defualt()
  |     ^^^^^^^
  |
help: you can add the `dyn` keyword if you want a trait object
  |
2 |     <dyn Default>::defualt()
  |     ++++        +

For more information about this error, try `rustc --explain E0782`.
error: could not compile `playground` (lib) due to 1 previous error

Desired output

error[E0425]: cannot find function `defualt` in trait `Default`
 --> src/lib.rs:2:14
  |
2 |     Default::defualt()
  |              ^^^^^^^ help: a function with a similar name exists: `default`
::: /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/default/default.rs:6:1
  |
6 |     fn default() -> T {
  |     ----------------------------- similarly named function `default` defined here

Rationale and extra context

I have three complaints with the current error message:

  • It says expected a type, found a trait, but a trait as the second last component in a path is valid... everywhere. This error message seems wrong? It certainly confused me for a second.
  • The proposed <dyn Default>::defualt() would give me a trait object... except Default can't be made into a trait object precisely because it has a method that takes no parameters. I could be wrong, but I suspect <dyn Ident1>::ident2() is never valid.
  • Rustc generally does a good job at spotting typos, I was surprised to see it not notice this one.

All can be fixed at once with the proposed output, which treats the trait Default like a module named Default containing a function default. If that's not desired because it would make other error messages worse, I hope the specific complaints can at least be improved on.

Other cases

Rust Version

$ rustc --version --verbose
rustc 1.86.0-nightly (824759493 2025-01-09)
binary: rustc
commit-hash: 824759493246ee383beb9cd5ceffa0e15deb9fa4
commit-date: 2025-01-09
host: x86_64-unknown-linux-gnu
release: 1.86.0-nightly
LLVM version: 19.1.6

Anything else?

No response

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-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