1 use super::{Parser, PResult, PathStyle};
3 use crate::{maybe_recover_from_interpolated_ty_qpath, maybe_whole};
5 use crate::ast::{self, Attribute, Pat, PatKind, FieldPat, RangeEnd, RangeSyntax, Mac};
6 use crate::ast::{BindingMode, Ident, Mutability, Path, QSelf, Expr, ExprKind};
7 use crate::parse::token::{self};
8 use crate::print::pprust;
9 use crate::source_map::{respan, Span, Spanned};
10 use crate::symbol::kw;
13 use errors::{Applicability, DiagnosticBuilder};
15 type Expected = Option<&'static str>;
19 pub fn parse_pat(&mut self, expected: Expected) -> PResult<'a, P<Pat>> {
20 self.parse_pat_with_range_pat(true, expected)
23 // FIXME(or_patterns, Centril | dlrobertson):
24 // remove this and use `parse_top_pat` everywhere it is used instead.
25 pub(super) fn parse_top_pat_unpack(&mut self, gate_or: bool) -> PResult<'a, Vec<P<Pat>>> {
26 self.parse_top_pat(gate_or)
27 .map(|pat| pat.and_then(|pat| match pat.node {
28 PatKind::Or(pats) => pats,
29 node => vec![self.mk_pat(pat.span, node)],
33 /// Entry point to the main pattern parser.
34 /// Corresponds to `top_pat` in RFC 2535 and allows or-pattern at the top level.
35 pub(super) fn parse_top_pat(&mut self, gate_or: bool) -> PResult<'a, P<Pat>> {
36 // Allow a '|' before the pats (RFCs 1925, 2530, and 2535).
37 if self.eat_or_separator() && gate_or {
38 self.sess.gated_spans.or_patterns.borrow_mut().push(self.prev_span);
41 self.parse_pat_with_or(None, gate_or, true)
44 pub(super) fn parse_top_level_pat(&mut self) -> PResult<'a, P<Pat>> {
45 let pat = self.parse_pat(None)?;
46 self.maybe_recover_unexpected_comma(pat.span, true)?;
50 /// Parses a pattern, that may be a or-pattern (e.g. `Foo | Bar` in `Some(Foo | Bar)`).
51 /// Corresponds to `pat<allow_top_alt>` in RFC 2535.
57 ) -> PResult<'a, P<Pat>> {
58 // Parse the first pattern.
59 let first_pat = self.parse_pat(expected)?;
60 self.maybe_recover_unexpected_comma(first_pat.span, top_level)?;
62 // If the next token is not a `|`,
63 // this is not an or-pattern and we should exit here.
64 if !self.check(&token::BinOp(token::Or)) && self.token != token::OrOr {
68 let lo = first_pat.span;
69 let mut pats = vec![first_pat];
70 while self.eat_or_separator() {
71 let pat = self.parse_pat(expected)?;
72 self.maybe_recover_unexpected_comma(pat.span, top_level)?;
75 let or_pattern_span = lo.to(self.prev_span);
77 // Feature gate the or-pattern if instructed:
79 self.sess.gated_spans.or_patterns.borrow_mut().push(or_pattern_span);
82 Ok(self.mk_pat(or_pattern_span, PatKind::Or(pats)))
85 /// Eat the or-pattern `|` separator.
86 /// If instead a `||` token is encountered, recover and pretend we parsed `|`.
87 fn eat_or_separator(&mut self) -> bool {
88 match self.token.kind {
90 // Found `||`; Recover and pretend we parsed `|`.
91 self.ban_unexpected_or_or();
95 _ => self.eat(&token::BinOp(token::Or)),
99 /// We have parsed `||` instead of `|`. Error and suggest `|` instead.
100 fn ban_unexpected_or_or(&mut self) {
101 self.struct_span_err(self.token.span, "unexpected token `||` after pattern")
104 "use a single `|` to specify multiple patterns",
106 Applicability::MachineApplicable
111 /// Some special error handling for the "top-level" patterns in a match arm,
112 /// `for` loop, `let`, &c. (in contrast to subpatterns within such).
113 fn maybe_recover_unexpected_comma(&mut self, lo: Span, top_level: bool) -> PResult<'a, ()> {
114 if !top_level || self.token != token::Comma {
118 // An unexpected comma after a top-level pattern is a clue that the
119 // user (perhaps more accustomed to some other language) forgot the
120 // parentheses in what should have been a tuple pattern; return a
121 // suggestion-enhanced error here rather than choking on the comma later.
122 let comma_span = self.token.span;
124 if let Err(mut err) = self.skip_pat_list() {
125 // We didn't expect this to work anyway; we just wanted to advance to the
126 // end of the comma-sequence so we know the span to suggest parenthesizing.
129 let seq_span = lo.to(self.prev_span);
130 let mut err = self.struct_span_err(comma_span, "unexpected `,` in pattern");
131 if let Ok(seq_snippet) = self.span_to_snippet(seq_span) {
134 "try adding parentheses to match on a tuple..",
135 format!("({})", seq_snippet),
136 Applicability::MachineApplicable
140 "..or a vertical bar to match on multiple alternatives",
141 format!("{}", seq_snippet.replace(",", " |")),
142 Applicability::MachineApplicable
148 /// Parse and throw away a parentesized comma separated
149 /// sequence of patterns until `)` is reached.
150 fn skip_pat_list(&mut self) -> PResult<'a, ()> {
151 while !self.check(&token::CloseDelim(token::Paren)) {
152 self.parse_pat(None)?;
153 if !self.eat(&token::Comma) {
160 /// Parses a pattern, with a setting whether modern range patterns (e.g., `a..=b`, `a..b` are
162 fn parse_pat_with_range_pat(
164 allow_range_pat: bool,
166 ) -> PResult<'a, P<Pat>> {
167 maybe_recover_from_interpolated_ty_qpath!(self, true);
168 maybe_whole!(self, NtPat, |x| x);
170 let lo = self.token.span;
171 let pat = match self.token.kind {
172 token::BinOp(token::And) | token::AndAnd => self.parse_pat_deref(expected)?,
173 token::OpenDelim(token::Paren) => self.parse_pat_tuple_or_parens()?,
174 token::OpenDelim(token::Bracket) => {
175 // Parse `[pat, pat,...]` as a slice pattern.
176 let (pats, _) = self.parse_delim_comma_seq(
178 |p| p.parse_pat_with_or(None, true, false),
184 if self.is_pat_range_end_start() {
185 // Parse `..42` for recovery.
186 self.parse_pat_range_to(RangeEnd::Excluded, "..")?
188 // A rest pattern `..`.
193 // Parse `..=42` for recovery.
195 self.parse_pat_range_to(RangeEnd::Included(RangeSyntax::DotDotEq), "..=")?
197 token::DotDotDot => {
198 // Parse `...42` for recovery.
200 self.parse_pat_range_to(RangeEnd::Included(RangeSyntax::DotDotDot), "...")?
202 // At this point, token != &, &&, (, [
203 _ => if self.eat_keyword(kw::Underscore) {
206 } else if self.eat_keyword(kw::Mut) {
207 self.recover_pat_ident_mut_first()?
208 } else if self.eat_keyword(kw::Ref) {
209 // Parse ref ident @ pat / ref mut ident @ pat
210 let mutbl = self.parse_mutability();
211 self.parse_pat_ident(BindingMode::ByRef(mutbl))?
212 } else if self.eat_keyword(kw::Box) {
214 PatKind::Box(self.parse_pat_with_range_pat(false, None)?)
215 } else if self.token.is_ident() && !self.token.is_reserved_ident() &&
216 self.parse_as_ident() {
217 // Parse `ident @ pat`
218 // This can give false positives and parse nullary enums,
219 // they are dealt with later in resolve.
220 self.parse_pat_ident(BindingMode::ByValue(Mutability::Immutable))?
221 } else if self.token.is_path_start() {
222 // Parse pattern starting with a path
223 let (qself, path) = if self.eat_lt() {
224 // Parse a qualified path
225 let (qself, path) = self.parse_qpath(PathStyle::Expr)?;
228 // Parse an unqualified path
229 (None, self.parse_path(PathStyle::Expr)?)
231 match self.token.kind {
232 token::Not if qself.is_none() => self.parse_pat_mac_invoc(lo, path)?,
233 token::DotDotDot | token::DotDotEq | token::DotDot => {
234 self.parse_pat_range_starting_with_path(lo, qself, path)?
236 token::OpenDelim(token::Brace) => self.parse_pat_struct(qself, path)?,
237 token::OpenDelim(token::Paren) => self.parse_pat_tuple_struct(qself, path)?,
238 _ => PatKind::Path(qself, path),
241 // Try to parse everything else as literal with optional minus
242 match self.parse_literal_maybe_minus() {
244 if self.check(&token::DotDot)
245 || self.check(&token::DotDotEq)
246 || self.check(&token::DotDotDot) =>
248 self.parse_pat_range_starting_with_lit(begin)?
250 Ok(begin) => PatKind::Lit(begin),
251 Err(err) => return self.fatal_unexpected_non_pat(err, expected),
256 let pat = self.mk_pat(lo.to(self.prev_span), pat);
257 let pat = self.maybe_recover_from_bad_qpath(pat, true)?;
259 if !allow_range_pat {
260 self.ban_pat_range_if_ambiguous(&pat)?
266 /// Ban a range pattern if it has an ambiguous interpretation.
267 fn ban_pat_range_if_ambiguous(&self, pat: &Pat) -> PResult<'a, ()> {
270 .., Spanned { node: RangeEnd::Included(RangeSyntax::DotDotDot), .. }
272 PatKind::Range(..) => {}
276 let mut err = self.struct_span_err(
278 "the range pattern here has ambiguous interpretation",
282 "add parentheses to clarify the precedence",
283 format!("({})", pprust::pat_to_string(&pat)),
284 // "ambiguous interpretation" implies that we have to be guessing
285 Applicability::MaybeIncorrect
290 /// Parse `&pat` / `&mut pat`.
291 fn parse_pat_deref(&mut self, expected: Expected) -> PResult<'a, PatKind> {
293 let mutbl = self.parse_mutability();
295 if let token::Lifetime(name) = self.token.kind {
296 let mut err = self.fatal(&format!("unexpected lifetime `{}` in pattern", name));
297 err.span_label(self.token.span, "unexpected lifetime");
301 let subpat = self.parse_pat_with_range_pat(false, expected)?;
302 Ok(PatKind::Ref(subpat, mutbl))
305 /// Parse a tuple or parenthesis pattern.
306 fn parse_pat_tuple_or_parens(&mut self) -> PResult<'a, PatKind> {
307 let (fields, trailing_comma) = self.parse_paren_comma_seq(|p| {
308 p.parse_pat_with_or(None, true, false)
311 // Here, `(pat,)` is a tuple pattern.
312 // For backward compatibility, `(..)` is a tuple pattern as well.
313 Ok(if fields.len() == 1 && !(trailing_comma || fields[0].is_rest()) {
314 PatKind::Paren(fields.into_iter().nth(0).unwrap())
316 PatKind::Tuple(fields)
320 /// Recover on `mut ref? ident @ pat` and suggest
321 /// that the order of `mut` and `ref` is incorrect.
322 fn recover_pat_ident_mut_first(&mut self) -> PResult<'a, PatKind> {
323 let mutref_span = self.prev_span.to(self.token.span);
324 let binding_mode = if self.eat_keyword(kw::Ref) {
325 self.struct_span_err(mutref_span, "the order of `mut` and `ref` is incorrect")
328 "try switching the order",
330 Applicability::MachineApplicable
333 BindingMode::ByRef(Mutability::Mutable)
335 BindingMode::ByValue(Mutability::Mutable)
337 self.parse_pat_ident(binding_mode)
340 /// Parse macro invocation
341 fn parse_pat_mac_invoc(&mut self, lo: Span, path: Path) -> PResult<'a, PatKind> {
343 let (delim, tts) = self.expect_delimited_token_tree()?;
348 span: lo.to(self.prev_span),
349 prior_type_ascription: self.last_type_ascription,
351 Ok(PatKind::Mac(mac))
354 /// Parse a range pattern `$path $form $end?` where `$form = ".." | "..." | "..=" ;`.
355 /// The `$path` has already been parsed and the next token is the `$form`.
356 fn parse_pat_range_starting_with_path(
359 qself: Option<QSelf>,
361 ) -> PResult<'a, PatKind> {
362 let (end_kind, form) = match self.token.kind {
363 token::DotDot => (RangeEnd::Excluded, ".."),
364 token::DotDotDot => (RangeEnd::Included(RangeSyntax::DotDotDot), "..."),
365 token::DotDotEq => (RangeEnd::Included(RangeSyntax::DotDotEq), "..="),
366 _ => panic!("can only parse `..`/`...`/`..=` for ranges (checked above)"),
368 let op_span = self.token.span;
370 let span = lo.to(self.prev_span);
371 let begin = self.mk_expr(span, ExprKind::Path(qself, path), ThinVec::new());
373 let end = self.parse_pat_range_end_opt(&begin, form)?;
374 Ok(PatKind::Range(begin, end, respan(op_span, end_kind)))
377 /// Parse a range pattern `$literal $form $end?` where `$form = ".." | "..." | "..=" ;`.
378 /// The `$path` has already been parsed and the next token is the `$form`.
379 fn parse_pat_range_starting_with_lit(&mut self, begin: P<Expr>) -> PResult<'a, PatKind> {
380 let op_span = self.token.span;
381 let (end_kind, form) = if self.eat(&token::DotDotDot) {
382 (RangeEnd::Included(RangeSyntax::DotDotDot), "...")
383 } else if self.eat(&token::DotDotEq) {
384 (RangeEnd::Included(RangeSyntax::DotDotEq), "..=")
385 } else if self.eat(&token::DotDot) {
386 (RangeEnd::Excluded, "..")
388 panic!("impossible case: we already matched on a range-operator token")
390 let end = self.parse_pat_range_end_opt(&begin, form)?;
391 Ok(PatKind::Range(begin, end, respan(op_span, end_kind)))
394 fn fatal_unexpected_non_pat(
396 mut err: DiagnosticBuilder<'a>,
398 ) -> PResult<'a, P<Pat>> {
399 self.cancel(&mut err);
401 let expected = expected.unwrap_or("pattern");
402 let msg = format!("expected {}, found {}", expected, self.this_token_descr());
404 let mut err = self.fatal(&msg);
405 err.span_label(self.token.span, format!("expected {}", expected));
407 let sp = self.sess.source_map().start_point(self.token.span);
408 if let Some(sp) = self.sess.ambiguous_block_expr_parse.borrow().get(&sp) {
409 self.sess.expr_parentheses_needed(&mut err, *sp, None);
415 // Helper function to decide whether to parse as ident binding
416 // or to try to do something more complex like range patterns.
417 fn parse_as_ident(&mut self) -> bool {
418 self.look_ahead(1, |t| match t.kind {
419 token::OpenDelim(token::Paren) | token::OpenDelim(token::Brace) |
420 token::DotDotDot | token::DotDotEq | token::DotDot |
421 token::ModSep | token::Not => false,
426 /// Is the current token suitable as the start of a range patterns end?
427 fn is_pat_range_end_start(&self) -> bool {
428 self.token.is_path_start() // e.g. `MY_CONST`;
429 || self.token == token::Dot // e.g. `.5` for recovery;
430 || self.token.can_begin_literal_or_bool() // e.g. `42`.
431 || self.token.is_whole_expr()
434 /// Parse a range-to pattern, e.g. `..X` and `..=X` for recovery.
435 fn parse_pat_range_to(&mut self, re: RangeEnd, form: &str) -> PResult<'a, PatKind> {
436 let lo = self.prev_span;
437 let end = self.parse_pat_range_end()?;
438 let range_span = lo.to(end.span);
439 let begin = self.mk_expr(range_span, ExprKind::Err, ThinVec::new());
442 .struct_span_err(range_span, &format!("`{}X` range patterns are not supported", form))
445 "try using the minimum value for the type",
446 format!("MIN{}{}", form, pprust::expr_to_string(&end)),
447 Applicability::HasPlaceholders,
451 Ok(PatKind::Range(begin, end, respan(lo, re)))
454 /// Parse the end of a `X..Y`, `X..=Y`, or `X...Y` range pattern or recover
455 /// if that end is missing treating it as `X..`, `X..=`, or `X...` respectively.
456 fn parse_pat_range_end_opt(&mut self, begin: &Expr, form: &str) -> PResult<'a, P<Expr>> {
457 if self.is_pat_range_end_start() {
458 // Parsing e.g. `X..=Y`.
459 self.parse_pat_range_end()
461 // Parsing e.g. `X..`.
462 let range_span = begin.span.to(self.prev_span);
467 &format!("`X{}` range patterns are not supported", form),
471 "try using the maximum value for the type",
472 format!("{}{}MAX", pprust::expr_to_string(&begin), form),
473 Applicability::HasPlaceholders,
477 Ok(self.mk_expr(range_span, ExprKind::Err, ThinVec::new()))
481 fn parse_pat_range_end(&mut self) -> PResult<'a, P<Expr>> {
482 if self.token.is_path_start() {
483 let lo = self.token.span;
484 let (qself, path) = if self.eat_lt() {
485 // Parse a qualified path
486 let (qself, path) = self.parse_qpath(PathStyle::Expr)?;
489 // Parse an unqualified path
490 (None, self.parse_path(PathStyle::Expr)?)
492 let hi = self.prev_span;
493 Ok(self.mk_expr(lo.to(hi), ExprKind::Path(qself, path), ThinVec::new()))
495 self.parse_literal_maybe_minus()
499 /// Parses `ident` or `ident @ pat`.
500 /// Used by the copy foo and ref foo patterns to give a good
501 /// error message when parsing mistakes like `ref foo(a, b)`.
502 fn parse_pat_ident(&mut self, binding_mode: BindingMode) -> PResult<'a, PatKind> {
503 let ident = self.parse_ident()?;
504 let sub = if self.eat(&token::At) {
505 Some(self.parse_pat(Some("binding pattern"))?)
510 // Just to be friendly, if they write something like `ref Some(i)`,
511 // we end up here with `(` as the current token.
512 // This shortly leads to a parse error. Note that if there is no explicit
513 // binding mode then we do not end up here, because the lookahead
514 // will direct us over to `parse_enum_variant()`.
515 if self.token == token::OpenDelim(token::Paren) {
516 return Err(self.span_fatal(
518 "expected identifier, found enum pattern",
522 Ok(PatKind::Ident(binding_mode, ident, sub))
525 /// Parse a struct ("record") pattern (e.g. `Foo { ... }` or `Foo::Bar { ... }`).
526 fn parse_pat_struct(&mut self, qself: Option<QSelf>, path: Path) -> PResult<'a, PatKind> {
528 let msg = "unexpected `{` after qualified path";
529 let mut err = self.fatal(msg);
530 err.span_label(self.token.span, msg);
535 let (fields, etc) = self.parse_pat_fields().unwrap_or_else(|mut e| {
541 Ok(PatKind::Struct(path, fields, etc))
544 /// Parse tuple struct or tuple variant pattern (e.g. `Foo(...)` or `Foo::Bar(...)`).
545 fn parse_pat_tuple_struct(&mut self, qself: Option<QSelf>, path: Path) -> PResult<'a, PatKind> {
547 let msg = "unexpected `(` after qualified path";
548 let mut err = self.fatal(msg);
549 err.span_label(self.token.span, msg);
552 let (fields, _) = self.parse_paren_comma_seq(|p| p.parse_pat_with_or(None, true, false))?;
553 Ok(PatKind::TupleStruct(path, fields))
556 /// Parses the fields of a struct-like pattern.
557 fn parse_pat_fields(&mut self) -> PResult<'a, (Vec<FieldPat>, bool)> {
558 let mut fields = Vec::new();
560 let mut ate_comma = true;
561 let mut delayed_err: Option<DiagnosticBuilder<'a>> = None;
562 let mut etc_span = None;
564 while self.token != token::CloseDelim(token::Brace) {
565 let attrs = match self.parse_outer_attributes() {
568 if let Some(mut delayed) = delayed_err {
574 let lo = self.token.span;
576 // check that a comma comes after every field
578 let err = self.struct_span_err(self.prev_span, "expected `,`");
579 if let Some(mut delayed) = delayed_err {
586 if self.check(&token::DotDot) || self.token == token::DotDotDot {
588 let mut etc_sp = self.token.span;
590 self.recover_one_fewer_dotdot();
591 self.bump(); // `..` || `...`
593 if self.token == token::CloseDelim(token::Brace) {
594 etc_span = Some(etc_sp);
597 let token_str = self.this_token_descr();
598 let mut err = self.fatal(&format!("expected `}}`, found {}", token_str));
600 err.span_label(self.token.span, "expected `}`");
601 let mut comma_sp = None;
602 if self.token == token::Comma { // Issue #49257
603 let nw_span = self.sess.source_map().span_until_non_whitespace(self.token.span);
604 etc_sp = etc_sp.to(nw_span);
605 err.span_label(etc_sp,
606 "`..` must be at the end and cannot have a trailing comma");
607 comma_sp = Some(self.token.span);
612 etc_span = Some(etc_sp.until(self.token.span));
613 if self.token == token::CloseDelim(token::Brace) {
614 // If the struct looks otherwise well formed, recover and continue.
615 if let Some(sp) = comma_sp {
616 err.span_suggestion_short(
620 Applicability::MachineApplicable,
625 } else if self.token.is_ident() && ate_comma {
626 // Accept fields coming after `..,`.
627 // This way we avoid "pattern missing fields" errors afterwards.
628 // We delay this error until the end in order to have a span for a
630 if let Some(mut delayed_err) = delayed_err {
634 delayed_err = Some(err);
637 if let Some(mut err) = delayed_err {
644 fields.push(match self.parse_pat_field(lo, attrs) {
647 if let Some(mut delayed_err) = delayed_err {
653 ate_comma = self.eat(&token::Comma);
656 if let Some(mut err) = delayed_err {
657 if let Some(etc_span) = etc_span {
658 err.multipart_suggestion(
659 "move the `..` to the end of the field list",
661 (etc_span, String::new()),
662 (self.token.span, format!("{}.. }}", if ate_comma { "" } else { ", " })),
664 Applicability::MachineApplicable,
669 return Ok((fields, etc));
672 /// Recover on `...` as if it were `..` to avoid further errors.
673 /// See issue #46718.
674 fn recover_one_fewer_dotdot(&self) {
675 if self.token != token::DotDotDot {
679 self.struct_span_err(self.token.span, "expected field pattern, found `...`")
682 "to omit remaining fields, use one fewer `.`",
684 Applicability::MachineApplicable
689 fn parse_pat_field(&mut self, lo: Span, attrs: Vec<Attribute>) -> PResult<'a, FieldPat> {
690 // Check if a colon exists one ahead. This means we're parsing a fieldname.
692 let (subpat, fieldname, is_shorthand) = if self.look_ahead(1, |t| t == &token::Colon) {
693 // Parsing a pattern of the form "fieldname: pat"
694 let fieldname = self.parse_field_name()?;
696 let pat = self.parse_pat_with_or(None, true, false)?;
698 (pat, fieldname, false)
700 // Parsing a pattern of the form "(box) (ref) (mut) fieldname"
701 let is_box = self.eat_keyword(kw::Box);
702 let boxed_span = self.token.span;
703 let is_ref = self.eat_keyword(kw::Ref);
704 let is_mut = self.eat_keyword(kw::Mut);
705 let fieldname = self.parse_ident()?;
708 let bind_type = match (is_ref, is_mut) {
709 (true, true) => BindingMode::ByRef(Mutability::Mutable),
710 (true, false) => BindingMode::ByRef(Mutability::Immutable),
711 (false, true) => BindingMode::ByValue(Mutability::Mutable),
712 (false, false) => BindingMode::ByValue(Mutability::Immutable),
715 let fieldpat = self.mk_pat_ident(boxed_span.to(hi), bind_type, fieldname);
716 let subpat = if is_box {
717 self.mk_pat(lo.to(hi), PatKind::Box(fieldpat))
721 (subpat, fieldname, true)
729 id: ast::DUMMY_NODE_ID,
734 pub(super) fn mk_pat_ident(&self, span: Span, bm: BindingMode, ident: Ident) -> P<Pat> {
735 self.mk_pat(span, PatKind::Ident(bm, ident, None))
738 fn mk_pat(&self, span: Span, node: PatKind) -> P<Pat> {
739 P(Pat { node, span, id: ast::DUMMY_NODE_ID })