Skip to content

Commit

Permalink
Refactor PR rust-lang#119397
Browse files Browse the repository at this point in the history
Co-authored-by: León Orell Valerian Liehr <[email protected]>
  • Loading branch information
ShE3py and fmease committed Dec 30, 2023
1 parent d114f47 commit 1ec28f8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 30 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_parse/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<Span>,
#[subdiagnostic]
pub sugg: UnexpectedParenInRangePatSugg,
}
Expand Down
50 changes: 22 additions & 28 deletions compiler/rustc_parse/src/parser/pat.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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};
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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`.
Expand All @@ -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,
},
});
Expand Down Expand Up @@ -825,25 +822,22 @@ impl<'a> Parser<'a> {
fn parse_pat_range_to(&mut self, mut re: Spanned<RangeEnd>) -> 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 {
*syn = RangeSyntax::DotDotEq;
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,
},
});
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit 1ec28f8

Please sign in to comment.