Skip to content

Commit 0a917f8

Browse files
authored
Rollup merge of #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 ebeedf0 + fd91925 commit 0a917f8

File tree

16 files changed

+91
-105
lines changed

16 files changed

+91
-105
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 9 additions & 6 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 3 deletions
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

Lines changed: 4 additions & 1 deletion
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

Lines changed: 3 additions & 7 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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
}

0 commit comments

Comments
 (0)