Skip to content

Commit

Permalink
Only call collect_tokens when we have an attribute to parse
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron1011 committed Oct 22, 2020
1 parent 920bed1 commit 5c7d8d0
Showing 1 changed file with 32 additions and 26 deletions.
58 changes: 32 additions & 26 deletions compiler/rustc_parse/src/parser/attr.rs
Expand Up @@ -29,9 +29,9 @@ impl<'a> Parser<'a> {
let mut attrs: Vec<ast::Attribute> = Vec::new();
let mut just_parsed_doc_comment = false;
loop {
let (attr, tokens) = self.collect_tokens(|this| {
debug!("parse_outer_attributes: self.token={:?}", this.token);
if this.check(&token::Pound) {
debug!("parse_outer_attributes: self.token={:?}", self.token);
let (attr, tokens) = if self.check(&token::Pound) {
self.collect_tokens(|this| {
let inner_error_reason = if just_parsed_doc_comment {
"an inner attribute is not permitted following an outer doc comment"
} else if !attrs.is_empty() {
Expand All @@ -47,7 +47,9 @@ impl<'a> Parser<'a> {
let attr = this.parse_attribute_with_inner_parse_policy(inner_parse_policy)?;
just_parsed_doc_comment = false;
Ok(Some(attr))
} else if let token::DocComment(comment_kind, attr_style, data) = this.token.kind {
})?
} else if let token::DocComment(comment_kind, attr_style, data) = self.token.kind {
self.collect_tokens(|this| {
let attr =
attr::mk_doc_comment(comment_kind, attr_style, data, this.token.span);
if attr.style != ast::AttrStyle::Outer {
Expand All @@ -67,10 +69,11 @@ impl<'a> Parser<'a> {
this.bump();
just_parsed_doc_comment = true;
Ok(Some(attr))
} else {
Ok(None)
}
})?;
})?
} else {
(None, None)
};

if let Some(mut attr) = attr {
attr.tokens = tokens;
attrs.push(attr);
Expand Down Expand Up @@ -192,26 +195,29 @@ impl<'a> Parser<'a> {
crate fn parse_inner_attributes(&mut self) -> PResult<'a, Vec<ast::Attribute>> {
let mut attrs: Vec<ast::Attribute> = vec![];
loop {
let (attr, tokens) = self.collect_tokens(|this| {
// Only try to parse if it is an inner attribute (has `!`).
if this.check(&token::Pound) && this.look_ahead(1, |t| t == &token::Not) {
let attr = this.parse_attribute(true)?;
assert_eq!(attr.style, ast::AttrStyle::Inner);
Ok(Some(attr))
} else if let token::DocComment(comment_kind, attr_style, data) = this.token.kind {
// We need to get the position of this token before we bump.
let attr =
attr::mk_doc_comment(comment_kind, attr_style, data, this.token.span);
if attr.style == ast::AttrStyle::Inner {
this.bump();
// Only try to parse if it is an inner attribute (has `!`).
let (attr, tokens) =
if self.check(&token::Pound) && self.look_ahead(1, |t| t == &token::Not) {
self.collect_tokens(|this| {
let attr = this.parse_attribute(true)?;
assert_eq!(attr.style, ast::AttrStyle::Inner);
Ok(Some(attr))
} else {
Ok(None)
}
})?
} else if let token::DocComment(comment_kind, attr_style, data) = self.token.kind {
self.collect_tokens(|this| {
// We need to get the position of this token before we bump.
let attr =
attr::mk_doc_comment(comment_kind, attr_style, data, this.token.span);
if attr.style == ast::AttrStyle::Inner {
this.bump();
Ok(Some(attr))
} else {
Ok(None)
}
})?
} else {
Ok(None)
}
})?;
(None, None)
};
if let Some(mut attr) = attr {
attr.tokens = tokens;
attrs.push(attr);
Expand Down

0 comments on commit 5c7d8d0

Please sign in to comment.