Skip to content

Commit e8d01ea

Browse files
committed
rustc: store type parameter defaults outside of ty::Generics.
1 parent 1572bf1 commit e8d01ea

34 files changed

+394
-385
lines changed

src/librustc/hir/lowering.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ impl<'a> LoweringContext<'a> {
525525
return n;
526526
}
527527
assert!(!def_id.is_local());
528-
let (n, _) = self.sess.cstore.item_generics_own_param_counts(def_id);
528+
let n = self.sess.cstore.item_generics(def_id).regions.len();
529529
self.type_def_lifetime_params.insert(def_id, n);
530530
n
531531
});

src/librustc/infer/mod.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,16 +1197,19 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
11971197
/// as the substitutions for the default, `(T, U)`.
11981198
pub fn type_var_for_def(&self,
11991199
span: Span,
1200-
def: &ty::TypeParameterDef<'tcx>,
1200+
def: &ty::TypeParameterDef,
12011201
substs: &[Kind<'tcx>])
12021202
-> Ty<'tcx> {
1203-
let default = def.default.map(|default| {
1204-
type_variable::Default {
1203+
let default = if def.has_default {
1204+
let default = self.tcx.item_type(def.def_id);
1205+
Some(type_variable::Default {
12051206
ty: default.subst_spanned(self.tcx, substs, Some(span)),
12061207
origin_span: span,
1207-
def_id: def.default_def_id
1208-
}
1209-
});
1208+
def_id: def.def_id
1209+
})
1210+
} else {
1211+
None
1212+
};
12101213

12111214

12121215
let ty_var_id = self.type_variables

src/librustc/middle/cstore.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ use hir::map as hir_map;
2828
use hir::map::definitions::{Definitions, DefKey, DisambiguatedDefPathData};
2929
use hir::svh::Svh;
3030
use middle::lang_items;
31-
use middle::resolve_lifetime::ObjectLifetimeDefault;
3231
use ty::{self, Ty, TyCtxt};
3332
use mir::Mir;
3433
use session::Session;
@@ -182,11 +181,7 @@ pub trait CrateStore<'tcx> {
182181
-> ty::GenericPredicates<'tcx>;
183182
fn item_super_predicates<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
184183
-> ty::GenericPredicates<'tcx>;
185-
fn item_generics<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
186-
-> ty::Generics<'tcx>;
187-
fn item_generics_own_param_counts(&self, def: DefId) -> (usize, usize);
188-
fn item_generics_object_lifetime_defaults(&self, def: DefId)
189-
-> Vec<ObjectLifetimeDefault>;
184+
fn item_generics(&self, def: DefId) -> ty::Generics;
190185
fn item_attrs(&self, def_id: DefId) -> Vec<ast::Attribute>;
191186
fn trait_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)-> ty::TraitDef;
192187
fn adt_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) -> &'tcx ty::AdtDef;
@@ -335,13 +330,7 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
335330
-> ty::GenericPredicates<'tcx> { bug!("item_predicates") }
336331
fn item_super_predicates<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
337332
-> ty::GenericPredicates<'tcx> { bug!("item_super_predicates") }
338-
fn item_generics<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
339-
-> ty::Generics<'tcx> { bug!("item_generics") }
340-
fn item_generics_own_param_counts(&self, def: DefId) -> (usize, usize)
341-
{ bug!("item_generics_own_param_counts") }
342-
fn item_generics_object_lifetime_defaults(&self, def: DefId)
343-
-> Vec<ObjectLifetimeDefault>
344-
{ bug!("item_generics_object_lifetime_defaults") }
333+
fn item_generics(&self, def: DefId) -> ty::Generics { bug!("item_generics") }
345334
fn item_attrs(&self, def_id: DefId) -> Vec<ast::Attribute> { bug!("item_attrs") }
346335
fn trait_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)-> ty::TraitDef
347336
{ bug!("trait_def") }

