From 1b422451aeb61b57e2843d379d83d710ea50b9d9 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Thu, 19 May 2022 15:51:49 +1000 Subject: [PATCH 1/5] Move condition out of `maybe_recover_from_bad_qpath`. --- compiler/rustc_parse/src/parser/diagnostics.rs | 5 ++--- compiler/rustc_parse/src/parser/expr.rs | 16 ++++++++-------- compiler/rustc_parse/src/parser/pat.rs | 2 +- compiler/rustc_parse/src/parser/stmt.rs | 2 +- compiler/rustc_parse/src/parser/ty.rs | 2 +- 5 files changed, 13 insertions(+), 14 deletions(-) diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index 3b2ce0de50915..51db52fbf40c4 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -1449,10 +1449,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 +1597,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)> { 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..d521db7a058d6 100644 --- a/compiler/rustc_parse/src/parser/pat.rs +++ b/compiler/rustc_parse/src/parser/pat.rs @@ -408,7 +408,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..fea1217887901 100644 --- a/compiler/rustc_parse/src/parser/ty.rs +++ b/compiler/rustc_parse/src/parser/ty.rs @@ -318,7 +318,7 @@ impl<'a> Parser<'a> { 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 allow_qpath_recovery { self.maybe_recover_from_bad_qpath(ty) } else { Ok(ty) } } /// Parses either: From a148a32fdc3f3767487d54147edbbe2c225c4fbf Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Thu, 19 May 2022 15:52:52 +1000 Subject: [PATCH 2/5] Move condition out of `maybe_recover_from_question_mark`. --- compiler/rustc_parse/src/parser/diagnostics.rs | 11 ++--------- compiler/rustc_parse/src/parser/ty.rs | 6 ++++-- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index 51db52fbf40c4..3ed19219fa437 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -1,5 +1,5 @@ use super::pat::Expected; -use super::ty::{AllowPlus, RecoverQuestionMark}; +use super::ty::AllowPlus; use super::{ BlockMode, CommaRecoveryMode, Parser, PathStyle, RecoverColon, RecoverComma, Restrictions, SemiColonMode, SeqSep, TokenExpectType, TokenType, @@ -1248,14 +1248,7 @@ impl<'a> Parser<'a> { } /// 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") diff --git a/compiler/rustc_parse/src/parser/ty.rs b/compiler/rustc_parse/src/parser/ty.rs index fea1217887901..6c8eb8b39d11f 100644 --- a/compiler/rustc_parse/src/parser/ty.rs +++ b/compiler/rustc_parse/src/parser/ty.rs @@ -312,12 +312,14 @@ 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); + 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) } } From 7a37e0c2ff8b8fb0346e519418e1e7cb2a33bc45 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Thu, 19 May 2022 15:59:52 +1000 Subject: [PATCH 3/5] Move condition out of `maybe_report_ambiguous_plus` and `maybe_recover_from_bad_type_plus`. --- compiler/rustc_parse/src/parser/diagnostics.rs | 18 ++++-------------- compiler/rustc_parse/src/parser/ty.rs | 7 +++++-- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index 3ed19219fa437..c58b43de1b8d5 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -1,5 +1,4 @@ use super::pat::Expected; -use super::ty::AllowPlus; use super::{ BlockMode, CommaRecoveryMode, Parser, PathStyle, RecoverColon, RecoverComma, Restrictions, SemiColonMode, SeqSep, TokenExpectType, TokenType, @@ -1236,13 +1235,8 @@ 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 }); } } @@ -1268,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(()); } diff --git a/compiler/rustc_parse/src/parser/ty.rs b/compiler/rustc_parse/src/parser/ty.rs index 6c8eb8b39d11f..708940c8aa8d9 100644 --- a/compiler/rustc_parse/src/parser/ty.rs +++ b/compiler/rustc_parse/src/parser/ty.rs @@ -315,8 +315,11 @@ impl<'a> Parser<'a> { 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)?; + 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); } From d4347ed6786d2f37218af1a4c112bf1d66500c02 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Thu, 19 May 2022 16:04:35 +1000 Subject: [PATCH 4/5] Move condition out of `maybe_recover_colon_colon_in_pat_typo`. --- compiler/rustc_parse/src/parser/diagnostics.rs | 7 +++---- compiler/rustc_parse/src/parser/pat.rs | 6 ++++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index c58b43de1b8d5..dc02a22b76982 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -1,7 +1,7 @@ use super::pat::Expected; use super::{ - BlockMode, CommaRecoveryMode, Parser, PathStyle, RecoverColon, RecoverComma, Restrictions, - SemiColonMode, SeqSep, TokenExpectType, TokenType, + BlockMode, CommaRecoveryMode, Parser, PathStyle, RecoverComma, Restrictions, SemiColonMode, + SeqSep, TokenExpectType, TokenType, }; use crate::lexer::UnmatchedBrace; @@ -2444,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(..)) diff --git a/compiler/rustc_parse/src/parser/pat.rs b/compiler/rustc_parse/src/parser/pat.rs index d521db7a058d6..aec3f269956dd 100644 --- a/compiler/rustc_parse/src/parser/pat.rs +++ b/compiler/rustc_parse/src/parser/pat.rs @@ -100,7 +100,7 @@ impl<'a> Parser<'a> { }; // Parse the first pattern (`p_0`). - let first_pat = self.parse_pat_no_top_alt(expected)?; + let mut first_pat = self.parse_pat_no_top_alt(expected)?; self.maybe_recover_unexpected_comma(first_pat.span, rc, rt)?; // If the next token is not a `|`, @@ -111,7 +111,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 From 7b6c5c76a5b4b1a7676f7792c92939c4a7e85f5b Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Thu, 19 May 2022 16:07:55 +1000 Subject: [PATCH 5/5] Move condition out of `maybe_recover_unexpected_comma`. --- compiler/rustc_parse/src/parser/diagnostics.rs | 7 +++---- compiler/rustc_parse/src/parser/pat.rs | 8 ++++++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index dc02a22b76982..cbbaaa1ae376f 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -1,7 +1,7 @@ use super::pat::Expected; use super::{ - BlockMode, CommaRecoveryMode, Parser, PathStyle, RecoverComma, Restrictions, SemiColonMode, - SeqSep, TokenExpectType, TokenType, + BlockMode, CommaRecoveryMode, Parser, PathStyle, Restrictions, SemiColonMode, SeqSep, + TokenExpectType, TokenType, }; use crate::lexer::UnmatchedBrace; @@ -2580,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/pat.rs b/compiler/rustc_parse/src/parser/pat.rs index aec3f269956dd..2ad3f3ec19d57 100644 --- a/compiler/rustc_parse/src/parser/pat.rs +++ b/compiler/rustc_parse/src/parser/pat.rs @@ -101,7 +101,9 @@ impl<'a> Parser<'a> { // Parse the first pattern (`p_0`). let mut first_pat = self.parse_pat_no_top_alt(expected)?; - self.maybe_recover_unexpected_comma(first_pat.span, rc, rt)?; + 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. @@ -141,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);