Skip to content

diagnostic about typoed trait bound could be better #95208

Closed
@ijackson

Description

@ijackson

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..)

Metadata

Metadata

Assignees

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