Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn unused type aliases #37631

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/librustc/hir/def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ impl PathResolution {
}

// Definition mapping
pub type DefMap = NodeMap<PathResolution>;
pub type DefMap = NodeMap<Def>;
pub type AssocMap = NodeMap<PathResolution>;
// This is the replacement export map. It maps a module to all of the exports
// within.
pub type ExportMap = NodeMap<Vec<Export>>;
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use hir;
use hir::map::Definitions;
use hir::map::definitions::DefPathData;
use hir::def_id::{DefIndex, DefId};
use hir::def::{Def, PathResolution};
use hir::def::Def;
use session::Session;

use std::collections::BTreeMap;
Expand Down Expand Up @@ -74,7 +74,7 @@ pub trait Resolver {
fn resolve_generated_global_path(&mut self, path: &hir::Path, is_value: bool) -> Def;

// Obtain the resolution for a node id
fn get_resolution(&mut self, id: NodeId) -> Option<PathResolution>;
fn get_resolution(&mut self, id: NodeId) -> Option<Def>;

// Record the resolution of a path or binding generated by the lowerer when expanding.
fn record_resolution(&mut self, id: NodeId, def: Def);
Expand Down Expand Up @@ -873,7 +873,7 @@ impl<'a> LoweringContext<'a> {
PatKind::Wild => hir::PatKind::Wild,
PatKind::Ident(ref binding_mode, pth1, ref sub) => {
self.with_parent_def(p.id, |this| {
match this.resolver.get_resolution(p.id).map(|d| d.base_def) {
match this.resolver.get_resolution(p.id) {
// `None` can occur in body-less function signatures
None | Some(Def::Local(..)) => {
hir::PatKind::Binding(this.lower_binding_mode(binding_mode),
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/hir/pat_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub fn pat_is_refutable(dm: &DefMap, pat: &hir::Pat) -> bool {
PatKind::TupleStruct(..) |
PatKind::Path(..) |
PatKind::Struct(..) => {
match dm.get(&pat.id).map(|d| d.full_def()) {
match dm.get(&pat.id).cloned() {
Some(Def::Variant(..)) | Some(Def::VariantCtor(..)) => true,
_ => false
}
Expand All @@ -70,7 +70,7 @@ pub fn pat_is_refutable(dm: &DefMap, pat: &hir::Pat) -> bool {
pub fn pat_is_const(dm: &DefMap, pat: &hir::Pat) -> bool {
match pat.node {
PatKind::Path(..) => {
match dm.get(&pat.id).map(|d| d.full_def()) {
match dm.get(&pat.id).cloned() {
Some(Def::Const(..)) | Some(Def::AssociatedConst(..)) => true,
_ => false
}
Expand Down Expand Up @@ -173,7 +173,7 @@ pub fn necessary_variants(dm: &DefMap, pat: &hir::Pat) -> Vec<DefId> {
PatKind::TupleStruct(..) |
PatKind::Path(..) |
PatKind::Struct(..) => {
match dm.get(&p.id).map(|d| d.full_def()) {
match dm.get(&p.id).cloned() {
Some(Def::Variant(id)) |
Some(Def::VariantCtor(id, ..)) => variants.push(id),
_ => ()
Expand Down
16 changes: 10 additions & 6 deletions src/librustc/middle/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,16 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
fn lookup_and_handle_definition(&mut self, id: ast::NodeId) {
let def = self.tcx.expect_def(id);

// If `bar` is a trait item, make sure to mark Foo as alive in `Foo::bar`
// If it is an associated item, mark the type as alive
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().item_substs.get(&id) {
if let ty::TyAdt(tyid, _) = substs.substs.type_at(0).sty {
self.check_def_id(tyid.did);
Def::AssociatedTy(..) | Def::Method(_) | Def::AssociatedConst(_) => {
if let Some(resolution) = self.tcx.assoc_map.get(&id) {
let base_def = resolution.base_def;
match base_def {
Def::Struct(..) | Def::Enum(..) | Def::TyAlias(..) => {
self.check_def_id(base_def.def_id());
}
_ => {}
}
}
}
Expand Down Expand Up @@ -430,6 +433,7 @@ impl<'a, 'tcx> DeadVisitor<'a, 'tcx> {
hir::ItemStatic(..)
| hir::ItemConst(..)
| hir::ItemFn(..)
| hir::ItemTy(..)
| hir::ItemEnum(..)
| hir::ItemStruct(..)
| hir::ItemUnion(..) => true,
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/resolve_lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
hir::TyPath(None, ref path) => {
// if this path references a trait, then this will resolve to
// a trait ref, which introduces a binding scope.
match self.def_map.get(&ty.id).map(|d| (d.base_def, d.depth)) {
Some((Def::Trait(..), 0)) => {
match self.def_map.get(&ty.id).cloned() {
Some(Def::Trait(..)) => {
self.with(LateScope(&[], self.scope), |_, this| {
this.visit_path(path, ty.id);
});
Expand Down
13 changes: 9 additions & 4 deletions src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use dep_graph::{DepGraph, DepTrackingMap};
use session::Session;
use middle;
use hir::TraitMap;
use hir::def::DefMap;
use hir::def::{AssocMap, DefMap};
use hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE};
use hir::map as ast_map;
use hir::map::{DefKey, DefPathData, DisambiguatedDefPathData};
Expand Down Expand Up @@ -379,11 +379,14 @@ pub struct GlobalCtxt<'tcx> {

pub sess: &'tcx Session,

/// Map from path id to the results from resolve; generated
/// initially by resolve and updated during typeck in some cases
/// (e.g., UFCS paths)
/// Map from path id to definition; generated
/// initially by resolve and updated during typeck for
/// associated items
pub def_map: RefCell<DefMap>,

/// Map from path id to partial resolution for associated items
pub assoc_map: AssocMap,

/// Map indicating what traits are in scope for places where this
/// is relevant; generated by resolve.
pub trait_map: TraitMap,
Expand Down Expand Up @@ -769,6 +772,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
pub fn create_and_enter<F, R>(s: &'tcx Session,
arenas: &'tcx CtxtArenas<'tcx>,
def_map: DefMap,
assoc_map: AssocMap,
trait_map: TraitMap,
named_region_map: resolve_lifetime::NamedRegionMap,
map: ast_map::Map<'tcx>,
Expand Down Expand Up @@ -798,6 +802,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
variance_computed: Cell::new(false),
sess: s,
def_map: RefCell::new(def_map),
assoc_map: assoc_map,
trait_map: trait_map,
tables: RefCell::new(Tables::empty()),
impl_trait_refs: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
Expand Down
14 changes: 10 additions & 4 deletions src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2259,18 +2259,24 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {

/// Returns a path resolution for node id if it exists, panics otherwise.
pub fn expect_resolution(self, id: NodeId) -> PathResolution {
*self.def_map.borrow().get(&id).expect("no def-map entry for node id")
if let Some(def) = self.def_map.borrow().get(&id) {
return PathResolution::new(*def);
}
if let Some(resolution) = self.assoc_map.get(&id) {
return *resolution;
}
panic!("no def-map entry for node id");
}

/// Returns a fully resolved definition for node id if it exists, panics otherwise.
pub fn expect_def(self, id: NodeId) -> Def {
self.expect_resolution(id).full_def()
*self.def_map.borrow().get(&id).expect("no def-map entry for node id")
}

/// Returns a fully resolved definition for node id if it exists, or none if no
/// definition exists, panics on partial resolutions to catch errors.
/// definition exists or only a partial resolution exists.
pub fn expect_def_or_none(self, id: NodeId) -> Option<Def> {
self.def_map.borrow().get(&id).map(|resolution| resolution.full_def())
self.def_map.borrow().get(&id).cloned()
}

// Returns `ty::VariantDef` if `def` refers to a struct,
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_const_eval/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use rustc::hir::map as ast_map;
use rustc::hir::map::blocks::FnLikeNode;
use rustc::middle::cstore::InlinedItem;
use rustc::traits;
use rustc::hir::def::{Def, CtorKind, PathResolution};
use rustc::hir::def::{Def, CtorKind};
use rustc::hir::def_id::DefId;
use rustc::hir::pat_util::def_to_path;
use rustc::ty::{self, Ty, TyCtxt};
Expand Down Expand Up @@ -284,7 +284,7 @@ pub fn const_expr_to_pat<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
hir::ExprCall(ref callee, ref args) => {
let def = tcx.expect_def(callee.id);
if let Vacant(entry) = tcx.def_map.borrow_mut().entry(expr.id) {
entry.insert(PathResolution::new(def));
entry.insert(def);
}
let path = match def {
Def::StructCtor(def_id, CtorKind::Fn) |
Expand Down
2 changes: 0 additions & 2 deletions src/librustc_data_structures/graph/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
use graph::*;
use std::fmt::Debug;

type TestNode = Node<&'static str>;
type TestEdge = Edge<&'static str>;
type TestGraph = Graph<&'static str, &'static str>;

fn create_graph() -> TestGraph {
Expand Down
5 changes: 4 additions & 1 deletion src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use rustc::hir;
use rustc::hir::{map as hir_map, FreevarMap, TraitMap};
use rustc::hir::def::DefMap;
use rustc::hir::def::{AssocMap, DefMap};
use rustc::hir::lowering::lower_crate;
use rustc_data_structures::blake2b::Blake2bHasher;
use rustc_data_structures::fmt_wrap::FmtWrap;
Expand Down Expand Up @@ -64,6 +64,7 @@ use derive_registrar;
#[derive(Clone)]
pub struct Resolutions {
pub def_map: DefMap,
pub assoc_map: AssocMap,
pub freevars: FreevarMap,
pub trait_map: TraitMap,
pub maybe_unused_trait_imports: NodeSet,
Expand Down Expand Up @@ -790,6 +791,7 @@ pub fn phase_2_configure_and_expand<'a, F>(sess: &Session,
},
resolutions: Resolutions {
def_map: resolver.def_map,
assoc_map: resolver.assoc_map,
freevars: resolver.freevars,
trait_map: resolver.trait_map,
maybe_unused_trait_imports: resolver.maybe_unused_trait_imports,
Expand Down Expand Up @@ -866,6 +868,7 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
TyCtxt::create_and_enter(sess,
arenas,
resolutions.def_map,
resolutions.assoc_map,
resolutions.trait_map,
named_region_map,
hir_map,
Expand Down
1 change: 1 addition & 0 deletions src/librustc_driver/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ fn test_env<F>(source_string: &str,
TyCtxt::create_and_enter(&sess,
&arenas,
resolutions.def_map,
resolutions.assoc_map,
resolutions.trait_map,
named_region_map.unwrap(),
ast_map,
Expand Down
7 changes: 6 additions & 1 deletion src/librustc_incremental/calculate_svh/svh_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,12 @@ impl<'a, 'hash, 'tcx> StrictVersionHashVisitor<'a, 'hash, 'tcx> {

if let Some(def) = self.tcx.def_map.borrow().get(&id) {
debug!("hash_resolve: id={:?} def={:?} st={:?}", id, def, self.st);
self.hash_partial_def(def);
self.hash_def(*def);
}

if let Some(partial_def) = self.tcx.assoc_map.get(&id) {
debug!("hash_resolve: id={:?} partial_def={:?} st={:?}", id, partial_def, self.st);
self.hash_partial_def(partial_def);
}

if let Some(traits) = self.tcx.trait_map.get(&id) {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_metadata/astencode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use schema::*;

use rustc::middle::cstore::{InlinedItem, InlinedItemRef};
use rustc::middle::const_qualif::ConstQualif;
use rustc::hir::def::{self, Def};
use rustc::hir::def::Def;
use rustc::hir::def_id::DefId;
use rustc::ty::{self, TyCtxt, Ty};

Expand Down Expand Up @@ -141,7 +141,7 @@ pub fn decode_inlined_item<'a, 'tcx>(cdata: &CrateMetadata,
for (id, entry) in ast.side_tables.decode((cdata, tcx, id_ranges)) {
match entry {
TableEntry::Def(def) => {
tcx.def_map.borrow_mut().insert(id, def::PathResolution::new(def));
tcx.def_map.borrow_mut().insert(id, def);
}
TableEntry::NodeType(ty) => {
tcx.tables.borrow_mut().node_types.insert(id, ty);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_passes/static_recursion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ impl<'a, 'ast: 'a> Visitor<'ast> for CheckItemRecursionVisitor<'a, 'ast> {
fn visit_expr(&mut self, e: &'ast hir::Expr) {
match e.node {
hir::ExprPath(..) => {
match self.def_map.get(&e.id).map(|d| d.base_def) {
match self.def_map.get(&e.id).cloned() {
Some(Def::Static(def_id, _)) |
Some(Def::AssociatedConst(def_id)) |
Some(Def::Const(def_id)) => {
Expand Down
36 changes: 25 additions & 11 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,7 @@ pub struct Resolver<'a> {
primitive_type_table: PrimitiveTypeTable,

pub def_map: DefMap,
pub assoc_map: AssocMap,
pub freevars: FreevarMap,
freevars_seen: NodeMap<NodeMap<usize>>,
pub export_map: ExportMap,
Expand Down Expand Up @@ -1223,12 +1224,12 @@ impl<'a> hir::lowering::Resolver for Resolver<'a> {
}
}

fn get_resolution(&mut self, id: NodeId) -> Option<PathResolution> {
fn get_resolution(&mut self, id: NodeId) -> Option<Def> {
self.def_map.get(&id).cloned()
}

fn record_resolution(&mut self, id: NodeId, def: Def) {
self.def_map.insert(id, PathResolution::new(def));
self.def_map.insert(id, def);
}

fn definitions(&mut self) -> &mut Definitions {
Expand Down Expand Up @@ -1306,6 +1307,7 @@ impl<'a> Resolver<'a> {
primitive_type_table: PrimitiveTypeTable::new(),

def_map: NodeMap(),
assoc_map: NodeMap(),
freevars: NodeMap(),
freevars_seen: NodeMap(),
export_map: NodeMap(),
Expand Down Expand Up @@ -2141,7 +2143,7 @@ impl<'a> Resolver<'a> {
pat.walk(&mut |pat| {
if let PatKind::Ident(binding_mode, ident, ref sub_pat) = pat.node {
if sub_pat.is_some() || match self.def_map.get(&pat.id) {
Some(&PathResolution { base_def: Def::Local(..), .. }) => true,
Some(&Def::Local(..)) => true,
_ => false,
} {
let binding_info = BindingInfo { span: ident.span, binding_mode: binding_mode };
Expand Down Expand Up @@ -2849,9 +2851,9 @@ impl<'a> Resolver<'a> {

if let Some(node_id) = self.current_self_type.as_ref().and_then(extract_node_id) {
// Look for a field with the same name in the current self_type.
if let Some(resolution) = self.def_map.get(&node_id) {
match resolution.base_def {
Def::Struct(did) | Def::Union(did) if resolution.depth == 0 => {
if let Some(def) = self.def_map.get(&node_id) {
match *def {
Def::Struct(did) | Def::Union(did) => {
if let Some(field_names) = self.field_names.get(&did) {
if field_names.iter().any(|&field_name| name == field_name) {
return Field;
Expand Down Expand Up @@ -3327,8 +3329,20 @@ impl<'a> Resolver<'a> {

fn record_def(&mut self, node_id: NodeId, resolution: PathResolution) {
debug!("(recording def) recording {:?} for {}", resolution, node_id);
if let Some(prev_res) = self.def_map.insert(node_id, resolution) {
panic!("path resolved multiple times ({:?} before, {:?} now)", prev_res, resolution);

if resolution.depth != 0 {
// partial resolution
if let Some(prev_res) = self.assoc_map.insert(node_id, resolution) {
panic!("path resolved multiple times ({:?} before, {:?} now)",
prev_res,
resolution);
}
return;
}

let def = resolution.base_def;
if let Some(prev_def) = self.def_map.insert(node_id, def) {
panic!("path resolved multiple times ({:?} before, {:?} now)", prev_def, def);
}
}

Expand All @@ -3343,10 +3357,10 @@ impl<'a> Resolver<'a> {
};

let segments: Vec<_> = path.segments.iter().map(|seg| seg.identifier).collect();
let mut path_resolution = err_path_resolution();
let mut def = Def::Err;
let vis = match self.resolve_module_path(&segments, DontUseLexicalScope, Some(path.span)) {
Success(module) => {
path_resolution = PathResolution::new(module.def().unwrap());
def = module.def().unwrap();
ty::Visibility::Restricted(module.normal_ancestor_id.unwrap())
}
Indeterminate => unreachable!(),
Expand All @@ -3357,7 +3371,7 @@ impl<'a> Resolver<'a> {
ty::Visibility::Public
}
};
self.def_map.insert(id, path_resolution);
self.def_map.insert(id, def);
if !self.is_accessible(vis) {
let msg = format!("visibilities can only be restricted to ancestor modules");
self.session.span_err(path.span, &msg);
Expand Down
Loading