Skip to content

Commit 743a872

Browse files
committed
Auto merge of #50531 - iancormac84:merge-typeidhasher-cleanup, r=<try>
[WIP] Cleanup uses of TypeIdHasher and replace them with StableHasher Fixes #50424 r? @michaelwoerister
2 parents 8ff4b42 + 2a996a7 commit 743a872

File tree

4 files changed

+86
-222
lines changed

4 files changed

+86
-222
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-152
Original file line numberDiff line numberDiff line change
@@ -15,24 +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;
2019
use ty::{self, Ty, TyCtxt, TypeFoldable};
21-
use ty::fold::TypeVisitor;
2220
use ty::subst::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;
28-
use mir::interpret::{Value, PrimVal};
2926

30-
use rustc_data_structures::stable_hasher::{StableHasher, StableHasherResult,
31-
HashStable};
27+
use rustc_data_structures::stable_hasher::{StableHasher, HashStable};
3228
use rustc_data_structures::fx::FxHashMap;
3329
use std::{cmp, fmt};
34-
use std::hash::Hash;
35-
use std::intrinsics;
3630
use syntax::ast;
3731
use syntax::attr::{self, SignedInt, UnsignedInt};
3832
use syntax_pos::{Span, DUMMY_SP};
@@ -606,151 +600,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
606600
}
607601
}
608602

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

src/librustc_trans/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::TransFnAttrFlags;
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)