src/librustc/middle/resolve_lifetime.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,9 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
995995
} else {
996996
let cstore = &self.sess.cstore;
997997
self.xcrate_object_lifetime_defaults.entry(def_id).or_insert_with(|| {
998-
cstore.item_generics_object_lifetime_defaults(def_id)
998+
cstore.item_generics(def_id).types.into_iter().map(|def| {
999+
def.object_lifetime_default
1000+
}).collect()
9991001
})
10001002
};
10011003
unsubst.iter().map(|set| {

src/librustc/ty/context.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub struct GlobalArenas<'tcx> {
6464
layout: TypedArena<Layout>,
6565

6666
// references
67-
generics: TypedArena<ty::Generics<'tcx>>,
67+
generics: TypedArena<ty::Generics>,
6868
trait_def: TypedArena<ty::TraitDef>,
6969
adt_def: TypedArena<ty::AdtDef>,
7070
mir: TypedArena<RefCell<Mir<'tcx>>>,
@@ -467,9 +467,6 @@ pub struct GlobalCtxt<'tcx> {
467467
// Cache for the type-contents routine. FIXME -- track deps?
468468
pub tc_cache: RefCell<FxHashMap<Ty<'tcx>, ty::contents::TypeContents>>,
469469

470-
// FIXME no dep tracking, but we should be able to remove this
471-
pub ty_param_defs: RefCell<NodeMap<ty::TypeParameterDef<'tcx>>>,
472-
473470
// FIXME dep tracking -- should be harmless enough
474471
pub normalized_cache: RefCell<FxHashMap<Ty<'tcx>, Ty<'tcx>>>,
475472

@@ -646,15 +643,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
646643
}
647644
}
648645

649-
pub fn type_parameter_def(self,
650-
node_id: NodeId)
651-
-> ty::TypeParameterDef<'tcx>
652-
{
653-
self.ty_param_defs.borrow().get(&node_id).unwrap().clone()
654-
}
655-
656-
pub fn alloc_generics(self, generics: ty::Generics<'gcx>)
657-
-> &'gcx ty::Generics<'gcx> {
646+
pub fn alloc_generics(self, generics: ty::Generics) -> &'gcx ty::Generics {
658647
self.global_arenas.generics.alloc(generics)
659648
}
660649

@@ -785,7 +774,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
785774
tc_cache: RefCell::new(FxHashMap()),
786775
associated_items: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
787776
associated_item_def_ids: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
788-
ty_param_defs: RefCell::new(NodeMap()),
789777
normalized_cache: RefCell::new(FxHashMap()),
790778
inhabitedness_cache: RefCell::new(FxHashMap()),
791779
lang_items: lang_items,

src/librustc/ty/error.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use hir::def_id::DefId;
1212
use infer::type_variable;
13-
use ty::{self, BoundRegion, Region, Ty, TyCtxt};
13+
use ty::{self, BoundRegion, DefIdTree, Region, Ty, TyCtxt};
1414

1515
use std::fmt;
1616
use syntax::abi;
@@ -287,8 +287,9 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
287287
db.span_note(span, "a default was defined here...");
288288
}
289289
None => {
290+
let item_def_id = self.parent(expected.def_id).unwrap();
290291
db.note(&format!("a default is defined on `{}`",
291-
self.item_path_str(expected.def_id)));
292+
self.item_path_str(item_def_id)));
292293
}
293294
}
294295

@@ -301,8 +302,9 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
301302
db.span_note(span, "a second default was defined here...");
302303
}
303304
None => {
305+
let item_def_id = self.parent(found.def_id).unwrap();
304306
db.note(&format!("a second default is defined on `{}`",
305-
self.item_path_str(found.def_id)));
307+
self.item_path_str(item_def_id)));
306308
}
307309
}
308310

