Skip to content

Commit 0f8f259

Browse files
committed
rustc: remove id's from Ty's in favor of pointer identity.
1 parent 3b1ee6a commit 0f8f259

File tree

4 files changed

+68
-90
lines changed

4 files changed

+68
-90
lines changed

src/librustc/middle/trans/context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ pub struct LocalCrateContext<'tcx> {
140140
/// Holds the LLVM values for closure IDs.
141141
unboxed_closure_vals: RefCell<HashMap<MonoId<'tcx>, ValueRef>>,
142142

143-
dbg_cx: Option<debuginfo::CrateDebugContext>,
143+
dbg_cx: Option<debuginfo::CrateDebugContext<'tcx>>,
144144

145145
eh_personality: RefCell<Option<ValueRef>>,
146146

@@ -694,7 +694,7 @@ impl<'b, 'tcx> CrateContext<'b, 'tcx> {
694694
&self.local.unboxed_closure_vals
695695
}
696696

697-
pub fn dbg_cx<'a>(&'a self) -> &'a Option<debuginfo::CrateDebugContext> {
697+
pub fn dbg_cx<'a>(&'a self) -> &'a Option<debuginfo::CrateDebugContext<'tcx>> {
698698
&self.local.dbg_cx
699699
}
700700

src/librustc/middle/trans/debuginfo.rs

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,11 @@ debuginfo, more than one `Ty` instance may map to the same debuginfo type
176176
metadata, that is, some struct `Struct<'a>` may have N instantiations with
177177
different concrete substitutions for `'a`, and thus there will be N `Ty`
178178
instances for the type `Struct<'a>` even though it is not generic otherwise.
179-
Unfortunately this means that we cannot use `ty::type_id()` as cheap identifier
179+
Unfortunately this means that we cannot use `&TyS` as cheap identifier
180180
for type metadata---we have done this in the past, but it led to unnecessary
181181
metadata duplication in the best case and LLVM assertions in the worst. However,
182182
the unique type ID as described above *can* be used as identifier. Since it is
183-
comparatively expensive to construct, though, `ty::type_id()` is still used
183+
comparatively expensive to construct, though, `&TyS` is still used
184184
additionally as an optimization for cases where the exact same type has been
185185
seen before (which is most of the time). */
186186

@@ -254,20 +254,20 @@ struct UniqueTypeId(ast::Name);
254254
// created so far. The metadata nodes are indexed by UniqueTypeId, and, for
255255
// faster lookup, also by Ty. The TypeMap is responsible for creating
256256
// UniqueTypeIds.
257-
struct TypeMap {
257+
struct TypeMap<'tcx> {
258258
// The UniqueTypeIds created so far
259259
unique_id_interner: Interner<Rc<String>>,
260260
// A map from UniqueTypeId to debuginfo metadata for that type. This is a 1:1 mapping.
261261
unique_id_to_metadata: HashMap<UniqueTypeId, DIType>,
262-
// A map from ty::type_id() to debuginfo metadata. This is a N:1 mapping.
263-
type_to_metadata: HashMap<uint, DIType>,
264-
// A map from ty::type_id() to UniqueTypeId. This is a N:1 mapping.
265-
type_to_unique_id: HashMap<uint, UniqueTypeId>
262+
// A map from types to debuginfo metadata. This is a N:1 mapping.
263+
type_to_metadata: HashMap<Ty<'tcx>, DIType>,
264+
// A map from types to UniqueTypeId. This is a N:1 mapping.
265+
type_to_unique_id: HashMap<Ty<'tcx>, UniqueTypeId>
266266
}
267267

