Skip to content

Commit

Permalink
rustc: remove type information from TraitDef.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Nov 29, 2016
1 parent f50dbd5 commit 3f338ee
Show file tree
Hide file tree
Showing 22 changed files with 123 additions and 160 deletions.
4 changes: 2 additions & 2 deletions src/librustc/middle/cstore.rs
Expand Up @@ -277,7 +277,7 @@ pub trait CrateStore<'tcx> {
fn item_generics<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
-> ty::Generics<'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 trait_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)-> ty::TraitDef;
fn adt_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) -> ty::AdtDefMaster<'tcx>;
fn fn_arg_names(&self, did: DefId) -> Vec<ast::Name>;
fn inherent_implementations_for_type(&self, def_id: DefId) -> Vec<DefId>;
Expand Down Expand Up @@ -423,7 +423,7 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
fn item_generics<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
-> ty::Generics<'tcx> { bug!("item_generics") }
fn item_attrs(&self, def_id: DefId) -> Vec<ast::Attribute> { bug!("item_attrs") }
fn trait_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)-> ty::TraitDef<'tcx>
fn trait_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)-> ty::TraitDef
{ bug!("trait_def") }
fn adt_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) -> ty::AdtDefMaster<'tcx>
{ bug!("adt_def") }
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/traits/error_reporting.rs
Expand Up @@ -244,11 +244,11 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
for item in self.tcx.get_attrs(def_id).iter() {
if item.check_name("rustc_on_unimplemented") {
let err_sp = item.meta().span.substitute_dummy(span);
let def = self.tcx.lookup_trait_def(trait_ref.def_id);
let trait_str = def.trait_ref.to_string();
let trait_str = self.tcx.item_path_str(trait_ref.def_id);
if let Some(istring) = item.value_str() {
let istring = &*istring.as_str();
let generic_map = def.generics.types.iter().map(|param| {
let generics = self.tcx.item_generics(trait_ref.def_id);
let generic_map = generics.types.iter().map(|param| {
(param.name.as_str().to_string(),
trait_ref.substs.type_for_def(param).to_string())
}).collect::<FxHashMap<String, String>>();
Expand Down
16 changes: 10 additions & 6 deletions src/librustc/traits/object_safety.rs
Expand Up @@ -21,7 +21,8 @@ use super::elaborate_predicates;

use hir::def_id::DefId;
use traits;
use ty::{self, ToPolyTraitRef, Ty, TyCtxt, TypeFoldable};
use ty::{self, Ty, TyCtxt, TypeFoldable};
use ty::subst::Substs;
use syntax::ast;

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -126,9 +127,10 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
}

fn supertraits_reference_self(self, trait_def_id: DefId) -> bool {
let trait_def = self.lookup_trait_def(trait_def_id);
let trait_ref = trait_def.trait_ref.clone();
let trait_ref = trait_ref.to_poly_trait_ref();
let trait_ref = ty::Binder(ty::TraitRef {
def_id: trait_def_id,
substs: Substs::identity_for_item(self, trait_def_id)
});
let predicates = self.item_super_predicates(trait_def_id);
predicates
.predicates
Expand Down Expand Up @@ -317,8 +319,10 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {

// Compute supertraits of current trait lazily.
if supertraits.is_none() {
let trait_def = self.lookup_trait_def(trait_def_id);
let trait_ref = ty::Binder(trait_def.trait_ref.clone());
let trait_ref = ty::Binder(ty::TraitRef {
def_id: trait_def_id,
substs: Substs::identity_for_item(self, trait_def_id)
});
supertraits = Some(traits::supertraits(self, trait_ref).collect());
}

Expand Down
14 changes: 6 additions & 8 deletions src/librustc/traits/specialize/specialization_graph.rs
Expand Up @@ -297,18 +297,18 @@ impl<'a, 'gcx, 'tcx> Node {
}
}

pub struct Ancestors<'a, 'tcx: 'a> {
trait_def: &'a TraitDef<'tcx>,
pub struct Ancestors<'a> {
trait_def: &'a TraitDef,
current_source: Option<Node>,
}

impl<'a, 'tcx> Iterator for Ancestors<'a, 'tcx> {
impl<'a> Iterator for Ancestors<'a> {
type Item = Node;
fn next(&mut self) -> Option<Node> {
let cur = self.current_source.take();
if let Some(Node::Impl(cur_impl)) = cur {
let parent = self.trait_def.specialization_graph.borrow().parent(cur_impl);
if parent == self.trait_def.def_id() {
if parent == self.trait_def.def_id {
self.current_source = Some(Node::Trait(parent));
} else {
self.current_source = Some(Node::Impl(parent));
Expand All @@ -332,7 +332,7 @@ impl<T> NodeItem<T> {
}
}

impl<'a, 'gcx, 'tcx> Ancestors<'a, 'tcx> {
impl<'a, 'gcx, 'tcx> Ancestors<'a> {
/// Search the items from the given ancestors, returning each definition
/// with the given name and the given kind.
#[inline] // FIXME(#35870) Avoid closures being unexported due to impl Trait.
Expand All @@ -347,9 +347,7 @@ impl<'a, 'gcx, 'tcx> Ancestors<'a, 'tcx> {

/// Walk up the specialization ancestors of a given impl, starting with that
/// impl itself.
pub fn ancestors<'a, 'tcx>(trait_def: &'a TraitDef<'tcx>,
start_from_impl: DefId)
-> Ancestors<'a, 'tcx> {
pub fn ancestors<'a>(trait_def: &'a TraitDef, start_from_impl: DefId) -> Ancestors<'a> {
Ancestors {
trait_def: trait_def,
current_source: Some(Node::Impl(start_from_impl)),
Expand Down
16 changes: 2 additions & 14 deletions src/librustc/ty/context.rs
Expand Up @@ -66,7 +66,7 @@ pub struct CtxtArenas<'tcx> {

// references
generics: TypedArena<ty::Generics<'tcx>>,
trait_def: TypedArena<ty::TraitDef<'tcx>>,
trait_def: TypedArena<ty::TraitDef>,
adt_def: TypedArena<ty::AdtDefData<'tcx, 'tcx>>,
mir: TypedArena<RefCell<Mir<'tcx>>>,
}
Expand Down Expand Up @@ -683,19 +683,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
self.global_interners.arenas.mir.alloc(RefCell::new(mir))
}

pub fn intern_trait_def(self, def: ty::TraitDef<'gcx>)
-> &'gcx ty::TraitDef<'gcx> {
let did = def.trait_ref.def_id;
let interned = self.alloc_trait_def(def);
if let Some(prev) = self.trait_defs.borrow_mut().insert(did, interned) {
bug!("Tried to overwrite interned TraitDef: {:?}", prev)
}
self.generics.borrow_mut().insert(did, interned.generics);
interned
}

pub fn alloc_trait_def(self, def: ty::TraitDef<'gcx>)
-> &'gcx ty::TraitDef<'gcx> {
pub fn alloc_trait_def(self, def: ty::TraitDef) -> &'gcx ty::TraitDef {
self.global_interners.arenas.trait_def.alloc(def)
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/maps.rs
Expand Up @@ -39,7 +39,7 @@ dep_map_ty! { Predicates: ItemSignature(DefId) -> ty::GenericPredicates<'tcx> }
dep_map_ty! { SuperPredicates: ItemSignature(DefId) -> ty::GenericPredicates<'tcx> }
dep_map_ty! { AssociatedItemDefIds: AssociatedItemDefIds(DefId) -> Rc<Vec<DefId>> }
dep_map_ty! { ImplTraitRefs: ItemSignature(DefId) -> Option<ty::TraitRef<'tcx>> }
dep_map_ty! { TraitDefs: ItemSignature(DefId) -> &'tcx ty::TraitDef<'tcx> }
dep_map_ty! { TraitDefs: ItemSignature(DefId) -> &'tcx ty::TraitDef }
dep_map_ty! { AdtDefs: ItemSignature(DefId) -> ty::AdtDefMaster<'tcx> }
dep_map_ty! { ItemVariances: ItemSignature(DefId) -> Rc<Vec<ty::Variance>> }
dep_map_ty! { InherentImpls: InherentImpls(DefId) -> Vec<DefId> }
Expand Down
6 changes: 1 addition & 5 deletions src/librustc/ty/mod.rs
Expand Up @@ -629,10 +629,6 @@ pub struct RegionParameterDef<'tcx> {
}

impl<'tcx> RegionParameterDef<'tcx> {
pub fn to_early_bound_region(&self) -> ty::Region {
ty::ReEarlyBound(self.to_early_bound_region_data())
}

pub fn to_early_bound_region_data(&self) -> ty::EarlyBoundRegion {
ty::EarlyBoundRegion {
index: self.index,
Expand Down Expand Up @@ -2400,7 +2396,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
}

/// Given the did of a trait, returns its canonical trait ref.
pub fn lookup_trait_def(self, did: DefId) -> &'gcx TraitDef<'gcx> {
pub fn lookup_trait_def(self, did: DefId) -> &'gcx TraitDef {
lookup_locally_or_in_crate_store(
"trait_defs", did, &self.trait_defs,
|| self.alloc_trait_def(self.sess.cstore.trait_def(self.global_tcx(), did))
Expand Down
8 changes: 8 additions & 0 deletions src/librustc/ty/subst.rs
Expand Up @@ -165,6 +165,14 @@ impl<'tcx> Decodable for Kind<'tcx> {
pub type Substs<'tcx> = Slice<Kind<'tcx>>;

impl<'a, 'gcx, 'tcx> Substs<'tcx> {
/// Creates a Substs that maps each generic parameter to itself.
pub fn identity_for_item(tcx: TyCtxt<'a, 'gcx, 'tcx>, def_id: DefId)
-> &'tcx Substs<'tcx> {
Substs::for_item(tcx, def_id, |def, _| {
tcx.mk_region(ty::ReEarlyBound(def.to_early_bound_region_data()))
}, |def, _| tcx.mk_param_from_def(def))
}

/// Creates a Substs for generic parameter definitions,
/// by calling closures to obtain each region and type.
/// The closures get to observe the Substs as they're
Expand Down
39 changes: 13 additions & 26 deletions src/librustc/ty/trait_def.rs
Expand Up @@ -19,7 +19,9 @@ use hir;
use util::nodemap::FxHashMap;

/// A trait's definition with type information.
pub struct TraitDef<'tcx> {
pub struct TraitDef {
pub def_id: DefId,

pub unsafety: hir::Unsafety,

/// If `true`, then this trait had the `#[rustc_paren_sugar]`
Expand All @@ -28,15 +30,6 @@ pub struct TraitDef<'tcx> {
/// be usable with the sugar (or without it).
pub paren_sugar: bool,

/// Generic type definitions. Note that `Self` is listed in here
/// as having a single bound, the trait itself (e.g., in the trait
/// `Eq`, there is a single bound `Self : Eq`). This is so that
/// default methods get to assume that the `Self` parameters
/// implements the trait.
pub generics: &'tcx ty::Generics<'tcx>,

pub trait_ref: ty::TraitRef<'tcx>,

// Impls of a trait. To allow for quicker lookup, the impls are indexed by a
// simplified version of their `Self` type: impls with a simplifiable `Self`
// are stored in `nonblanket_impls` keyed by it, while all other impls are
Expand Down Expand Up @@ -72,18 +65,16 @@ pub struct TraitDef<'tcx> {
pub def_path_hash: u64,
}

impl<'a, 'gcx, 'tcx> TraitDef<'tcx> {
pub fn new(unsafety: hir::Unsafety,
impl<'a, 'gcx, 'tcx> TraitDef {
pub fn new(def_id: DefId,
unsafety: hir::Unsafety,
paren_sugar: bool,
generics: &'tcx ty::Generics<'tcx>,
trait_ref: ty::TraitRef<'tcx>,
def_path_hash: u64)
-> TraitDef<'tcx> {
-> TraitDef {
TraitDef {
def_id: def_id,
paren_sugar: paren_sugar,
unsafety: unsafety,
generics: generics,
trait_ref: trait_ref,
nonblanket_impls: RefCell::new(FxHashMap()),
blanket_impls: RefCell::new(vec![]),
flags: Cell::new(ty::TraitFlags::NO_TRAIT_FLAGS),
Expand All @@ -92,10 +83,6 @@ impl<'a, 'gcx, 'tcx> TraitDef<'tcx> {
}
}

pub fn def_id(&self) -> DefId {
self.trait_ref.def_id
}

// returns None if not yet calculated
pub fn object_safety(&self) -> Option<bool> {
if self.flags.get().intersects(TraitFlags::OBJECT_SAFETY_VALID) {
Expand All @@ -117,11 +104,11 @@ impl<'a, 'gcx, 'tcx> TraitDef<'tcx> {
}

fn write_trait_impls(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) {
tcx.dep_graph.write(DepNode::TraitImpls(self.trait_ref.def_id));
tcx.dep_graph.write(DepNode::TraitImpls(self.def_id));
}

fn read_trait_impls(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) {
tcx.dep_graph.read(DepNode::TraitImpls(self.trait_ref.def_id));
tcx.dep_graph.read(DepNode::TraitImpls(self.def_id));
}

/// Records a basic trait-to-implementation mapping.
Expand Down Expand Up @@ -203,13 +190,13 @@ impl<'a, 'gcx, 'tcx> TraitDef<'tcx> {
.insert(tcx, impl_def_id)
}

pub fn ancestors(&'a self, of_impl: DefId) -> specialization_graph::Ancestors<'a, 'tcx> {
pub fn ancestors(&'a self, of_impl: DefId) -> specialization_graph::Ancestors<'a> {
specialization_graph::ancestors(self, of_impl)
}

pub fn for_each_impl<F: FnMut(DefId)>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>, mut f: F) {
self.read_trait_impls(tcx);
tcx.populate_implementations_for_trait_if_necessary(self.trait_ref.def_id);
tcx.populate_implementations_for_trait_if_necessary(self.def_id);

for &impl_def_id in self.blanket_impls.borrow().iter() {
f(impl_def_id);
Expand All @@ -231,7 +218,7 @@ impl<'a, 'gcx, 'tcx> TraitDef<'tcx> {
{
self.read_trait_impls(tcx);

tcx.populate_implementations_for_trait_if_necessary(self.trait_ref.def_id);
tcx.populate_implementations_for_trait_if_necessary(self.def_id);

for &impl_def_id in self.blanket_impls.borrow().iter() {
f(impl_def_id);
Expand Down
7 changes: 4 additions & 3 deletions src/librustc/util/ppaux.rs
Expand Up @@ -432,10 +432,11 @@ impl<'tcx> fmt::Debug for ty::ExistentialTraitRef<'tcx> {
}
}

impl<'tcx> fmt::Debug for ty::TraitDef<'tcx> {
impl fmt::Debug for ty::TraitDef {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "TraitDef(generics={:?}, trait_ref={:?})",
self.generics, self.trait_ref)
ty::tls::with(|tcx| {
write!(f, "{}", tcx.item_path_str(self.def_id))
})
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_metadata/cstore_impl.rs
Expand Up @@ -110,7 +110,7 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
self.get_crate_data(def_id.krate).get_item_attrs(def_id.index)
}

fn trait_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) -> ty::TraitDef<'tcx>
fn trait_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) -> ty::TraitDef
{
self.dep_graph.read(DepNode::MetaData(def));
self.get_crate_data(def.krate).get_trait_def(def.index, tcx)
Expand Down
7 changes: 3 additions & 4 deletions src/librustc_metadata/decoder.rs
Expand Up @@ -531,16 +531,15 @@ impl<'a, 'tcx> CrateMetadata {
pub fn get_trait_def(&self,
item_id: DefIndex,
tcx: TyCtxt<'a, 'tcx, 'tcx>)
-> ty::TraitDef<'tcx> {
-> ty::TraitDef {
let data = match self.entry(item_id).kind {
EntryKind::Trait(data) => data.decode(self),
_ => bug!(),
};

ty::TraitDef::new(data.unsafety,
ty::TraitDef::new(self.local_def_id(item_id),
data.unsafety,
data.paren_sugar,
tcx.item_generics(self.local_def_id(item_id)),
data.trait_ref.decode((self, tcx)),
self.def_path(item_id).unwrap().deterministic_hash(tcx))
}

Expand Down
1 change: 0 additions & 1 deletion src/librustc_metadata/encoder.rs
Expand Up @@ -730,7 +730,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
unsafety: trait_def.unsafety,
paren_sugar: trait_def.paren_sugar,
has_default_impl: tcx.trait_has_default_impl(def_id),
trait_ref: self.lazy(&trait_def.trait_ref),
super_predicates: self.lazy(&tcx.item_super_predicates(def_id)),
};

Expand Down
1 change: 0 additions & 1 deletion src/librustc_metadata/schema.rs
Expand Up @@ -278,7 +278,6 @@ pub struct TraitData<'tcx> {
pub unsafety: hir::Unsafety,
pub paren_sugar: bool,
pub has_default_impl: bool,
pub trait_ref: Lazy<ty::TraitRef<'tcx>>,
pub super_predicates: Lazy<ty::GenericPredicates<'tcx>>,
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_typeck/astconv.rs
Expand Up @@ -91,7 +91,7 @@ pub trait AstConv<'gcx, 'tcx> {
/// Returns the `TraitDef` for a given trait. This allows you to
/// figure out the set of type parameters defined on the trait.
fn get_trait_def(&self, span: Span, id: DefId)
-> Result<&'tcx ty::TraitDef<'tcx>, ErrorReported>;
-> Result<&'tcx ty::TraitDef, ErrorReported>;

/// Ensure that the super-predicates for the trait with the given
/// id are available and also for the transitive set of
Expand Down
7 changes: 0 additions & 7 deletions src/librustc_typeck/check/method/mod.rs
Expand Up @@ -192,13 +192,6 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
m_name,
trait_def_id);

let trait_def = self.tcx.lookup_trait_def(trait_def_id);

if let Some(ref input_types) = opt_input_types {
assert_eq!(trait_def.generics.types.len() - 1, input_types.len());
}
assert!(trait_def.generics.regions.is_empty());

// Construct a trait-reference `self_ty : Trait<input_tys>`
let substs = Substs::for_item(self.tcx,
trait_def_id,
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_typeck/check/mod.rs
Expand Up @@ -1040,7 +1040,7 @@ fn report_forbidden_specialization<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
}

fn check_specialization_validity<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
trait_def: &ty::TraitDef<'tcx>,
trait_def: &ty::TraitDef,
impl_id: DefId,
impl_item: &hir::ImplItem)
{
Expand Down Expand Up @@ -1400,7 +1400,7 @@ impl<'a, 'gcx, 'tcx> AstConv<'gcx, 'tcx> for FnCtxt<'a, 'gcx, 'tcx> {
}

fn get_trait_def(&self, _: Span, id: DefId)
-> Result<&'tcx ty::TraitDef<'tcx>, ErrorReported>
-> Result<&'tcx ty::TraitDef, ErrorReported>
{
Ok(self.tcx().lookup_trait_def(id))
}
Expand Down

0 comments on commit 3f338ee

Please sign in to comment.