Skip to content

Commit a75f647

Browse files
committed
Remove a second DiagnosticBuilder::emit_without_consuming call.
Instead of taking `seq` as a mutable reference, the function now consumes `seq` on the recovery path, and returns `seq` unchanged on the non-recovery path. 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 dc92ba9 commit a75f647

File tree

2 files changed

+29
-35
lines changed

2 files changed

+29
-35
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: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,15 +1208,13 @@ impl<'a> Parser<'a> {
12081208
};
12091209
let open_paren = self.token.span;
12101210

1211-
let mut seq = self
1211+
let seq = self
12121212
.parse_expr_paren_seq()
12131213
.map(|args| self.mk_expr(lo.to(self.prev_token.span), self.mk_call(fun, args)));
1214-
if let Some(expr) =
1215-
self.maybe_recover_struct_lit_bad_delims(lo, open_paren, &mut seq, snapshot)
1216-
{
1217-
return expr;
1214+
match self.maybe_recover_struct_lit_bad_delims(lo, open_paren, seq, snapshot) {
1215+
Ok(expr) => expr,
1216+
Err(err) => self.recover_seq_parse_error(Delimiter::Parenthesis, lo, err),
12181217
}
1219-
self.recover_seq_parse_error(Delimiter::Parenthesis, lo, seq)
12201218
}
12211219

12221220
/// If we encounter a parser state that looks like the user has written a `struct` literal with
@@ -1226,10 +1224,10 @@ impl<'a> Parser<'a> {
12261224
&mut self,
12271225
lo: Span,
12281226
open_paren: Span,
1229-
seq: &mut PResult<'a, P<Expr>>,
1227+
seq: PResult<'a, P<Expr>>,
12301228
snapshot: Option<(SnapshotParser<'a>, ExprKind)>,
1231-
) -> Option<P<Expr>> {
1232-
match (self.may_recover(), seq.as_mut(), snapshot) {
1229+
) -> PResult<'a, P<Expr>> {
1230+
match (self.may_recover(), seq, snapshot) {
12331231
(true, Err(err), Some((mut snapshot, ExprKind::Path(None, path)))) => {
12341232
snapshot.bump(); // `(`
12351233
match snapshot.parse_struct_fields(path.clone(), false, Delimiter::Parenthesis) {
@@ -1248,11 +1246,13 @@ impl<'a> Parser<'a> {
12481246
if !fields.is_empty() &&
12491247
// `token.kind` should not be compared here.
12501248
// This is because the `snapshot.token.kind` is treated as the same as
1251-
// that of the open delim in `TokenTreesReader::parse_token_tree`, even if they are different.
1249+
// that of the open delim in `TokenTreesReader::parse_token_tree`, even
1250+
// if they are different.
12521251
self.span_to_snippet(close_paren).is_ok_and(|snippet| snippet == ")")
12531252
{
1254-
let mut replacement_err =
1255-
self.dcx().create_err(errors::ParenthesesWithStructFields {
1253+
err.cancel();
1254+
self.dcx()
1255+
.create_err(errors::ParenthesesWithStructFields {
12561256
span,
12571257
r#type: path,
12581258
braces_for_struct: errors::BracesForStructLiteral {
@@ -1265,23 +1265,22 @@ impl<'a> Parser<'a> {
12651265
.map(|field| field.span.until(field.expr.span))
12661266
.collect(),
12671267
},
1268-
});
1269-
replacement_err.emit_without_consuming();
1270-
1271-
let old_err = mem::replace(err, replacement_err);
1272-
old_err.cancel();
1268+
})
1269+
.emit();
12731270
} else {
1274-
err.emit_without_consuming();
1271+
err.emit();
12751272
}
1276-
return Some(self.mk_expr_err(span));
1273+
Ok(self.mk_expr_err(span))
1274+
}
1275+
Ok(_) => Err(err),
1276+
Err(err2) => {
1277+
err2.cancel();
1278+
Err(err)
12771279
}
1278-
Ok(_) => {}
1279-
Err(err) => err.cancel(),
12801280
}
12811281
}
1282-
_ => {}
1282+
(_, seq, _) => seq,
12831283
}
1284-
None
12851284
}
12861285

12871286
/// Parse an indexing expression `expr[...]`.
@@ -1485,7 +1484,7 @@ impl<'a> Parser<'a> {
14851484
) {
14861485
Ok(x) => x,
14871486
Err(err) => {
1488-
return Ok(self.recover_seq_parse_error(Delimiter::Parenthesis, lo, Err(err)));
1487+
return Ok(self.recover_seq_parse_error(Delimiter::Parenthesis, lo, err));
14891488
}
14901489
};
14911490
let kind = if es.len() == 1 && !trailing_comma {

0 commit comments

Comments
 (0)