rustc_errors: take self by value in DiagnosticBuilder::cancel.

This commit is contained in:
Eduard-Mihai Burtescu 2022-01-26 03:39:14 +00:00
parent 8562d6b752
commit 0b9d70cf6d
31 changed files with 176 additions and 146 deletions

View file

@ -165,7 +165,7 @@ impl<'a> Parser<'a> {
loop {
// skip any other attributes, we want the item
if snapshot.token.kind == token::Pound {
if let Err(mut err) = snapshot.parse_attribute(InnerAttrPolicy::Permitted) {
if let Err(err) = snapshot.parse_attribute(InnerAttrPolicy::Permitted) {
err.cancel();
return Some(replacement_span);
}
@ -206,7 +206,7 @@ impl<'a> Parser<'a> {
);
return None;
}
Err(mut item_err) => {
Err(item_err) => {
item_err.cancel();
}
Ok(None) => {}
@ -412,12 +412,12 @@ impl<'a> Parser<'a> {
fn parse_meta_item_inner(&mut self) -> PResult<'a, ast::NestedMetaItem> {
match self.parse_unsuffixed_lit() {
Ok(lit) => return Ok(ast::NestedMetaItem::Literal(lit)),
Err(ref mut err) => err.cancel(),
Err(err) => err.cancel(),
}
match self.parse_meta_item() {
Ok(mi) => return Ok(ast::NestedMetaItem::MetaItem(mi)),
Err(ref mut err) => err.cancel(),
Err(err) => err.cancel(),
}
let found = pprust::token_to_string(&self.token);

View file

@ -461,12 +461,12 @@ impl<'a> Parser<'a> {
tail.could_be_bare_literal = true;
Ok(tail)
}
(Err(mut err), Ok(tail)) => {
(Err(err), Ok(tail)) => {
// We have a block tail that contains a somehow valid type ascription expr.
err.cancel();
Ok(tail)
}
(Err(mut snapshot_err), Err(err)) => {
(Err(snapshot_err), Err(err)) => {
// We don't know what went wrong, emit the normal error.
snapshot_err.cancel();
self.consume_block(token::Brace, ConsumeClosingDelim::Yes);
@ -537,7 +537,7 @@ impl<'a> Parser<'a> {
/// Eats and discards tokens until one of `kets` is encountered. Respects token trees,
/// passes through any errors encountered. Used for error recovery.
pub(super) fn eat_to_tokens(&mut self, kets: &[&TokenKind]) {
if let Err(ref mut err) =
if let Err(err) =
self.parse_seq_to_before_tokens(kets, SeqSep::none(), TokenExpectType::Expect, |p| {
Ok(p.parse_token_tree())
})
@ -703,7 +703,7 @@ impl<'a> Parser<'a> {
*self = snapshot;
}
}
Err(mut err) => {
Err(err) => {
// We couldn't parse generic parameters, unlikely to be a turbofish. Rely on
// generic parse error instead.
err.cancel();
@ -744,14 +744,14 @@ impl<'a> Parser<'a> {
self.mk_expr_err(expr.span.to(self.prev_token.span));
return Ok(());
}
Err(mut err) => {
Err(err) => {
*expr = self.mk_expr_err(expr.span);
err.cancel();
}
}
}
}
Err(mut err) => {
Err(err) => {
err.cancel();
}
_ => {}
@ -821,7 +821,7 @@ impl<'a> Parser<'a> {
enclose(r1.span, r2.span);
true
}
Err(mut expr_err) => {
Err(expr_err) => {
expr_err.cancel();
*self = snapshot;
false
@ -838,7 +838,7 @@ impl<'a> Parser<'a> {
enclose(l1.span, r1.span);
true
}
Err(mut expr_err) => {
Err(expr_err) => {
expr_err.cancel();
*self = snapshot;
false
@ -938,7 +938,7 @@ impl<'a> Parser<'a> {
// `ExprKind::Err` placeholder.
mk_err_expr(self, inner_op.span.to(self.prev_token.span))
}
Err(mut expr_err) => {
Err(expr_err) => {
expr_err.cancel();
// Not entirely sure now, but we bubble the error up with the
// suggestion.
@ -1946,17 +1946,14 @@ impl<'a> Parser<'a> {
Ok(expr)
}
fn recover_const_param_decl(
&mut self,
ty_generics: Option<&Generics>,
) -> PResult<'a, Option<GenericArg>> {
fn recover_const_param_decl(&mut self, ty_generics: Option<&Generics>) -> Option<GenericArg> {
let snapshot = self.clone();
let param = match self.parse_const_param(vec![]) {
Ok(param) => param,
Err(mut err) => {
Err(err) => {
err.cancel();
*self = snapshot;
return Err(err);
return None;
}
};
let mut err =
@ -1977,7 +1974,7 @@ impl<'a> Parser<'a> {
}
let value = self.mk_expr_err(param.span());
err.emit();
return Ok(Some(GenericArg::Const(AnonConst { id: ast::DUMMY_NODE_ID, value })));
Some(GenericArg::Const(AnonConst { id: ast::DUMMY_NODE_ID, value }))
}
pub fn recover_const_param_declaration(
@ -1985,8 +1982,8 @@ impl<'a> Parser<'a> {
ty_generics: Option<&Generics>,
) -> PResult<'a, Option<GenericArg>> {
// We have to check for a few different cases.
if let Ok(arg) = self.recover_const_param_decl(ty_generics) {
return Ok(arg);
if let Some(arg) = self.recover_const_param_decl(ty_generics) {
return Ok(Some(arg));
}
// We haven't consumed `const` yet.
@ -2085,7 +2082,7 @@ impl<'a> Parser<'a> {
return Ok(GenericArg::Const(AnonConst { id: ast::DUMMY_NODE_ID, value }));
}
}
Err(mut err) => {
Err(err) => {
err.cancel();
}
}
@ -2139,7 +2136,7 @@ impl<'a> Parser<'a> {
Err(mut err) => {
self.bump(); // Skip the `:`.
match self.parse_pat_no_top_alt(expected) {
Err(mut inner_err) => {
Err(inner_err) => {
// Carry on as if we had not done anything, callers will emit a
// reasonable error.
inner_err.cancel();
@ -2246,7 +2243,7 @@ impl<'a> Parser<'a> {
// suggestion-enhanced error here rather than choking on the comma later.
let comma_span = self.token.span;
self.bump();
if let Err(mut err) = self.skip_pat_list() {
if let Err(err) = self.skip_pat_list() {
// We didn't expect this to work anyway; we just wanted to advance to the
// end of the comma-sequence so we know the span to suggest parenthesizing.
err.cancel();

View file

@ -684,7 +684,7 @@ impl<'a> Parser<'a> {
let parser_snapshot_before_type = self.clone();
let cast_expr = match self.parse_as_cast_ty() {
Ok(rhs) => mk_expr(self, lhs, rhs),
Err(mut type_err) => {
Err(type_err) => {
// Rewind to before attempting to parse the type with generics, to recover
// from situations like `x as usize < y` in which we first tried to parse
// `usize < y` as a type with generic arguments.
@ -717,7 +717,7 @@ impl<'a> Parser<'a> {
.emit();
return Ok(expr);
}
Err(mut err) => {
Err(err) => {
err.cancel();
*self = snapshot;
}
@ -773,7 +773,7 @@ impl<'a> Parser<'a> {
expr
}
Err(mut path_err) => {
Err(path_err) => {
// Couldn't parse as a path, return original error and parser state.
path_err.cancel();
*self = parser_snapshot_after_type;
@ -1127,7 +1127,7 @@ impl<'a> Parser<'a> {
snapshot: Option<(Self, ExprKind)>,
) -> Option<P<Expr>> {
match (seq.as_mut(), snapshot) {
(Err(ref mut err), Some((mut snapshot, ExprKind::Path(None, path)))) => {
(Err(err), Some((mut snapshot, ExprKind::Path(None, path)))) => {
let name = pprust::path_to_string(&path);
snapshot.bump(); // `(`
match snapshot.parse_struct_fields(path, false, token::Paren) {
@ -1138,11 +1138,12 @@ impl<'a> Parser<'a> {
let close_paren = self.prev_token.span;
let span = lo.to(self.prev_token.span);
if !fields.is_empty() {
err.cancel();
let mut err = self.struct_span_err(
let replacement_err = self.struct_span_err(
span,
"invalid `struct` delimiters or `fn` call arguments",
);
mem::replace(err, replacement_err).cancel();
err.multipart_suggestion(
&format!("if `{}` is a struct, use braces as delimiters", name),
vec![
@ -1878,7 +1879,7 @@ impl<'a> Parser<'a> {
*self = snapshot;
Some(self.mk_expr_err(arr.span))
}
Err(mut e) => {
Err(e) => {
e.cancel();
None
}
@ -2381,7 +2382,7 @@ impl<'a> Parser<'a> {
return Some(err(self, stmts));
}
}
Err(mut err) => {
Err(err) => {
err.cancel();
}
}
@ -2398,7 +2399,7 @@ impl<'a> Parser<'a> {
}
// We couldn't parse either yet another statement missing it's
// enclosing block nor the next arm's pattern or closing brace.
Err(mut stmt_err) => {
Err(stmt_err) => {
stmt_err.cancel();
*self = start_snapshot;
break;

View file

@ -130,7 +130,7 @@ impl<'a> Parser<'a> {
// FIXME - try to continue parsing other generics?
return Ok((None, TrailingToken::None));
}
Err(mut err) => {
Err(err) => {
err.cancel();
// FIXME - maybe we should overwrite 'self' outside of `collect_tokens`?
*this = snapshot;

View file

@ -1114,7 +1114,7 @@ impl<'a> Parser<'a> {
// Only try to recover if this is implementing a trait for a type
let mut impl_info = match self.parse_item_impl(attrs, defaultness) {
Ok(impl_info) => impl_info,
Err(mut recovery_error) => {
Err(recovery_error) => {
// Recovery failed, raise the "expected identifier" error
recovery_error.cancel();
return Err(err);
@ -1476,7 +1476,9 @@ impl<'a> Parser<'a> {
// after the comma
self.eat(&token::Comma);
// `check_trailing_angle_brackets` already emitted a nicer error
err.cancel();
// NOTE(eddyb) this was `.cancel()`, but `err`
// gets returned, so we can't fully defuse it.
err.downgrade_to_delayed_bug();
}
}
}
@ -2073,7 +2075,7 @@ impl<'a> Parser<'a> {
if let Ok(snippet) = self.span_to_snippet(sp) {
let current_vis = match self.parse_visibility(FollowedByType::No) {
Ok(v) => v,
Err(mut d) => {
Err(d) => {
d.cancel();
return Err(err);
}
@ -2216,7 +2218,7 @@ impl<'a> Parser<'a> {
// If this is a C-variadic argument and we hit an error, return the error.
Err(err) if this.token == token::DotDotDot => return Err(err),
// Recover from attempting to parse the argument as a type without pattern.
Err(mut err) => {
Err(err) => {
err.cancel();
*this = parser_snapshot_before_ty;
this.recover_arg_parse()?
@ -2358,7 +2360,7 @@ impl<'a> Parser<'a> {
match self
.parse_outer_attributes()
.and_then(|_| self.parse_self_param())
.map_err(|mut e| e.cancel())
.map_err(|e| e.cancel())
{
Ok(Some(_)) => "method",
_ => "function",

View file

@ -849,7 +849,7 @@ impl<'a> Parser<'a> {
v.push(t);
continue;
}
Err(mut e) => {
Err(e) => {
// Parsing failed, therefore it must be something more serious
// than just a missing separator.
expect_err.emit();

View file

@ -655,7 +655,7 @@ impl<'a> Parser<'a> {
fn fatal_unexpected_non_pat(
&mut self,
mut err: DiagnosticBuilder<'a>,
err: DiagnosticBuilder<'a>,
expected: Expected,
) -> PResult<'a, P<Pat>> {
err.cancel();
@ -722,7 +722,7 @@ impl<'a> Parser<'a> {
// Ensure the user doesn't receive unhelpful unexpected token errors
self.bump();
if self.is_pat_range_end_start(0) {
let _ = self.parse_pat_range_end().map_err(|mut e| e.cancel());
let _ = self.parse_pat_range_end().map_err(|e| e.cancel());
}
self.error_inclusive_range_with_extra_equals(span_with_eq);

View file

@ -394,7 +394,7 @@ impl<'a> Parser<'a> {
debug!("parse_generic_args_with_leading_angle_bracket_recovery: (snapshotting)");
match self.parse_angle_args(ty_generics) {
Ok(args) => Ok(args),
Err(mut e) if is_first_invocation && self.unmatched_angle_bracket_count > 0 => {
Err(e) if is_first_invocation && self.unmatched_angle_bracket_count > 0 => {
// Swap `self` with our backup of the parser state before attempting to parse
// generic arguments.
let snapshot = mem::replace(self, snapshot.unwrap());

View file

@ -296,7 +296,7 @@ impl<'a> Parser<'a> {
// extra noise.
init
}
(Err(mut init_err), Some((snapshot, _, ty_err))) => {
(Err(init_err), Some((snapshot, _, ty_err))) => {
// init error, ty error
init_err.cancel();
// Couldn't parse the type nor the initializer, only raise the type error and
@ -449,7 +449,7 @@ impl<'a> Parser<'a> {
);
}
}
Err(mut e) => {
Err(e) => {
self.recover_stmt_(SemiColonMode::Break, BlockMode::Ignore);
e.cancel();
}