Skip to content

Commit f8caa32

Browse files
committed
Auto merge of #56999 - petrochenkov:macrecov2, r=estebank
AST/HIR: Introduce `ExprKind::Err` for better error recovery in the front-end This way we can avoid aborting compilation if expansion produces errors and generate `ExprKind::Err`s instead.
2 parents fb86d60 + bc16ede commit f8caa32

File tree

127 files changed

+712
-367
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

127 files changed

+712
-367
lines changed

src/librustc/cfg/construct.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,8 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
392392

393393
hir::ExprKind::Closure(..) |
394394
hir::ExprKind::Lit(..) |
395-
hir::ExprKind::Path(_) => {
395+
hir::ExprKind::Path(_) |
396+
hir::ExprKind::Err => {
396397
self.straightline(expr, pred, None::<hir::Expr>.iter())
397398
}
398399
}

src/librustc/hir/intravisit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,7 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
10991099
ExprKind::Yield(ref subexpression) => {
11001100
visitor.visit_expr(subexpression);
11011101
}
1102+
ExprKind::Err => {}
11021103
}
11031104
}
11041105

src/librustc/hir/lowering.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2709,7 +2709,6 @@ impl<'a> LoweringContext<'a> {
27092709
rules: self.lower_block_check_mode(&b.rules),
27102710
span: b.span,
27112711
targeted_by_break,
2712-
recovered: b.recovered,
27132712
})
27142713
}
27152714

@@ -3781,7 +3780,6 @@ impl<'a> LoweringContext<'a> {
37813780
rules: hir::DefaultBlock,
37823781
span,
37833782
targeted_by_break: false,
3784-
recovered: blk.recovered,
37853783
});
37863784
P(self.expr_block(blk, ThinVec::new()))
37873785
}
@@ -4117,6 +4115,8 @@ impl<'a> LoweringContext<'a> {
41174115
hir::ExprKind::Yield(P(expr))
41184116
}
41194117

4118+
ExprKind::Err => hir::ExprKind::Err,
4119+
41204120
// Desugar `ExprIfLet`
41214121
// from: `if let <pat> = <sub_expr> <body> [<else_opt>]`
41224122
ExprKind::IfLet(ref pats, ref sub_expr, ref body, ref else_opt) => {
@@ -4821,7 +4821,6 @@ impl<'a> LoweringContext<'a> {
48214821
rules: hir::DefaultBlock,
48224822
span,
48234823
targeted_by_break: false,
4824-
recovered: false,
48254824
}
48264825
}
48274826

src/librustc/hir/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -807,11 +807,6 @@ pub struct Block {
807807
/// break out of this block early.
808808
/// Used by `'label: {}` blocks and by `catch` statements.
809809
pub targeted_by_break: bool,
810-
/// If true, don't emit return value type errors as the parser had
811-
/// to recover from a parse error so this block will not have an
812-
/// appropriate type. A parse error will have been emitted so the
813-
/// compilation will never succeed if this is true.
814-
pub recovered: bool,
815810
}
816811

817812
#[derive(Clone, RustcEncodable, RustcDecodable)]
@@ -1362,6 +1357,7 @@ impl Expr {
13621357
ExprKind::Struct(..) => ExprPrecedence::Struct,
13631358
ExprKind::Repeat(..) => ExprPrecedence::Repeat,
13641359
ExprKind::Yield(..) => ExprPrecedence::Yield,
1360+
ExprKind::Err => ExprPrecedence::Err,
13651361
}
13661362
}
13671363

@@ -1412,7 +1408,8 @@ impl Expr {
14121408
ExprKind::AddrOf(..) |
14131409
ExprKind::Binary(..) |
14141410
ExprKind::Yield(..) |
1415-
ExprKind::Cast(..) => {
1411+
ExprKind::Cast(..) |
1412+
ExprKind::Err => {
14161413
false
14171414
}
14181415
}
@@ -1525,6 +1522,9 @@ pub enum ExprKind {
15251522

15261523
/// A suspension point for generators. This is `yield <expr>` in Rust.
15271524
Yield(P<Expr>),
1525+
1526+
/// Placeholder for an expression that wasn't syntactically well formed in some way.
1527+
Err,
15281528
}
15291529

15301530
/// Optionally `Self`-qualified value/type path or associated extension.

src/librustc/hir/print.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,9 @@ impl<'a> State<'a> {
430430
self.s.word("_")?;
431431
}
432432
hir::TyKind::Err => {
433-
self.s.word("?")?;
433+
self.popen()?;
434+
self.s.word("/*ERROR*/")?;
435+
self.pclose()?;
434436
}
435437
}
436438
self.end()
@@ -1540,6 +1542,11 @@ impl<'a> State<'a> {
15401542
self.word_space("yield")?;
15411543
self.print_expr_maybe_paren(&expr, parser::PREC_JUMP)?;
15421544
}
1545+
hir::ExprKind::Err => {
1546+
self.popen()?;
1547+
self.s.word("/*ERROR*/")?;
1548+
self.pclose()?;
1549+
}
15431550
}
15441551
self.ann.post(self, AnnNode::Expr(expr))?;
15451552
self.end()

