2013-12-08 01:55:27 -06:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
// This implements the dead-code warning pass. It follows middle::reachable
|
|
|
|
// closely. The idea is that all reachable symbols are live, codes called
|
|
|
|
// from live codes are live, and everything else is dead.
|
|
|
|
|
2015-12-22 15:35:02 -06:00
|
|
|
use dep_graph::DepNode;
|
2016-03-29 00:50:44 -05:00
|
|
|
use hir::map as ast_map;
|
2016-03-29 04:54:26 -05:00
|
|
|
use hir::{self, pat_util, PatKind};
|
2016-03-29 00:50:44 -05:00
|
|
|
use hir::intravisit::{self, Visitor};
|
2015-07-31 02:04:06 -05:00
|
|
|
|
2016-03-29 04:54:26 -05:00
|
|
|
use middle::privacy;
|
2016-08-16 22:32:00 -05:00
|
|
|
use ty::{self, TyCtxt};
|
2016-03-29 04:54:26 -05:00
|
|
|
use hir::def::Def;
|
|
|
|
use hir::def_id::{DefId};
|
2014-09-20 06:26:10 -05:00
|
|
|
use lint;
|
2016-08-22 17:57:54 -05:00
|
|
|
use util::nodemap::FnvHashSet;
|
2013-12-08 01:55:27 -06:00
|
|
|
|
2015-06-09 18:40:45 -05:00
|
|
|
use syntax::{ast, codemap};
|
2016-04-12 08:58:55 -05:00
|
|
|
use syntax::attr;
|
2016-06-21 17:08:13 -05:00
|
|
|
use syntax_pos;
|
2013-12-08 01:55:27 -06:00
|
|
|
|
|
|
|
// Any local node that may call something in its body block should be
|
2014-01-09 07:05:33 -06:00
|
|
|
// explored. For example, if it's a live NodeItem that is a
|
2013-12-08 01:55:27 -06:00
|
|
|
// function, then we should explore its block to check for codes that
|
|
|
|
// may need to be marked as live.
|
2016-05-02 21:23:22 -05:00
|
|
|
fn should_explore<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
node_id: ast::NodeId) -> bool {
|
2015-09-04 12:52:28 -05:00
|
|
|
match tcx.map.find(node_id) {
|
|
|
|
Some(ast_map::NodeItem(..)) |
|
|
|
|
Some(ast_map::NodeImplItem(..)) |
|
|
|
|
Some(ast_map::NodeForeignItem(..)) |
|
|
|
|
Some(ast_map::NodeTraitItem(..)) =>
|
|
|
|
true,
|
|
|
|
_ =>
|
|
|
|
false
|
2013-12-08 01:55:27 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-22 07:56:37 -05:00
|
|
|
struct MarkSymbolVisitor<'a, 'tcx: 'a> {
|
2014-03-05 21:07:47 -06:00
|
|
|
worklist: Vec<ast::NodeId>,
|
2016-05-02 21:23:22 -05:00
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2016-08-22 17:57:54 -05:00
|
|
|
live_symbols: Box<FnvHashSet<ast::NodeId>>,
|
2014-09-20 06:26:10 -05:00
|
|
|
struct_has_extern_repr: bool,
|
2014-11-08 18:54:33 -06:00
|
|
|
ignore_non_const_paths: bool,
|
|
|
|
inherited_pub_visibility: bool,
|
2015-09-04 12:52:28 -05:00
|
|
|
ignore_variant_stack: Vec<DefId>,
|
2014-06-12 01:38:30 -05:00
|
|
|
}
|
|
|
|
|
2014-04-22 07:56:37 -05:00
|
|
|
impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
|
2016-05-02 21:23:22 -05:00
|
|
|
fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2014-04-22 07:56:37 -05:00
|
|
|
worklist: Vec<ast::NodeId>) -> MarkSymbolVisitor<'a, 'tcx> {
|
2013-12-08 01:55:27 -06:00
|
|
|
MarkSymbolVisitor {
|
|
|
|
worklist: worklist,
|
|
|
|
tcx: tcx,
|
2016-08-22 17:57:54 -05:00
|
|
|
live_symbols: box FnvHashSet(),
|
2014-09-20 06:26:10 -05:00
|
|
|
struct_has_extern_repr: false,
|
2014-11-08 18:54:33 -06:00
|
|
|
ignore_non_const_paths: false,
|
|
|
|
inherited_pub_visibility: false,
|
2015-06-02 06:31:40 -05:00
|
|
|
ignore_variant_stack: vec![],
|
2013-12-08 01:55:27 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-16 05:32:28 -05:00
|
|
|
fn check_def_id(&mut self, def_id: DefId) {
|
2015-09-04 12:52:28 -05:00
|
|
|
if let Some(node_id) = self.tcx.map.as_local_node_id(def_id) {
|
|
|
|
if should_explore(self.tcx, node_id) {
|
|
|
|
self.worklist.push(node_id);
|
|
|
|
}
|
|
|
|
self.live_symbols.insert(node_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn insert_def_id(&mut self, def_id: DefId) {
|
|
|
|
if let Some(node_id) = self.tcx.map.as_local_node_id(def_id) {
|
|
|
|
debug_assert!(!should_explore(self.tcx, node_id));
|
|
|
|
self.live_symbols.insert(node_id);
|
2013-12-31 15:19:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-03 15:15:00 -05:00
|
|
|
fn lookup_and_handle_definition(&mut self, id: ast::NodeId) {
|
2016-08-10 13:00:17 -05:00
|
|
|
use ty::TypeVariants::{TyEnum, TyStruct, TyUnion};
|
2015-08-11 17:15:00 -05:00
|
|
|
|
2016-08-08 15:39:49 -05:00
|
|
|
let def = self.tcx.expect_def(id);
|
|
|
|
|
2015-08-11 17:15:00 -05:00
|
|
|
// If `bar` is a trait item, make sure to mark Foo as alive in `Foo::bar`
|
2016-08-08 15:39:49 -05:00
|
|
|
match def {
|
|
|
|
Def::AssociatedTy(..) | Def::Method(_) | Def::AssociatedConst(_)
|
|
|
|
if self.tcx.trait_of_item(def.def_id()).is_some() => {
|
|
|
|
if let Some(substs) = self.tcx.tables.borrow().item_substs.get(&id) {
|
2016-08-18 00:32:50 -05:00
|
|
|
match substs.substs.type_at(0).sty {
|
2016-08-10 13:00:17 -05:00
|
|
|
TyEnum(tyid, _) | TyStruct(tyid, _) | TyUnion(tyid, _) => {
|
2016-08-08 15:39:49 -05:00
|
|
|
self.check_def_id(tyid.did)
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2015-08-11 17:15:00 -05:00
|
|
|
|
2016-06-03 15:15:00 -05:00
|
|
|
match def {
|
|
|
|
Def::Const(_) | Def::AssociatedConst(..) => {
|
|
|
|
self.check_def_id(def.def_id());
|
|
|
|
}
|
|
|
|
_ if self.ignore_non_const_paths => (),
|
|
|
|
Def::PrimTy(_) => (),
|
|
|
|
Def::SelfTy(..) => (),
|
|
|
|
Def::Variant(enum_id, variant_id) => {
|
|
|
|
self.check_def_id(enum_id);
|
|
|
|
if !self.ignore_variant_stack.contains(&variant_id) {
|
|
|
|
self.check_def_id(variant_id);
|
2014-09-20 06:26:10 -05:00
|
|
|
}
|
|
|
|
}
|
2016-06-03 15:15:00 -05:00
|
|
|
_ => {
|
|
|
|
self.check_def_id(def.def_id());
|
|
|
|
}
|
|
|
|
}
|
2013-12-31 15:19:57 -06:00
|
|
|
}
|
|
|
|
|
2015-07-03 23:07:10 -05:00
|
|
|
fn lookup_and_handle_method(&mut self, id: ast::NodeId) {
|
2014-11-25 13:21:20 -06:00
|
|
|
let method_call = ty::MethodCall::expr(id);
|
2015-07-03 23:07:10 -05:00
|
|
|
let method = self.tcx.tables.borrow().method_map[&method_call];
|
|
|
|
self.check_def_id(method.def_id);
|
2013-12-08 01:55:27 -06:00
|
|
|
}
|
|
|
|
|
2015-07-31 02:04:06 -05:00
|
|
|
fn handle_field_access(&mut self, lhs: &hir::Expr, name: ast::Name) {
|
2016-08-10 13:00:17 -05:00
|
|
|
match self.tcx.expr_ty_adjusted(lhs).sty {
|
|
|
|
ty::TyStruct(def, _) | ty::TyUnion(def, _) => {
|
|
|
|
self.insert_def_id(def.struct_variant().field_named(name).did);
|
|
|
|
}
|
2016-08-18 07:43:24 -05:00
|
|
|
_ => span_bug!(lhs.span, "named field access on non-struct/union"),
|
2014-06-05 17:00:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-31 02:04:06 -05:00
|
|
|
fn handle_tup_field_access(&mut self, lhs: &hir::Expr, idx: usize) {
|
2015-08-02 14:52:50 -05:00
|
|
|
if let ty::TyStruct(def, _) = self.tcx.expr_ty_adjusted(lhs).sty {
|
2015-09-04 12:52:28 -05:00
|
|
|
self.insert_def_id(def.struct_variant().fields[idx].did);
|
2014-08-09 22:54:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-31 02:04:06 -05:00
|
|
|
fn handle_field_pattern_match(&mut self, lhs: &hir::Pat,
|
|
|
|
pats: &[codemap::Spanned<hir::FieldPat>]) {
|
2016-06-03 15:15:00 -05:00
|
|
|
let variant = match self.tcx.node_id_to_type(lhs.id).sty {
|
2016-08-10 13:00:17 -05:00
|
|
|
ty::TyStruct(adt, _) | ty::TyUnion(adt, _) | ty::TyEnum(adt, _) => {
|
2016-06-03 15:15:00 -05:00
|
|
|
adt.variant_of_def(self.tcx.expect_def(lhs.id))
|
|
|
|
}
|
2016-03-25 12:31:27 -05:00
|
|
|
_ => span_bug!(lhs.span, "non-ADT in struct pattern")
|
2014-07-04 18:45:47 -05:00
|
|
|
};
|
2015-01-31 11:20:46 -06:00
|
|
|
for pat in pats {
|
2016-02-14 06:25:12 -06:00
|
|
|
if let PatKind::Wild = pat.node.pat.node {
|
2015-06-22 07:36:14 -05:00
|
|
|
continue;
|
|
|
|
}
|
2015-09-04 12:52:28 -05:00
|
|
|
self.insert_def_id(variant.field_named(pat.node.name).did);
|
2014-06-05 17:00:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-08 01:55:27 -06:00
|
|
|
fn mark_live_symbols(&mut self) {
|
2016-08-22 17:57:54 -05:00
|
|
|
let mut scanned = FnvHashSet();
|
2015-03-24 18:54:09 -05:00
|
|
|
while !self.worklist.is_empty() {
|
2013-12-23 09:20:52 -06:00
|
|
|
let id = self.worklist.pop().unwrap();
|
2013-12-08 01:55:27 -06:00
|
|
|
if scanned.contains(&id) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
scanned.insert(id);
|
2013-12-27 18:09:29 -06:00
|
|
|
|
2016-06-14 00:43:30 -05:00
|
|
|
if let Some(ref node) = self.tcx.map.find(id) {
|
|
|
|
self.live_symbols.insert(id);
|
|
|
|
self.visit_node(node);
|
2013-12-08 01:55:27 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn visit_node(&mut self, node: &ast_map::Node) {
|
2014-09-12 05:10:30 -05:00
|
|
|
let had_extern_repr = self.struct_has_extern_repr;
|
|
|
|
self.struct_has_extern_repr = false;
|
2014-11-08 18:54:33 -06:00
|
|
|
let had_inherited_pub_visibility = self.inherited_pub_visibility;
|
|
|
|
self.inherited_pub_visibility = false;
|
2013-12-08 01:55:27 -06:00
|
|
|
match *node {
|
2014-02-13 23:07:09 -06:00
|
|
|
ast_map::NodeItem(item) => {
|
2013-12-08 01:55:27 -06:00
|
|
|
match item.node {
|
2016-08-10 13:00:17 -05:00
|
|
|
hir::ItemStruct(..) | hir::ItemUnion(..) => {
|
2014-09-12 05:10:30 -05:00
|
|
|
self.struct_has_extern_repr = item.attrs.iter().any(|attr| {
|
|
|
|
attr::find_repr_attrs(self.tcx.sess.diagnostic(), attr)
|
|
|
|
.contains(&attr::ReprExtern)
|
2014-05-27 01:56:52 -05:00
|
|
|
});
|
2014-06-12 01:38:30 -05:00
|
|
|
|
2016-02-09 15:00:20 -06:00
|
|
|
intravisit::walk_item(self, &item);
|
2014-06-05 17:00:29 -05:00
|
|
|
}
|
2015-07-31 02:04:06 -05:00
|
|
|
hir::ItemEnum(..) => {
|
|
|
|
self.inherited_pub_visibility = item.vis == hir::Public;
|
2016-02-09 15:00:20 -06:00
|
|
|
intravisit::walk_item(self, &item);
|
2014-11-08 18:54:33 -06:00
|
|
|
}
|
2015-07-31 02:04:06 -05:00
|
|
|
hir::ItemFn(..)
|
|
|
|
| hir::ItemTy(..)
|
|
|
|
| hir::ItemStatic(..)
|
|
|
|
| hir::ItemConst(..) => {
|
2016-02-09 15:00:20 -06:00
|
|
|
intravisit::walk_item(self, &item);
|
2013-12-08 01:55:27 -06:00
|
|
|
}
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
2015-03-10 05:28:44 -05:00
|
|
|
ast_map::NodeTraitItem(trait_item) => {
|
2015-11-17 17:56:13 -06:00
|
|
|
intravisit::walk_trait_item(self, trait_item);
|
2013-12-08 01:55:27 -06:00
|
|
|
}
|
2014-08-04 15:56:56 -05:00
|
|
|
ast_map::NodeImplItem(impl_item) => {
|
2015-11-17 17:56:13 -06:00
|
|
|
intravisit::walk_impl_item(self, impl_item);
|
2013-12-08 01:55:27 -06:00
|
|
|
}
|
2014-02-13 23:07:09 -06:00
|
|
|
ast_map::NodeForeignItem(foreign_item) => {
|
2016-02-09 15:00:20 -06:00
|
|
|
intravisit::walk_foreign_item(self, &foreign_item);
|
2013-12-13 23:08:26 -06:00
|
|
|
}
|
2013-12-08 01:55:27 -06:00
|
|
|
_ => ()
|
|
|
|
}
|
2014-09-12 05:10:30 -05:00
|
|
|
self.struct_has_extern_repr = had_extern_repr;
|
2014-11-08 18:54:33 -06:00
|
|
|
self.inherited_pub_visibility = had_inherited_pub_visibility;
|
2013-12-08 01:55:27 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-09 17:54:36 -05:00
|
|
|
impl<'a, 'tcx, 'v> Visitor<'v> for MarkSymbolVisitor<'a, 'tcx> {
|
2014-06-12 01:38:30 -05:00
|
|
|
|
2015-10-07 19:20:57 -05:00
|
|
|
fn visit_variant_data(&mut self, def: &hir::VariantData, _: ast::Name,
|
2016-06-21 17:08:13 -05:00
|
|
|
_: &hir::Generics, _: ast::NodeId, _: syntax_pos::Span) {
|
2014-09-12 05:10:30 -05:00
|
|
|
let has_extern_repr = self.struct_has_extern_repr;
|
2014-11-08 18:54:33 -06:00
|
|
|
let inherited_pub_visibility = self.inherited_pub_visibility;
|
2015-10-25 10:33:51 -05:00
|
|
|
let live_fields = def.fields().iter().filter(|f| {
|
2016-02-27 02:34:29 -06:00
|
|
|
has_extern_repr || inherited_pub_visibility || f.vis == hir::Public
|
2014-06-12 01:38:30 -05:00
|
|
|
});
|
2016-02-27 02:34:29 -06:00
|
|
|
self.live_symbols.extend(live_fields.map(|f| f.id));
|
2014-06-12 01:38:30 -05:00
|
|
|
|
2015-11-17 17:56:13 -06:00
|
|
|
intravisit::walk_struct_def(self, def);
|
2014-06-12 01:38:30 -05:00
|
|
|
}
|
2013-12-08 01:55:27 -06:00
|
|
|
|
2015-07-31 02:04:06 -05:00
|
|
|
fn visit_expr(&mut self, expr: &hir::Expr) {
|
2013-12-08 01:55:27 -06:00
|
|
|
match expr.node {
|
2015-07-31 02:04:06 -05:00
|
|
|
hir::ExprMethodCall(..) => {
|
2015-07-03 23:07:10 -05:00
|
|
|
self.lookup_and_handle_method(expr.id);
|
2013-12-08 01:55:27 -06:00
|
|
|
}
|
2015-09-20 06:00:18 -05:00
|
|
|
hir::ExprField(ref lhs, ref name) => {
|
2016-02-09 15:00:20 -06:00
|
|
|
self.handle_field_access(&lhs, name.node);
|
2014-06-05 17:00:29 -05:00
|
|
|
}
|
2015-07-31 02:04:06 -05:00
|
|
|
hir::ExprTupField(ref lhs, idx) => {
|
2016-02-09 15:00:20 -06:00
|
|
|
self.handle_tup_field_access(&lhs, idx.node);
|
2014-08-09 22:54:33 -05:00
|
|
|
}
|
2013-12-08 01:55:27 -06:00
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
|
2015-11-17 17:56:13 -06:00
|
|
|
intravisit::walk_expr(self, expr);
|
2013-12-08 01:55:27 -06:00
|
|
|
}
|
|
|
|
|
2015-07-31 02:04:06 -05:00
|
|
|
fn visit_arm(&mut self, arm: &hir::Arm) {
|
2015-06-02 06:31:40 -05:00
|
|
|
if arm.pats.len() == 1 {
|
|
|
|
let pat = &*arm.pats[0];
|
2015-11-04 06:26:00 -06:00
|
|
|
let variants = pat_util::necessary_variants(&self.tcx.def_map.borrow(), pat);
|
2015-06-02 06:31:40 -05:00
|
|
|
|
|
|
|
// Inside the body, ignore constructions of variants
|
|
|
|
// necessary for the pattern to match. Those construction sites
|
|
|
|
// can't be reached unless the variant is constructed elsewhere.
|
|
|
|
let len = self.ignore_variant_stack.len();
|
2016-02-09 15:00:20 -06:00
|
|
|
self.ignore_variant_stack.extend_from_slice(&variants);
|
2015-11-17 17:56:13 -06:00
|
|
|
intravisit::walk_arm(self, arm);
|
2015-06-02 06:31:40 -05:00
|
|
|
self.ignore_variant_stack.truncate(len);
|
|
|
|
} else {
|
2015-11-17 17:56:13 -06:00
|
|
|
intravisit::walk_arm(self, arm);
|
2015-06-02 06:31:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-31 02:04:06 -05:00
|
|
|
fn visit_pat(&mut self, pat: &hir::Pat) {
|
2014-09-20 06:26:10 -05:00
|
|
|
let def_map = &self.tcx.def_map;
|
2014-06-05 17:00:29 -05:00
|
|
|
match pat.node {
|
2016-02-14 06:25:12 -06:00
|
|
|
PatKind::Struct(_, ref fields, _) => {
|
2015-02-01 20:53:25 -06:00
|
|
|
self.handle_field_pattern_match(pat, fields);
|
2014-06-05 17:00:29 -05:00
|
|
|
}
|
2015-11-04 06:26:00 -06:00
|
|
|
_ if pat_util::pat_is_const(&def_map.borrow(), pat) => {
|
2014-10-30 18:19:01 -05:00
|
|
|
// it might be the only use of a const
|
2016-06-03 15:15:00 -05:00
|
|
|
self.lookup_and_handle_definition(pat.id)
|
2014-06-30 20:02:14 -05:00
|
|
|
}
|
2014-06-05 17:00:29 -05:00
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
|
2014-10-30 18:19:01 -05:00
|
|
|
self.ignore_non_const_paths = true;
|
2015-11-17 17:56:13 -06:00
|
|
|
intravisit::walk_pat(self, pat);
|
2014-10-30 18:19:01 -05:00
|
|
|
self.ignore_non_const_paths = false;
|
2014-06-05 17:00:29 -05:00
|
|
|
}
|
|
|
|
|
2015-07-31 02:04:06 -05:00
|
|
|
fn visit_path(&mut self, path: &hir::Path, id: ast::NodeId) {
|
2016-06-03 15:15:00 -05:00
|
|
|
self.lookup_and_handle_definition(id);
|
2015-11-17 17:56:13 -06:00
|
|
|
intravisit::walk_path(self, path);
|
2013-12-11 19:49:46 -06:00
|
|
|
}
|
|
|
|
|
2015-09-12 08:10:12 -05:00
|
|
|
fn visit_path_list_item(&mut self, path: &hir::Path, item: &hir::PathListItem) {
|
2016-08-12 04:15:02 -05:00
|
|
|
self.lookup_and_handle_definition(item.node.id);
|
2015-11-17 17:56:13 -06:00
|
|
|
intravisit::walk_path_list_item(self, path, item);
|
2013-12-08 01:55:27 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-14 04:58:20 -05:00
|
|
|
fn has_allow_dead_code_or_lang_attr(attrs: &[ast::Attribute]) -> bool {
|
2015-02-01 20:53:25 -06:00
|
|
|
if attr::contains_name(attrs, "lang") {
|
2014-06-04 16:35:58 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-13 17:03:26 -05:00
|
|
|
let dead_code = lint::builtin::DEAD_CODE.name_lower();
|
2015-09-14 04:58:20 -05:00
|
|
|
for attr in lint::gather_attrs(attrs) {
|
2014-06-13 17:03:26 -05:00
|
|
|
match attr {
|
|
|
|
Ok((ref name, lint::Allow, _))
|
2015-02-18 13:48:57 -06:00
|
|
|
if &name[..] == dead_code => return true,
|
2014-06-04 16:35:58 -05:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
2014-01-11 02:21:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// This visitor seeds items that
|
|
|
|
// 1) We want to explicitly consider as live:
|
|
|
|
// * Item annotated with #[allow(dead_code)]
|
|
|
|
// - This is done so that if we want to suppress warnings for a
|
|
|
|
// group of dead functions, we only have to annotate the "root".
|
|
|
|
// For example, if both `f` and `g` are dead and `f` calls `g`,
|
|
|
|
// then annotating `f` with `#[allow(dead_code)]` will suppress
|
|
|
|
// warning for both `f` and `g`.
|
|
|
|
// * Item annotated with #[lang=".."]
|
|
|
|
// - This is because lang items are always callable from elsewhere.
|
|
|
|
// or
|
|
|
|
// 2) We are not sure to be live or not
|
|
|
|
// * Implementation of a trait method
|
|
|
|
struct LifeSeeder {
|
2014-09-20 06:26:10 -05:00
|
|
|
worklist: Vec<ast::NodeId>
|
2013-12-08 01:55:27 -06:00
|
|
|
}
|
|
|
|
|
2014-09-09 17:54:36 -05:00
|
|
|
impl<'v> Visitor<'v> for LifeSeeder {
|
2015-07-31 02:04:06 -05:00
|
|
|
fn visit_item(&mut self, item: &hir::Item) {
|
2015-02-01 20:53:25 -06:00
|
|
|
let allow_dead_code = has_allow_dead_code_or_lang_attr(&item.attrs);
|
2014-09-20 06:26:10 -05:00
|
|
|
if allow_dead_code {
|
2014-01-11 02:21:53 -06:00
|
|
|
self.worklist.push(item.id);
|
|
|
|
}
|
2013-12-08 01:55:27 -06:00
|
|
|
match item.node {
|
2015-07-31 02:04:06 -05:00
|
|
|
hir::ItemEnum(ref enum_def, _) if allow_dead_code => {
|
2015-10-09 19:28:40 -05:00
|
|
|
self.worklist.extend(enum_def.variants.iter()
|
|
|
|
.map(|variant| variant.node.data.id()));
|
2014-09-20 06:26:10 -05:00
|
|
|
}
|
2016-08-26 11:23:42 -05:00
|
|
|
hir::ItemTrait(.., ref trait_items) => {
|
2015-03-10 05:28:44 -05:00
|
|
|
for trait_item in trait_items {
|
|
|
|
match trait_item.node {
|
2015-07-31 02:04:06 -05:00
|
|
|
hir::ConstTraitItem(_, Some(_)) |
|
|
|
|
hir::MethodTraitItem(_, Some(_)) => {
|
2015-03-10 05:28:44 -05:00
|
|
|
if has_allow_dead_code_or_lang_attr(&trait_item.attrs) {
|
|
|
|
self.worklist.push(trait_item.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-26 11:23:42 -05:00
|
|
|
hir::ItemImpl(.., ref opt_trait, _, ref impl_items) => {
|
2015-01-31 11:20:46 -06:00
|
|
|
for impl_item in impl_items {
|
2016-03-09 12:12:24 -06:00
|
|
|
if opt_trait.is_some() ||
|
|
|
|
has_allow_dead_code_or_lang_attr(&impl_item.attrs) {
|
|
|
|
self.worklist.push(impl_item.id);
|
2014-08-04 15:56:56 -05:00
|
|
|
}
|
2013-12-08 01:55:27 -06:00
|
|
|
}
|
|
|
|
}
|
2014-01-11 02:21:53 -06:00
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
2013-12-08 01:55:27 -06:00
|
|
|
}
|
|
|
|
|
2016-05-02 21:23:22 -05:00
|
|
|
fn create_and_seed_worklist<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
access_levels: &privacy::AccessLevels,
|
|
|
|
krate: &hir::Crate)
|
|
|
|
-> Vec<ast::NodeId> {
|
2014-03-04 12:02:49 -06:00
|
|
|
let mut worklist = Vec::new();
|
2015-11-19 05:16:35 -06:00
|
|
|
for (id, _) in &access_levels.map {
|
2014-07-21 22:54:28 -05:00
|
|
|
worklist.push(*id);
|
2013-12-08 01:55:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Seed entry point
|
2016-06-14 00:43:30 -05:00
|
|
|
if let Some((id, _)) = *tcx.sess.entry_fn.borrow() {
|
|
|
|
worklist.push(id);
|
2013-12-08 01:55:27 -06:00
|
|
|
}
|
|
|
|
|
2015-03-22 20:24:56 -05:00
|
|
|
// Seed implemented trait items
|
2014-01-11 02:21:53 -06:00
|
|
|
let mut life_seeder = LifeSeeder {
|
2013-12-08 01:55:27 -06:00
|
|
|
worklist: worklist
|
|
|
|
};
|
2015-11-17 17:56:13 -06:00
|
|
|
krate.visit_all_items(&mut life_seeder);
|
2013-12-08 01:55:27 -06:00
|
|
|
|
2014-01-11 02:21:53 -06:00
|
|
|
return life_seeder.worklist;
|
2013-12-08 01:55:27 -06:00
|
|
|
}
|
|
|
|
|
2016-05-02 21:23:22 -05:00
|
|
|
fn find_live<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
access_levels: &privacy::AccessLevels,
|
|
|
|
krate: &hir::Crate)
|
2016-08-22 17:57:54 -05:00
|
|
|
-> Box<FnvHashSet<ast::NodeId>> {
|
2015-11-19 05:16:35 -06:00
|
|
|
let worklist = create_and_seed_worklist(tcx, access_levels, krate);
|
2014-04-09 10:18:40 -05:00
|
|
|
let mut symbol_visitor = MarkSymbolVisitor::new(tcx, worklist);
|
2013-12-08 01:55:27 -06:00
|
|
|
symbol_visitor.mark_live_symbols();
|
|
|
|
symbol_visitor.live_symbols
|
|
|
|
}
|
|
|
|
|
2015-07-31 02:04:06 -05:00
|
|
|
fn get_struct_ctor_id(item: &hir::Item) -> Option<ast::NodeId> {
|
2013-12-08 01:55:27 -06:00
|
|
|
match item.node {
|
2015-10-08 15:45:46 -05:00
|
|
|
hir::ItemStruct(ref struct_def, _) if !struct_def.is_struct() => {
|
2015-10-09 19:28:40 -05:00
|
|
|
Some(struct_def.id())
|
2015-10-01 19:53:28 -05:00
|
|
|
}
|
2013-12-08 01:55:27 -06:00
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-22 07:56:37 -05:00
|
|
|
struct DeadVisitor<'a, 'tcx: 'a> {
|
2016-05-02 21:23:22 -05:00
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2016-08-22 17:57:54 -05:00
|
|
|
live_symbols: Box<FnvHashSet<ast::NodeId>>,
|
2013-12-08 01:55:27 -06:00
|
|
|
}
|
|
|
|
|
2014-04-22 07:56:37 -05:00
|
|
|
impl<'a, 'tcx> DeadVisitor<'a, 'tcx> {
|
2015-07-31 02:04:06 -05:00
|
|
|
fn should_warn_about_item(&mut self, item: &hir::Item) -> bool {
|
2014-09-20 06:26:10 -05:00
|
|
|
let should_warn = match item.node {
|
2015-07-31 02:04:06 -05:00
|
|
|
hir::ItemStatic(..)
|
|
|
|
| hir::ItemConst(..)
|
|
|
|
| hir::ItemFn(..)
|
|
|
|
| hir::ItemEnum(..)
|
2016-08-10 13:00:17 -05:00
|
|
|
| hir::ItemStruct(..)
|
|
|
|
| hir::ItemUnion(..) => true,
|
2014-09-20 06:26:10 -05:00
|
|
|
_ => false
|
|
|
|
};
|
|
|
|
let ctor_id = get_struct_ctor_id(item);
|
|
|
|
should_warn && !self.symbol_is_live(item.id, ctor_id)
|
|
|
|
}
|
|
|
|
|
2016-02-27 02:34:29 -06:00
|
|
|
fn should_warn_about_field(&mut self, field: &hir::StructField) -> bool {
|
|
|
|
let field_type = self.tcx.node_id_to_type(field.id);
|
2015-06-25 15:42:17 -05:00
|
|
|
let is_marker_field = match field_type.ty_to_def_id() {
|
2016-01-28 15:59:00 -06:00
|
|
|
Some(def_id) => self.tcx.lang_items.items().iter().any(|item| *item == Some(def_id)),
|
2014-06-05 17:00:29 -05:00
|
|
|
_ => false
|
|
|
|
};
|
2016-02-27 02:34:29 -06:00
|
|
|
!field.is_positional()
|
|
|
|
&& !self.symbol_is_live(field.id, None)
|
2014-06-05 17:00:29 -05:00
|
|
|
&& !is_marker_field
|
2016-02-27 02:34:29 -06:00
|
|
|
&& !has_allow_dead_code_or_lang_attr(&field.attrs)
|
2014-06-05 17:00:29 -05:00
|
|
|
}
|
|
|
|
|
2015-07-31 02:04:06 -05:00
|
|
|
fn should_warn_about_variant(&mut self, variant: &hir::Variant_) -> bool {
|
2015-10-09 19:28:40 -05:00
|
|
|
!self.symbol_is_live(variant.data.id(), None)
|
2015-02-01 20:53:25 -06:00
|
|
|
&& !has_allow_dead_code_or_lang_attr(&variant.attrs)
|
2014-09-20 06:26:10 -05:00
|
|
|
}
|
|
|
|
|
2013-12-08 01:55:27 -06:00
|
|
|
// id := node id of an item's definition.
|
|
|
|
// ctor_id := `Some` if the item is a struct_ctor (tuple struct),
|
|
|
|
// `None` otherwise.
|
|
|
|
// If the item is a struct_ctor, then either its `id` or
|
|
|
|
// `ctor_id` (unwrapped) is in the live_symbols set. More specifically,
|
|
|
|
// DefMap maps the ExprPath of a struct_ctor to the node referred by
|
|
|
|
// `ctor_id`. On the other hand, in a statement like
|
|
|
|
// `type <ident> <generics> = <ty>;` where <ty> refers to a struct_ctor,
|
|
|
|
// DefMap maps <ty> to `id` instead.
|
2015-09-04 12:52:28 -05:00
|
|
|
fn symbol_is_live(&mut self,
|
|
|
|
id: ast::NodeId,
|
|
|
|
ctor_id: Option<ast::NodeId>)
|
|
|
|
-> bool {
|
2013-12-08 01:55:27 -06:00
|
|
|
if self.live_symbols.contains(&id)
|
2013-12-06 12:51:10 -06:00
|
|
|
|| ctor_id.map_or(false,
|
|
|
|
|ctor| self.live_symbols.contains(&ctor)) {
|
2013-12-08 01:55:27 -06:00
|
|
|
return true;
|
|
|
|
}
|
2015-03-22 20:24:56 -05:00
|
|
|
// If it's a type whose items are live, then it's live, too.
|
2013-12-08 01:55:27 -06:00
|
|
|
// This is done to handle the case where, for example, the static
|
|
|
|
// method of a private type is used, but the type itself is never
|
|
|
|
// called directly.
|
2014-08-04 15:56:56 -05:00
|
|
|
let impl_items = self.tcx.impl_items.borrow();
|
2016-06-14 00:43:30 -05:00
|
|
|
if let Some(impl_list) =
|
|
|
|
self.tcx.inherent_impls.borrow().get(&self.tcx.map.local_def_id(id)) {
|
|
|
|
for impl_did in impl_list.iter() {
|
|
|
|
for item_did in impl_items.get(impl_did).unwrap().iter() {
|
|
|
|
if let Some(item_node_id) =
|
|
|
|
self.tcx.map.as_local_node_id(item_did.def_id()) {
|
|
|
|
if self.live_symbols.contains(&item_node_id) {
|
|
|
|
return true;
|
2013-12-08 01:55:27 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
2013-12-13 23:08:26 -06:00
|
|
|
|
2014-05-09 20:45:36 -05:00
|
|
|
fn warn_dead_code(&mut self,
|
|
|
|
id: ast::NodeId,
|
2016-06-21 17:08:13 -05:00
|
|
|
span: syntax_pos::Span,
|
2015-03-23 19:41:35 -05:00
|
|
|
name: ast::Name,
|
2014-09-20 07:08:10 -05:00
|
|
|
node_type: &str) {
|
2015-03-23 19:41:35 -05:00
|
|
|
let name = name.as_str();
|
2014-08-03 01:40:06 -05:00
|
|
|
if !name.starts_with("_") {
|
|
|
|
self.tcx
|
|
|
|
.sess
|
|
|
|
.add_lint(lint::builtin::DEAD_CODE,
|
|
|
|
id,
|
|
|
|
span,
|
2014-09-20 07:08:10 -05:00
|
|
|
format!("{} is never used: `{}`", node_type, name));
|
2014-08-03 01:40:06 -05:00
|
|
|
}
|
2013-12-13 23:08:26 -06:00
|
|
|
}
|
2013-12-08 01:55:27 -06:00
|
|
|
}
|
|
|
|
|
2014-09-09 17:54:36 -05:00
|
|
|
impl<'a, 'tcx, 'v> Visitor<'v> for DeadVisitor<'a, 'tcx> {
|
2015-11-17 17:56:13 -06:00
|
|
|
/// Walk nested items in place so that we don't report dead-code
|
|
|
|
/// on inner functions when the outer function is already getting
|
|
|
|
/// an error. We could do this also by checking the parents, but
|
|
|
|
/// this is how the code is setup and it seems harmless enough.
|
|
|
|
fn visit_nested_item(&mut self, item: hir::ItemId) {
|
2016-05-02 20:56:42 -05:00
|
|
|
let tcx = self.tcx;
|
|
|
|
self.visit_item(tcx.map.expect_item(item.id))
|
2015-11-17 17:56:13 -06:00
|
|
|
}
|
|
|
|
|
2015-07-31 02:04:06 -05:00
|
|
|
fn visit_item(&mut self, item: &hir::Item) {
|
2014-09-20 06:26:10 -05:00
|
|
|
if self.should_warn_about_item(item) {
|
2015-03-23 19:41:35 -05:00
|
|
|
self.warn_dead_code(
|
|
|
|
item.id,
|
|
|
|
item.span,
|
2015-09-19 20:50:30 -05:00
|
|
|
item.name,
|
2015-03-23 19:41:35 -05:00
|
|
|
item.node.descriptive_variant()
|
|
|
|
);
|
2014-09-20 06:26:10 -05:00
|
|
|
} else {
|
2015-11-07 16:07:13 -06:00
|
|
|
// Only continue if we didn't warn
|
2015-11-17 17:56:13 -06:00
|
|
|
intravisit::walk_item(self, item);
|
2015-11-07 16:07:13 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_variant(&mut self, variant: &hir::Variant, g: &hir::Generics, id: ast::NodeId) {
|
|
|
|
if self.should_warn_about_variant(&variant.node) {
|
|
|
|
self.warn_dead_code(variant.node.data.id(), variant.span,
|
|
|
|
variant.node.name, "variant");
|
|
|
|
} else {
|
2015-11-17 17:56:13 -06:00
|
|
|
intravisit::walk_variant(self, variant, g, id);
|
2013-12-08 01:55:27 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-31 02:04:06 -05:00
|
|
|
fn visit_foreign_item(&mut self, fi: &hir::ForeignItem) {
|
2013-12-13 23:08:26 -06:00
|
|
|
if !self.symbol_is_live(fi.id, None) {
|
2015-09-19 20:50:30 -05:00
|
|
|
self.warn_dead_code(fi.id, fi.span, fi.name, fi.node.descriptive_variant());
|
2013-12-13 23:08:26 -06:00
|
|
|
}
|
2015-11-17 17:56:13 -06:00
|
|
|
intravisit::walk_foreign_item(self, fi);
|
2013-12-13 23:08:26 -06:00
|
|
|
}
|
|
|
|
|
2015-07-31 02:04:06 -05:00
|
|
|
fn visit_struct_field(&mut self, field: &hir::StructField) {
|
2016-02-27 02:34:29 -06:00
|
|
|
if self.should_warn_about_field(&field) {
|
|
|
|
self.warn_dead_code(field.id, field.span,
|
2016-09-03 17:29:16 -05:00
|
|
|
field.name, "field");
|
2014-06-05 17:00:29 -05:00
|
|
|
}
|
|
|
|
|
2015-11-17 17:56:13 -06:00
|
|
|
intravisit::walk_struct_field(self, field);
|
2014-06-05 17:00:29 -05:00
|
|
|
}
|
|
|
|
|
2015-07-31 02:04:06 -05:00
|
|
|
fn visit_impl_item(&mut self, impl_item: &hir::ImplItem) {
|
2015-03-22 20:24:56 -05:00
|
|
|
match impl_item.node {
|
2015-11-12 08:57:51 -06:00
|
|
|
hir::ImplItemKind::Const(_, ref expr) => {
|
2015-03-22 20:24:56 -05:00
|
|
|
if !self.symbol_is_live(impl_item.id, None) {
|
|
|
|
self.warn_dead_code(impl_item.id, impl_item.span,
|
2015-09-19 20:50:30 -05:00
|
|
|
impl_item.name, "associated const");
|
2015-03-22 20:24:56 -05:00
|
|
|
}
|
2015-11-17 17:56:13 -06:00
|
|
|
intravisit::walk_expr(self, expr)
|
2015-03-22 20:24:56 -05:00
|
|
|
}
|
2015-11-12 08:57:51 -06:00
|
|
|
hir::ImplItemKind::Method(_, ref body) => {
|
2015-03-22 20:24:56 -05:00
|
|
|
if !self.symbol_is_live(impl_item.id, None) {
|
|
|
|
self.warn_dead_code(impl_item.id, impl_item.span,
|
2015-09-19 20:50:30 -05:00
|
|
|
impl_item.name, "method");
|
2015-03-22 20:24:56 -05:00
|
|
|
}
|
2015-11-17 17:56:13 -06:00
|
|
|
intravisit::walk_block(self, body)
|
2015-03-22 20:24:56 -05:00
|
|
|
}
|
2015-11-12 08:57:51 -06:00
|
|
|
hir::ImplItemKind::Type(..) => {}
|
2015-03-22 20:24:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Overwrite so that we don't warn the trait item itself.
|
2015-07-31 02:04:06 -05:00
|
|
|
fn visit_trait_item(&mut self, trait_item: &hir::TraitItem) {
|
2015-03-22 20:24:56 -05:00
|
|
|
match trait_item.node {
|
2015-07-31 02:04:06 -05:00
|
|
|
hir::ConstTraitItem(_, Some(ref expr)) => {
|
2015-11-17 17:56:13 -06:00
|
|
|
intravisit::walk_expr(self, expr)
|
2015-03-22 20:24:56 -05:00
|
|
|
}
|
2015-07-31 02:04:06 -05:00
|
|
|
hir::MethodTraitItem(_, Some(ref body)) => {
|
2015-11-17 17:56:13 -06:00
|
|
|
intravisit::walk_block(self, body)
|
2014-07-07 18:35:15 -05:00
|
|
|
}
|
2015-07-31 02:04:06 -05:00
|
|
|
hir::ConstTraitItem(_, None) |
|
|
|
|
hir::MethodTraitItem(_, None) |
|
|
|
|
hir::TypeTraitItem(..) => {}
|
2013-12-08 01:55:27 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-02 21:23:22 -05:00
|
|
|
pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
access_levels: &privacy::AccessLevels) {
|
2015-12-22 15:35:02 -06:00
|
|
|
let _task = tcx.dep_graph.in_task(DepNode::DeadCheck);
|
2014-09-07 12:09:06 -05:00
|
|
|
let krate = tcx.map.krate();
|
2015-11-19 05:16:35 -06:00
|
|
|
let live_symbols = find_live(tcx, access_levels, krate);
|
2013-12-08 01:55:27 -06:00
|
|
|
let mut visitor = DeadVisitor { tcx: tcx, live_symbols: live_symbols };
|
2015-11-17 17:56:13 -06:00
|
|
|
intravisit::walk_crate(&mut visitor, krate);
|
2013-12-08 01:55:27 -06:00
|
|
|
}
|