]> git.proxmox.com Git - rustc.git/blobdiff - src/librustc/middle/dead.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / librustc / middle / dead.rs
index 92372870cd16e0d2f8ae7b4df59b4f6ae3355eb7..dcdc02c9df6097d62f26c935da6f4552fd7ad586 100644 (file)
 // from live codes are live, and everything else is dead.
 
 use dep_graph::DepNode;
-use front::map as ast_map;
-use rustc_front::hir::{self, PatKind};
-use rustc_front::intravisit::{self, Visitor};
-
-use middle::{pat_util, privacy, ty};
-use middle::def::Def;
-use middle::def_id::{DefId};
+use hir::map as ast_map;
+use hir::{self, pat_util, PatKind};
+use hir::intravisit::{self, Visitor};
+
+use middle::privacy;
+use ty::{self, TyCtxt};
+use hir::def::Def;
+use hir::def_id::{DefId};
 use lint;
 
 use std::collections::HashSet;
@@ -30,7 +31,7 @@ use syntax::attr::{self, AttrMetaMethods};
 // explored. For example, if it's a live NodeItem that is a
 // function, then we should explore its block to check for codes that
 // may need to be marked as live.
-fn should_explore(tcx: &ty::ctxt, node_id: ast::NodeId) -> bool {
+fn should_explore(tcx: &TyCtxt, node_id: ast::NodeId) -> bool {
     match tcx.map.find(node_id) {
         Some(ast_map::NodeItem(..)) |
         Some(ast_map::NodeImplItem(..)) |
@@ -44,7 +45,7 @@ fn should_explore(tcx: &ty::ctxt, node_id: ast::NodeId) -> bool {
 
 struct MarkSymbolVisitor<'a, 'tcx: 'a> {
     worklist: Vec<ast::NodeId>,
-    tcx: &'a ty::ctxt<'tcx>,
+    tcx: &'a TyCtxt<'tcx>,
     live_symbols: Box<HashSet<ast::NodeId>>,
     struct_has_extern_repr: bool,
     ignore_non_const_paths: bool,
@@ -53,7 +54,7 @@ struct MarkSymbolVisitor<'a, 'tcx: 'a> {
 }
 
 impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
-    fn new(tcx: &'a ty::ctxt<'tcx>,
+    fn new(tcx: &'a TyCtxt<'tcx>,
            worklist: Vec<ast::NodeId>) -> MarkSymbolVisitor<'a, 'tcx> {
         MarkSymbolVisitor {
             worklist: worklist,
@@ -83,7 +84,7 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
     }
 
     fn lookup_and_handle_definition(&mut self, id: &ast::NodeId) {
-        use middle::ty::TypeVariants::{TyEnum, TyStruct};
+        use ty::TypeVariants::{TyEnum, TyStruct};
 
         // If `bar` is a trait item, make sure to mark Foo as alive in `Foo::bar`
         self.tcx.tables.borrow().item_substs.get(id)
@@ -124,7 +125,7 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
         if let ty::TyStruct(def, _) = self.tcx.expr_ty_adjusted(lhs).sty {
             self.insert_def_id(def.struct_variant().field_named(name).did);
         } else {
-            self.tcx.sess.span_bug(lhs.span, "named field access on non-struct")
+            span_bug!(lhs.span, "named field access on non-struct")
         }
     }
 
@@ -140,7 +141,7 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
         let pat_ty = self.tcx.node_id_to_type(lhs.id);
         let variant = match pat_ty.sty {
             ty::TyStruct(adt, _) | ty::TyEnum(adt, _) => adt.variant_of_def(def),
-            _ => self.tcx.sess.span_bug(lhs.span, "non-ADT in struct pattern")
+            _ => span_bug!(lhs.span, "non-ADT in struct pattern")
         };
         for pat in pats {
             if let PatKind::Wild = pat.node.pat.node {
@@ -221,12 +222,9 @@ impl<'a, 'tcx, 'v> Visitor<'v> for MarkSymbolVisitor<'a, 'tcx> {
         let has_extern_repr = self.struct_has_extern_repr;
         let inherited_pub_visibility = self.inherited_pub_visibility;
         let live_fields = def.fields().iter().filter(|f| {
-            has_extern_repr || inherited_pub_visibility || match f.node.kind {
-                hir::NamedField(_, hir::Public) => true,
-                _ => false
-            }
+            has_extern_repr || inherited_pub_visibility || f.vis == hir::Public
         });
-        self.live_symbols.extend(live_fields.map(|f| f.node.id));
+        self.live_symbols.extend(live_fields.map(|f| f.id));
 
         intravisit::walk_struct_def(self, def);
     }
@@ -353,15 +351,9 @@ impl<'v> Visitor<'v> for LifeSeeder {
             }
             hir::ItemImpl(_, _, _, ref opt_trait, _, ref impl_items) => {
                 for impl_item in impl_items {
-                    match impl_item.node {
-                        hir::ImplItemKind::Const(..) |
-                        hir::ImplItemKind::Method(..) => {
-                            if opt_trait.is_some() ||
-                                    has_allow_dead_code_or_lang_attr(&impl_item.attrs) {
-                                self.worklist.push(impl_item.id);
-                            }
-                        }
-                        hir::ImplItemKind::Type(_) => {}
+                    if opt_trait.is_some() ||
+                            has_allow_dead_code_or_lang_attr(&impl_item.attrs) {
+                        self.worklist.push(impl_item.id);
                     }
                 }
             }
@@ -370,7 +362,7 @@ impl<'v> Visitor<'v> for LifeSeeder {
     }
 }
 
-fn create_and_seed_worklist(tcx: &ty::ctxt,
+fn create_and_seed_worklist(tcx: &TyCtxt,
                             access_levels: &privacy::AccessLevels,
                             krate: &hir::Crate) -> Vec<ast::NodeId> {
     let mut worklist = Vec::new();
@@ -393,7 +385,7 @@ fn create_and_seed_worklist(tcx: &ty::ctxt,
     return life_seeder.worklist;
 }
 
-fn find_live(tcx: &ty::ctxt,
+fn find_live(tcx: &TyCtxt,
              access_levels: &privacy::AccessLevels,
              krate: &hir::Crate)
              -> Box<HashSet<ast::NodeId>> {
@@ -413,7 +405,7 @@ fn get_struct_ctor_id(item: &hir::Item) -> Option<ast::NodeId> {
 }
 
 struct DeadVisitor<'a, 'tcx: 'a> {
-    tcx: &'a ty::ctxt<'tcx>,
+    tcx: &'a TyCtxt<'tcx>,
     live_symbols: Box<HashSet<ast::NodeId>>,
 }
 
@@ -431,17 +423,16 @@ impl<'a, 'tcx> DeadVisitor<'a, 'tcx> {
         should_warn && !self.symbol_is_live(item.id, ctor_id)
     }
 
-    fn should_warn_about_field(&mut self, node: &hir::StructField_) -> bool {
-        let is_named = node.name().is_some();
-        let field_type = self.tcx.node_id_to_type(node.id);
+    fn should_warn_about_field(&mut self, field: &hir::StructField) -> bool {
+        let field_type = self.tcx.node_id_to_type(field.id);
         let is_marker_field = match field_type.ty_to_def_id() {
             Some(def_id) => self.tcx.lang_items.items().iter().any(|item| *item == Some(def_id)),
             _ => false
         };
-        is_named
-            && !self.symbol_is_live(node.id, None)
+        !field.is_positional()
+            && !self.symbol_is_live(field.id, None)
             && !is_marker_field
-            && !has_allow_dead_code_or_lang_attr(&node.attrs)
+            && !has_allow_dead_code_or_lang_attr(&field.attrs)
     }
 
     fn should_warn_about_variant(&mut self, variant: &hir::Variant_) -> bool {
@@ -547,9 +538,9 @@ impl<'a, 'tcx, 'v> Visitor<'v> for DeadVisitor<'a, 'tcx> {
     }
 
     fn visit_struct_field(&mut self, field: &hir::StructField) {
-        if self.should_warn_about_field(&field.node) {
-            self.warn_dead_code(field.node.id, field.span,
-                                field.node.name().unwrap(), "struct field");
+        if self.should_warn_about_field(&field) {
+            self.warn_dead_code(field.id, field.span,
+                                field.name, "struct field");
         }
 
         intravisit::walk_struct_field(self, field);
@@ -591,7 +582,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for DeadVisitor<'a, 'tcx> {
     }
 }
 
-pub fn check_crate(tcx: &ty::ctxt, access_levels: &privacy::AccessLevels) {
+pub fn check_crate(tcx: &TyCtxt, access_levels: &privacy::AccessLevels) {
     let _task = tcx.dep_graph.in_task(DepNode::DeadCheck);
     let krate = tcx.map.krate();
     let live_symbols = find_live(tcx, access_levels, krate);