Skip to content

Gracefully recover from incorrectly nested items #66127

Closed
@estebank

Description

@estebank

The parser currently only attempts to parse items and sub-items based on what the parent item is and pretty much gives up if, for example, a trait item is inside of an ADT, or an ADT is defined inside an ADT. The parser could try exhausting all the valid possible parses, and if it fails start trying to parse other cases that are invalid there and explain why it is not allowed and supply guidance on what to do instead. For example, a case taken from https://www.reddit.com/r/rust/comments/5tnjt8/question_regarding_nested_enum/:

enum Foo {
    enum Bar {
         Baz,
     },
    Bat,
}

gives

error: expected identifier, found keyword `enum`
 --> src/lib.rs:2:5
  |
2 |     enum Bar {
  |     ^^^^ expected identifier, found keyword
  |
help: you can escape reserved keywords to use them as identifiers
  |
2 |     r#enum Bar {
  |     ^^^^^^

error: missing comma
 --> src/lib.rs:2:9
  |
2 |     enum Bar {
  |         ^ help: missing comma

error: expected `:`, found `,`
 --> src/lib.rs:3:13
  |
3 |          Baz,
  |             ^ expected `:`

when instead it could give a single error saying enum definitions cannot be nested, or something to that effect.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an ASTA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.P-lowLow priorityT-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