Skip to content

Commit e481258

Browse files
committed
parser: refactoring on recovery from invalid variable declarations
Previously, the `recover_local_after_let` function was called from the body of the `recover_stmt_local` function. Unifying these two functions make it more simple and more readable.
1 parent 690addc commit e481258

File tree

1 file changed

+21
-16
lines changed
  • compiler/rustc_parse/src/parser

1 file changed

+21
-16
lines changed

compiler/rustc_parse/src/parser/stmt.rs

+21-16
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,21 @@ impl<'a> Parser<'a> {
7373
Ok(Some(if self.token.is_keyword(kw::Let) {
7474
self.parse_local_mk(lo, attrs, capture_semi, force_collect)?
7575
} else if self.is_kw_followed_by_ident(kw::Mut) && self.may_recover() {
76-
self.recover_stmt_local(lo, attrs, InvalidVariableDeclarationSub::MissingLet)?
76+
self.recover_stmt_local_after_let(lo, attrs, InvalidVariableDeclarationSub::MissingLet)?
7777
} else if self.is_kw_followed_by_ident(kw::Auto) && self.may_recover() {
7878
self.bump(); // `auto`
79-
self.recover_stmt_local(lo, attrs, InvalidVariableDeclarationSub::UseLetNotAuto)?
79+
self.recover_stmt_local_after_let(
80+
lo,
81+
attrs,
82+
InvalidVariableDeclarationSub::UseLetNotAuto,
83+
)?
8084
} else if self.is_kw_followed_by_ident(sym::var) && self.may_recover() {
8185
self.bump(); // `var`
82-
self.recover_stmt_local(lo, attrs, InvalidVariableDeclarationSub::UseLetNotVar)?
86+
self.recover_stmt_local_after_let(
87+
lo,
88+
attrs,
89+
InvalidVariableDeclarationSub::UseLetNotVar,
90+
)?
8391
} else if self.check_path() && !self.token.is_qpath_start() && !self.is_path_start_item() {
8492
// We have avoided contextual keywords like `union`, items with `crate` visibility,
8593
// or `auto trait` items. We aim to parse an arbitrary path `a::b` but not something
@@ -213,13 +221,21 @@ impl<'a> Parser<'a> {
213221
}
214222
}
215223

216-
fn recover_stmt_local(
224+
fn recover_stmt_local_after_let(
217225
&mut self,
218226
lo: Span,
219227
attrs: AttrWrapper,
220228
subdiagnostic: fn(Span) -> InvalidVariableDeclarationSub,
221229
) -> PResult<'a, Stmt> {
222-
let stmt = self.recover_local_after_let(lo, attrs)?;
230+
let stmt =
231+
self.collect_tokens_trailing_token(attrs, ForceCollect::Yes, |this, attrs| {
232+
let local = this.parse_local(attrs)?;
233+
// FIXME - maybe capture semicolon in recovery?
234+
Ok((
235+
this.mk_stmt(lo.to(this.prev_token.span), StmtKind::Local(local)),
236+
TrailingToken::None,
237+
))
238+
})?;
223239
self.sess.emit_err(InvalidVariableDeclaration { span: lo, sub: subdiagnostic(lo) });
224240
Ok(stmt)
225241
}
@@ -243,17 +259,6 @@ impl<'a> Parser<'a> {
243259
})
244260
}
245261

246-
fn recover_local_after_let(&mut self, lo: Span, attrs: AttrWrapper) -> PResult<'a, Stmt> {
247-
self.collect_tokens_trailing_token(attrs, ForceCollect::Yes, |this, attrs| {
248-
let local = this.parse_local(attrs)?;
249-
// FIXME - maybe capture semicolon in recovery?
250-
Ok((
251-
this.mk_stmt(lo.to(this.prev_token.span), StmtKind::Local(local)),
252-
TrailingToken::None,
253-
))
254-
})
255-
}
256-
257262
/// Parses a local variable declaration.
258263
fn parse_local(&mut self, attrs: AttrVec) -> PResult<'a, P<Local>> {
259264
let lo = self.prev_token.span;

0 commit comments

Comments
 (0)