Skip to content

Commit 5c7d8d0

Browse files
committed
Only call collect_tokens when we have an attribute to parse
1 parent 920bed1 commit 5c7d8d0

File tree

1 file changed

+32
-26
lines changed
  • compiler/rustc_parse/src/parser

1 file changed

+32
-26
lines changed

compiler/rustc_parse/src/parser/attr.rs

+32-26
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ impl<'a> Parser<'a> {
2929
let mut attrs: Vec<ast::Attribute> = Vec::new();
3030
let mut just_parsed_doc_comment = false;
3131
loop {
32-
let (attr, tokens) = self.collect_tokens(|this| {
33-
debug!("parse_outer_attributes: self.token={:?}", this.token);
34-
if this.check(&token::Pound) {
32+
debug!("parse_outer_attributes: self.token={:?}", self.token);
33+
let (attr, tokens) = if self.check(&token::Pound) {
34+
self.collect_tokens(|this| {
3535
let inner_error_reason = if just_parsed_doc_comment {
3636
"an inner attribute is not permitted following an outer doc comment"
3737
} else if !attrs.is_empty() {
@@ -47,7 +47,9 @@ impl<'a> Parser<'a> {
4747
let attr = this.parse_attribute_with_inner_parse_policy(inner_parse_policy)?;
4848
just_parsed_doc_comment = false;
4949
Ok(Some(attr))
50-
} else if let token::DocComment(comment_kind, attr_style, data) = this.token.kind {
50+
})?
51+
} else if let token::DocComment(comment_kind, attr_style, data) = self.token.kind {
52+
self.collect_tokens(|this| {
5153
let attr =
5254
attr::mk_doc_comment(comment_kind, attr_style, data, this.token.span);
5355
if attr.style != ast::AttrStyle::Outer {
@@ -67,10 +69,11 @@ impl<'a> Parser<'a> {
6769
this.bump();
6870
just_parsed_doc_comment = true;
6971
Ok(Some(attr))
70-
} else {
71-
Ok(None)
72-
}
73-
})?;
72+
})?
73+
} else {
74+
(None, None)
75+
};
76+
7477
if let Some(mut attr) = attr {
7578
attr.tokens = tokens;
7679
attrs.push(attr);
@@ -192,26 +195,29 @@ impl<'a> Parser<'a> {
192195
crate fn parse_inner_attributes(&mut self) -> PResult<'a, Vec<ast::Attribute>> {
193196
let mut attrs: Vec<ast::Attribute> = vec![];
194197
loop {
195-
let (attr, tokens) = self.collect_tokens(|this| {
196-
// Only try to parse if it is an inner attribute (has `!`).
197-
if this.check(&token::Pound) && this.look_ahead(1, |t| t == &token::Not) {
198-
let attr = this.parse_attribute(true)?;
199-
assert_eq!(attr.style, ast::AttrStyle::Inner);
200-
Ok(Some(attr))
201-
} else if let token::DocComment(comment_kind, attr_style, data) = this.token.kind {
202-
// We need to get the position of this token before we bump.
203-
let attr =
204-
attr::mk_doc_comment(comment_kind, attr_style, data, this.token.span);
205-
if attr.style == ast::AttrStyle::Inner {
206-
this.bump();
198+
// Only try to parse if it is an inner attribute (has `!`).
199+
let (attr, tokens) =
200+
if self.check(&token::Pound) && self.look_ahead(1, |t| t == &token::Not) {
201+
self.collect_tokens(|this| {
202+
let attr = this.parse_attribute(true)?;
203+
assert_eq!(attr.style, ast::AttrStyle::Inner);
207204
Ok(Some(attr))
208-
} else {
209-
Ok(None)
210-
}
205+
})?
206+
} else if let token::DocComment(comment_kind, attr_style, data) = self.token.kind {
207+
self.collect_tokens(|this| {
208+
// We need to get the position of this token before we bump.
209+
let attr =
210+
attr::mk_doc_comment(comment_kind, attr_style, data, this.token.span);
211+
if attr.style == ast::AttrStyle::Inner {
212+
this.bump();
213+
Ok(Some(attr))
214+
} else {
215+
Ok(None)
216+
}
217+
})?
211218
} else {
212-
Ok(None)
213-
}
214-
})?;
219+
(None, None)
220+
};
215221
if let Some(mut attr) = attr {
216222
attr.tokens = tokens;
217223
attrs.push(attr);

0 commit comments

Comments
 (0)