Only capture tokens for items with outer attributes

Suggested by @petrochenkov in https://github.com/rust-lang/rust/issues/43081#issuecomment-633389225
This commit is contained in:
Aaron Hill 2020-05-25 15:53:41 -04:00
parent a0f06d11ae
commit 4e2696f54a
No known key found for this signature in database
GPG key ID: B4087E510E98B164
3 changed files with 18 additions and 7 deletions

View file

@ -106,11 +106,20 @@ impl<'a> Parser<'a> {
});
let mut unclosed_delims = vec![];
let (mut item, tokens) = self.collect_tokens(|this| {
let has_attrs = !attrs.is_empty();
let parse_item = |this: &mut Self| {
let item = this.parse_item_common_(attrs, mac_allowed, attrs_allowed, req_name);
unclosed_delims.append(&mut this.unclosed_delims);
item
})?;
};
let (mut item, tokens) = if has_attrs {
let (item, tokens) = self.collect_tokens(parse_item)?;
(item, Some(tokens))
} else {
(parse_item(self)?, None)
};
self.unclosed_delims.append(&mut unclosed_delims);
// Once we've parsed an item and recorded the tokens we got while
@ -127,9 +136,11 @@ impl<'a> Parser<'a> {
// it (bad!). To work around this case for now we just avoid recording
// `tokens` if we detect any inner attributes. This should help keep
// expansion correct, but we should fix this bug one day!
if let Some(item) = &mut item {
if !item.attrs.iter().any(|attr| attr.style == AttrStyle::Inner) {
item.tokens = Some(tokens);
if let Some(tokens) = tokens {
if let Some(item) = &mut item {
if !item.attrs.iter().any(|attr| attr.style == AttrStyle::Inner) {
item.tokens = Some(tokens);
}
}
}
Ok(item)