Skip to content

Commit 4158a1c

Browse files
committed
Only check force_collect in collect_tokens_trailing_token.
There are three places where we currently check `force_collect` and call `collect_tokens_no_attrs` for `ForceCollect::Yes` and a vanilla parsing function for `ForceCollect::No`. But we can instead just pass in `force_collect` and let `collect_tokens_trailing_token` do the appropriate thing.
1 parent 9d908a2 commit 4158a1c

File tree

2 files changed

+21
-24
lines changed

2 files changed

+21
-24
lines changed

compiler/rustc_parse/src/parser/attr.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,12 @@ impl<'a> Parser<'a> {
251251
pub fn parse_attr_item(&mut self, force_collect: ForceCollect) -> PResult<'a, ast::AttrItem> {
252252
maybe_whole!(self, NtMeta, |attr| attr.into_inner());
253253

254-
let do_parse = |this: &mut Self| {
254+
let do_parse = |this: &mut Self, _empty_attrs| {
255255
let is_unsafe = this.eat_keyword(kw::Unsafe);
256256
let unsafety = if is_unsafe {
257257
let unsafe_span = this.prev_token.span;
258258
this.psess.gated_spans.gate(sym::unsafe_attributes, unsafe_span);
259259
this.expect(&token::OpenDelim(Delimiter::Parenthesis))?;
260-
261260
ast::Safety::Unsafe(unsafe_span)
262261
} else {
263262
ast::Safety::Default
@@ -268,13 +267,10 @@ impl<'a> Parser<'a> {
268267
if is_unsafe {
269268
this.expect(&token::CloseDelim(Delimiter::Parenthesis))?;
270269
}
271-
Ok(ast::AttrItem { unsafety, path, args, tokens: None })
270+
Ok((ast::AttrItem { unsafety, path, args, tokens: None }, false))
272271
};
273-
// Attr items don't have attributes
274-
match force_collect {
275-
ForceCollect::Yes => self.collect_tokens_no_attrs(do_parse),
276-
ForceCollect::No => do_parse(self),
277-
}
272+
// Attr items don't have attributes.
273+
self.collect_tokens_trailing_token(AttrWrapper::empty(), force_collect, do_parse)
278274
}
279275

280276
/// Parses attributes that appear after the opening of an item. These should

compiler/rustc_parse/src/parser/stmt.rs

+17-16
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,17 @@ impl<'a> Parser<'a> {
9999
// or `auto trait` items. We aim to parse an arbitrary path `a::b` but not something
100100
// that starts like a path (1 token), but it fact not a path.
101101
// Also, we avoid stealing syntax from `parse_item_`.
102-
match force_collect {
103-
ForceCollect::Yes => {
104-
self.collect_tokens_no_attrs(|this| this.parse_stmt_path_start(lo, attrs))?
102+
let stmt = self.collect_tokens_trailing_token(
103+
AttrWrapper::empty(),
104+
force_collect,
105+
|this, _empty_attrs| Ok((this.parse_stmt_path_start(lo, attrs)?, false)),
106+
);
107+
match stmt {
108+
Ok(stmt) => stmt,
109+
Err(mut err) => {
110+
self.suggest_add_missing_let_for_stmt(&mut err);
111+
return Err(err);
105112
}
106-
ForceCollect::No => match self.parse_stmt_path_start(lo, attrs) {
107-
Ok(stmt) => stmt,
108-
Err(mut err) => {
109-
self.suggest_add_missing_let_for_stmt(&mut err);
110-
return Err(err);
111-
}
112-
},
113113
}
114114
} else if let Some(item) = self.parse_item_common(
115115
attrs.clone(),
@@ -126,12 +126,13 @@ impl<'a> Parser<'a> {
126126
self.mk_stmt(lo, StmtKind::Empty)
127127
} else if self.token != token::CloseDelim(Delimiter::Brace) {
128128
// Remainder are line-expr stmts.
129-
let e = match force_collect {
130-
ForceCollect::Yes => self.collect_tokens_no_attrs(|this| {
131-
this.parse_expr_res(Restrictions::STMT_EXPR, attrs)
132-
})?,
133-
ForceCollect::No => self.parse_expr_res(Restrictions::STMT_EXPR, attrs)?,
134-
};
129+
let e = self.collect_tokens_trailing_token(
130+
AttrWrapper::empty(),
131+
force_collect,
132+
|this, _empty_attrs| {
133+
Ok((this.parse_expr_res(Restrictions::STMT_EXPR, attrs)?, false))
134+
},
135+
)?;
135136
if matches!(e.kind, ExprKind::Assign(..)) && self.eat_keyword(kw::Else) {
136137
let bl = self.parse_block()?;
137138
// Destructuring assignment ... else.

0 commit comments

Comments
 (0)