diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index 3b2ce0de50915..cbbaaa1ae376f 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -1,8 +1,7 @@ use super::pat::Expected; -use super::ty::{AllowPlus, RecoverQuestionMark}; use super::{ - BlockMode, CommaRecoveryMode, Parser, PathStyle, RecoverColon, RecoverComma, Restrictions, - SemiColonMode, SeqSep, TokenExpectType, TokenType, + BlockMode, CommaRecoveryMode, Parser, PathStyle, Restrictions, SemiColonMode, SeqSep, + TokenExpectType, TokenType, }; use crate::lexer::UnmatchedBrace; @@ -1236,26 +1235,14 @@ impl<'a> Parser<'a> { } } - pub(super) fn maybe_report_ambiguous_plus( - &mut self, - allow_plus: AllowPlus, - impl_dyn_multi: bool, - ty: &Ty, - ) { - if matches!(allow_plus, AllowPlus::No) && impl_dyn_multi { + pub(super) fn maybe_report_ambiguous_plus(&mut self, impl_dyn_multi: bool, ty: &Ty) { + if impl_dyn_multi { self.sess.emit_err(AmbiguousPlus { sum_ty: pprust::ty_to_string(&ty), span: ty.span }); } } /// Swift lets users write `Ty?` to mean `Option`. Parse the construct and recover from it. - pub(super) fn maybe_recover_from_question_mark( - &mut self, - ty: P, - recover_question_mark: RecoverQuestionMark, - ) -> P { - if let RecoverQuestionMark::No = recover_question_mark { - return ty; - } + pub(super) fn maybe_recover_from_question_mark(&mut self, ty: P) -> P { if self.token == token::Question { self.bump(); self.struct_span_err(self.prev_token.span, "invalid `?` in type") @@ -1275,13 +1262,9 @@ impl<'a> Parser<'a> { } } - pub(super) fn maybe_recover_from_bad_type_plus( - &mut self, - allow_plus: AllowPlus, - ty: &Ty, - ) -> PResult<'a, ()> { + pub(super) fn maybe_recover_from_bad_type_plus(&mut self, ty: &Ty) -> PResult<'a, ()> { // Do not add `+` to expected tokens. - if matches!(allow_plus, AllowPlus::No) || !self.token.is_like_plus() { + if !self.token.is_like_plus() { return Ok(()); } @@ -1449,10 +1432,9 @@ impl<'a> Parser<'a> { pub(super) fn maybe_recover_from_bad_qpath( &mut self, base: P, - allow_recovery: bool, ) -> PResult<'a, P> { // Do not add `::` to expected tokens. - if allow_recovery && self.token == token::ModSep { + if self.token == token::ModSep { if let Some(ty) = base.to_ty() { return self.maybe_recover_from_bad_qpath_stage_2(ty.span, ty); } @@ -1598,7 +1580,7 @@ impl<'a> Parser<'a> { _ => ExprKind::Await(expr), }; let expr = self.mk_expr(lo.to(sp), kind, attrs); - self.maybe_recover_from_bad_qpath(expr, true) + self.maybe_recover_from_bad_qpath(expr) } fn recover_await_macro(&mut self) -> PResult<'a, (Span, P, bool)> { @@ -2462,10 +2444,9 @@ impl<'a> Parser<'a> { crate fn maybe_recover_colon_colon_in_pat_typo( &mut self, mut first_pat: P, - ra: RecoverColon, expected: Expected, ) -> P { - if RecoverColon::Yes != ra || token::Colon != self.token.kind { + if token::Colon != self.token.kind { return first_pat; } if !matches!(first_pat.kind, PatKind::Ident(_, _, None) | PatKind::Path(..)) @@ -2599,10 +2580,9 @@ impl<'a> Parser<'a> { crate fn maybe_recover_unexpected_comma( &mut self, lo: Span, - rc: RecoverComma, rt: CommaRecoveryMode, ) -> PResult<'a, ()> { - if rc == RecoverComma::No || self.token != token::Comma { + if self.token != token::Comma { return Ok(()); } diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index 6114e7aaa7bd7..6e97c66571549 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -1417,7 +1417,7 @@ impl<'a> Parser<'a> { match self.parse_opt_lit() { Some(literal) => { let expr = self.mk_expr(lo.to(self.prev_token.span), ExprKind::Lit(literal), attrs); - self.maybe_recover_from_bad_qpath(expr, true) + self.maybe_recover_from_bad_qpath(expr) } None => self.try_macro_suggestion(), } @@ -1444,7 +1444,7 @@ impl<'a> Parser<'a> { ExprKind::Tup(es) }; let expr = self.mk_expr(lo.to(self.prev_token.span), kind, attrs); - self.maybe_recover_from_bad_qpath(expr, true) + self.maybe_recover_from_bad_qpath(expr) } fn parse_array_or_repeat_expr( @@ -1481,7 +1481,7 @@ impl<'a> Parser<'a> { } }; let expr = self.mk_expr(lo.to(self.prev_token.span), kind, attrs); - self.maybe_recover_from_bad_qpath(expr, true) + self.maybe_recover_from_bad_qpath(expr) } fn parse_path_start_expr(&mut self, attrs: AttrVec) -> PResult<'a, P> { @@ -1519,7 +1519,7 @@ impl<'a> Parser<'a> { }; let expr = self.mk_expr(lo.to(hi), kind, attrs); - self.maybe_recover_from_bad_qpath(expr, true) + self.maybe_recover_from_bad_qpath(expr) } /// Parse `'label: $expr`. The label is already parsed. @@ -1604,7 +1604,7 @@ impl<'a> Parser<'a> { let lo = self.prev_token.span; let kind = ExprKind::Ret(self.parse_expr_opt()?); let expr = self.mk_expr(lo.to(self.prev_token.span), kind, attrs); - self.maybe_recover_from_bad_qpath(expr, true) + self.maybe_recover_from_bad_qpath(expr) } /// Parse `"do" "yeet" expr?`. @@ -1619,7 +1619,7 @@ impl<'a> Parser<'a> { let span = lo.to(self.prev_token.span); self.sess.gated_spans.gate(sym::yeet_expr, span); let expr = self.mk_expr(span, kind, attrs); - self.maybe_recover_from_bad_qpath(expr, true) + self.maybe_recover_from_bad_qpath(expr) } /// Parse `"break" (('label (:? expr)?) | expr?)` with `"break"` token already eaten. @@ -1679,7 +1679,7 @@ impl<'a> Parser<'a> { None }; let expr = self.mk_expr(lo.to(self.prev_token.span), ExprKind::Break(label, kind), attrs); - self.maybe_recover_from_bad_qpath(expr, true) + self.maybe_recover_from_bad_qpath(expr) } /// Parse `"yield" expr?`. @@ -1689,7 +1689,7 @@ impl<'a> Parser<'a> { let span = lo.to(self.prev_token.span); self.sess.gated_spans.gate(sym::generators, span); let expr = self.mk_expr(span, kind, attrs); - self.maybe_recover_from_bad_qpath(expr, true) + self.maybe_recover_from_bad_qpath(expr) } /// Returns a string literal if the next token is a string literal. diff --git a/compiler/rustc_parse/src/parser/pat.rs b/compiler/rustc_parse/src/parser/pat.rs index 8019c5fb67cb9..2ad3f3ec19d57 100644 --- a/compiler/rustc_parse/src/parser/pat.rs +++ b/compiler/rustc_parse/src/parser/pat.rs @@ -100,8 +100,10 @@ impl<'a> Parser<'a> { }; // Parse the first pattern (`p_0`). - let first_pat = self.parse_pat_no_top_alt(expected)?; - self.maybe_recover_unexpected_comma(first_pat.span, rc, rt)?; + let mut first_pat = self.parse_pat_no_top_alt(expected)?; + if rc == RecoverComma::Yes { + self.maybe_recover_unexpected_comma(first_pat.span, rt)?; + } // If the next token is not a `|`, // this is not an or-pattern and we should exit here. @@ -111,7 +113,9 @@ impl<'a> Parser<'a> { // This complicated procedure is done purely for diagnostics UX. // Check if the user wrote `foo:bar` instead of `foo::bar`. - let first_pat = self.maybe_recover_colon_colon_in_pat_typo(first_pat, ra, expected); + if ra == RecoverColon::Yes { + first_pat = self.maybe_recover_colon_colon_in_pat_typo(first_pat, expected); + } if let Some(leading_vert_span) = leading_vert_span { // If there was a leading vert, treat this as an or-pattern. This improves @@ -139,7 +143,9 @@ impl<'a> Parser<'a> { err.span_label(lo, WHILE_PARSING_OR_MSG); err })?; - self.maybe_recover_unexpected_comma(pat.span, rc, rt)?; + if rc == RecoverComma::Yes { + self.maybe_recover_unexpected_comma(pat.span, rt)?; + } pats.push(pat); } let or_pattern_span = lo.to(self.prev_token.span); @@ -408,7 +414,7 @@ impl<'a> Parser<'a> { }; let pat = self.mk_pat(lo.to(self.prev_token.span), pat); - let pat = self.maybe_recover_from_bad_qpath(pat, true)?; + let pat = self.maybe_recover_from_bad_qpath(pat)?; let pat = self.recover_intersection_pat(pat)?; if !allow_range_pat { diff --git a/compiler/rustc_parse/src/parser/stmt.rs b/compiler/rustc_parse/src/parser/stmt.rs index 56ebac0953b19..85faa84e9cfa3 100644 --- a/compiler/rustc_parse/src/parser/stmt.rs +++ b/compiler/rustc_parse/src/parser/stmt.rs @@ -180,7 +180,7 @@ impl<'a> Parser<'a> { } else { // Since none of the above applied, this is an expression statement macro. let e = self.mk_expr(lo.to(hi), ExprKind::MacCall(mac), AttrVec::new()); - let e = self.maybe_recover_from_bad_qpath(e, true)?; + let e = self.maybe_recover_from_bad_qpath(e)?; let e = self.parse_dot_or_call_expr_with(e, lo, attrs.into())?; let e = self.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(e))?; StmtKind::Expr(e) diff --git a/compiler/rustc_parse/src/parser/ty.rs b/compiler/rustc_parse/src/parser/ty.rs index b0439a5987afa..708940c8aa8d9 100644 --- a/compiler/rustc_parse/src/parser/ty.rs +++ b/compiler/rustc_parse/src/parser/ty.rs @@ -312,13 +312,18 @@ impl<'a> Parser<'a> { }; let span = lo.to(self.prev_token.span); - let ty = self.mk_ty(span, kind); + let mut ty = self.mk_ty(span, kind); // Try to recover from use of `+` with incorrect priority. - self.maybe_report_ambiguous_plus(allow_plus, impl_dyn_multi, &ty); - self.maybe_recover_from_bad_type_plus(allow_plus, &ty)?; - let ty = self.maybe_recover_from_question_mark(ty, recover_question_mark); - self.maybe_recover_from_bad_qpath(ty, allow_qpath_recovery) + if matches!(allow_plus, AllowPlus::Yes) { + self.maybe_recover_from_bad_type_plus(&ty)?; + } else { + self.maybe_report_ambiguous_plus(impl_dyn_multi, &ty); + } + if let RecoverQuestionMark::Yes = recover_question_mark { + ty = self.maybe_recover_from_question_mark(ty); + } + if allow_qpath_recovery { self.maybe_recover_from_bad_qpath(ty) } else { Ok(ty) } } /// Parses either: