Skip to content

Commit 3ce34f4

Browse files
committed
Remove a second DiagnosticBuilder::emit_without_consuming call.
Instead of taking `seq` as a mutable reference, `maybe_recover_struct_lit_bad_delims` now consumes `seq` on the recovery path, and returns `seq` unchanged on the non-recovery path. The commit also combines an `if` and a `match` to merge two identical paths. Also change `recover_seq_parse_error` so it receives a `PErr` instead of a `PResult`, because all the call sites now handle the `Ok`/`Err` distinction themselves.
1 parent 1881055 commit 3ce34f4

File tree

2 files changed

+30
-39
lines changed

2 files changed

+30
-39
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use rustc_ast_pretty::pprust;
3535
use rustc_data_structures::fx::FxHashSet;
3636
use rustc_errors::{
3737
pluralize, AddToDiagnostic, Applicability, DiagCtxt, Diagnostic, DiagnosticBuilder, FatalError,
38-
PResult,
38+
PErr, PResult,
3939
};
4040
use rustc_session::errors::ExprParenthesesNeeded;
4141
use rustc_span::source_map::Spanned;
@@ -2044,17 +2044,12 @@ impl<'a> Parser<'a> {
20442044
&mut self,
20452045
delim: Delimiter,
20462046
lo: Span,
2047-
result: PResult<'a, P<Expr>>,
2047+
err: PErr<'a>,
20482048
) -> P<Expr> {
2049-
match result {
2050-
Ok(x) => x,
2051-
Err(err) => {
2052-
err.emit();
2053-
// Recover from parse error, callers expect the closing delim to be consumed.
2054-
self.consume_block(delim, ConsumeClosingDelim::Yes);
2055-
self.mk_expr(lo.to(self.prev_token.span), ExprKind::Err)
2056-
}
2057-
}
2049+
err.emit();
2050+
// Recover from parse error, callers expect the closing delim to be consumed.
2051+
self.consume_block(delim, ConsumeClosingDelim::Yes);
2052+
self.mk_expr(lo.to(self.prev_token.span), ExprKind::Err)
20582053
}
20592054

20602055
/// Eats tokens until we can be relatively sure we reached the end of the

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,15 +1272,13 @@ impl<'a> Parser<'a> {
12721272
};
12731273
let open_paren = self.token.span;
12741274

1275-
let mut seq = self
1275+
let seq = self
12761276
.parse_expr_paren_seq()
12771277
.map(|args| self.mk_expr(lo.to(self.prev_token.span), self.mk_call(fun, args)));
1278-
if let Some(expr) =
1279-
self.maybe_recover_struct_lit_bad_delims(lo, open_paren, &mut seq, snapshot)
1280-
{
1281-
return expr;
1278+
match self.maybe_recover_struct_lit_bad_delims(lo, open_paren, seq, snapshot) {
1279+
Ok(expr) => expr,
1280+
Err(err) => self.recover_seq_parse_error(Delimiter::Parenthesis, lo, err),
12821281
}
1283-
self.recover_seq_parse_error(Delimiter::Parenthesis, lo, seq)
12841282
}
12851283

12861284
/// If we encounter a parser state that looks like the user has written a `struct` literal with
@@ -1290,14 +1288,11 @@ impl<'a> Parser<'a> {
12901288
&mut self,
12911289
lo: Span,
12921290
open_paren: Span,
1293-
seq: &mut PResult<'a, P<Expr>>,
1291+
seq: PResult<'a, P<Expr>>,
12941292
snapshot: Option<(SnapshotParser<'a>, ExprKind)>,
1295-
) -> Option<P<Expr>> {
1296-
if !self.may_recover() {
1297-
return None;
1298-
}
1299-
match (seq.as_mut(), snapshot) {
1300-
(Err(err), Some((mut snapshot, ExprKind::Path(None, path)))) => {
1293+
) -> PResult<'a, P<Expr>> {
1294+
match (self.may_recover(), seq, snapshot) {
1295+
(true, Err(err), Some((mut snapshot, ExprKind::Path(None, path)))) => {
13011296
snapshot.bump(); // `(`
13021297
match snapshot.parse_struct_fields(path.clone(), false, Delimiter::Parenthesis) {
13031298
Ok((fields, ..))
@@ -1315,11 +1310,13 @@ impl<'a> Parser<'a> {
13151310
if !fields.is_empty() &&
13161311
// `token.kind` should not be compared here.
13171312
// This is because the `snapshot.token.kind` is treated as the same as
1318-
// that of the open delim in `TokenTreesReader::parse_token_tree`, even if they are different.
1313+
// that of the open delim in `TokenTreesReader::parse_token_tree`, even
1314+
// if they are different.
13191315
self.span_to_snippet(close_paren).is_ok_and(|snippet| snippet == ")")
13201316
{
1321-
let mut replacement_err =
1322-
self.dcx().create_err(errors::ParenthesesWithStructFields {
1317+
err.cancel();
1318+
self.dcx()
1319+
.create_err(errors::ParenthesesWithStructFields {
13231320
span,
13241321
r#type: path,
13251322
braces_for_struct: errors::BracesForStructLiteral {
@@ -1332,23 +1329,22 @@ impl<'a> Parser<'a> {
13321329
.map(|field| field.span.until(field.expr.span))
13331330
.collect(),
13341331
},
1335-
});
1336-
replacement_err.emit_without_consuming();
1337-
1338-
let old_err = mem::replace(err, replacement_err);
1339-
old_err.cancel();
1332+
})
1333+
.emit();
13401334
} else {
1341-
err.emit_without_consuming();
1335+
err.emit();
13421336
}
1343-
return Some(self.mk_expr_err(span));
1337+
Ok(self.mk_expr_err(span))
1338+
}
1339+
Ok(_) => Err(err),
1340+
Err(err2) => {
1341+
err2.cancel();
1342+
Err(err)
13441343
}
1345-
Ok(_) => {}
1346-
Err(err) => err.cancel(),
13471344
}
13481345
}
1349-
_ => {}
1346+
(_, seq, _) => seq,
13501347
}
1351-
None
13521348
}
13531349

13541350
/// Parse an indexing expression `expr[...]`.
@@ -1552,7 +1548,7 @@ impl<'a> Parser<'a> {
15521548
) {
15531549
Ok(x) => x,
15541550
Err(err) => {
1555-
return Ok(self.recover_seq_parse_error(Delimiter::Parenthesis, lo, Err(err)));
1551+
return Ok(self.recover_seq_parse_error(Delimiter::Parenthesis, lo, err));
15561552
}
15571553
};
15581554
let kind = if es.len() == 1 && !trailing_comma {

0 commit comments

Comments
 (0)