Skip to content

Commit 9e914cc

Browse files
authored
Rollup merge of rust-lang#50531 - iancormac84:merge-typeidhasher-cleanup, r=michaelwoerister
Cleanup uses of TypeIdHasher and replace them with StableHasher Fixes rust-lang#50424 r? @michaelwoerister
2 parents ef8ee64 + 0349394 commit 9e914cc

File tree

4 files changed

+86
-220
lines changed

4 files changed

+86
-220
lines changed

src/librustc/ich/impls_ty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ for ty::RegionKind {
132132
ty::ReLateBound(..) |
133133
ty::ReVar(..) |
134134
ty::ReSkolemized(..) => {
135-
bug!("TypeIdHasher: unexpected region {:?}", *self)
135+
bug!("StableHasher: unexpected region {:?}", *self)
136136
}
137137
}
138138
}

src/librustc/ty/util.rs

+1-150
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,18 @@ use hir::def_id::DefId;
1515
use hir::map::{DefPathData, Node};
1616
use hir;
1717
use ich::NodeIdHashingMode;
18-
use middle::const_val::ConstVal;
1918
use traits::{self, ObligationCause};
2019
use ty::{self, Ty, TyCtxt, GenericParamDefKind, TypeFoldable};
21-
use ty::fold::TypeVisitor;
2220
use ty::subst::{Substs, UnpackedKind};
2321
use ty::maps::TyCtxtAt;
2422
use ty::TypeVariants::*;
2523
use ty::layout::{Integer, IntegerExt};
2624
use util::common::ErrorReported;
2725
use middle::lang_items;
2826

29-
use rustc_data_structures::stable_hasher::{StableHasher, StableHasherResult,
30-
HashStable};
27+
use rustc_data_structures::stable_hasher::{StableHasher, HashStable};
3128
use rustc_data_structures::fx::FxHashMap;
3229
use std::{cmp, fmt};
33-
use std::hash::Hash;
34-
use std::intrinsics;
3530
use syntax::ast;
3631
use syntax::attr::{self, SignedInt, UnsignedInt};
3732
use syntax_pos::{Span, DUMMY_SP};
@@ -615,150 +610,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
615610
}
616611
}
617612

