Skip to content

Clearer error messages when parser encounters an outer attribute when parsing inner attributes. #34516

Closed
@brson

Description

@brson

Consider this example:

/**
 * My mod
 */

#![recursion_limit = "100"]
fn main() {}

Produces:

error: an inner attribute is not permitted in this context
 --> <anon>:5:3
  |>
5 |> #![recursion_limit = "100"]
  |>   ^
help: place inner attribute at the top of the module or block

error: aborting due to previous error

Compilation failed.

It's quite impossible to understand what's going on if you don't know that /** */ is an outer attribute. Unfortunately after looking at the parser a bit it's not obvious what to do to make this situation clearer. Possibly parse_outer_attributes should emit a new error if its call to to parse_attribute fails. It can look at whether it has a previously-parsed attribute, and whether it was a doc comment, and emit better errors, e.g.

For non-doc comments: "inner attributes may not follow outer attributes"; for doc comments: "inner attributes may not follow outer doc-comments". A good comparison to draw is with the existing error in parser_outer_attributes when it encounters a incorrect inner doc comment. The errors should be similar for incorrect inner attributes vs. incorrect inner doc comments.

Original issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-parserArea: The lexing & parsing of Rust source code to an ASTE-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.E-help-wantedCall for participation: Help is requested to fix this issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions