Skip to content

Commit 7a8f03b

Browse files
authored
Unrolled build for rust-lang#124919
Rollup merge of rust-lang#124919 - nnethercote:Recovered-Yes-ErrorGuaranteed, r=compiler-errors Add `ErrorGuaranteed` to `Recovered::Yes` and use it more. The starting point for this was identical comments on two different fields, in `ast::VariantData::Struct` and `hir::VariantData::Struct`: ``` // FIXME: investigate making this a `Option<ErrorGuaranteed>` recovered: bool ``` I tried that, and then found that I needed to add an `ErrorGuaranteed` to `Recovered::Yes`. Then I ended up using `Recovered` instead of `Option<ErrorGuaranteed>` for these two places and elsewhere, which required moving `ErrorGuaranteed` from `rustc_parse` to `rustc_ast`. This makes things more consistent, because `Recovered` is used in more places, and there are fewer uses of `bool` and `Option<ErrorGuaranteed>`. And safer, because it's difficult/impossible to set `recovered` to `Recovered::Yes` without having emitted an error. r? `@oli-obk`
2 parents 238c1e7 + fd91925 commit 7a8f03b

File tree

16 files changed

+91
-105
lines changed

16 files changed

+91
-105
lines changed

compiler/rustc_ast/src/ast.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -1422,7 +1422,7 @@ pub enum ExprKind {
14221422
/// of `if` / `while` expressions. (e.g., `if let 0 = x { .. }`).
14231423
///
14241424
/// `Span` represents the whole `let pat = expr` statement.
1425-
Let(P<Pat>, P<Expr>, Span, Option<ErrorGuaranteed>),
1425+
Let(P<Pat>, P<Expr>, Span, Recovered),
14261426
/// An `if` block, with an optional `else` block.
14271427
///
14281428
/// `if expr { block } else { expr }`
@@ -2881,17 +2881,20 @@ pub struct FieldDef {
28812881
pub is_placeholder: bool,
28822882
}
28832883

2884+
/// Was parsing recovery performed?
2885+
#[derive(Copy, Clone, Debug, Encodable, Decodable, HashStable_Generic)]
2886+
pub enum Recovered {
2887+
No,
2888+
Yes(ErrorGuaranteed),
2889+
}
2890+
28842891
/// Fields and constructor ids of enum variants and structs.
28852892
#[derive(Clone, Encodable, Decodable, Debug)]
28862893
pub enum VariantData {
28872894
/// Struct variant.
28882895
///
28892896
/// E.g., `Bar { .. }` as in `enum Foo { Bar { .. } }`.
2890-
Struct {
2891-
fields: ThinVec<FieldDef>,
2892-
// FIXME: investigate making this a `Option<ErrorGuaranteed>`
2893-
recovered: bool,
2894-
},
2897+
Struct { fields: ThinVec<FieldDef>, recovered: Recovered },
28952898
/// Tuple variant.
28962899
///
28972900
/// E.g., `Bar(..)` as in `enum Foo { Bar(..) }`.

compiler/rustc_ast_lowering/src/expr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
158158
let ohs = self.lower_expr(ohs);
159159
hir::ExprKind::AddrOf(*k, *m, ohs)
160160
}
161-
ExprKind::Let(pat, scrutinee, span, is_recovered) => {
161+
ExprKind::Let(pat, scrutinee, span, recovered) => {
162162
hir::ExprKind::Let(self.arena.alloc(hir::LetExpr {
163163
span: self.lower_span(*span),
164164
pat: self.lower_pat(pat),
165165
ty: None,
166166
init: self.lower_expr(scrutinee),
167-
is_recovered: *is_recovered,
167+
recovered: *recovered,
168168
}))
169169
}
170170
ExprKind::If(cond, then, else_opt) => {

compiler/rustc_ast_lowering/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1283,7 +1283,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12831283
fields.iter().enumerate().map(|f| this.lower_field_def(f)),
12841284
);
12851285
let span = t.span;
1286-
let variant_data = hir::VariantData::Struct { fields, recovered: false };
1286+
let variant_data =
1287+
hir::VariantData::Struct { fields, recovered: ast::Recovered::No };
12871288
// FIXME: capture the generics from the outer adt.
12881289
let generics = hir::Generics::empty();
12891290
let kind = match t.kind {

compiler/rustc_builtin_macros/src/format.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@ use rustc_ast::{token, StmtKind};
77
use rustc_ast::{
88
Expr, ExprKind, FormatAlignment, FormatArgPosition, FormatArgPositionKind, FormatArgs,
99
FormatArgsPiece, FormatArgument, FormatArgumentKind, FormatArguments, FormatCount,
10-
FormatDebugHex, FormatOptions, FormatPlaceholder, FormatSign, FormatTrait,
10+
FormatDebugHex, FormatOptions, FormatPlaceholder, FormatSign, FormatTrait, Recovered,
1111
};
1212
use rustc_data_structures::fx::FxHashSet;
1313
use rustc_errors::{Applicability, Diag, MultiSpan, PResult, SingleLabelManySpans};
1414
use rustc_expand::base::*;
1515
use rustc_lint_defs::builtin::NAMED_ARGUMENTS_USED_POSITIONALLY;
1616
use rustc_lint_defs::{BufferedEarlyLint, BuiltinLintDiag, LintId};
17-
use rustc_parse::parser::Recovered;
1817
use rustc_parse_format as parse;
1918
use rustc_span::symbol::{Ident, Symbol};
2019
use rustc_span::{BytePos, ErrorGuaranteed, InnerSpan, Span};
@@ -112,7 +111,7 @@ fn parse_args<'a>(ecx: &ExtCtxt<'a>, sp: Span, tts: TokenStream) -> PResult<'a,
112111
_ => return Err(err),
113112
}
114113
}
115-
Ok(Recovered::Yes) => (),
114+
Ok(Recovered::Yes(_)) => (),
116115
Ok(Recovered::No) => unreachable!(),
117116
}
118117
}

