Skip to content

Commit 90793c0

Browse files
committed
extract parse_pat_deref
1 parent a4af9d1 commit 90793c0

File tree

1 file changed

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

1 file changed

+16
-12
lines changed

src/libsyntax/parse/parser/pat.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,7 @@ impl<'a> Parser<'a> {
109109

110110
let lo = self.token.span;
111111
let pat = match self.token.kind {
112-
token::BinOp(token::And) | token::AndAnd => {
113-
// Parse &pat / &mut pat
114-
self.expect_and()?;
115-
let mutbl = self.parse_mutability();
116-
if let token::Lifetime(name) = self.token.kind {
117-
let mut err = self.fatal(&format!("unexpected lifetime `{}` in pattern", name));
118-
err.span_label(self.token.span, "unexpected lifetime");
119-
return Err(err);
120-
}
121-
let subpat = self.parse_pat_with_range_pat(false, expected)?;
122-
PatKind::Ref(subpat, mutbl)
123-
}
112+
token::BinOp(token::And) | token::AndAnd => self.parse_pat_deref(expected)?,
124113
token::OpenDelim(token::Paren) => {
125114
// Parse a tuple or parenthesis pattern.
126115
let (fields, trailing_comma) = self.parse_paren_comma_seq(|p| p.parse_pat(None))?;
@@ -332,6 +321,21 @@ impl<'a> Parser<'a> {
332321
Ok(pat)
333322
}
334323

324+
/// Parse `&pat` / `&mut pat`.
325+
fn parse_pat_deref(&mut self, expected: Option<&'static str>) -> PResult<'a, PatKind> {
326+
self.expect_and()?;
327+
let mutbl = self.parse_mutability();
328+
329+
if let token::Lifetime(name) = self.token.kind {
330+
let mut err = self.fatal(&format!("unexpected lifetime `{}` in pattern", name));
331+
err.span_label(self.token.span, "unexpected lifetime");
332+
return Err(err);
333+
}
334+
335+
let subpat = self.parse_pat_with_range_pat(false, expected)?;
336+
Ok(PatKind::Ref(subpat, mutbl))
337+
}
338+
335339
// Helper function to decide whether to parse as ident binding
336340
// or to try to do something more complex like range patterns.
337341
fn parse_as_ident(&mut self) -> bool {

0 commit comments

Comments
 (0)