extract parse_pat_deref

This commit is contained in:
Mazdak Farrokhzad 2019-08-12 07:37:08 +02:00
parent a4af9d1ac2
commit 90793c0f12

View File

@ -109,18 +109,7 @@ impl<'a> Parser<'a> {
let lo = self.token.span;
let pat = match self.token.kind {
token::BinOp(token::And) | token::AndAnd => {
// Parse &pat / &mut pat
self.expect_and()?;
let mutbl = self.parse_mutability();
if let token::Lifetime(name) = self.token.kind {
let mut err = self.fatal(&format!("unexpected lifetime `{}` in pattern", name));
err.span_label(self.token.span, "unexpected lifetime");
return Err(err);
}
let subpat = self.parse_pat_with_range_pat(false, expected)?;
PatKind::Ref(subpat, mutbl)
}
token::BinOp(token::And) | token::AndAnd => self.parse_pat_deref(expected)?,
token::OpenDelim(token::Paren) => {
// Parse a tuple or parenthesis pattern.
let (fields, trailing_comma) = self.parse_paren_comma_seq(|p| p.parse_pat(None))?;
@ -332,6 +321,21 @@ impl<'a> Parser<'a> {
Ok(pat)
}
/// Parse `&pat` / `&mut pat`.
fn parse_pat_deref(&mut self, expected: Option<&'static str>) -> PResult<'a, PatKind> {
self.expect_and()?;
let mutbl = self.parse_mutability();
if let token::Lifetime(name) = self.token.kind {
let mut err = self.fatal(&format!("unexpected lifetime `{}` in pattern", name));
err.span_label(self.token.span, "unexpected lifetime");
return Err(err);
}
let subpat = self.parse_pat_with_range_pat(false, expected)?;
Ok(PatKind::Ref(subpat, mutbl))
}
// Helper function to decide whether to parse as ident binding
// or to try to do something more complex like range patterns.
fn parse_as_ident(&mut self) -> bool {