-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Require passing an AttrWrapper
to collect_tokens_trailing_token
#81286
Conversation
r? @lcnr (rust-highfive has picked a reviewer for you, use r? to override) |
@bors try @rust-timer queue |
Awaiting bors try build completion. |
⌛ Trying commit 31748687020643dfc694f3dc9c70a6021c6b0b54 with merge 7db61ae90bf5c11a0be4801fa0c76f791029c5b4... |
☀️ Try build successful - checks-actions |
Queued 7db61ae90bf5c11a0be4801fa0c76f791029c5b4 with parent b814b63, future comparison URL. @rustbot label: +S-waiting-on-perf |
Finished benchmarking try commit (7db61ae90bf5c11a0be4801fa0c76f791029c5b4): comparison url. Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. Please note that if the perf results are neutral, you should likely undo the rollup=never given below by specifying Importantly, though, if the results of this run are non-neutral do not roll this PR up -- it will mask other regressions or improvements in the roll up. @bors rollup=never |
3174868
to
6922177
Compare
I'm unlikely to get to reviewing this until the next weekend (Feb 7). |
mem::swap(stmt_attrs, &mut attrs); | ||
stmt_attrs.extend(attrs); | ||
}); | ||
// Make sure we capture the token::Interpolated |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this necessary?
The interpolated token is going to always be replaced by its underlying tokens.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same applies to parse_item_common
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently, I don't think this is strictly necessary, since we'll end up with the same attributes and tokens even if we don't call collect_tokens_trailing_token
.
However, this will become necessary when we add PreexpTokenStream
- calling collect_tokens_trailing_token
will ensure that we properly insert any new attributes into the PreexpTokenStream
. That is, we need #[cfg(FALSE)] $item
to get removed from the PreexpTokenStream
.
Alternatively, we could remove this logic, and directly manipulate the PreexpTokenStream
(once it gets added). However, I think it's cleaner to have all of the token-related attribute handling go through collect_tokens_trailing_token
.
}?; | ||
ate_comma = this.eat(&token::Comma); | ||
// We just ate a comma, so there's no need to use | ||
// `TrailingToken::Comma` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should migrate to this scheme everywhere and eliminate TrailingToken
entirely (not in this PR).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The main obstacle to doing this will be parse_seq_to_before_token
- it relies on being able to eat the separating tokens, and performs some non-trivial recovery logic. However, I have no objection to removing TrailingToken
.
This is a pure refactoring split out from rust-lang#80689. It represents the most invasive part of that PR, requiring changes in every caller of `parse_outer_attributes` In order to eagerly expand `#[cfg]` attributes while preserving the original `TokenStream`, we need to know the range of tokens that corresponds to every attribute target. This is accomplished by making `parse_outer_attributes` return an opaque `AttrWrapper` struct. An `AttrWrapper` must be converted to a plain `AttrVec` by passing it to `collect_tokens_trailing_token`. This makes it difficult to accidentally construct an AST node with attributes without calling `collect_tokens_trailing_token`, since AST nodes store an `AttrVec`, not an `AttrWrapper`. As a result, we now call `collect_tokens_trailing_token` for attribute targets which only support inert attributes, such as generic arguments and struct fields. Currently, the constructed `LazyTokenStream` is simply discarded. Future PRs will record the token range corresponding to the attribute target, allowing those tokens to be removed from an enclosing `collect_tokens_trailing_token` call if necessary.
6922177
to
474d1a6
Compare
@petrochenkov: I've addressed your comments. I've also moved |
This comment has been minimized.
This comment has been minimized.
474d1a6
to
3321d70
Compare
@bors r+ |
📌 Commit 3321d70 has been approved by |
☀️ Test successful - checks-actions |
This is a pure refactoring split out from #80689.
It represents the most invasive part of that PR, requiring changes in
every caller of
parse_outer_attributes
In order to eagerly expand
#[cfg]
attributes while preserving theoriginal
TokenStream
, we need to know the range of tokens thatcorresponds to every attribute target. This is accomplished by making
parse_outer_attributes
return an opaqueAttrWrapper
struct. AnAttrWrapper
must be converted to a plainAttrVec
by passing it tocollect_tokens_trailing_token
. This makes it difficult to accidentallyconstruct an AST node with attributes without calling
collect_tokens_trailing_token
,since AST nodes store an
AttrVec
, not anAttrWrapper
.As a result, we now call
collect_tokens_trailing_token
for attributetargets which only support inert attributes, such as generic arguments
and struct fields. Currently, the constructed
LazyTokenStream
issimply discarded. Future PRs will record the token range corresponding
to the attribute target, allowing those tokens to be removed from an
enclosing
collect_tokens_trailing_token
call if necessary.