Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

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

Closed
brson opened this issue Jun 28, 2016 · 3 comments
Labels
A-parser Area: The parsing of Rust source code to an AST E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-help-wanted Call for participation: Help is requested to fix this issue.

Comments

@brson
Copy link
Contributor

brson commented Jun 28, 2016

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.

@brson brson added E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-help-wanted Call for participation: Help is requested to fix this issue. labels Jun 28, 2016
@brson
Copy link
Contributor Author

brson commented Jun 28, 2016

I marked this easy assuming my solution above is workable, but it'll still take some hacking to figure out.

@brson brson added the A-parser Area: The parsing of Rust source code to an AST label Jun 28, 2016
@aravind-pg
Copy link
Contributor

I've looked into this a little, and it's not obvious how to handle this in a clean and non-invasive way. But how about the following approach?

  • parse_attribute currently takes in a boolean permit_inner that says whether the context in which it's being called permits inner attributes.
  • Change this parameter to instead be an enum, say InnerAttributeParsePolicy, that basically allows us to attach an optional reason (as a string for now) why we're not allowing inner attributes. Something of the following form:
enum InnerAttributeParsePolicy {
  Permitted,
  // the string specifies the reason in the form of
  // a recommended error message, perhaps
  NotPermitted(Option<&str>)
}

Alternatively to keep things simple for now:

enum InnerAttributeParsePolicy {
  Permitted,
  NotPermitted,
  NotPermittedFollowingDocComment
}
  • And now like you suggested, in parse_outer_attributes, before calling parse_attribute we check what the last attribute parsed was. If it was a doc comment, set the reason appropriately, otherwise leave it None.

Overall this approach doesn't look too horrible, I feel (we can bikeshed names freely later), and it has the benefit of being pretty extensible. Would really appreciate high-level feedback on the overall idea before diving into this though, being a complete beginner.

@brson
Copy link
Contributor Author

brson commented Jul 5, 2016

@aravind-pg Yes, that looks like it will work! 👍

bors added a commit that referenced this issue Jul 16, 2016
Better error message for inner attribute following doc comment

Before it was always just "an inner attribute is not permitted in this context", whereas now we add a special case for when an inner attr follows an outer attr. If the outer attr is a doc comment, then the error is "an inner attr is not permitted following a doc comment", and otherwise it's "an inner attr is not permitted following an outer attribute". In all other cases it's still  "an inner attribute is not permitted in this context".

Note that the public API and behaviour of `parse_attribute` is unchanged. Also, all new names are very open to bikeshedding -- they're arguably clunky.

Fixes #34516. cc @brson
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-parser Area: The parsing of Rust source code to an AST E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-help-wanted Call for participation: Help is requested to fix this issue.
Projects
None yet
Development

No branches or pull requests

2 participants