Skip to content

Commit 3b65133

Browse files
committed
extract recover_pat_ident_mut_first
1 parent c69b3ed commit 3b65133

File tree

1 file changed

+20
-16
lines changed
  • src/libsyntax/parse/parser

1 file changed

+20
-16
lines changed

src/libsyntax/parse/parser/pat.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -140,22 +140,7 @@ impl<'a> Parser<'a> {
140140
// Parse _
141141
PatKind::Wild
142142
} else if self.eat_keyword(kw::Mut) {
143-
// Parse mut ident @ pat / mut ref ident @ pat
144-
let mutref_span = self.prev_span.to(self.token.span);
145-
let binding_mode = if self.eat_keyword(kw::Ref) {
146-
self.diagnostic()
147-
.struct_span_err(mutref_span, "the order of `mut` and `ref` is incorrect")
148-
.span_suggestion(
149-
mutref_span,
150-
"try switching the order",
151-
"ref mut".into(),
152-
Applicability::MachineApplicable
153-
).emit();
154-
BindingMode::ByRef(Mutability::Mutable)
155-
} else {
156-
BindingMode::ByValue(Mutability::Mutable)
157-
};
158-
self.parse_pat_ident(binding_mode)?
143+
self.recover_pat_ident_mut_first()?
159144
} else if self.eat_keyword(kw::Ref) {
160145
// Parse ref ident @ pat / ref mut ident @ pat
161146
let mutbl = self.parse_mutability();
@@ -338,6 +323,25 @@ impl<'a> Parser<'a> {
338323
})
339324
}
340325

326+
// Recover on `mut ref? ident @ pat` and suggest that the order of `mut` and `ref` is incorrect.
327+
fn recover_pat_ident_mut_first(&mut self) -> PResult<'a, PatKind> {
328+
let mutref_span = self.prev_span.to(self.token.span);
329+
let binding_mode = if self.eat_keyword(kw::Ref) {
330+
self.struct_span_err(mutref_span, "the order of `mut` and `ref` is incorrect")
331+
.span_suggestion(
332+
mutref_span,
333+
"try switching the order",
334+
"ref mut".into(),
335+
Applicability::MachineApplicable
336+
)
337+
.emit();
338+
BindingMode::ByRef(Mutability::Mutable)
339+
} else {
340+
BindingMode::ByValue(Mutability::Mutable)
341+
};
342+
self.parse_pat_ident(binding_mode)
343+
}
344+
341345
// Helper function to decide whether to parse as ident binding
342346
// or to try to do something more complex like range patterns.
343347
fn parse_as_ident(&mut self) -> bool {

0 commit comments

Comments
 (0)