compiler/rustc_expand/src/placeholders.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,10 @@ pub(crate) fn placeholder(
174174
}]),
175175
AstFragmentKind::Variants => AstFragment::Variants(smallvec![ast::Variant {
176176
attrs: Default::default(),
177-
data: ast::VariantData::Struct { fields: Default::default(), recovered: false },
177+
data: ast::VariantData::Struct {
178+
fields: Default::default(),
179+
recovered: ast::Recovered::No
180+
},
178181
disr_expr: None,
179182
id,
180183
ident,

compiler/rustc_hir/src/hir.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -1308,9 +1308,9 @@ pub struct LetExpr<'hir> {
13081308
pub pat: &'hir Pat<'hir>,
13091309
pub ty: Option<&'hir Ty<'hir>>,
13101310
pub init: &'hir Expr<'hir>,
1311-
/// `Some` when this let expressions is not in a syntanctically valid location.
1311+
/// `Recovered::Yes` when this let expressions is not in a syntanctically valid location.
13121312
/// Used to prevent building MIR in such situations.
1313-
pub is_recovered: Option<ErrorGuaranteed>,
1313+
pub recovered: ast::Recovered,
13141314
}
13151315

13161316
#[derive(Debug, Clone, Copy, HashStable_Generic)]
@@ -3030,11 +3030,7 @@ pub enum VariantData<'hir> {
30303030
/// A struct variant.
30313031
///
30323032
/// E.g., `Bar { .. }` as in `enum Foo { Bar { .. } }`.
3033-
Struct {
3034-
fields: &'hir [FieldDef<'hir>],
3035-
// FIXME: investigate making this a `Option<ErrorGuaranteed>`
3036-
recovered: bool,
3037-
},
3033+
Struct { fields: &'hir [FieldDef<'hir>], recovered: ast::Recovered },
30383034
/// A tuple variant.
30393035
///
30403036
/// E.g., `Bar(..)` as in `enum Foo { Bar(..) }`.

