Skip to content

Commit d126de1

Browse files
authored
Rollup merge of #97166 - nnethercote:move-conditions-out, r=estebank
Move conditions out of recover/report functions. `Parser` has six recover/report functions that are passed a boolean, and nothing is done if the boolean has a particular value. This PR moves the tests outside the functions. This has the following effects. - The number of lines of code goes down. - Some `use` items become shorter. - Avoids the strangeness whereby 11 out of 12 calls to `maybe_recover_from_bad_qpath` pass `true` as the second argument. - Makes it clear at the call site that only one of `maybe_recover_from_bad_type_plus` and `maybe_report_ambiguous_plus` will be run. r? `@estebank`
2 parents 9fc3fc3 + 7b6c5c7 commit d126de1

File tree

5 files changed

+41
-50
lines changed

5 files changed

+41
-50
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

+11-31
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use super::pat::Expected;
2-
use super::ty::{AllowPlus, RecoverQuestionMark};
32
use super::{
4-
BlockMode, CommaRecoveryMode, Parser, PathStyle, RecoverColon, RecoverComma, Restrictions,
5-
SemiColonMode, SeqSep, TokenExpectType, TokenType,
3+
BlockMode, CommaRecoveryMode, Parser, PathStyle, Restrictions, SemiColonMode, SeqSep,
4+
TokenExpectType, TokenType,
65
};
76

87
use crate::lexer::UnmatchedBrace;
@@ -1233,26 +1232,14 @@ impl<'a> Parser<'a> {
12331232
}
12341233
}
12351234

