Skip to content

Commit 4095722

Browse files
committed
Remove unreachable_code check from liveness
1 parent 7fcf850 commit 4095722

File tree

1 file changed

+12
-65
lines changed

1 file changed

+12
-65
lines changed

compiler/rustc_passes/src/liveness.rs

Lines changed: 12 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ use rustc_hir::intravisit::{self, Visitor};
9494
use rustc_hir::{Expr, HirId, HirIdMap, HirIdSet};
9595
use rustc_index::vec::IndexVec;
9696
use rustc_middle::ty::query::Providers;
97-
use rustc_middle::ty::{self, DefIdTree, RootVariableMinCaptureList, Ty, TyCtxt};
97+
use rustc_middle::ty::{self, DefIdTree, RootVariableMinCaptureList, TyCtxt};
9898
use rustc_session::lint;
9999
use rustc_span::symbol::{kw, sym, Symbol};
100100
use rustc_span::{BytePos, Span};
@@ -121,8 +121,8 @@ rustc_index::newtype_index! {
121121
#[derive(Copy, Clone, PartialEq, Debug)]
122122
enum LiveNodeKind {
123123
UpvarNode(Span),
124-
ExprNode(Span, HirId),
125-
VarDefNode(Span, HirId),
124+
ExprNode(Span),
125+
VarDefNode(Span),
126126
ClosureNode,
127127
ExitNode,
128128
}
@@ -131,8 +131,8 @@ fn live_node_kind_to_string(lnk: LiveNodeKind, tcx: TyCtxt<'_>) -> String {
131131
let sm = tcx.sess.source_map();
132132
match lnk {
133133
UpvarNode(s) => format!("Upvar node [{}]", sm.span_to_diagnostic_string(s)),
134-
ExprNode(s, _) => format!("Expr node [{}]", sm.span_to_diagnostic_string(s)),
135-
VarDefNode(s, _) => format!("Var def node [{}]", sm.span_to_diagnostic_string(s)),
134+
ExprNode(s) => format!("Expr node [{}]", sm.span_to_diagnostic_string(s)),
135+
VarDefNode(s) => format!("Var def node [{}]", sm.span_to_diagnostic_string(s)),
136136
ClosureNode => "Closure node".to_owned(),
137137
ExitNode => "Exit node".to_owned(),
138138
}
@@ -359,7 +359,7 @@ impl<'tcx> IrMaps<'tcx> {
359359
let shorthand_field_ids = self.collect_shorthand_field_ids(pat);
360360

361361
pat.each_binding(|_, hir_id, _, ident| {
362-
self.add_live_node_for_node(hir_id, VarDefNode(ident.span, hir_id));
362+
self.add_live_node_for_node(hir_id, VarDefNode(ident.span));
363363
self.add_variable(Local(LocalInfo {
364364
id: hir_id,
365365
name: ident.name,
@@ -373,7 +373,7 @@ impl<'tcx> Visitor<'tcx> for IrMaps<'tcx> {
373373
fn visit_local(&mut self, local: &'tcx hir::Local<'tcx>) {
374374
self.add_from_pat(&local.pat);
375375
if local.els.is_some() {
376-
self.add_live_node_for_node(local.hir_id, ExprNode(local.span, local.hir_id));
376+
self.add_live_node_for_node(local.hir_id, ExprNode(local.span));
377377
}
378378
intravisit::walk_local(self, local);
379379
}
@@ -408,14 +408,14 @@ impl<'tcx> Visitor<'tcx> for IrMaps<'tcx> {
408408
hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) => {
409409
debug!("expr {}: path that leads to {:?}", expr.hir_id, path.res);
410410
if let Res::Local(_var_hir_id) = path.res {
411-
self.add_live_node_for_node(expr.hir_id, ExprNode(expr.span, expr.hir_id));
411+
self.add_live_node_for_node(expr.hir_id, ExprNode(expr.span));
412412
}
413413
intravisit::walk_expr(self, expr);
414414
}
415415
hir::ExprKind::Closure { .. } => {
416416
// Interesting control flow (for loops can contain labeled
417417
// breaks or continues)
418-
self.add_live_node_for_node(expr.hir_id, ExprNode(expr.span, expr.hir_id));
418+
self.add_live_node_for_node(expr.hir_id, ExprNode(expr.span));
419419

420420
// Make a live_node for each mentioned variable, with the span
421421
// being the location that the variable is used. This results
@@ -444,11 +444,11 @@ impl<'tcx> Visitor<'tcx> for IrMaps<'tcx> {
444444
| hir::ExprKind::Match(..)
445445
| hir::ExprKind::Loop(..)
446446
| hir::ExprKind::Yield(..) => {
447-
self.add_live_node_for_node(expr.hir_id, ExprNode(expr.span, expr.hir_id));
447+
self.add_live_node_for_node(expr.hir_id, ExprNode(expr.span));
448448
intravisit::walk_expr(self, expr);
449449
}
450450
hir::ExprKind::Binary(op, ..) if op.node.is_lazy() => {
451-
self.add_live_node_for_node(expr.hir_id, ExprNode(expr.span, expr.hir_id));
451+
self.add_live_node_for_node(expr.hir_id, ExprNode(expr.span));
452452
intravisit::walk_expr(self, expr);
453453
}
454454

@@ -1284,60 +1284,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
12841284
fn check_is_ty_uninhabited(&mut self, expr: &Expr<'_>, succ: LiveNode) -> LiveNode {
12851285
let ty = self.typeck_results.expr_ty(expr);
12861286
let m = self.ir.tcx.parent_module(expr.hir_id).to_def_id();
1287-
if self.ir.tcx.is_ty_uninhabited_from(m, ty, self.param_env) {
1288-
match self.ir.lnks[succ] {
1289-
LiveNodeKind::ExprNode(succ_span, succ_id) => {
1290-
self.warn_about_unreachable(expr.span, ty, succ_span, succ_id, "expression");
1291-
}
1292-
LiveNodeKind::VarDefNode(succ_span, succ_id) => {
1293-
self.warn_about_unreachable(expr.span, ty, succ_span, succ_id, "definition");
1294-
}
1295-
_ => {}
1296-
};
1297-
self.exit_ln
1298-
} else {
1299-
succ
1300-
}
1301-
}
1302-
1303-
fn warn_about_unreachable(
1304-
&mut self,
1305-
orig_span: Span,
1306-
orig_ty: Ty<'tcx>,
1307-
expr_span: Span,
1308-
expr_id: HirId,
1309-
descr: &str,
1310-
) {
1311-
if !orig_ty.is_never() {
1312-
// Unreachable code warnings are already emitted during type checking.
1313-
// However, during type checking, full type information is being
1314-
// calculated but not yet available, so the check for diverging
1315-
// expressions due to uninhabited result types is pretty crude and
1316-
// only checks whether ty.is_never(). Here, we have full type
1317-
// information available and can issue warnings for less obviously
1318-
// uninhabited types (e.g. empty enums). The check above is used so
1319-
// that we do not emit the same warning twice if the uninhabited type
1320-
// is indeed `!`.
1321-
1322-
let msg = format!("unreachable {}", descr);
1323-
self.ir.tcx.struct_span_lint_hir(
1324-
lint::builtin::UNREACHABLE_CODE,
1325-
expr_id,
1326-
expr_span,
1327-
&msg,
1328-
|diag| {
1329-
diag.span_label(expr_span, &msg)
1330-
.span_label(orig_span, "any code following this expression is unreachable")
1331-
.span_note(
1332-
orig_span,
1333-
&format!(
1334-
"this expression has type `{}`, which is uninhabited",
1335-
orig_ty
1336-
),
1337-
)
1338-
},
1339-
);
1340-
}
1287+
if self.ir.tcx.is_ty_uninhabited_from(m, ty, self.param_env) { self.exit_ln } else { succ }
13411288
}
13421289
}
13431290

0 commit comments

Comments
 (0)