compiler/rustc_hir/src/intravisit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>)
768768
ExprKind::DropTemps(ref subexpression) => {
769769
try_visit!(visitor.visit_expr(subexpression));
770770
}
771-
ExprKind::Let(LetExpr { span: _, pat, ty, init, is_recovered: _ }) => {
771+
ExprKind::Let(LetExpr { span: _, pat, ty, init, recovered: _ }) => {
772772
// match the visit order in walk_local
773773
try_visit!(visitor.visit_expr(init));
774774
try_visit!(visitor.visit_pat(pat));

compiler/rustc_hir_analysis/src/collect.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
//! At present, however, we do run collection across all items in the
1515
//! crate as a kind of pass. This should eventually be factored away.
1616
17+
use rustc_ast::Recovered;
1718
use rustc_data_structures::captures::Captures;
1819
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
1920
use rustc_data_structures::unord::UnordMap;
@@ -1005,10 +1006,7 @@ fn lower_variant(
10051006
vis: tcx.visibility(f.def_id),
10061007
})
10071008
.collect();
1008-
let recovered = match def {
1009-
hir::VariantData::Struct { recovered, .. } => *recovered,
1010-
_ => false,
1011-
};
1009+
let recovered = matches!(def, hir::VariantData::Struct { recovered: Recovered::Yes(_), .. });
10121010
ty::VariantDef::new(
10131011
ident.name,
10141012
variant_did.map(LocalDefId::to_def_id),

compiler/rustc_hir_typeck/src/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1271,7 +1271,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12711271
// otherwise check exactly as a let statement
12721272
self.check_decl((let_expr, hir_id).into());
12731273
// but return a bool, for this is a boolean expression
1274-
if let Some(error_guaranteed) = let_expr.is_recovered {
1274+
if let ast::Recovered::Yes(error_guaranteed) = let_expr.recovered {
12751275
self.set_tainted_by_errors(error_guaranteed);
12761276
Ty::new_error(self.tcx, error_guaranteed)
12771277
} else {

compiler/rustc_hir_typeck/src/gather_locals.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl<'a> From<&'a hir::LetStmt<'a>> for Declaration<'a> {
5050

5151
impl<'a> From<(&'a hir::LetExpr<'a>, HirId)> for Declaration<'a> {
5252
fn from((let_expr, hir_id): (&'a hir::LetExpr<'a>, HirId)) -> Self {
53-
let hir::LetExpr { pat, ty, span, init, is_recovered: _ } = *let_expr;
53+
let hir::LetExpr { pat, ty, span, init, recovered: _ } = *let_expr;
5454
Declaration { hir_id, pat, ty, span, init: Some(init), origin: DeclOrigin::LetExpr }
5555
}
5656
}

compiler/rustc_parse/src/parser/diagnostics.rs

+21-22
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use crate::fluent_generated as fluent;
2222
use crate::parser;
2323
use crate::parser::attr::InnerAttrPolicy;
2424
use ast::token::IdentIsRaw;
25-
use parser::Recovered;
2625
use rustc_ast as ast;
2726
use rustc_ast::ptr::P;
2827
use rustc_ast::token::{self, Delimiter, Lit, LitKind, Token, TokenKind};
@@ -31,7 +30,7 @@ use rustc_ast::util::parser::AssocOp;
3130
use rustc_ast::{
3231
AngleBracketedArg, AngleBracketedArgs, AnonConst, AttrVec, BinOpKind, BindingMode, Block,
3332
BlockCheckMode, Expr, ExprKind, GenericArg, Generics, HasTokens, Item, ItemKind, Param, Pat,
34-
PatKind, Path, PathSegment, QSelf, Ty, TyKind,
33+
PatKind, Path, PathSegment, QSelf, Recovered, Ty, TyKind,
3534
};
3635
use rustc_ast_pretty::pprust;
3736
use rustc_data_structures::fx::FxHashSet;
@@ -527,14 +526,14 @@ impl<'a> Parser<'a> {
527526
//
528527
// let x = 32:
529528
// let y = 42;
530-
self.dcx().emit_err(ExpectedSemi {
529+
let guar = self.dcx().emit_err(ExpectedSemi {
531530
span: self.token.span,
532531
token: self.token.clone(),
533532
unexpected_token_label: None,
534533
sugg: ExpectedSemiSugg::ChangeToSemi(self.token.span),
535534
});
536535
self.bump();
537-
return Ok(Recovered::Yes);
536+
return Ok(Recovered::Yes(guar));
538537
} else if self.look_ahead(0, |t| {
539538
t == &token::CloseDelim(Delimiter::Brace)
540539
|| ((t.can_begin_expr() || t.can_begin_item())
@@ -552,13 +551,13 @@ impl<'a> Parser<'a> {
552551
// let x = 32
553552
// let y = 42;
554553
let span = self.prev_token.span.shrink_to_hi();
555-
self.dcx().emit_err(ExpectedSemi {
554+
let guar = self.dcx().emit_err(ExpectedSemi {
556555
span,
557556
token: self.token.clone(),
558557
unexpected_token_label: Some(self.token.span),
559558
sugg: ExpectedSemiSugg::AddSemi(span),
560559
});
561-
return Ok(Recovered::Yes);
560+
return Ok(Recovered::Yes(guar));
562561
}
563562
}
564563

@@ -712,8 +711,8 @@ impl<'a> Parser<'a> {
712711

713712
if self.check_too_many_raw_str_terminators(&mut err) {
714713
if expected.contains(&TokenType::Token(token::Semi)) && self.eat(&token::Semi) {
715-
err.emit();
716-
return Ok(Recovered::Yes);
714+
let guar = err.emit();
715+
return Ok(Recovered::Yes(guar));
717716
} else {
718717
return Err(err);
719718
}
@@ -1251,7 +1250,7 @@ impl<'a> Parser<'a> {
12511250
}
12521251
}
12531252
}
1254-
Ok((_, _, Recovered::Yes)) => {}
1253+
Ok((_, _, Recovered::Yes(_))) => {}
12551254
Err(err) => {
12561255
err.cancel();
12571256
}
@@ -1284,21 +1283,21 @@ impl<'a> Parser<'a> {
12841283

12851284
/// Check to see if a pair of chained operators looks like an attempt at chained comparison,
12861285
/// e.g. `1 < x <= 3`. If so, suggest either splitting the comparison into two, or
1287-
/// parenthesising the leftmost comparison.
1286+
/// parenthesising the leftmost comparison. The return value indicates if recovery happened.
12881287
fn attempt_chained_comparison_suggestion(
12891288
&mut self,
12901289
err: &mut ComparisonOperatorsCannotBeChained,
12911290
inner_op: &Expr,
12921291
outer_op: &Spanned<AssocOp>,
1293-
) -> Recovered {
1292+
) -> bool {
12941293
if let ExprKind::Binary(op, l1, r1) = &inner_op.kind {
12951294
if let ExprKind::Field(_, ident) = l1.kind
12961295
&& ident.as_str().parse::<i32>().is_err()
12971296
&& !matches!(r1.kind, ExprKind::Lit(_))
12981297
{
12991298
// The parser has encountered `foo.bar<baz`, the likelihood of the turbofish
13001299
// suggestion being the only one to apply is high.
1301-
return Recovered::No;
1300+
return false;
13021301
}
13031302
return match (op.node, &outer_op.node) {
13041303
// `x == y == z`
@@ -1317,7 +1316,7 @@ impl<'a> Parser<'a> {
13171316
span: inner_op.span.shrink_to_hi(),
13181317
middle_term: expr_to_str(r1),
13191318
});
1320-
Recovered::No // Keep the current parse behavior, where the AST is `(x < y) < z`.
1319+
false // Keep the current parse behavior, where the AST is `(x < y) < z`.
13211320
}
13221321
// `x == y < z`
13231322
(BinOpKind::Eq, AssocOp::Less | AssocOp::LessEqual | AssocOp::Greater | AssocOp::GreaterEqual) => {
@@ -1331,12 +1330,12 @@ impl<'a> Parser<'a> {
13311330
left: r1.span.shrink_to_lo(),
13321331
right: r2.span.shrink_to_hi(),
13331332
});
1334-
Recovered::Yes
1333+
true
13351334
}
13361335
Err(expr_err) => {
13371336
expr_err.cancel();
13381337
self.restore_snapshot(snapshot);
1339-
Recovered::Yes
1338+
true
13401339
}
13411340
}
13421341
}
@@ -1351,19 +1350,19 @@ impl<'a> Parser<'a> {
13511350
left: l1.span.shrink_to_lo(),
13521351
right: r1.span.shrink_to_hi(),
13531352
});
1354-
Recovered::Yes
1353+
true
13551354
}
13561355
Err(expr_err) => {
13571356
expr_err.cancel();
13581357
self.restore_snapshot(snapshot);
1359-
Recovered::No
1358+
false
13601359
}
13611360
}
13621361
}
1363-
_ => Recovered::No,
1362+
_ => false
13641363
};
13651364
}
1366-
Recovered::No
1365+
false
13671366
}
13681367

13691368
/// Produces an error if comparison operators are chained (RFC #558).
@@ -1494,7 +1493,7 @@ impl<'a> Parser<'a> {
14941493
// misformatted turbofish, for instance), suggest a correct form.
14951494
let recovered = self
14961495
.attempt_chained_comparison_suggestion(&mut err, inner_op, outer_op);
1497-
if matches!(recovered, Recovered::Yes) {
1496+
if recovered {
14981497
let guar = self.dcx().emit_err(err);
14991498
mk_err_expr(self, inner_op.span.to(self.prev_token.span), guar)
15001499
} else {
@@ -1503,10 +1502,10 @@ impl<'a> Parser<'a> {
15031502
}
15041503
};
15051504
}
1506-
let recover =
1505+
let recovered =
15071506
self.attempt_chained_comparison_suggestion(&mut err, inner_op, outer_op);
15081507
let guar = self.dcx().emit_err(err);
1509-
if matches!(recover, Recovered::Yes) {
1508+
if recovered {
15101509
return mk_err_expr(self, inner_op.span.to(self.prev_token.span), guar);
15111510
}
15121511
}

0 commit comments

Comments
 (0)