diff --git a/compiler/rustc_parse/src/errors.rs b/compiler/rustc_parse/src/errors.rs index d1eb69fbbc7b..d67ae1493442 100644 --- a/compiler/rustc_parse/src/errors.rs +++ b/compiler/rustc_parse/src/errors.rs @@ -3,7 +3,7 @@ use std::borrow::Cow; use rustc_ast::token::Token; use rustc_ast::{Path, Visibility}; use rustc_errors::{ - AddToDiagnostic, Applicability, DiagCtxt, DiagnosticBuilder, IntoDiagnostic, Level, MultiSpan, + AddToDiagnostic, Applicability, DiagCtxt, DiagnosticBuilder, IntoDiagnostic, Level, SubdiagnosticMessage, }; use rustc_macros::{Diagnostic, Subdiagnostic}; @@ -2382,7 +2382,7 @@ pub(crate) struct ExpectedCommaAfterPatternField { #[diag(parse_unexpected_paren_in_range_pat)] pub(crate) struct UnexpectedParenInRangePat { #[primary_span] - pub span: MultiSpan, + pub span: Vec, #[subdiagnostic] pub sugg: UnexpectedParenInRangePatSugg, } diff --git a/compiler/rustc_parse/src/parser/pat.rs b/compiler/rustc_parse/src/parser/pat.rs index 50cf332ee7f4..5fc10c5a1ecf 100644 --- a/compiler/rustc_parse/src/parser/pat.rs +++ b/compiler/rustc_parse/src/parser/pat.rs @@ -1,14 +1,14 @@ use super::{ForceCollect, Parser, PathStyle, TrailingToken}; use crate::errors::{ - AmbiguousRangePattern, BoxNotPat, DotDotDotForRemainingFields, - DotDotDotRangeToPatternNotAllowed, DotDotDotRestPattern, EnumPatternInsteadOfIdentifier, - ExpectedBindingLeftOfAt, ExpectedCommaAfterPatternField, - GenericArgsInPatRequireTurbofishSyntax, InclusiveRangeExtraEquals, InclusiveRangeMatchArrow, - InclusiveRangeNoEnd, InvalidMutInPattern, PatternOnWrongSideOfAt, RefMutOrderIncorrect, - RemoveLet, RepeatedMutInPattern, SwitchRefBoxOrder, TopLevelOrPatternNotAllowed, - TopLevelOrPatternNotAllowedSugg, TrailingVertNotAllowed, UnexpectedLifetimeInPattern, - UnexpectedParenInRangePat, UnexpectedParenInRangePatSugg, - UnexpectedVertVertBeforeFunctionParam, UnexpectedVertVertInPattern, + self, AmbiguousRangePattern, DotDotDotForRemainingFields, DotDotDotRangeToPatternNotAllowed, + DotDotDotRestPattern, EnumPatternInsteadOfIdentifier, ExpectedBindingLeftOfAt, + ExpectedCommaAfterPatternField, GenericArgsInPatRequireTurbofishSyntax, + InclusiveRangeExtraEquals, InclusiveRangeMatchArrow, InclusiveRangeNoEnd, InvalidMutInPattern, + PatternOnWrongSideOfAt, RefMutOrderIncorrect, RemoveLet, RepeatedMutInPattern, + SwitchRefBoxOrder, TopLevelOrPatternNotAllowed, TopLevelOrPatternNotAllowedSugg, + TrailingVertNotAllowed, UnexpectedLifetimeInPattern, UnexpectedParenInRangePat, + UnexpectedParenInRangePatSugg, UnexpectedVertVertBeforeFunctionParam, + UnexpectedVertVertInPattern, }; use crate::{maybe_recover_from_interpolated_ty_qpath, maybe_whole}; use rustc_ast::mut_visit::{noop_visit_pat, MutVisitor}; @@ -19,7 +19,7 @@ use rustc_ast::{ PatField, PatFieldsRest, PatKind, Path, QSelf, RangeEnd, RangeSyntax, }; use rustc_ast_pretty::pprust; -use rustc_errors::{Applicability, DiagnosticBuilder, MultiSpan, PResult}; +use rustc_errors::{Applicability, DiagnosticBuilder, PResult}; use rustc_session::errors::ExprParenthesesNeeded; use rustc_span::source_map::{respan, Spanned}; use rustc_span::symbol::{kw, sym, Ident}; @@ -604,7 +604,7 @@ impl<'a> Parser<'a> { && let Some(form) = self.parse_range_end() => { self.dcx().emit_err(UnexpectedParenInRangePat { - span: MultiSpan::from_spans(vec![open_paren, close_paren]), + span: vec![open_paren, close_paren], sugg: UnexpectedParenInRangePatSugg { start_span: open_paren, end_span: close_paren, @@ -754,11 +754,8 @@ impl<'a> Parser<'a> { ) -> PResult<'a, PatKind> { // recover from `(` let open_paren = (self.may_recover() - && self.token.kind == token::OpenDelim(Delimiter::Parenthesis)) - .then(|| { - self.bump(); - self.prev_token.span - }); + && self.eat_noexpect(&token::OpenDelim(Delimiter::Parenthesis))) + .then_some(self.prev_token.span); let end = if self.is_pat_range_end_start(0) { // Parsing e.g. `X..=Y`. @@ -772,13 +769,13 @@ impl<'a> Parser<'a> { None }; - if let Some(span) = open_paren { + if let Some(open_paren) = open_paren { self.expect(&token::CloseDelim(Delimiter::Parenthesis))?; self.dcx().emit_err(UnexpectedParenInRangePat { - span: MultiSpan::from_spans(vec![span, self.prev_token.span]), + span: vec![open_paren, self.prev_token.span], sugg: UnexpectedParenInRangePatSugg { - start_span: span, + start_span: open_paren, end_span: self.prev_token.span, }, }); @@ -825,11 +822,8 @@ impl<'a> Parser<'a> { fn parse_pat_range_to(&mut self, mut re: Spanned) -> PResult<'a, PatKind> { // recover from `(` let open_paren = (self.may_recover() - && self.token.kind == token::OpenDelim(Delimiter::Parenthesis)) - .then(|| { - self.bump(); - self.prev_token.span - }); + && self.eat_noexpect(&token::OpenDelim(Delimiter::Parenthesis))) + .then_some(self.prev_token.span); let end = self.parse_pat_range_end()?; if let RangeEnd::Included(syn @ RangeSyntax::DotDotDot) = &mut re.node { @@ -837,13 +831,13 @@ impl<'a> Parser<'a> { self.dcx().emit_err(DotDotDotRangeToPatternNotAllowed { span: re.span }); } - if let Some(span) = open_paren { + if let Some(open_paren) = open_paren { self.expect(&token::CloseDelim(Delimiter::Parenthesis))?; self.dcx().emit_err(UnexpectedParenInRangePat { - span: MultiSpan::from_spans(vec![span, self.prev_token.span]), + span: vec![open_paren, self.prev_token.span], sugg: UnexpectedParenInRangePatSugg { - start_span: span, + start_span: open_paren, end_span: self.prev_token.span, }, }); @@ -1013,7 +1007,7 @@ impl<'a> Parser<'a> { if self.isnt_pattern_start() { let descr = super::token_descr(&self.token); - self.dcx().emit_err(BoxNotPat { + self.dcx().emit_err(errors::BoxNotPat { span: self.token.span, kw: box_span, lo: box_span.shrink_to_lo(),