268-
impl TypeMap {
268+
impl<'tcx> TypeMap<'tcx> {
269269

270-
fn new() -> TypeMap {
270+
fn new() -> TypeMap<'tcx> {
271271
TypeMap {
272272
unique_id_interner: Interner::new(),
273273
type_to_metadata: HashMap::new(),
@@ -278,11 +278,11 @@ impl TypeMap {
278278

279279
// Adds a Ty to metadata mapping to the TypeMap. The method will fail if
280280
// the mapping already exists.
281-
fn register_type_with_metadata<'a, 'tcx>(&mut self,
282-
cx: &CrateContext<'a, 'tcx>,
283-
type_: Ty<'tcx>,
284-
metadata: DIType) {
285-
if !self.type_to_metadata.insert(ty::type_id(type_), metadata) {
281+
fn register_type_with_metadata<'a>(&mut self,
282+
cx: &CrateContext<'a, 'tcx>,
283+
type_: Ty<'tcx>,
284+
metadata: DIType) {
285+
if !self.type_to_metadata.insert(type_, metadata) {
286286
cx.sess().bug(format!("Type metadata for Ty '{}' is already in the TypeMap!",
287287
ppaux::ty_to_string(cx.tcx(), type_)).as_slice());
288288
}
@@ -301,8 +301,8 @@ impl TypeMap {
301301
}
302302
}
303303

304-
fn find_metadata_for_type(&self, type_: Ty) -> Option<DIType> {
305-
self.type_to_metadata.find_copy(&ty::type_id(type_))
304+
fn find_metadata_for_type(&self, type_: Ty<'tcx>) -> Option<DIType> {
305+
self.type_to_metadata.find_copy(&type_)
306306
}
307307

308308
fn find_metadata_for_unique_id(&self, unique_type_id: UniqueTypeId) -> Option<DIType> {
@@ -319,8 +319,8 @@ impl TypeMap {
319319
// Get the UniqueTypeId for the given type. If the UniqueTypeId for the given
320320
// type has been requested before, this is just a table lookup. Otherwise an
321321
// ID will be generated and stored for later lookup.
322-
fn get_unique_type_id_of_type<'a, 'tcx>(&mut self, cx: &CrateContext<'a, 'tcx>,
323-
type_: Ty<'tcx>) -> UniqueTypeId {
322+
fn get_unique_type_id_of_type<'a>(&mut self, cx: &CrateContext<'a, 'tcx>,
323+
type_: Ty<'tcx>) -> UniqueTypeId {
324324

325325
// basic type -> {:name of the type:}
326326
// tuple -> {tuple_(:param-uid:)*}
@@ -343,7 +343,7 @@ impl TypeMap {
343343
// unique vec box (~[]) -> {HEAP_VEC_BOX<:pointee-uid:>}
344344
// gc box -> {GC_BOX<:pointee-uid:>}
345345

346-
match self.type_to_unique_id.find_copy(&ty::type_id(type_)) {
346+
match self.type_to_unique_id.find_copy(&type_) {
347347
Some(unique_type_id) => return unique_type_id,
348348
None => { /* generate one */}
349349
};
@@ -487,11 +487,11 @@ impl TypeMap {
487487
unique_type_id.shrink_to_fit();
488488

489489
let key = self.unique_id_interner.intern(Rc::new(unique_type_id));
490-
self.type_to_unique_id.insert(ty::type_id(type_), UniqueTypeId(key));
490+
self.type_to_unique_id.insert(type_, UniqueTypeId(key));
491491

492492
return UniqueTypeId(key);
493493

494-
fn from_def_id_and_substs<'a, 'tcx>(type_map: &mut TypeMap,
494+
fn from_def_id_and_substs<'a, 'tcx>(type_map: &mut TypeMap<'tcx>,
495495
cx: &CrateContext<'a, 'tcx>,
496496
def_id: ast::DefId,
497497
substs: &subst::Substs<'tcx>,
@@ -542,10 +542,10 @@ impl TypeMap {
542542
}
543543
}
544544

545-
fn get_unique_type_id_of_closure_type<'a, 'tcx>(&mut self,
546-
cx: &CrateContext<'a, 'tcx>,
547-
closure_ty: ty::ClosureTy<'tcx>,
548-
unique_type_id: &mut String) {
545+
fn get_unique_type_id_of_closure_type<'a>(&mut self,
546+
cx: &CrateContext<'a, 'tcx>,
547+
closure_ty: ty::ClosureTy<'tcx>,
548+
unique_type_id: &mut String) {
549549
let ty::ClosureTy { fn_style,
550550
onceness,
551551
store,
@@ -612,11 +612,11 @@ impl TypeMap {
612612
// Get the UniqueTypeId for an enum variant. Enum variants are not really
613613
// types of their own, so they need special handling. We still need a
614614
// UniqueTypeId for them, since to debuginfo they *are* real types.
615-
fn get_unique_type_id_of_enum_variant<'a, 'tcx>(&mut self,
616-
cx: &CrateContext<'a, 'tcx>,
617-
enum_type: Ty<'tcx>,
618-
variant_name: &str)
619-
-> UniqueTypeId {
615+
fn get_unique_type_id_of_enum_variant<'a>(&mut self,
616+
cx: &CrateContext<'a, 'tcx>,
617+
enum_type: Ty<'tcx>,
618+
variant_name: &str)
619+
-> UniqueTypeId {
620620
let enum_type_id = self.get_unique_type_id_of_type(cx, enum_type);
621621
let enum_variant_type_id = format!("{}::{}",
622622
self.get_unique_type_id_as_string(enum_type_id)
@@ -642,23 +642,23 @@ macro_rules! return_if_metadata_created_in_meantime(
642642

643643

644644
/// A context object for maintaining all state needed by the debuginfo module.
645-
pub struct CrateDebugContext {
645+
pub struct CrateDebugContext<'tcx> {
646646
llcontext: ContextRef,
647647
builder: DIBuilderRef,
648648
current_debug_location: Cell<DebugLocation>,
649649
created_files: RefCell<HashMap<String, DIFile>>,
650650
created_enum_disr_types: RefCell<HashMap<ast::DefId, DIType>>,
651651

652-
type_map: RefCell<TypeMap>,
652+
type_map: RefCell<TypeMap<'tcx>>,
653653
namespace_map: RefCell<HashMap<Vec<ast::Name>, Rc<NamespaceTreeNode>>>,
654654

655655
// This collection is used to assert that composite types (structs, enums,
656656
// ...) have their members only set once:
657657
composite_types_completed: RefCell<HashSet<DIType>>,
658658
}
659659

660-
impl CrateDebugContext {
661-
pub fn new(llmod: ModuleRef) -> CrateDebugContext {
660+
impl<'tcx> CrateDebugContext<'tcx> {
661+
pub fn new(llmod: ModuleRef) -> CrateDebugContext<'tcx> {
662662
debug!("CrateDebugContext::new");
663663
let builder = unsafe { llvm::LLVMDIBuilderCreate(llmod) };
664664
// DIBuilder inherits context from the module, so we'd better use the same one
@@ -3108,9 +3108,9 @@ fn bytes_to_bits(bytes: u64) -> u64 {
31083108
}
31093109

31103110
#[inline]
3111-
fn debug_context<'a>(cx: &'a CrateContext) -> &'a CrateDebugContext {
3112-
let debug_context: &'a CrateDebugContext = cx.dbg_cx().as_ref().unwrap();
3113-
debug_context
3111+
fn debug_context<'a, 'tcx>(cx: &'a CrateContext<'a, 'tcx>) -> &'a CrateDebugContext<'tcx> {
3112+
let dbg_cx: &'a CrateDebugContext<'tcx> = cx.dbg_cx().as_ref().unwrap();
3113+
dbg_cx
31143114
}
31153115

31163116
#[inline]

src/librustc/middle/ty.rs

Lines changed: 28 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use middle;
3232
use util::ppaux::{note_and_explain_region, bound_region_ptr_to_string};
3333
use util::ppaux::{trait_store_to_string, ty_to_string};
3434
use util::ppaux::{Repr, UserString};
35-
use util::common::{indenter, memoized, memoized_with_key};
35+
use util::common::{indenter, memoized};
3636
use util::nodemap::{NodeMap, NodeSet, DefIdMap, DefIdSet, FnvHashMap};
3737

3838
use std::cell::{Cell, RefCell};
@@ -437,7 +437,6 @@ pub struct ctxt<'tcx> {
437437
// FIXME(eddyb) use a FnvHashSet<InternedTy<'tcx>> when equivalent keys can
438438
// queried from a HashSet.
439439
interner: RefCell<FnvHashMap<InternedTy<'tcx>, Ty<'tcx>>>,
440-
pub next_id: Cell<uint>,
441440
pub sess: Session,
442441
pub def_map: resolve::DefMap,
443442

@@ -481,7 +480,7 @@ pub struct ctxt<'tcx> {
481480
pub rcache: creader_cache<'tcx>,
482481
pub short_names_cache: RefCell<HashMap<Ty<'tcx>, String>>,
483482
pub needs_unwind_cleanup_cache: RefCell<HashMap<Ty<'tcx>, bool>>,
484-
pub tc_cache: RefCell<HashMap<uint, TypeContents>>,
483+
pub tc_cache: RefCell<HashMap<Ty<'tcx>, TypeContents>>,
485484
pub ast_ty_to_ty_cache: RefCell<NodeMap<ast_ty_to_ty_cache_entry<'tcx>>>,
486485
pub enum_var_cache: RefCell<DefIdMap<Rc<Vec<Rc<VariantInfo<'tcx>>>>>>,
487486
pub ty_param_defs: RefCell<NodeMap<TypeParameterDef<'tcx>>>,
@@ -604,7 +603,6 @@ bitflags! {
604603
#[deriving(Show)]
605604
pub struct TyS<'tcx> {
606605
pub sty: sty<'tcx>,
607-
pub id: uint,
608606
pub flags: TypeFlags,
609607
}
610608

@@ -669,7 +667,6 @@ pub fn type_has_ty_infer(ty: Ty) -> bool {
669667
pub fn type_needs_infer(ty: Ty) -> bool {
670668
tbox_has_flag(ty, HAS_TY_INFER | HAS_RE_INFER)
671669
}
672-
pub fn type_id(ty: Ty) -> uint { ty.id }
673670

674671

675672
#[deriving(Clone, PartialEq, Eq, Hash, Show)]
@@ -948,38 +945,34 @@ mod primitives {
948945
use syntax::ast;
949946

950947
macro_rules! def_prim_ty(
951-
($name:ident, $sty:expr, $id:expr) => (
948+
($name:ident, $sty:expr) => (
952949
pub static $name: TyS<'static> = TyS {
953950
sty: $sty,
954-
id: $id,
955951
flags: super::NO_TYPE_FLAGS,
956952
};
957953
)
958954
)
959955

960-
def_prim_ty!(TY_NIL, super::ty_nil, 0)
961-
def_prim_ty!(TY_BOOL, super::ty_bool, 1)
962-
def_prim_ty!(TY_CHAR, super::ty_char, 2)
963-
def_prim_ty!(TY_INT, super::ty_int(ast::TyI), 3)
964-
def_prim_ty!(TY_I8, super::ty_int(ast::TyI8), 4)
965-
def_prim_ty!(TY_I16, super::ty_int(ast::TyI16), 5)
966-
def_prim_ty!(TY_I32, super::ty_int(ast::TyI32), 6)
967-
def_prim_ty!(TY_I64, super::ty_int(ast::TyI64), 7)
968-
def_prim_ty!(TY_UINT, super::ty_uint(ast::TyU), 8)
969-
def_prim_ty!(TY_U8, super::ty_uint(ast::TyU8), 9)
970-
def_prim_ty!(TY_U16, super::ty_uint(ast::TyU16), 10)
971-
def_prim_ty!(TY_U32, super::ty_uint(ast::TyU32), 11)
972-
def_prim_ty!(TY_U64, super::ty_uint(ast::TyU64), 12)
973-
def_prim_ty!(TY_F32, super::ty_float(ast::TyF32), 14)
974-
def_prim_ty!(TY_F64, super::ty_float(ast::TyF64), 15)
956+
def_prim_ty!(TY_NIL, super::ty_nil)
957+
def_prim_ty!(TY_BOOL, super::ty_bool)
958+
def_prim_ty!(TY_CHAR, super::ty_char)
959+
def_prim_ty!(TY_INT, super::ty_int(ast::TyI))
960+
def_prim_ty!(TY_I8, super::ty_int(ast::TyI8))
961+
def_prim_ty!(TY_I16, super::ty_int(ast::TyI16))
962+
def_prim_ty!(TY_I32, super::ty_int(ast::TyI32))
963+
def_prim_ty!(TY_I64, super::ty_int(ast::TyI64))
964+
def_prim_ty!(TY_UINT, super::ty_uint(ast::TyU))
965+
def_prim_ty!(TY_U8, super::ty_uint(ast::TyU8))
966+
def_prim_ty!(TY_U16, super::ty_uint(ast::TyU16))
967+
def_prim_ty!(TY_U32, super::ty_uint(ast::TyU32))
968+
def_prim_ty!(TY_U64, super::ty_uint(ast::TyU64))
969+
def_prim_ty!(TY_F32, super::ty_float(ast::TyF32))
970+
def_prim_ty!(TY_F64, super::ty_float(ast::TyF64))
975971

976972
pub static TY_ERR: TyS<'static> = TyS {
977973
sty: super::ty_err,
978-
id: 17,
979974
flags: super::HAS_TY_ERR,
980975
};
981-
982-
pub const LAST_PRIMITIVE_ID: uint = 18;
983976
}
984977

985978
// NB: If you change this, you'll probably want to change the corresponding
@@ -1595,7 +1588,6 @@ pub fn mk_ctxt<'tcx>(s: Session,
15951588
named_region_map: named_region_map,
15961589
item_variance_map: RefCell::new(DefIdMap::new()),
15971590
variance_computed: Cell::new(false),
1598-
next_id: Cell::new(primitives::LAST_PRIMITIVE_ID),
15991591
sess: s,
16001592
def_map: dm,
16011593
region_maps: region_maps,
@@ -1760,14 +1752,11 @@ pub fn mk_t<'tcx>(cx: &ctxt<'tcx>, st: sty<'tcx>) -> Ty<'tcx> {
17601752

17611753
let ty = cx.type_arena.alloc(TyS {
17621754
sty: st,
1763-
id: cx.next_id.get(),
17641755
flags: flags,
17651756
});
17661757

17671758
cx.interner.borrow_mut().insert(InternedTy { ty: ty }, ty);
17681759

1769-
cx.next_id.set(cx.next_id.get() + 1);
1770-
17711760
ty
17721761
}
17731762

@@ -2464,13 +2453,13 @@ pub fn type_interior_is_unsafe<'tcx>(cx: &ctxt<'tcx>, ty: Ty<'tcx>) -> bool {
24642453
}
24652454

24662455
pub fn type_contents<'tcx>(cx: &ctxt<'tcx>, ty: Ty<'tcx>) -> TypeContents {
2467-
return memoized_with_key(&cx.tc_cache, ty, |ty| {
2456+
return memoized(&cx.tc_cache, ty, |ty| {
24682457
tc_ty(cx, ty, &mut HashMap::new())
2469-
}, |&ty| type_id(ty));
2458+
});
24702459

24712460
fn tc_ty<'tcx>(cx: &ctxt<'tcx>,
24722461
ty: Ty<'tcx>,
2473-
cache: &mut HashMap<uint, TypeContents>) -> TypeContents
2462+
cache: &mut HashMap<Ty<'tcx>, TypeContents>) -> TypeContents
24742463
{
24752464
// Subtle: Note that we are *not* using cx.tc_cache here but rather a
24762465
// private cache for this walk. This is needed in the case of cyclic
@@ -2493,16 +2482,15 @@ pub fn type_contents<'tcx>(cx: &ctxt<'tcx>, ty: Ty<'tcx>) -> TypeContents {
24932482
// which is incorrect. This value was computed based on the crutch
24942483
// value for the type contents of list. The correct value is
24952484
// TC::OwnsOwned. This manifested as issue #4821.
2496-
let ty_id = type_id(ty);
2497-
match cache.find(&ty_id) {
2485+
match cache.find(&ty) {
24982486
Some(tc) => { return *tc; }
24992487
None => {}
25002488
}
2501-
match cx.tc_cache.borrow().find(&ty_id) { // Must check both caches!
2489+
match cx.tc_cache.borrow().find(&ty) { // Must check both caches!
25022490
Some(tc) => { return *tc; }
25032491
None => {}
25042492
}
2505-
cache.insert(ty_id, TC::None);
2493+
cache.insert(ty, TC::None);
25062494

25072495
let result = match ty.sty {
25082496
// uint and int are ffi-unsafe
@@ -2672,13 +2660,13 @@ pub fn type_contents<'tcx>(cx: &ctxt<'tcx>, ty: Ty<'tcx>) -> TypeContents {
26722660
}
26732661
};
26742662

2675-
cache.insert(ty_id, result);
2676-
return result;
2663+
cache.insert(ty, result);
2664+
result
26772665
}
26782666

26792667
fn tc_mt<'tcx>(cx: &ctxt<'tcx>,
26802668
mt: mt<'tcx>,
2681-
cache: &mut HashMap<uint, TypeContents>) -> TypeContents
2669+
cache: &mut HashMap<Ty<'tcx>, TypeContents>) -> TypeContents
26822670
{
26832671
let mc = TC::ReachesMutable.when(mt.mutbl == MutMutable);
26842672
mc | tc_ty(cx, mt.ty, cache)
@@ -2987,7 +2975,7 @@ pub fn is_type_representable<'tcx>(cx: &ctxt<'tcx>, sp: Span, ty: Ty<'tcx>)
29872975
pairs.all(|(&a, &b)| same_type(a, b))
29882976
}
29892977
_ => {
2990-
type_id(a) == type_id(b)
2978+
a == b
29912979
}
29922980
}
29932981
}

src/librustc/util/common.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -189,17 +189,7 @@ pub fn memoized<T: Clone, U: Clone, M: MutableMap<T, U>>(
189189
arg: T,
190190
f: |T| -> U
191191
) -> U {
192-
memoized_with_key(cache, arg, f, |arg| arg.clone())
193-
}
194-
195-
#[inline(always)]
196-
pub fn memoized_with_key<T, K, U: Clone, M: MutableMap<K, U>>(
197-
cache: &RefCell<M>,
198-
arg: T,
199-
f: |T| -> U,
200-
k: |&T| -> K
201-
) -> U {
202-
let key = k(&arg);
192+
let key = arg.clone();
203193
let result = cache.borrow().find(&key).map(|result| result.clone());
204194
match result {
205195
Some(result) => result,

0 commit comments

Comments
 (0)