Skip to content

explain what an item is when invalid code is placed at the module-level #113110

Closed
@dawnofmidnight

Description

@dawnofmidnight

Right now, it can be somewhat confusing what an item is when one gets an error like the one below. Most people who see this error likely don't know enough about Rust to know what an item is, making it fairly unhelpful. The let example does have a better error (although notably not for let mut), but that's only one particular case.

Code

5

Current output

error: expected item, found `5`
 --> src/lib.rs:1:1
  |
1 | 5
  | ^ expected item

Desired output

error: expected item, found `5`
 --> src/main.rs:1:1
  |
1 | 5
  | ^ expected item
  | 
  | help: items are things that can appear in a module. for example:
  |     - global variables: `const`, `static`
  |     - types and traits: `struct`, `enum`, `trait`,
  |     - functions: `fn`, `async fn`
  |     - implementations: `impl`
  |     - module definitions and uses: `mod`, `use`
  | 
  | for a full list of possible items see https://doc.rust-lang.org/reference/items.html.

The above "desired output" is not an exhaustive list. In particular, it doesn't include macros which expand to items, attributes, doc comments, unsafe, extern, and a number of other potentially relevant things. I'm not sure just how exhaustive this list should be, as it's a long one and preferably shouldn't overwhelm the reader.

Another consideration is whether a list is useful at all. There are a lot of things, and especially if one is making a mistake like this, it is entirely possible that they would not know what many of the things on this list are. A link to the reference is included here, but again, the reference isn't exactly an introductory resource that one reads to learn the language.

Other cases

error: expected item, found keyword `let`
 --> src/lib.rs:1:1
  |
1 | let x = 5;
  | ^^^ consider using `const` or `static` instead of `let` for global variables

Anything else?

Other similar issues:

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an ASTD-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.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