Skip to content

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

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

Closed
gmorenz opened this issue Jan 10, 2025 · 0 comments · Fixed by #135374
Closed

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

gmorenz opened this issue Jan 10, 2025 · 0 comments · Fixed by #135374
Assignees
Labels
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.

Comments

@gmorenz
Copy link

gmorenz commented Jan 10, 2025

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

@gmorenz gmorenz 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 Jan 10, 2025
@compiler-errors compiler-errors self-assigned this Jan 11, 2025
@bors bors closed this as completed in 55503a1 Jan 12, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jan 12, 2025
Rollup merge of rust-lang#135374 - compiler-errors:typo-trait-method, r=fee1-dead

Suggest typo fix when trait path expression is typo'ed

When users write something like `Default::defualt()` (notice the typo), failure to resolve the erroneous `defualt` item will cause resolution + lowering to interpret this as a type-dependent path whose self type is `Default` which is a trait object without `dyn`, rather than a trait function like `<_ as Default>::default()`.

Try to provide a bit of guidance in this situation when we can detect the typo.

Fixes rust-lang#135349
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 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