Skip to content

Commit 3f338ee

Browse files
committed
rustc: remove type information from TraitDef.
1 parent f50dbd5 commit 3f338ee

File tree

22 files changed

+123
-160
lines changed

22 files changed

+123
-160
lines changed

src/librustc/middle/cstore.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ pub trait CrateStore<'tcx> {
277277
fn item_generics<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
278278
-> ty::Generics<'tcx>;
279279
fn item_attrs(&self, def_id: DefId) -> Vec<ast::Attribute>;
280-
fn trait_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)-> ty::TraitDef<'tcx>;
280+
fn trait_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)-> ty::TraitDef;
281281
fn adt_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) -> ty::AdtDefMaster<'tcx>;
282282
fn fn_arg_names(&self, did: DefId) -> Vec<ast::Name>;
283283
fn inherent_implementations_for_type(&self, def_id: DefId) -> Vec<DefId>;
@@ -423,7 +423,7 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
423423
fn item_generics<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
424424
-> ty::Generics<'tcx> { bug!("item_generics") }
425425
fn item_attrs(&self, def_id: DefId) -> Vec<ast::Attribute> { bug!("item_attrs") }
426-
fn trait_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)-> ty::TraitDef<'tcx>
426+
fn trait_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)-> ty::TraitDef
427427
{ bug!("trait_def") }
428428
fn adt_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) -> ty::AdtDefMaster<'tcx>
429429
{ bug!("adt_def") }

