Skip to content

diagnostic about typoed trait bound could be better #95208

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
ijackson opened this issue Mar 22, 2022 · 0 comments · Fixed by #95318
Closed

diagnostic about typoed trait bound could be better #95208

ijackson opened this issue Mar 22, 2022 · 0 comments · Fixed by #95318
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

@ijackson
Copy link
Contributor

Given the following code:

struct Struct<T>(T);

impl<T> Struct<T> where T:: std::fmt::Display {
}

The current output is:

error: expected one of `!`, `(`, `+`, `::`, `:`, `<`, `==`, or `=`, found `{`
 --> src/lib.rs:3:47
  |
3 | impl<T> Struct<T> where T:: std::fmt::Display {
  |                                               ^ expected one of 8 possible tokens

Better might be to add something like this:

help: perhaps you meant to specify a trait bound
  |
3 | impl<T> Struct<T> where T:: std::fmt::Display {
  |                          ^^ two colons here, but perhaps only one intended

This is the converse sitution to #84566.

I think that spotting the likely mistake location might involve trying to speculatively resolve path prefixes. Eg, here T resolves but T::fmt doesn't.

In general, syntax error messages from rustc are often poor. I wonder if it would be worth considering, as an alternative to guesswork such as the above (which has to be implmeneted separately for every kind of slip), something like:

help: the program text preceding the error was parsed like this:
  |
3 | impl<T> Struct<T> where T:: std::fmt::Display {
  |                                       ^^^^^^^ IDENTIFIER
  |                                       ^^^^^^^ PathIdentSegment
  |                                       ^^^^^^^ TypePathSegment
  |                         ^^^^^^^^^^^^^^^^^^^^^ TypePath
  |                         ^^^^^^^^^^^^^^^^^^^^^... WhereClauseItem
  |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^... WhereClause
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^... Implementation
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^... Item
  | in: Module (1..)
@ijackson ijackson 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 Mar 22, 2022
@notriddle notriddle self-assigned this Mar 25, 2022
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Mar 28, 2022
…wesleywiser

diagnostics: correct generic bounds with doubled colon

Fixes rust-lang#95208
@bors bors closed this as completed in 2a78372 Mar 28, 2022
# for free to join this conversation on GitHub. Already have an account? # 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