src/librustc/ty/maps.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ macro_rules! dep_map_ty {
3535

3636
dep_map_ty! { AssociatedItems: AssociatedItems(DefId) -> ty::AssociatedItem }
3737
dep_map_ty! { Types: ItemSignature(DefId) -> Ty<'tcx> }
38-
dep_map_ty! { Generics: ItemSignature(DefId) -> &'tcx ty::Generics<'tcx> }
38+
dep_map_ty! { Generics: ItemSignature(DefId) -> &'tcx ty::Generics }
3939
dep_map_ty! { Predicates: ItemSignature(DefId) -> ty::GenericPredicates<'tcx> }
4040
dep_map_ty! { SuperPredicates: ItemSignature(DefId) -> ty::GenericPredicates<'tcx> }
4141
dep_map_ty! { AssociatedItemDefIds: AssociatedItemDefIds(DefId) -> Rc<Vec<DefId>> }

src/librustc/ty/mod.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ use dep_graph::{self, DepNode};
1919
use hir::{map as hir_map, FreevarMap, TraitMap};
2020
use middle;
2121
use hir::def::{Def, CtorKind, ExportMap};
22-
use hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
22+
use hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX, LOCAL_CRATE};
2323
use middle::lang_items::{FnTraitLangItem, FnMutTraitLangItem, FnOnceTraitLangItem};
2424
use middle::region::{CodeExtent, ROOT_CODE_EXTENT};
25+
use middle::resolve_lifetime::ObjectLifetimeDefault;
2526
use mir::Mir;
2627
use traits;
2728
use ty;
@@ -33,6 +34,7 @@ use util::nodemap::{NodeSet, NodeMap, FxHashMap};
3334
use serialize::{self, Encodable, Encoder};
3435
use std::borrow::Cow;
3536
use std::cell::{Cell, RefCell, Ref};
37+
use std::collections::BTreeMap;
3638
use std::hash::{Hash, Hasher};
3739
use std::ops::Deref;
3840
use std::rc::Rc;
@@ -585,13 +587,13 @@ pub enum IntVarValue {
585587
UintType(ast::UintTy),
586588
}
587589

588-
#[derive(Clone, RustcEncodable, RustcDecodable)]
589-
pub struct TypeParameterDef<'tcx> {
590+
#[derive(Copy, Clone, RustcEncodable, RustcDecodable)]
591+
pub struct TypeParameterDef {
590592
pub name: Name,
591593
pub def_id: DefId,
592594
pub index: u32,
593-
pub default_def_id: DefId, // for use in error reporing about defaults
594-
pub default: Option<Ty<'tcx>>,
595+
pub has_default: bool,
596+
pub object_lifetime_default: ObjectLifetimeDefault,
595597

596598
/// `pure_wrt_drop`, set by the (unsafe) `#[may_dangle]` attribute
597599
/// on generic parameter `T`, asserts data behind the parameter
@@ -628,16 +630,21 @@ impl RegionParameterDef {
628630
/// Information about the formal type/lifetime parameters associated
629631
/// with an item or method. Analogous to hir::Generics.
630632
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
631-
pub struct Generics<'tcx> {
633+
pub struct Generics {
632634
pub parent: Option<DefId>,
633635
pub parent_regions: u32,
634636
pub parent_types: u32,
635637
pub regions: Vec<RegionParameterDef>,
636-
pub types: Vec<TypeParameterDef<'tcx>>,
638+
pub types: Vec<TypeParameterDef>,
639+
640+
/// Reverse map to each `TypeParameterDef`'s `index` field, from
641+
/// `def_id.index` (`def_id.krate` is the same as the item's).
642+
pub type_param_to_index: BTreeMap<DefIndex, u32>,
643+
637644
pub has_self: bool,
638645
}
639646

640-
impl<'tcx> Generics<'tcx> {
647+
impl Generics {
641648
pub fn parent_count(&self) -> usize {
642649
self.parent_regions as usize + self.parent_types as usize
643650
}
@@ -651,10 +658,12 @@ impl<'tcx> Generics<'tcx> {
651658
}
652659

653660
pub fn region_param(&self, param: &EarlyBoundRegion) -> &RegionParameterDef {
661+
assert_eq!(self.parent_count(), 0);
654662
&self.regions[param.index as usize - self.has_self as usize]
655663
}
656664

657-
pub fn type_param(&self, param: &ParamTy) -> &TypeParameterDef<'tcx> {
665+
pub fn type_param(&self, param: &ParamTy) -> &TypeParameterDef {
666+
assert_eq!(self.parent_count(), 0);
658667
&self.types[param.idx as usize - self.has_self as usize - self.regions.len()]
659668
}
660669
}
@@ -2319,10 +2328,10 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
23192328
}
23202329

23212330
/// Given the did of an item, returns its generics.
2322-
pub fn item_generics(self, did: DefId) -> &'gcx Generics<'gcx> {
2331+
pub fn item_generics(self, did: DefId) -> &'gcx Generics {
23232332
lookup_locally_or_in_crate_store(
23242333
"generics", did, &self.generics,
2325-
|| self.alloc_generics(self.sess.cstore.item_generics(self.global_tcx(), did)))
2334+
|| self.alloc_generics(self.sess.cstore.item_generics(did)))
23262335
}
23272336

23282337
/// Given the did of an item, returns its full set of predicates.

src/librustc/ty/structural_impls.rs

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ macro_rules! CopyImpls {
353353
}
354354
}
355355

356-
CopyImpls! { (), hir::Unsafety, abi::Abi, ty::RegionParameterDef }
356+
CopyImpls! { (), hir::Unsafety, abi::Abi }
357357

358358
impl<'tcx, T:TypeFoldable<'tcx>, U:TypeFoldable<'tcx>> TypeFoldable<'tcx> for (T, U) {
359359
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> (T, U) {
@@ -716,40 +716,6 @@ impl<'tcx> TypeFoldable<'tcx> for ty::adjustment::AutoBorrow<'tcx> {
716716
}
717717
}
718718

719-
impl<'tcx> TypeFoldable<'tcx> for ty::TypeParameterDef<'tcx> {
720-
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
721-
ty::TypeParameterDef {
722-
name: self.name,
723-
def_id: self.def_id,
724-
index: self.index,
725-
default: self.default.fold_with(folder),
726-
default_def_id: self.default_def_id,
727-
pure_wrt_drop: self.pure_wrt_drop,
728-
}
729-
}
730-
731-
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
732-
self.default.visit_with(visitor)
733-
}
734-
}
735-
736-
impl<'tcx> TypeFoldable<'tcx> for ty::Generics<'tcx> {
737-
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
738-
ty::Generics {
739-
parent: self.parent,
740-
parent_regions: self.parent_regions,
741-
parent_types: self.parent_types,
742-
regions: self.regions.fold_with(folder),
743-
types: self.types.fold_with(folder),
744-
has_self: self.has_self,
745-
}
746-
}
747-
748-
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
749-
self.regions.visit_with(visitor) || self.types.visit_with(visitor)
750-
}
751-
}
752-
753719
impl<'tcx> TypeFoldable<'tcx> for ty::GenericPredicates<'tcx> {
754720
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
755721
ty::GenericPredicates {

src/librustc/ty/subst.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ impl<'a, 'gcx, 'tcx> Substs<'tcx> {
184184
mut mk_type: FT)
185185
-> &'tcx Substs<'tcx>
186186
where FR: FnMut(&ty::RegionParameterDef, &[Kind<'tcx>]) -> &'tcx ty::Region,
187-
FT: FnMut(&ty::TypeParameterDef<'tcx>, &[Kind<'tcx>]) -> Ty<'tcx> {
187+
FT: FnMut(&ty::TypeParameterDef, &[Kind<'tcx>]) -> Ty<'tcx> {
188188
let defs = tcx.item_generics(def_id);
189189
let mut substs = Vec::with_capacity(defs.count());
190190
Substs::fill_item(&mut substs, tcx, defs, &mut mk_region, &mut mk_type);
@@ -198,7 +198,7 @@ impl<'a, 'gcx, 'tcx> Substs<'tcx> {
198198
mut mk_type: FT)
199199
-> &'tcx Substs<'tcx>
200200
where FR: FnMut(&ty::RegionParameterDef, &[Kind<'tcx>]) -> &'tcx ty::Region,
201-
FT: FnMut(&ty::TypeParameterDef<'tcx>, &[Kind<'tcx>]) -> Ty<'tcx>
201+
FT: FnMut(&ty::TypeParameterDef, &[Kind<'tcx>]) -> Ty<'tcx>
202202
{
203203
let defs = tcx.item_generics(def_id);
204204
let mut result = Vec::with_capacity(defs.count());
@@ -209,11 +209,11 @@ impl<'a, 'gcx, 'tcx> Substs<'tcx> {
209209

210210
fn fill_item<FR, FT>(substs: &mut Vec<Kind<'tcx>>,
211211
tcx: TyCtxt<'a, 'gcx, 'tcx>,
212-
defs: &ty::Generics<'tcx>,
212+
defs: &ty::Generics,
213213
mk_region: &mut FR,
214214
mk_type: &mut FT)
215215
where FR: FnMut(&ty::RegionParameterDef, &[Kind<'tcx>]) -> &'tcx ty::Region,
216-
FT: FnMut(&ty::TypeParameterDef<'tcx>, &[Kind<'tcx>]) -> Ty<'tcx> {
216+
FT: FnMut(&ty::TypeParameterDef, &[Kind<'tcx>]) -> Ty<'tcx> {
217217

218218
if let Some(def_id) = defs.parent {
219219
let parent_defs = tcx.item_generics(def_id);
@@ -223,11 +223,11 @@ impl<'a, 'gcx, 'tcx> Substs<'tcx> {
223223
}
224224

225225
fn fill_single<FR, FT>(substs: &mut Vec<Kind<'tcx>>,
226-
defs: &ty::Generics<'tcx>,
226+
defs: &ty::Generics,
227227
mk_region: &mut FR,
228228
mk_type: &mut FT)
229229
where FR: FnMut(&ty::RegionParameterDef, &[Kind<'tcx>]) -> &'tcx ty::Region,
230-
FT: FnMut(&ty::TypeParameterDef<'tcx>, &[Kind<'tcx>]) -> Ty<'tcx> {
230+
FT: FnMut(&ty::TypeParameterDef, &[Kind<'tcx>]) -> Ty<'tcx> {
231231
// Handle Self first, before all regions.
232232
let mut types = defs.types.iter();
233233
if defs.parent.is_none() && defs.has_self {
@@ -301,7 +301,7 @@ impl<'a, 'gcx, 'tcx> Substs<'tcx> {
301301
tcx.mk_substs(target_substs.iter().chain(&self[defs.own_count()..]).cloned())
302302
}
303303

304-
pub fn truncate_to(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>, generics: &ty::Generics<'tcx>)
304+
pub fn truncate_to(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>, generics: &ty::Generics)
305305
-> &'tcx Substs<'tcx> {
306306
tcx.mk_substs(self.iter().take(generics.count()).cloned())
307307
}

src/librustc/util/ppaux.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,14 @@ pub fn parameterized(f: &mut fmt::Formatter,
137137
}
138138

139139
if !verbose {
140-
if generics.types.last().map_or(false, |def| def.default.is_some()) {
140+
if generics.types.last().map_or(false, |def| def.has_default) {
141141
if let Some(substs) = tcx.lift(&substs) {
142142
let tps = substs.types().rev().skip(child_types);
143143
for (def, actual) in generics.types.iter().rev().zip(tps) {
144-
if def.default.subst(tcx, substs) != Some(actual) {
144+
if !def.has_default {
145+
break;
146+
}
147+
if tcx.item_type(def.def_id).subst(tcx, substs) != actual {
145148
break;
146149
}
147150
num_supplied_defaults += 1;
@@ -326,7 +329,7 @@ impl<'tcx> fmt::Display for &'tcx ty::Slice<ty::ExistentialPredicate<'tcx>> {
326329
}
327330
}
328331

329-
impl<'tcx> fmt::Debug for ty::TypeParameterDef<'tcx> {
332+
impl fmt::Debug for ty::TypeParameterDef {
330333
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
331334
write!(f, "TypeParameterDef({}, {:?}, {})",
332335
self.name,

0 commit comments

Comments
 (0)