Skip to content

Commit 5ae1d54

Browse files
committed
rustc: remove id's from Ty's in favor of pointer identity.
1 parent 1035de8 commit 5ae1d54

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
@@ -139,7 +139,7 @@ pub struct LocalCrateContext<'tcx> {
139139
/// Holds the LLVM values for closure IDs.
140140
unboxed_closure_vals: RefCell<HashMap<MonoId<'tcx>, ValueRef>>,
141141

142-
dbg_cx: Option<debuginfo::CrateDebugContext>,
142+
dbg_cx: Option<debuginfo::CrateDebugContext<'tcx>>,
143143

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

@@ -685,7 +685,7 @@ impl<'b, 'tcx> CrateContext<'b, 'tcx> {
685685
&self.local.unboxed_closure_vals
686686
}
687687

688-
pub fn dbg_cx<'a>(&'a self) -> &'a Option<debuginfo::CrateDebugContext> {
688+
pub fn dbg_cx<'a>(&'a self) -> &'a Option<debuginfo::CrateDebugContext<'tcx>> {
689689
&self.local.dbg_cx
690690
}
691691

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
@@ -3107,9 +3107,9 @@ fn bytes_to_bits(bytes: u64) -> u64 {
31073107
}
31083108

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

31153115
#[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>>>,
@@ -600,7 +599,6 @@ bitflags! {
600599
#[deriving(Show)]
601600
pub struct TyS<'tcx> {
602601
pub sty: sty<'tcx>,
603-
pub id: uint,
604602
pub flags: TypeFlags,
605603
}
606604

@@ -665,7 +663,6 @@ pub fn type_has_ty_infer(ty: Ty) -> bool {
665663
pub fn type_needs_infer(ty: Ty) -> bool {
666664
tbox_has_flag(ty, HAS_TY_INFER | HAS_RE_INFER)
667665
}
668-
pub fn type_id(ty: Ty) -> uint { ty.id }
669666

670667

671668
#[deriving(Clone, PartialEq, Eq, Hash, Show)]
@@ -944,38 +941,34 @@ mod primitives {
944941
use syntax::ast;
945942

946943
macro_rules! def_prim_ty(
947-
($name:ident, $sty:expr, $id:expr) => (
944+
($name:ident, $sty:expr) => (
948945
pub static $name: TyS<'static> = TyS {
949946
sty: $sty,
950-
id: $id,
951947
flags: super::NO_TYPE_FLAGS,
952948
};
953949
)
954950
)
955951

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

972968
pub static TY_ERR: TyS<'static> = TyS {
973969
sty: super::ty_err,
974-
id: 17,
975970
flags: super::HAS_TY_ERR,
976971
};
977-
978-
pub const LAST_PRIMITIVE_ID: uint = 18;
979972
}
980973

981974
// NB: If you change this, you'll probably want to change the corresponding
@@ -1591,7 +1584,6 @@ pub fn mk_ctxt<'tcx>(s: Session,
15911584
named_region_map: named_region_map,
15921585
item_variance_map: RefCell::new(DefIdMap::new()),
15931586
variance_computed: Cell::new(false),
1594-
next_id: Cell::new(primitives::LAST_PRIMITIVE_ID),
15951587
sess: s,
15961588
def_map: dm,
15971589
region_maps: region_maps,
@@ -1755,14 +1747,11 @@ pub fn mk_t<'tcx>(cx: &ctxt<'tcx>, st: sty<'tcx>) -> Ty<'tcx> {
17551747

17561748
let ty = cx.type_arena.alloc(TyS {
17571749
sty: st,
1758-
id: cx.next_id.get(),
17591750
flags: flags,
17601751
});
17611752

17621753
cx.interner.borrow_mut().insert(InternedTy { ty: ty }, ty);
17631754

1764-
cx.next_id.set(cx.next_id.get() + 1);
1765-
17661755
ty
17671756
}
17681757

@@ -2469,13 +2458,13 @@ pub fn type_interior_is_unsafe<'tcx>(cx: &ctxt<'tcx>, ty: Ty<'tcx>) -> bool {
24692458
}
24702459

24712460
pub fn type_contents<'tcx>(cx: &ctxt<'tcx>, ty: Ty<'tcx>) -> TypeContents {
2472-
return memoized_with_key(&cx.tc_cache, ty, |ty| {
2461+
return memoized(&cx.tc_cache, ty, |ty| {
24732462
tc_ty(cx, ty, &mut HashMap::new())
2474-
}, |&ty| type_id(ty));
2463+
});
24752464

24762465
fn tc_ty<'tcx>(cx: &ctxt<'tcx>,
24772466
ty: Ty<'tcx>,
2478-
cache: &mut HashMap<uint, TypeContents>) -> TypeContents
2467+
cache: &mut HashMap<Ty<'tcx>, TypeContents>) -> TypeContents
24792468
{
24802469
// Subtle: Note that we are *not* using cx.tc_cache here but rather a
24812470
// private cache for this walk. This is needed in the case of cyclic
@@ -2498,16 +2487,15 @@ pub fn type_contents<'tcx>(cx: &ctxt<'tcx>, ty: Ty<'tcx>) -> TypeContents {
24982487
// which is incorrect. This value was computed based on the crutch
24992488
// value for the type contents of list. The correct value is
25002489
// TC::OwnsOwned. This manifested as issue #4821.
2501-
let ty_id = type_id(ty);
2502-
match cache.find(&ty_id) {
2490+
match cache.find(&ty) {
25032491
Some(tc) => { return *tc; }
25042492
None => {}
25052493
}
2506-
match cx.tc_cache.borrow().find(&ty_id) { // Must check both caches!
2494+
match cx.tc_cache.borrow().find(&ty) { // Must check both caches!
25072495
Some(tc) => { return *tc; }
25082496
None => {}
25092497
}
2510-
cache.insert(ty_id, TC::None);
2498+
cache.insert(ty, TC::None);
25112499

25122500
let result = match ty.sty {
25132501
// uint and int are ffi-unsafe
@@ -2677,13 +2665,13 @@ pub fn type_contents<'tcx>(cx: &ctxt<'tcx>, ty: Ty<'tcx>) -> TypeContents {
26772665
}
26782666
};
26792667

2680-
cache.insert(ty_id, result);
2681-
return result;
2668+
cache.insert(ty, result);
2669+
result
26822670
}
26832671

26842672
fn tc_mt<'tcx>(cx: &ctxt<'tcx>,
26852673
mt: mt<'tcx>,
2686-
cache: &mut HashMap<uint, TypeContents>) -> TypeContents
2674+
cache: &mut HashMap<Ty<'tcx>, TypeContents>) -> TypeContents
26872675
{
26882676
let mc = TC::ReachesMutable.when(mt.mutbl == MutMutable);
26892677
mc | tc_ty(cx, mt.ty, cache)
@@ -2992,7 +2980,7 @@ pub fn is_type_representable<'tcx>(cx: &ctxt<'tcx>, sp: Span, ty: Ty<'tcx>)
29922980
pairs.all(|(&a, &b)| same_type(a, b))
29932981
}
29942982
_ => {
2995-
type_id(a) == type_id(b)
2983+
a == b
29962984
}
29972985
}
29982986
}

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 + Hash<S> + Eq, U: Clone, S, H: Hasher<S>>(
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: Hash<S> + Eq, U: Clone, S, H: Hasher<S>>(
197-
cache: &RefCell<HashMap<K, U, H>>,
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)