Skip to content

Move conditions out of recover/report functions. #97166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 11 additions & 31 deletions compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<Ty>`. Parse the construct and recover from it.
pub(super) fn maybe_recover_from_question_mark(
&mut self,
ty: P<Ty>,
recover_question_mark: RecoverQuestionMark,
) -> P<Ty> {
if let RecoverQuestionMark::No = recover_question_mark {
return ty;
}
pub(super) fn maybe_recover_from_question_mark(&mut self, ty: P<Ty>) -> P<Ty> {
if self.token == token::Question {
self.bump();
self.struct_span_err(self.prev_token.span, "invalid `?` in type")
Expand All @@ -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(());
}

Expand Down Expand Up @@ -1449,10 +1432,9 @@ impl<'a> Parser<'a> {
pub(super) fn maybe_recover_from_bad_qpath<T: RecoverQPath>(
&mut self,
base: P<T>,
allow_recovery: bool,
) -> PResult<'a, P<T>> {
// 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);
}
Expand Down Expand Up @@ -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<Expr>, bool)> {
Expand Down Expand Up @@ -2462,10 +2444,9 @@ impl<'a> Parser<'a> {
crate fn maybe_recover_colon_colon_in_pat_typo(
&mut self,
mut first_pat: P<Pat>,
ra: RecoverColon,
expected: Expected,
) -> P<Pat> {
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(..))
Expand Down Expand Up @@ -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(());
}

Expand Down
16 changes: 8 additions & 8 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
Expand All @@ -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(
Expand Down Expand Up @@ -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<Expr>> {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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?`.
Expand All @@ -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.
Expand Down Expand Up @@ -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?`.
Expand All @@ -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.
Expand Down
16 changes: 11 additions & 5 deletions compiler/rustc_parse/src/parser/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_parse/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 10 additions & 5 deletions compiler/rustc_parse/src/parser/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down