1236-
pub(super) fn maybe_report_ambiguous_plus(
1237-
&mut self,
1238-
allow_plus: AllowPlus,
1239-
impl_dyn_multi: bool,
1240-
ty: &Ty,
1241-
) {
1242-
if matches!(allow_plus, AllowPlus::No) && impl_dyn_multi {
1235+
pub(super) fn maybe_report_ambiguous_plus(&mut self, impl_dyn_multi: bool, ty: &Ty) {
1236+
if impl_dyn_multi {
12431237
self.sess.emit_err(AmbiguousPlus { sum_ty: pprust::ty_to_string(&ty), span: ty.span });
12441238
}
12451239
}
12461240

12471241
/// Swift lets users write `Ty?` to mean `Option<Ty>`. Parse the construct and recover from it.
1248-
pub(super) fn maybe_recover_from_question_mark(
1249-
&mut self,
1250-
ty: P<Ty>,
1251-
recover_question_mark: RecoverQuestionMark,
1252-
) -> P<Ty> {
1253-
if let RecoverQuestionMark::No = recover_question_mark {
1254-
return ty;
1255-
}
1242+
pub(super) fn maybe_recover_from_question_mark(&mut self, ty: P<Ty>) -> P<Ty> {
12561243
if self.token == token::Question {
12571244
self.bump();
12581245
self.struct_span_err(self.prev_token.span, "invalid `?` in type")
@@ -1272,13 +1259,9 @@ impl<'a> Parser<'a> {
12721259
}
12731260
}
12741261

1275-
pub(super) fn maybe_recover_from_bad_type_plus(
1276-
&mut self,
1277-
allow_plus: AllowPlus,
1278-
ty: &Ty,
1279-
) -> PResult<'a, ()> {
1262+
pub(super) fn maybe_recover_from_bad_type_plus(&mut self, ty: &Ty) -> PResult<'a, ()> {
12801263
// Do not add `+` to expected tokens.
1281-
if matches!(allow_plus, AllowPlus::No) || !self.token.is_like_plus() {
1264+
if !self.token.is_like_plus() {
12821265
return Ok(());
12831266
}
12841267

@@ -1444,10 +1427,9 @@ impl<'a> Parser<'a> {
14441427
pub(super) fn maybe_recover_from_bad_qpath<T: RecoverQPath>(
14451428
&mut self,
14461429
base: P<T>,
1447-
allow_recovery: bool,
14481430
) -> PResult<'a, P<T>> {
14491431
// Do not add `::` to expected tokens.
1450-
if allow_recovery && self.token == token::ModSep {
1432+
if self.token == token::ModSep {
14511433
if let Some(ty) = base.to_ty() {
14521434
return self.maybe_recover_from_bad_qpath_stage_2(ty.span, ty);
14531435
}
@@ -1593,7 +1575,7 @@ impl<'a> Parser<'a> {
15931575
_ => ExprKind::Await(expr),
15941576
};
15951577
let expr = self.mk_expr(lo.to(sp), kind, attrs);
1596-
self.maybe_recover_from_bad_qpath(expr, true)
1578+
self.maybe_recover_from_bad_qpath(expr)
15971579
}
15981580

15991581
fn recover_await_macro(&mut self) -> PResult<'a, (Span, P<Expr>, bool)> {
@@ -2457,10 +2439,9 @@ impl<'a> Parser<'a> {
24572439
pub(crate) fn maybe_recover_colon_colon_in_pat_typo(
24582440
&mut self,
24592441
mut first_pat: P<Pat>,
2460-
ra: RecoverColon,
24612442
expected: Expected,
24622443
) -> P<Pat> {
2463-
if RecoverColon::Yes != ra || token::Colon != self.token.kind {
2444+
if token::Colon != self.token.kind {
24642445
return first_pat;
24652446
}
24662447
if !matches!(first_pat.kind, PatKind::Ident(_, _, None) | PatKind::Path(..))
@@ -2594,10 +2575,9 @@ impl<'a> Parser<'a> {
25942575
pub(crate) fn maybe_recover_unexpected_comma(
25952576
&mut self,
25962577
lo: Span,
2597-
rc: RecoverComma,
25982578
rt: CommaRecoveryMode,
25992579
) -> PResult<'a, ()> {
2600-
if rc == RecoverComma::No || self.token != token::Comma {
2580+
if self.token != token::Comma {
26012581
return Ok(());
26022582
}
26032583

compiler/rustc_parse/src/parser/expr.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1417,7 +1417,7 @@ impl<'a> Parser<'a> {
14171417
match self.parse_opt_lit() {
14181418
Some(literal) => {
14191419
let expr = self.mk_expr(lo.to(self.prev_token.span), ExprKind::Lit(literal), attrs);
1420-
self.maybe_recover_from_bad_qpath(expr, true)
1420+
self.maybe_recover_from_bad_qpath(expr)
14211421
}
14221422
None => self.try_macro_suggestion(),
14231423
}
@@ -1444,7 +1444,7 @@ impl<'a> Parser<'a> {
14441444
ExprKind::Tup(es)
14451445
};
14461446
let expr = self.mk_expr(lo.to(self.prev_token.span), kind, attrs);
1447-
self.maybe_recover_from_bad_qpath(expr, true)
1447+
self.maybe_recover_from_bad_qpath(expr)
14481448
}
14491449

14501450
fn parse_array_or_repeat_expr(
@@ -1481,7 +1481,7 @@ impl<'a> Parser<'a> {
14811481
}
14821482
};
14831483
let expr = self.mk_expr(lo.to(self.prev_token.span), kind, attrs);
1484-
self.maybe_recover_from_bad_qpath(expr, true)
1484+
self.maybe_recover_from_bad_qpath(expr)
14851485
}
14861486

14871487
fn parse_path_start_expr(&mut self, attrs: AttrVec) -> PResult<'a, P<Expr>> {
@@ -1519,7 +1519,7 @@ impl<'a> Parser<'a> {
15191519
};
15201520

15211521
let expr = self.mk_expr(lo.to(hi), kind, attrs);
1522-
self.maybe_recover_from_bad_qpath(expr, true)
1522+
self.maybe_recover_from_bad_qpath(expr)
15231523
}
15241524

15251525
/// Parse `'label: $expr`. The label is already parsed.
@@ -1604,7 +1604,7 @@ impl<'a> Parser<'a> {
16041604
let lo = self.prev_token.span;
16051605
let kind = ExprKind::Ret(self.parse_expr_opt()?);
16061606
let expr = self.mk_expr(lo.to(self.prev_token.span), kind, attrs);
1607-
self.maybe_recover_from_bad_qpath(expr, true)
1607+
self.maybe_recover_from_bad_qpath(expr)
16081608
}
16091609

16101610
/// Parse `"do" "yeet" expr?`.
@@ -1619,7 +1619,7 @@ impl<'a> Parser<'a> {
16191619
let span = lo.to(self.prev_token.span);
16201620
self.sess.gated_spans.gate(sym::yeet_expr, span);
16211621
let expr = self.mk_expr(span, kind, attrs);
1622-
self.maybe_recover_from_bad_qpath(expr, true)
1622+
self.maybe_recover_from_bad_qpath(expr)
16231623
}
16241624

16251625
/// Parse `"break" (('label (:? expr)?) | expr?)` with `"break"` token already eaten.
@@ -1679,7 +1679,7 @@ impl<'a> Parser<'a> {
16791679
None
16801680
};
16811681
let expr = self.mk_expr(lo.to(self.prev_token.span), ExprKind::Break(label, kind), attrs);
1682-
self.maybe_recover_from_bad_qpath(expr, true)
1682+
self.maybe_recover_from_bad_qpath(expr)
16831683
}
16841684

16851685
/// Parse `"yield" expr?`.
@@ -1689,7 +1689,7 @@ impl<'a> Parser<'a> {
16891689
let span = lo.to(self.prev_token.span);
16901690
self.sess.gated_spans.gate(sym::generators, span);
16911691
let expr = self.mk_expr(span, kind, attrs);
1692-
self.maybe_recover_from_bad_qpath(expr, true)
1692+
self.maybe_recover_from_bad_qpath(expr)
16931693
}
16941694

16951695
/// Returns a string literal if the next token is a string literal.

compiler/rustc_parse/src/parser/pat.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,10 @@ impl<'a> Parser<'a> {
100100
};
101101

102102
// Parse the first pattern (`p_0`).
103-
let first_pat = self.parse_pat_no_top_alt(expected)?;
104-
self.maybe_recover_unexpected_comma(first_pat.span, rc, rt)?;
103+
let mut first_pat = self.parse_pat_no_top_alt(expected)?;
104+
if rc == RecoverComma::Yes {
105+
self.maybe_recover_unexpected_comma(first_pat.span, rt)?;
106+
}
105107

106108
// If the next token is not a `|`,
107109
// this is not an or-pattern and we should exit here.
@@ -111,7 +113,9 @@ impl<'a> Parser<'a> {
111113
// This complicated procedure is done purely for diagnostics UX.
112114

113115
// Check if the user wrote `foo:bar` instead of `foo::bar`.
114-
let first_pat = self.maybe_recover_colon_colon_in_pat_typo(first_pat, ra, expected);
116+
if ra == RecoverColon::Yes {
117+
first_pat = self.maybe_recover_colon_colon_in_pat_typo(first_pat, expected);
118+
}
115119

116120
if let Some(leading_vert_span) = leading_vert_span {
117121
// If there was a leading vert, treat this as an or-pattern. This improves
@@ -139,7 +143,9 @@ impl<'a> Parser<'a> {
139143
err.span_label(lo, WHILE_PARSING_OR_MSG);
140144
err
141145
})?;
142-
self.maybe_recover_unexpected_comma(pat.span, rc, rt)?;
146+
if rc == RecoverComma::Yes {
147+
self.maybe_recover_unexpected_comma(pat.span, rt)?;
148+
}
143149
pats.push(pat);
144150
}
145151
let or_pattern_span = lo.to(self.prev_token.span);
@@ -408,7 +414,7 @@ impl<'a> Parser<'a> {
408414
};
409415

410416
let pat = self.mk_pat(lo.to(self.prev_token.span), pat);
411-
let pat = self.maybe_recover_from_bad_qpath(pat, true)?;
417+
let pat = self.maybe_recover_from_bad_qpath(pat)?;
412418
let pat = self.recover_intersection_pat(pat)?;
413419

414420
if !allow_range_pat {

compiler/rustc_parse/src/parser/stmt.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ impl<'a> Parser<'a> {
180180
} else {
181181
// Since none of the above applied, this is an expression statement macro.
182182
let e = self.mk_expr(lo.to(hi), ExprKind::MacCall(mac), AttrVec::new());
183-
let e = self.maybe_recover_from_bad_qpath(e, true)?;
183+
let e = self.maybe_recover_from_bad_qpath(e)?;
184184
let e = self.parse_dot_or_call_expr_with(e, lo, attrs.into())?;
185185
let e = self.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(e))?;
186186
StmtKind::Expr(e)

compiler/rustc_parse/src/parser/ty.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -312,13 +312,18 @@ impl<'a> Parser<'a> {
312312
};
313313

314314
let span = lo.to(self.prev_token.span);
315-
let ty = self.mk_ty(span, kind);
315+
let mut ty = self.mk_ty(span, kind);
316316

317317
// Try to recover from use of `+` with incorrect priority.
318-
self.maybe_report_ambiguous_plus(allow_plus, impl_dyn_multi, &ty);
319-
self.maybe_recover_from_bad_type_plus(allow_plus, &ty)?;
320-
let ty = self.maybe_recover_from_question_mark(ty, recover_question_mark);
321-
self.maybe_recover_from_bad_qpath(ty, allow_qpath_recovery)
318+
if matches!(allow_plus, AllowPlus::Yes) {
319+
self.maybe_recover_from_bad_type_plus(&ty)?;
320+
} else {
321+
self.maybe_report_ambiguous_plus(impl_dyn_multi, &ty);
322+
}
323+
if let RecoverQuestionMark::Yes = recover_question_mark {
324+
ty = self.maybe_recover_from_question_mark(ty);
325+
}
326+
if allow_qpath_recovery { self.maybe_recover_from_bad_qpath(ty) } else { Ok(ty) }
322327
}
323328

324329
/// Parses either:

0 commit comments

Comments
 (0)