Skip to content

Commit

Permalink
rustc_metadata: group information into less tags.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Sep 20, 2016
1 parent 6742b23 commit ef4352f
Show file tree
Hide file tree
Showing 35 changed files with 972 additions and 1,533 deletions.
10 changes: 4 additions & 6 deletions src/librustc/hir/def.rs
Expand Up @@ -18,15 +18,14 @@ pub enum Def {
Fn(DefId),
SelfTy(Option<DefId> /* trait */, Option<DefId> /* impl */),
Mod(DefId),
ForeignMod(DefId),
Static(DefId, bool /* is_mutbl */),
Const(DefId),
AssociatedConst(DefId),
Local(DefId),
Variant(DefId /* enum */, DefId /* variant */),
Variant(DefId),
Enum(DefId),
TyAlias(DefId),
AssociatedTy(DefId /* trait */, DefId),
AssociatedTy(DefId),
Trait(DefId),
PrimTy(hir::PrimTy),
TyParam(DefId),
Expand Down Expand Up @@ -101,8 +100,8 @@ pub struct Export {
impl Def {
pub fn def_id(&self) -> DefId {
match *self {
Def::Fn(id) | Def::Mod(id) | Def::ForeignMod(id) | Def::Static(id, _) |
Def::Variant(_, id) | Def::Enum(id) | Def::TyAlias(id) | Def::AssociatedTy(_, id) |
Def::Fn(id) | Def::Mod(id) | Def::Static(id, _) |
Def::Variant(id) | Def::Enum(id) | Def::TyAlias(id) | Def::AssociatedTy(id) |
Def::TyParam(id) | Def::Struct(id) | Def::Union(id) | Def::Trait(id) |
Def::Method(id) | Def::Const(id) | Def::AssociatedConst(id) |
Def::Local(id) | Def::Upvar(id, ..) => {
Expand All @@ -122,7 +121,6 @@ impl Def {
match *self {
Def::Fn(..) => "function",
Def::Mod(..) => "module",
Def::ForeignMod(..) => "foreign module",
Def::Static(..) => "static",
Def::Variant(..) => "variant",
Def::Enum(..) => "enum",
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/hir/pat_util.rs
Expand Up @@ -174,7 +174,7 @@ pub fn necessary_variants(dm: &DefMap, pat: &hir::Pat) -> Vec<DefId> {
PatKind::Path(..) |
PatKind::Struct(..) => {
match dm.get(&p.id) {
Some(&PathResolution { base_def: Def::Variant(_, id), .. }) => {
Some(&PathResolution { base_def: Def::Variant(id), .. }) => {
variants.push(id);
}
_ => ()
Expand Down
11 changes: 2 additions & 9 deletions src/librustc/middle/cstore.rs
Expand Up @@ -135,11 +135,9 @@ pub trait CrateStore<'tcx> {
fn closure_ty<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId)
-> ty::ClosureTy<'tcx>;
fn item_variances(&self, def: DefId) -> Vec<ty::Variance>;
fn repr_attrs(&self, def: DefId) -> Vec<attr::ReprAttr>;
fn item_type<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
-> Ty<'tcx>;
fn visible_parent_map<'a>(&'a self) -> ::std::cell::RefMut<'a, DefIdMap<DefId>>;
fn item_name(&self, def: DefId) -> ast::Name;
fn opt_item_name(&self, def: DefId) -> Option<ast::Name>;
fn item_predicates<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
-> ty::GenericPredicates<'tcx>;
Expand All @@ -150,7 +148,7 @@ pub trait CrateStore<'tcx> {
fn item_attrs(&self, def_id: DefId) -> Vec<ast::Attribute>;
fn trait_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)-> ty::TraitDef<'tcx>;
fn adt_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) -> ty::AdtDefMaster<'tcx>;
fn method_arg_names(&self, did: DefId) -> Vec<String>;
fn fn_arg_names(&self, did: DefId) -> Vec<String>;
fn inherent_implementations_for_type(&self, def_id: DefId) -> Vec<DefId>;

// trait info
Expand Down Expand Up @@ -211,7 +209,6 @@ pub trait CrateStore<'tcx> {
fn def_key(&self, def: DefId) -> hir_map::DefKey;
fn relative_def_path(&self, def: DefId) -> Option<hir_map::DefPath>;
fn struct_ctor_def_id(&self, struct_def_id: DefId) -> Option<DefId>;
fn tuple_struct_definition_if_ctor(&self, did: DefId) -> Option<DefId>;
fn struct_field_names(&self, def: DefId) -> Vec<ast::Name>;
fn item_children(&self, did: DefId) -> Vec<ChildItem>;

Expand Down Expand Up @@ -297,13 +294,11 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
fn closure_ty<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId)
-> ty::ClosureTy<'tcx> { bug!("closure_ty") }
fn item_variances(&self, def: DefId) -> Vec<ty::Variance> { bug!("item_variances") }
fn repr_attrs(&self, def: DefId) -> Vec<attr::ReprAttr> { bug!("repr_attrs") }
fn item_type<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
-> Ty<'tcx> { bug!("item_type") }
fn visible_parent_map<'a>(&'a self) -> ::std::cell::RefMut<'a, DefIdMap<DefId>> {
bug!("visible_parent_map")
}
fn item_name(&self, def: DefId) -> ast::Name { bug!("item_name") }
fn opt_item_name(&self, def: DefId) -> Option<ast::Name> { bug!("opt_item_name") }
fn item_predicates<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
-> ty::GenericPredicates<'tcx> { bug!("item_predicates") }
Expand All @@ -316,7 +311,7 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
{ bug!("trait_def") }
fn adt_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) -> ty::AdtDefMaster<'tcx>
{ bug!("adt_def") }
fn method_arg_names(&self, did: DefId) -> Vec<String> { bug!("method_arg_names") }
fn fn_arg_names(&self, did: DefId) -> Vec<String> { bug!("fn_arg_names") }
fn inherent_implementations_for_type(&self, def_id: DefId) -> Vec<DefId> { vec![] }

// trait info
Expand Down Expand Up @@ -393,8 +388,6 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
}
fn struct_ctor_def_id(&self, struct_def_id: DefId) -> Option<DefId>
{ bug!("struct_ctor_def_id") }
fn tuple_struct_definition_if_ctor(&self, did: DefId) -> Option<DefId>
{ bug!("tuple_struct_definition_if_ctor") }
fn struct_field_names(&self, def: DefId) -> Vec<ast::Name> { bug!("struct_field_names") }
fn item_children(&self, did: DefId) -> Vec<ChildItem> { bug!("item_children") }

Expand Down
6 changes: 4 additions & 2 deletions src/librustc/middle/dead.rs
Expand Up @@ -108,8 +108,10 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
_ if self.ignore_non_const_paths => (),
Def::PrimTy(_) => (),
Def::SelfTy(..) => (),
Def::Variant(enum_id, variant_id) => {
self.check_def_id(enum_id);
Def::Variant(variant_id) => {
if let Some(enum_id) = self.tcx.parent_def_id(variant_id) {
self.check_def_id(enum_id);
}
if !self.ignore_variant_stack.contains(&variant_id) {
self.check_def_id(variant_id);
}
Expand Down
3 changes: 2 additions & 1 deletion src/librustc/middle/expr_use_visitor.rs
Expand Up @@ -1003,7 +1003,8 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
// the leaves of the pattern tree structure.
return_if_err!(mc.cat_pattern(cmt_discr, pat, |mc, cmt_pat, pat| {
match tcx.expect_def_or_none(pat.id) {
Some(Def::Variant(enum_did, variant_did)) => {
Some(Def::Variant(variant_did)) => {
let enum_did = tcx.parent_def_id(variant_did).unwrap();
let downcast_cmt = if tcx.lookup_adt_def(enum_did).is_univariant() {
cmt_pat
} else {
Expand Down
13 changes: 9 additions & 4 deletions src/librustc/middle/mem_categorization.rs
Expand Up @@ -529,7 +529,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
Ok(self.cat_rvalue_node(id, span, expr_ty))
}

Def::Mod(_) | Def::ForeignMod(_) |
Def::Mod(_) |
Def::Trait(_) | Def::Enum(..) | Def::TyAlias(..) | Def::PrimTy(_) |
Def::TyParam(..) |
Def::Label(_) | Def::SelfTy(..) |
Expand Down Expand Up @@ -1077,18 +1077,23 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
// alone) because PatKind::Struct can also refer to variants.
let cmt = match self.tcx().expect_def_or_none(pat.id) {
Some(Def::Err) => return Err(()),
Some(Def::Variant(enum_did, variant_did))
Some(Def::Variant(variant_did)) => {
// univariant enums do not need downcasts
if !self.tcx().lookup_adt_def(enum_did).is_univariant() => {
let enum_did = self.tcx().parent_def_id(variant_did).unwrap();
if !self.tcx().lookup_adt_def(enum_did).is_univariant() {
self.cat_downcast(pat, cmt.clone(), cmt.ty, variant_did)
} else {
cmt
}
}
_ => cmt
};

match pat.node {
PatKind::TupleStruct(_, ref subpats, ddpos) => {
let expected_len = match self.tcx().expect_def(pat.id) {
Def::Variant(enum_def, def_id) => {
Def::Variant(def_id) => {
let enum_def = self.tcx().parent_def_id(def_id).unwrap();
self.tcx().lookup_adt_def(enum_def).variant_with_id(def_id).fields.len()
}
Def::Struct(..) => {
Expand Down
10 changes: 3 additions & 7 deletions src/librustc/ty/context.rs
Expand Up @@ -1404,13 +1404,9 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
/// Obtain the representation annotation for a struct definition.
pub fn lookup_repr_hints(self, did: DefId) -> Rc<Vec<attr::ReprAttr>> {
self.repr_hint_cache.memoize(did, || {
Rc::new(if did.is_local() {
self.get_attrs(did).iter().flat_map(|meta| {
attr::find_repr_attrs(self.sess.diagnostic(), meta).into_iter()
}).collect()
} else {
self.sess.cstore.repr_attrs(did)
})
Rc::new(self.get_attrs(did).iter().flat_map(|meta| {
attr::find_repr_attrs(self.sess.diagnostic(), meta).into_iter()
}).collect())
})
}
}
2 changes: 1 addition & 1 deletion src/librustc/ty/item_path.rs
Expand Up @@ -303,7 +303,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
/// Returns the def-id of `def_id`'s parent in the def tree. If
/// this returns `None`, then `def_id` represents a crate root or
/// inlined root.
fn parent_def_id(&self, def_id: DefId) -> Option<DefId> {
pub fn parent_def_id(self, def_id: DefId) -> Option<DefId> {
let key = self.def_key(def_id);
key.parent.map(|index| DefId { krate: def_id.krate, index: index })
}
Expand Down
18 changes: 9 additions & 9 deletions src/librustc/ty/mod.rs
Expand Up @@ -193,7 +193,7 @@ impl<'tcx> ImplOrTraitItem<'tcx> {
match *self {
ConstTraitItem(ref associated_const) => Def::AssociatedConst(associated_const.def_id),
MethodTraitItem(ref method) => Def::Method(method.def_id),
TypeTraitItem(ref ty) => Def::AssociatedTy(ty.container.id(), ty.def_id),
TypeTraitItem(ref ty) => Def::AssociatedTy(ty.def_id),
}
}

Expand Down Expand Up @@ -1666,7 +1666,7 @@ impl<'a, 'gcx, 'tcx, 'container> AdtDefData<'gcx, 'container> {

pub fn variant_of_def(&self, def: Def) -> &VariantDefData<'gcx, 'container> {
match def {
Def::Variant(_, vid) => self.variant_with_id(vid),
Def::Variant(vid) => self.variant_with_id(vid),
Def::Struct(..) | Def::Union(..) |
Def::TyAlias(..) | Def::AssociatedTy(..) => self.struct_variant(),
_ => bug!("unexpected def {:?} in variant_of_def", def)
Expand Down Expand Up @@ -2325,7 +2325,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
// or variant or their constructors, panics otherwise.
pub fn expect_variant_def(self, def: Def) -> VariantDef<'tcx> {
match def {
Def::Variant(enum_did, did) => {
Def::Variant(did) => {
let enum_did = self.parent_def_id(did).unwrap();
self.lookup_adt_def(enum_did).variant_with_id(did)
}
Def::Struct(did) | Def::Union(did) => {
Expand Down Expand Up @@ -2387,7 +2388,9 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
if let Some(id) = self.map.as_local_node_id(id) {
self.map.name(id)
} else {
self.sess.cstore.item_name(id)
self.sess.cstore.opt_item_name(id).unwrap_or_else(|| {
bug!("item_name: no name for {:?}", self.def_path(id));
})
}
}

Expand Down Expand Up @@ -2631,11 +2634,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
let trait_ref = self.impl_trait_ref(impl_def_id).unwrap();

// Record the trait->implementation mapping.
if let Some(parent) = self.sess.cstore.impl_parent(impl_def_id) {
def.record_remote_impl(self, impl_def_id, trait_ref, parent);
} else {
def.record_remote_impl(self, impl_def_id, trait_ref, trait_id);
}
let parent = self.sess.cstore.impl_parent(impl_def_id).unwrap_or(trait_id);
def.record_remote_impl(self, impl_def_id, trait_ref, parent);

// For any methods that use a default implementation, add them to
// the map. This is a bit unfortunate.
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_const_eval/check_match.rs
Expand Up @@ -801,7 +801,7 @@ fn pat_constructors(cx: &MatchCheckCtxt, p: &Pat,
match pat.node {
PatKind::Struct(..) | PatKind::TupleStruct(..) | PatKind::Path(..) =>
match cx.tcx.expect_def(pat.id) {
Def::Variant(_, id) => vec![Variant(id)],
Def::Variant(id) => vec![Variant(id)],
Def::Struct(..) | Def::Union(..) |
Def::TyAlias(..) | Def::AssociatedTy(..) => vec![Single],
Def::Const(..) | Def::AssociatedConst(..) =>
Expand Down Expand Up @@ -913,7 +913,7 @@ pub fn specialize<'a, 'b, 'tcx>(
Def::Const(..) | Def::AssociatedConst(..) =>
span_bug!(pat_span, "const pattern should've \
been rewritten"),
Def::Variant(_, id) if *constructor != Variant(id) => None,
Def::Variant(id) if *constructor != Variant(id) => None,
Def::Variant(..) | Def::Struct(..) => Some(Vec::new()),
def => span_bug!(pat_span, "specialize: unexpected \
definition {:?}", def),
Expand All @@ -925,7 +925,7 @@ pub fn specialize<'a, 'b, 'tcx>(
Def::Const(..) | Def::AssociatedConst(..) =>
span_bug!(pat_span, "const pattern should've \
been rewritten"),
Def::Variant(_, id) if *constructor != Variant(id) => None,
Def::Variant(id) if *constructor != Variant(id) => None,
Def::Variant(..) | Def::Struct(..) => {
match ddpos {
Some(ddpos) => {
Expand Down
11 changes: 5 additions & 6 deletions src/librustc_const_eval/eval.rs
Expand Up @@ -57,7 +57,6 @@ macro_rules! math {
}

fn lookup_variant_by_id<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
enum_def: DefId,
variant_def: DefId)
-> Option<&'tcx Expr> {
fn variant_expr<'a>(variants: &'a [hir::Variant], id: ast::NodeId)
Expand All @@ -70,8 +69,8 @@ fn lookup_variant_by_id<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
None
}

if let Some(enum_node_id) = tcx.map.as_local_node_id(enum_def) {
let variant_node_id = tcx.map.as_local_node_id(variant_def).unwrap();
if let Some(variant_node_id) = tcx.map.as_local_node_id(variant_def) {
let enum_node_id = tcx.map.get_parent(variant_node_id);
match tcx.map.find(enum_node_id) {
None => None,
Some(ast_map::NodeItem(it)) => match it.node {
Expand Down Expand Up @@ -289,7 +288,7 @@ pub fn const_expr_to_pat<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
}
let path = match def {
Def::Struct(def_id) => def_to_path(tcx, def_id),
Def::Variant(_, variant_did) => def_to_path(tcx, variant_did),
Def::Variant(variant_did) => def_to_path(tcx, variant_did),
Def::Fn(..) | Def::Method(..) => return Ok(P(hir::Pat {
id: expr.id,
node: PatKind::Lit(P(expr.clone())),
Expand Down Expand Up @@ -808,8 +807,8 @@ pub fn eval_const_expr_partial<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
signal!(e, NonConstPath);
}
},
Def::Variant(enum_def, variant_def) => {
if let Some(const_expr) = lookup_variant_by_id(tcx, enum_def, variant_def) {
Def::Variant(variant_def) => {
if let Some(const_expr) = lookup_variant_by_id(tcx, variant_def) {
match eval_const_expr_partial(tcx, const_expr, ty_hint, None) {
Ok(val) => val,
Err(err) => {
Expand Down
1 change: 0 additions & 1 deletion src/librustc_incremental/calculate_svh/svh_visitor.rs
Expand Up @@ -602,7 +602,6 @@ impl<'a, 'hash, 'tcx> StrictVersionHashVisitor<'a, 'hash, 'tcx> {
// def-id is the same, so it suffices to hash the def-id
Def::Fn(..) |
Def::Mod(..) |
Def::ForeignMod(..) |
Def::Static(..) |
Def::Variant(..) |
Def::Enum(..) |
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_metadata/astencode.rs
Expand Up @@ -18,7 +18,7 @@ use cstore::CrateMetadata;
use decoder::DecodeContext;
use encoder::EncodeContext;

use middle::cstore::{InlinedItem, InlinedItemRef};
use rustc::middle::cstore::{InlinedItem, InlinedItemRef};
use rustc::hir::def;
use rustc::hir::def_id::DefId;
use rustc::ty::TyCtxt;
Expand Down

0 comments on commit ef4352f

Please sign in to comment.