src/librustc/traits/error_reporting.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,11 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
244244
for item in self.tcx.get_attrs(def_id).iter() {
245245
if item.check_name("rustc_on_unimplemented") {
246246
let err_sp = item.meta().span.substitute_dummy(span);
247-
let def = self.tcx.lookup_trait_def(trait_ref.def_id);
248-
let trait_str = def.trait_ref.to_string();
247+
let trait_str = self.tcx.item_path_str(trait_ref.def_id);
249248
if let Some(istring) = item.value_str() {
250249
let istring = &*istring.as_str();
251-
let generic_map = def.generics.types.iter().map(|param| {
250+
let generics = self.tcx.item_generics(trait_ref.def_id);
251+
let generic_map = generics.types.iter().map(|param| {
252252
(param.name.as_str().to_string(),
253253
trait_ref.substs.type_for_def(param).to_string())
254254
}).collect::<FxHashMap<String, String>>();

src/librustc/traits/object_safety.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ use super::elaborate_predicates;
2121

2222
use hir::def_id::DefId;
2323
use traits;
24-
use ty::{self, ToPolyTraitRef, Ty, TyCtxt, TypeFoldable};
24+
use ty::{self, Ty, TyCtxt, TypeFoldable};
25+
use ty::subst::Substs;
2526
use syntax::ast;
2627

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

128129
fn supertraits_reference_self(self, trait_def_id: DefId) -> bool {
129-
let trait_def = self.lookup_trait_def(trait_def_id);
130-
let trait_ref = trait_def.trait_ref.clone();
131-
let trait_ref = trait_ref.to_poly_trait_ref();
130+
let trait_ref = ty::Binder(ty::TraitRef {
131+
def_id: trait_def_id,
132+
substs: Substs::identity_for_item(self, trait_def_id)
133+
});
132134
let predicates = self.item_super_predicates(trait_def_id);
133135
predicates
134136
.predicates
@@ -317,8 +319,10 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
317319

318320
// Compute supertraits of current trait lazily.
319321
if supertraits.is_none() {
320-
let trait_def = self.lookup_trait_def(trait_def_id);
321-
let trait_ref = ty::Binder(trait_def.trait_ref.clone());
322+
let trait_ref = ty::Binder(ty::TraitRef {
323+
def_id: trait_def_id,
324+
substs: Substs::identity_for_item(self, trait_def_id)
325+
});
322326
supertraits = Some(traits::supertraits(self, trait_ref).collect());
323327
}
324328

src/librustc/traits/specialize/specialization_graph.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -297,18 +297,18 @@ impl<'a, 'gcx, 'tcx> Node {
297297
}
298298
}
299299

300-
pub struct Ancestors<'a, 'tcx: 'a> {
301-
trait_def: &'a TraitDef<'tcx>,
300+
pub struct Ancestors<'a> {
301+
trait_def: &'a TraitDef,
302302
current_source: Option<Node>,
303303
}
304304

305-
impl<'a, 'tcx> Iterator for Ancestors<'a, 'tcx> {
305+
impl<'a> Iterator for Ancestors<'a> {
306306
type Item = Node;
307307
fn next(&mut self) -> Option<Node> {
308308
let cur = self.current_source.take();
309309
if let Some(Node::Impl(cur_impl)) = cur {
310310
let parent = self.trait_def.specialization_graph.borrow().parent(cur_impl);
311-
if parent == self.trait_def.def_id() {
311+
if parent == self.trait_def.def_id {
312312
self.current_source = Some(Node::Trait(parent));
313313
} else {
314314
self.current_source = Some(Node::Impl(parent));
@@ -332,7 +332,7 @@ impl<T> NodeItem<T> {
332332
}
333333
}
334334

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

348348
/// Walk up the specialization ancestors of a given impl, starting with that
349349
/// impl itself.
350-
pub fn ancestors<'a, 'tcx>(trait_def: &'a TraitDef<'tcx>,
351-
start_from_impl: DefId)
352-
-> Ancestors<'a, 'tcx> {
350+
pub fn ancestors<'a>(trait_def: &'a TraitDef, start_from_impl: DefId) -> Ancestors<'a> {
353351
Ancestors {
354352
trait_def: trait_def,
355353
current_source: Some(Node::Impl(start_from_impl)),

src/librustc/ty/context.rs

+2-14
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub struct CtxtArenas<'tcx> {
6666

6767
// references
6868
generics: TypedArena<ty::Generics<'tcx>>,
69-
trait_def: TypedArena<ty::TraitDef<'tcx>>,
69+
trait_def: TypedArena<ty::TraitDef>,
7070
adt_def: TypedArena<ty::AdtDefData<'tcx, 'tcx>>,
7171
mir: TypedArena<RefCell<Mir<'tcx>>>,
7272
}
@@ -683,19 +683,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
683683
self.global_interners.arenas.mir.alloc(RefCell::new(mir))
684684
}
685685

686-
pub fn intern_trait_def(self, def: ty::TraitDef<'gcx>)
687-
-> &'gcx ty::TraitDef<'gcx> {
688-
let did = def.trait_ref.def_id;
689-
let interned = self.alloc_trait_def(def);
690-
if let Some(prev) = self.trait_defs.borrow_mut().insert(did, interned) {
691-
bug!("Tried to overwrite interned TraitDef: {:?}", prev)
692-
}
693-
self.generics.borrow_mut().insert(did, interned.generics);
694-
interned
695-
}
696-
697-
pub fn alloc_trait_def(self, def: ty::TraitDef<'gcx>)
698-
-> &'gcx ty::TraitDef<'gcx> {
686+
pub fn alloc_trait_def(self, def: ty::TraitDef) -> &'gcx ty::TraitDef {
699687
self.global_interners.arenas.trait_def.alloc(def)
700688
}
701689

src/librustc/ty/maps.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ dep_map_ty! { Predicates: ItemSignature(DefId) -> ty::GenericPredicates<'tcx> }
3939
dep_map_ty! { SuperPredicates: ItemSignature(DefId) -> ty::GenericPredicates<'tcx> }
4040
dep_map_ty! { AssociatedItemDefIds: AssociatedItemDefIds(DefId) -> Rc<Vec<DefId>> }
4141
dep_map_ty! { ImplTraitRefs: ItemSignature(DefId) -> Option<ty::TraitRef<'tcx>> }
42-
dep_map_ty! { TraitDefs: ItemSignature(DefId) -> &'tcx ty::TraitDef<'tcx> }
42+
dep_map_ty! { TraitDefs: ItemSignature(DefId) -> &'tcx ty::TraitDef }
4343
dep_map_ty! { AdtDefs: ItemSignature(DefId) -> ty::AdtDefMaster<'tcx> }
4444
dep_map_ty! { ItemVariances: ItemSignature(DefId) -> Rc<Vec<ty::Variance>> }
4545
dep_map_ty! { InherentImpls: InherentImpls(DefId) -> Vec<DefId> }

src/librustc/ty/mod.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -629,10 +629,6 @@ pub struct RegionParameterDef<'tcx> {
629629
}
630630

631631
impl<'tcx> RegionParameterDef<'tcx> {
632-
pub fn to_early_bound_region(&self) -> ty::Region {
633-
ty::ReEarlyBound(self.to_early_bound_region_data())
634-
}
635-
636632
pub fn to_early_bound_region_data(&self) -> ty::EarlyBoundRegion {
637633
ty::EarlyBoundRegion {
638634
index: self.index,
@@ -2400,7 +2396,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
24002396
}
24012397

24022398
/// Given the did of a trait, returns its canonical trait ref.
2403-
pub fn lookup_trait_def(self, did: DefId) -> &'gcx TraitDef<'gcx> {
2399+
pub fn lookup_trait_def(self, did: DefId) -> &'gcx TraitDef {
24042400
lookup_locally_or_in_crate_store(
24052401
"trait_defs", did, &self.trait_defs,
24062402
|| self.alloc_trait_def(self.sess.cstore.trait_def(self.global_tcx(), did))

src/librustc/ty/subst.rs

+8
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,14 @@ impl<'tcx> Decodable for Kind<'tcx> {
165165
pub type Substs<'tcx> = Slice<Kind<'tcx>>;
166166

167167
impl<'a, 'gcx, 'tcx> Substs<'tcx> {
168+
/// Creates a Substs that maps each generic parameter to itself.
169+
pub fn identity_for_item(tcx: TyCtxt<'a, 'gcx, 'tcx>, def_id: DefId)
170+
-> &'tcx Substs<'tcx> {
171+
Substs::for_item(tcx, def_id, |def, _| {
172+
tcx.mk_region(ty::ReEarlyBound(def.to_early_bound_region_data()))
173+
}, |def, _| tcx.mk_param_from_def(def))
174+
}
175+
168176
/// Creates a Substs for generic parameter definitions,
169177
/// by calling closures to obtain each region and type.
170178
/// The closures get to observe the Substs as they're

src/librustc/ty/trait_def.rs

+13-26
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ use hir;
1919
use util::nodemap::FxHashMap;
2020

2121
/// A trait's definition with type information.
22-
pub struct TraitDef<'tcx> {
22+
pub struct TraitDef {
23+
pub def_id: DefId,
24+
2325
pub unsafety: hir::Unsafety,
2426

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

31-
/// Generic type definitions. Note that `Self` is listed in here
32-
/// as having a single bound, the trait itself (e.g., in the trait
33-
/// `Eq`, there is a single bound `Self : Eq`). This is so that
34-
/// default methods get to assume that the `Self` parameters
35-
/// implements the trait.
36-
pub generics: &'tcx ty::Generics<'tcx>,
37-
38-
pub trait_ref: ty::TraitRef<'tcx>,
39-
4033
// Impls of a trait. To allow for quicker lookup, the impls are indexed by a
4134
// simplified version of their `Self` type: impls with a simplifiable `Self`
4235
// are stored in `nonblanket_impls` keyed by it, while all other impls are
@@ -72,18 +65,16 @@ pub struct TraitDef<'tcx> {
7265
pub def_path_hash: u64,
7366
}
7467

75-
impl<'a, 'gcx, 'tcx> TraitDef<'tcx> {
76-
pub fn new(unsafety: hir::Unsafety,
68+
impl<'a, 'gcx, 'tcx> TraitDef {
69+
pub fn new(def_id: DefId,
70+
unsafety: hir::Unsafety,
7771
paren_sugar: bool,
78-
generics: &'tcx ty::Generics<'tcx>,
79-
trait_ref: ty::TraitRef<'tcx>,
8072
def_path_hash: u64)
81-
-> TraitDef<'tcx> {
73+
-> TraitDef {
8274
TraitDef {
75+
def_id: def_id,
8376
paren_sugar: paren_sugar,
8477
unsafety: unsafety,
85-
generics: generics,
86-
trait_ref: trait_ref,
8778
nonblanket_impls: RefCell::new(FxHashMap()),
8879
blanket_impls: RefCell::new(vec![]),
8980
flags: Cell::new(ty::TraitFlags::NO_TRAIT_FLAGS),
@@ -92,10 +83,6 @@ impl<'a, 'gcx, 'tcx> TraitDef<'tcx> {
9283
}
9384
}
9485

95-
pub fn def_id(&self) -> DefId {
96-
self.trait_ref.def_id
97-
}
98-
9986
// returns None if not yet calculated
10087
pub fn object_safety(&self) -> Option<bool> {
10188
if self.flags.get().intersects(TraitFlags::OBJECT_SAFETY_VALID) {
@@ -117,11 +104,11 @@ impl<'a, 'gcx, 'tcx> TraitDef<'tcx> {
117104
}
118105

119106
fn write_trait_impls(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) {
120-
tcx.dep_graph.write(DepNode::TraitImpls(self.trait_ref.def_id));
107+
tcx.dep_graph.write(DepNode::TraitImpls(self.def_id));
121108
}
122109

123110
fn read_trait_impls(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) {
124-
tcx.dep_graph.read(DepNode::TraitImpls(self.trait_ref.def_id));
111+
tcx.dep_graph.read(DepNode::TraitImpls(self.def_id));
125112
}
126113

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

206-
pub fn ancestors(&'a self, of_impl: DefId) -> specialization_graph::Ancestors<'a, 'tcx> {
193+
pub fn ancestors(&'a self, of_impl: DefId) -> specialization_graph::Ancestors<'a> {
207194
specialization_graph::ancestors(self, of_impl)
208195
}
209196

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

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

234-
tcx.populate_implementations_for_trait_if_necessary(self.trait_ref.def_id);
221+
tcx.populate_implementations_for_trait_if_necessary(self.def_id);
235222

236223
for &impl_def_id in self.blanket_impls.borrow().iter() {
237224
f(impl_def_id);

src/librustc/util/ppaux.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -432,10 +432,11 @@ impl<'tcx> fmt::Debug for ty::ExistentialTraitRef<'tcx> {
432432
}
433433
}
434434

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

src/librustc_metadata/cstore_impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
110110
self.get_crate_data(def_id.krate).get_item_attrs(def_id.index)
111111
}
112112

113-
fn trait_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) -> ty::TraitDef<'tcx>
113+
fn trait_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) -> ty::TraitDef
114114
{
115115
self.dep_graph.read(DepNode::MetaData(def));
116116
self.get_crate_data(def.krate).get_trait_def(def.index, tcx)

src/librustc_metadata/decoder.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -531,16 +531,15 @@ impl<'a, 'tcx> CrateMetadata {
531531
pub fn get_trait_def(&self,
532532
item_id: DefIndex,
533533
tcx: TyCtxt<'a, 'tcx, 'tcx>)
534-
-> ty::TraitDef<'tcx> {
534+
-> ty::TraitDef {
535535
let data = match self.entry(item_id).kind {
536536
EntryKind::Trait(data) => data.decode(self),
537537
_ => bug!(),
538538
};
539539

540-
ty::TraitDef::new(data.unsafety,
540+
ty::TraitDef::new(self.local_def_id(item_id),
541+
data.unsafety,
541542
data.paren_sugar,
542-
tcx.item_generics(self.local_def_id(item_id)),
543-
data.trait_ref.decode((self, tcx)),
544543
self.def_path(item_id).unwrap().deterministic_hash(tcx))
545544
}
546545

src/librustc_metadata/encoder.rs

-1
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
730730
unsafety: trait_def.unsafety,
731731
paren_sugar: trait_def.paren_sugar,
732732
has_default_impl: tcx.trait_has_default_impl(def_id),
733-
trait_ref: self.lazy(&trait_def.trait_ref),
734733
super_predicates: self.lazy(&tcx.item_super_predicates(def_id)),
735734
};
736735

src/librustc_metadata/schema.rs

-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,6 @@ pub struct TraitData<'tcx> {
278278
pub unsafety: hir::Unsafety,
279279
pub paren_sugar: bool,
280280
pub has_default_impl: bool,
281-
pub trait_ref: Lazy<ty::TraitRef<'tcx>>,
282281
pub super_predicates: Lazy<ty::GenericPredicates<'tcx>>,
283282
}
284283

src/librustc_typeck/astconv.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ pub trait AstConv<'gcx, 'tcx> {
9191
/// Returns the `TraitDef` for a given trait. This allows you to
9292
/// figure out the set of type parameters defined on the trait.
9393
fn get_trait_def(&self, span: Span, id: DefId)
94-
-> Result<&'tcx ty::TraitDef<'tcx>, ErrorReported>;
94+
-> Result<&'tcx ty::TraitDef, ErrorReported>;
9595

9696
/// Ensure that the super-predicates for the trait with the given
9797
/// id are available and also for the transitive set of

src/librustc_typeck/check/method/mod.rs

-7
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,6 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
192192
m_name,
193193
trait_def_id);
194194

195-
let trait_def = self.tcx.lookup_trait_def(trait_def_id);
196-
197-
if let Some(ref input_types) = opt_input_types {
198-
assert_eq!(trait_def.generics.types.len() - 1, input_types.len());
199-
}
200-
assert!(trait_def.generics.regions.is_empty());
201-
202195
// Construct a trait-reference `self_ty : Trait<input_tys>`
203196
let substs = Substs::for_item(self.tcx,
204197
trait_def_id,

src/librustc_typeck/check/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,7 @@ fn report_forbidden_specialization<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
10401040
}
10411041

10421042
fn check_specialization_validity<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
1043-
trait_def: &ty::TraitDef<'tcx>,
1043+
trait_def: &ty::TraitDef,
10441044
impl_id: DefId,
10451045
impl_item: &hir::ImplItem)
10461046
{
@@ -1400,7 +1400,7 @@ impl<'a, 'gcx, 'tcx> AstConv<'gcx, 'tcx> for FnCtxt<'a, 'gcx, 'tcx> {
14001400
}
14011401

14021402
fn get_trait_def(&self, _: Span, id: DefId)
1403-
-> Result<&'tcx ty::TraitDef<'tcx>, ErrorReported>
1403+
-> Result<&'tcx ty::TraitDef, ErrorReported>
14041404
{
14051405
Ok(self.tcx().lookup_trait_def(id))
14061406
}

0 commit comments

Comments
 (0)