AttrTarget demo #1
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The first commit creates
AttrTarget
. This passes down an enum to the attribute parsers so that they can tell what item they're on. This will allow us to move a lot of validation code from various passes directly into the parsers.The second commit adds an associated const, so that we can do position checking similar to how duplicate attributes are handled with a const of
AttributeOrder
. You just declare it, it does the rest. We can do more than theallowed!
macro; possibilities areunstable!
,future_error!
etc.For more complicated relationships between the attribute and its item, like the #[naked] attribute and function abi, or diagnostic::on_unimplemented and the item's generic parameters, you cannot use that associated const; you still need to look at
cx.target
. The vast majority of attributes won't need to do that.Random thoughts:
rustc_hir::Target
is a similar but fieldless enum. However it is quite overloaded, it is used for lang items a lot for example, so reusing it would be quite a painful refactor. It's also sort of lacking, having no distinction between a trait method and an impl method for example.rustc_ast
types is the easiest,rustc_hir
types would be more useful but complicatedlower_maybe_coroutine_body
need to have the attributes parsed, and switching lowering order here might be ugly, impractical or impossible.AttrTarget
? Putting it outside rustc_ast and rustc_hir has the advantage that it's obviously not some kind of syntax, element or data type etc.Because
ast::Struct
doesn't exist - it's just a bunch of fields inItemKind
. Do we want to refactor that into a proper ast type, just keep passing fields, or something else?