src/librustc/ich/impls_hir.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,6 @@ impl_stable_hash_for!(struct hir::Block {
410410
rules,
411411
span,
412412
targeted_by_break,
413-
recovered,
414413
});
415414

416415
impl_stable_hash_for!(struct hir::Pat {
@@ -592,7 +591,8 @@ impl_stable_hash_for!(enum hir::ExprKind {
592591
InlineAsm(asm, inputs, outputs),
593592
Struct(path, fields, base),
594593
Repeat(val, times),
595-
Yield(val)
594+
Yield(val),
595+
Err
596596
});
597597

598598
impl_stable_hash_for!(enum hir::LocalSource {

src/librustc/lint/levels.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,24 +222,22 @@ impl<'a> LintLevelsBuilder<'a> {
222222
match item.node {
223223
ast::MetaItemKind::Word => {} // actual lint names handled later
224224
ast::MetaItemKind::NameValue(ref name_value) => {
225-
let gate_reasons = !self.sess.features_untracked().lint_reasons;
226225
if item.ident == "reason" {
227226
// found reason, reslice meta list to exclude it
228227
metas = &metas[0..metas.len()-1];
229228
// FIXME (#55112): issue unused-attributes lint if we thereby
230229
// don't have any lint names (`#[level(reason = "foo")]`)
231230
if let ast::LitKind::Str(rationale, _) = name_value.node {
232-
if gate_reasons {
231+
if !self.sess.features_untracked().lint_reasons {
233232
feature_gate::emit_feature_err(
234233
&self.sess.parse_sess,
235234
"lint_reasons",
236235
item.span,
237236
feature_gate::GateIssue::Language,
238237
"lint reasons are experimental"
239238
);
240-
} else {
241-
reason = Some(rationale);
242239
}
240+
reason = Some(rationale);
243241
} else {
244242
let mut err = bad_attr(name_value.span);
245243
err.help("reason must be a string literal");

src/librustc/middle/expr_use_visitor.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,8 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
479479
}
480480

481481
hir::ExprKind::Continue(..) |
482-
hir::ExprKind::Lit(..) => {}
482+
hir::ExprKind::Lit(..) |
483+
hir::ExprKind::Err => {}
483484

484485
hir::ExprKind::Loop(ref blk, _, _) => {
485486
self.walk_block(&blk);

src/librustc/middle/liveness.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,7 @@ fn visit_expr<'a, 'tcx>(ir: &mut IrMaps<'a, 'tcx>, expr: &'tcx Expr) {
515515
hir::ExprKind::Box(..) |
516516
hir::ExprKind::Yield(..) |
517517
hir::ExprKind::Type(..) |
518+
hir::ExprKind::Err |
518519
hir::ExprKind::Path(hir::QPath::TypeRelative(..)) => {
519520
intravisit::walk_expr(ir, expr);
520521
}
@@ -1254,7 +1255,8 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
12541255
self.propagate_through_exprs(inputs, succ)
12551256
}
12561257

1257-
hir::ExprKind::Lit(..) | hir::ExprKind::Path(hir::QPath::TypeRelative(..)) => {
1258+
hir::ExprKind::Lit(..) | hir::ExprKind::Err |
1259+
hir::ExprKind::Path(hir::QPath::TypeRelative(..)) => {
12581260
succ
12591261
}
12601262

@@ -1521,7 +1523,7 @@ fn check_expr<'a, 'tcx>(this: &mut Liveness<'a, 'tcx>, expr: &'tcx Expr) {
15211523
hir::ExprKind::Block(..) | hir::ExprKind::AddrOf(..) |
15221524
hir::ExprKind::Struct(..) | hir::ExprKind::Repeat(..) |
15231525
hir::ExprKind::Closure(..) | hir::ExprKind::Path(_) | hir::ExprKind::Yield(..) |
1524-
hir::ExprKind::Box(..) | hir::ExprKind::Type(..) => {
1526+
hir::ExprKind::Box(..) | hir::ExprKind::Type(..) | hir::ExprKind::Err => {
15251527
intravisit::walk_expr(this, expr);
15261528
}
15271529
}

src/librustc/middle/mem_categorization.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
687687
hir::ExprKind::Block(..) | hir::ExprKind::Loop(..) | hir::ExprKind::Match(..) |
688688
hir::ExprKind::Lit(..) | hir::ExprKind::Break(..) |
689689
hir::ExprKind::Continue(..) | hir::ExprKind::Struct(..) | hir::ExprKind::Repeat(..) |
690-
hir::ExprKind::InlineAsm(..) | hir::ExprKind::Box(..) => {
690+
hir::ExprKind::InlineAsm(..) | hir::ExprKind::Box(..) | hir::ExprKind::Err => {
691691
Ok(self.cat_rvalue_node(expr.hir_id, expr.span, expr_ty))
692692
}
693693
}

0 commit comments

Comments
 (0)