Skip to content

Commit 1ae1879

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 7710656 commit 1ae1879

File tree

2 files changed

+24
-25
lines changed

2 files changed

+24
-25
lines changed

compiler/rustc_parse/src/parser/attr.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::errors;
22
use crate::fluent_generated as fluent;
33
use crate::maybe_whole;
44

5-
use super::{AttrWrapper, Capturing, FnParseMode, ForceCollect, Parser, PathStyle};
5+
use super::{AttrWrapper, Capturing, FnParseMode, ForceCollect, Parser, PathStyle, TrailingToken};
66
use rustc_ast as ast;
77
use rustc_ast::attr;
88
use rustc_ast::token::{self, Delimiter};
@@ -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 }, TrailingToken::None))
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

+19-16
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,19 @@ impl<'a> Parser<'a> {
100100
// or `auto trait` items. We aim to parse an arbitrary path `a::b` but not something
101101
// that starts like a path (1 token), but it fact not a path.
102102
// Also, we avoid stealing syntax from `parse_item_`.
103-
match force_collect {
104-
ForceCollect::Yes => {
105-
self.collect_tokens_no_attrs(|this| this.parse_stmt_path_start(lo, attrs))?
106-
}
107-
ForceCollect::No => match self.parse_stmt_path_start(lo, attrs) {
108-
Ok(stmt) => stmt,
109-
Err(mut err) => {
110-
self.suggest_add_missing_let_for_stmt(&mut err);
111-
return Err(err);
112-
}
103+
let stmt = self.collect_tokens_trailing_token(
104+
AttrWrapper::empty(),
105+
force_collect,
106+
|this, _empty_attrs| {
107+
Ok((this.parse_stmt_path_start(lo, attrs)?, TrailingToken::None))
113108
},
109+
);
110+
match stmt {
111+
Ok(stmt) => stmt,
112+
Err(mut err) => {
113+
self.suggest_add_missing_let_for_stmt(&mut err);
114+
return Err(err);
115+
}
114116
}
115117
} else if let Some(item) = self.parse_item_common(
116118
attrs.clone(),
@@ -127,12 +129,13 @@ impl<'a> Parser<'a> {
127129
self.mk_stmt(lo, StmtKind::Empty)
128130
} else if self.token != token::CloseDelim(Delimiter::Brace) {
129131
// Remainder are line-expr stmts.
130-
let e = match force_collect {
131-
ForceCollect::Yes => self.collect_tokens_no_attrs(|this| {
132-
this.parse_expr_res(Restrictions::STMT_EXPR, attrs)
133-
})?,
134-
ForceCollect::No => self.parse_expr_res(Restrictions::STMT_EXPR, attrs)?,
135-
};
132+
let e = self.collect_tokens_trailing_token(
133+
AttrWrapper::empty(),
134+
force_collect,
135+
|this, _empty_attrs| {
136+
Ok((this.parse_expr_res(Restrictions::STMT_EXPR, attrs)?, TrailingToken::None))
137+
},
138+
)?;
136139
if matches!(e.kind, ExprKind::Assign(..)) && self.eat_keyword(kw::Else) {
137140
let bl = self.parse_block()?;
138141
// Destructuring assignment ... else.

0 commit comments

Comments
 (0)