618-
pub struct TypeIdHasher<'a, 'gcx: 'a+'tcx, 'tcx: 'a, W> {
619-
tcx: TyCtxt<'a, 'gcx, 'tcx>,
620-
state: StableHasher<W>,
621-
}
622-
623-
impl<'a, 'gcx, 'tcx, W> TypeIdHasher<'a, 'gcx, 'tcx, W>
624-
where W: StableHasherResult
625-
{
626-
pub fn new(tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Self {
627-
TypeIdHasher { tcx: tcx, state: StableHasher::new() }
628-
}
629-
630-
pub fn finish(self) -> W {
631-
self.state.finish()
632-
}
633-
634-
pub fn hash<T: Hash>(&mut self, x: T) {
635-
x.hash(&mut self.state);
636-
}
637-
638-
fn hash_discriminant_u8<T>(&mut self, x: &T) {
639-
let v = unsafe {
640-
intrinsics::discriminant_value(x)
641-
};
642-
let b = v as u8;
643-
assert_eq!(v, b as u64);
644-
self.hash(b)
645-
}
646-
647-
fn def_id(&mut self, did: DefId) {
648-
// Hash the DefPath corresponding to the DefId, which is independent
649-
// of compiler internal state. We already have a stable hash value of
650-
// all DefPaths available via tcx.def_path_hash(), so we just feed that
651-
// into the hasher.
652-
let hash = self.tcx.def_path_hash(did);
653-
self.hash(hash);
654-
}
655-
}
656-
657-
impl<'a, 'gcx, 'tcx, W> TypeVisitor<'tcx> for TypeIdHasher<'a, 'gcx, 'tcx, W>
658-
where W: StableHasherResult
659-
{
660-
fn visit_ty(&mut self, ty: Ty<'tcx>) -> bool {
661-
// Distinguish between the Ty variants uniformly.
662-
self.hash_discriminant_u8(&ty.sty);
663-
664-
match ty.sty {
665-
TyInt(i) => self.hash(i),
666-
TyUint(u) => self.hash(u),
667-
TyFloat(f) => self.hash(f),
668-
TyArray(_, n) => {
669-
self.hash_discriminant_u8(&n.val);
670-
match n.val {
671-
ConstVal::Value(alloc) => self.hash(alloc),
672-
ConstVal::Unevaluated(def_id, _) => self.def_id(def_id),
673-
}
674-
}
675-
TyRawPtr(m) => self.hash(m.mutbl),
676-
TyRef(_, _, mutbl) => self.hash(mutbl),
677-
TyClosure(def_id, _) |
678-
TyGenerator(def_id, _, _) |
679-
TyAnon(def_id, _) |
680-
TyFnDef(def_id, _) => self.def_id(def_id),
681-
TyAdt(d, _) => self.def_id(d.did),
682-
TyForeign(def_id) => self.def_id(def_id),
683-
TyFnPtr(f) => {
684-
self.hash(f.unsafety());
685-
self.hash(f.abi());
686-
self.hash(f.variadic());
687-
self.hash(f.inputs().skip_binder().len());
688-
}
689-
TyDynamic(ref data, ..) => {
690-
if let Some(p) = data.principal() {
691-
self.def_id(p.def_id());
692-
}
693-
for d in data.auto_traits() {
694-
self.def_id(d);
695-
}
696-
}
697-
TyGeneratorWitness(tys) => {
698-
self.hash(tys.skip_binder().len());
699-
}
700-
TyTuple(tys) => {
701-
self.hash(tys.len());
702-
}
703-
TyParam(p) => {
704-
self.hash(p.idx);
705-
self.hash(p.name);
706-
}
707-
TyProjection(ref data) => {
708-
self.def_id(data.item_def_id);
709-
}
710-
TyNever |
711-
TyBool |
712-
TyChar |
713-
TyStr |
714-
TySlice(_) => {}
715-
716-
TyError |
717-
TyInfer(_) => bug!("TypeIdHasher: unexpected type {}", ty)
718-
}
719-
720-
ty.super_visit_with(self)
721-
}
722-
723-
fn visit_region(&mut self, r: ty::Region<'tcx>) -> bool {
724-
self.hash_discriminant_u8(r);
725-
match *r {
726-
ty::ReErased |
727-
ty::ReStatic |
728-
ty::ReEmpty => {
729-
// No variant fields to hash for these ...
730-
}
731-
ty::ReCanonical(c) => {
732-
self.hash(c);
733-
}
734-
ty::ReLateBound(db, ty::BrAnon(i)) => {
735-
self.hash(db.depth);
736-
self.hash(i);
737-
}
738-
ty::ReEarlyBound(ty::EarlyBoundRegion { def_id, .. }) => {
739-
self.def_id(def_id);
740-
}
741-
742-
ty::ReClosureBound(..) |
743-
ty::ReLateBound(..) |
744-
ty::ReFree(..) |
745-
ty::ReScope(..) |
746-
ty::ReVar(..) |
747-
ty::ReSkolemized(..) => {
748-
bug!("TypeIdHasher: unexpected region {:?}", r)
749-
}
750-
}
751-
false
752-
}
753-
754-
fn visit_binder<T: TypeFoldable<'tcx>>(&mut self, x: &ty::Binder<T>) -> bool {
755-
// Anonymize late-bound regions so that, for example:
756-
// `for<'a, b> fn(&'a &'b T)` and `for<'a, b> fn(&'b &'a T)`
757-
// result in the same TypeId (the two types are equivalent).
758-
self.tcx.anonymize_late_bound_regions(x).super_visit_with(self)
759-
}
760-
}
761-
762613
impl<'a, 'tcx> ty::TyS<'tcx> {
763614
pub fn moves_by_default(&'tcx self,
764615
tcx: TyCtxt<'a, 'tcx, 'tcx>,

src/librustc_codegen_llvm/debuginfo/metadata.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,11 @@ use llvm::{self, ValueRef};
2323
use llvm::debuginfo::{DIType, DIFile, DIScope, DIDescriptor,
2424
DICompositeType, DILexicalBlock, DIFlags};
2525

26+
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
2627
use rustc::hir::CodegenFnAttrFlags;
2728
use rustc::hir::def::CtorKind;
2829
use rustc::hir::def_id::{DefId, CrateNum, LOCAL_CRATE};
29-
use rustc::ty::fold::TypeVisitor;
30-
use rustc::ty::util::TypeIdHasher;
31-
use rustc::ich::Fingerprint;
30+
use rustc::ich::{Fingerprint, NodeIdHashingMode};
3231
use rustc::ty::Instance;
3332
use common::CodegenCx;
3433
use rustc::ty::{self, AdtKind, ParamEnv, Ty, TyCtxt};
@@ -144,9 +143,15 @@ impl<'tcx> TypeMap<'tcx> {
144143

145144
// The hasher we are using to generate the UniqueTypeId. We want
146145
// something that provides more than the 64 bits of the DefaultHasher.
147-
let mut type_id_hasher = TypeIdHasher::<Fingerprint>::new(cx.tcx);
148-
type_id_hasher.visit_ty(type_);
149-
let unique_type_id = type_id_hasher.finish().to_hex();
146+
let mut hasher = StableHasher::<Fingerprint>::new();
147+
let mut hcx = cx.tcx.create_stable_hashing_context();
148+
let type_ = cx.tcx.erase_regions(&type_);
149+
hcx.while_hashing_spans(false, |hcx| {
150+
hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
151+
type_.hash_stable(hcx, &mut hasher);
152+
});
153+
});
154+
let unique_type_id = hasher.finish().to_hex();
150155

151156
let key = self.unique_id_interner.intern(&unique_type_id);
152157
self.type_to_unique_id.insert(type_, UniqueTypeId(key));

0 commit comments

Comments
 (0)