1 use crate::ich::StableHashingContext;
2 use crate::ty::{self, Ty, TyCtxt};
4 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
5 use rustc_hir::def_id::DefId;
10 use self::SimplifiedTypeGen::*;
12 pub type SimplifiedType = SimplifiedTypeGen<DefId>;
14 /// See `simplify_type`
16 /// Note that we keep this type generic over the type of identifier it uses
17 /// because we sometimes need to use SimplifiedTypeGen values as stable sorting
18 /// keys (in which case we use a DefPathHash as id-type) but in the general case
19 /// the non-stable but fast to construct DefId-version is the better choice.
20 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, TyEncodable, TyDecodable)]
21 pub enum SimplifiedTypeGen<D>
23 D: Copy + Debug + Ord + Eq,
27 IntSimplifiedType(ast::IntTy),
28 UintSimplifiedType(ast::UintTy),
29 FloatSimplifiedType(ast::FloatTy),
35 TupleSimplifiedType(usize),
36 /// A trait object, all of whose components are markers
37 /// (e.g., `dyn Send + Sync`).
38 MarkerTraitObjectSimplifiedType,
39 TraitSimplifiedType(D),
40 ClosureSimplifiedType(D),
41 GeneratorSimplifiedType(D),
42 GeneratorWitnessSimplifiedType(usize),
43 OpaqueSimplifiedType(D),
44 FunctionSimplifiedType(usize),
45 ParameterSimplifiedType,
46 ForeignSimplifiedType(DefId),
49 /// Tries to simplify a type by dropping type parameters, deref'ing away any reference types, etc.
50 /// The idea is to get something simple that we can use to quickly decide if two types could unify
51 /// during method lookup.
53 /// If `can_simplify_params` is false, then we will fail to simplify type parameters entirely. This
54 /// is useful when those type parameters would be instantiated with fresh type variables, since
55 /// then we can't say much about whether two types would unify. Put another way,
56 /// `can_simplify_params` should be true if type parameters appear free in `ty` and `false` if they
57 /// are to be considered bound.
61 can_simplify_params: bool,
62 ) -> Option<SimplifiedType> {
64 ty::Bool => Some(BoolSimplifiedType),
65 ty::Char => Some(CharSimplifiedType),
66 ty::Int(int_type) => Some(IntSimplifiedType(int_type)),
67 ty::Uint(uint_type) => Some(UintSimplifiedType(uint_type)),
68 ty::Float(float_type) => Some(FloatSimplifiedType(float_type)),
69 ty::Adt(def, _) => Some(AdtSimplifiedType(def.did)),
70 ty::Str => Some(StrSimplifiedType),
71 ty::Array(..) | ty::Slice(_) => Some(ArraySimplifiedType),
72 ty::RawPtr(_) => Some(PtrSimplifiedType),
73 ty::Dynamic(ref trait_info, ..) => match trait_info.principal_def_id() {
74 Some(principal_def_id) if !tcx.trait_is_auto(principal_def_id) => {
75 Some(TraitSimplifiedType(principal_def_id))
77 _ => Some(MarkerTraitObjectSimplifiedType),
79 ty::Ref(_, ty, _) => {
80 // since we introduce auto-refs during method lookup, we
81 // just treat &T and T as equivalent from the point of
82 // view of possibly unifying
83 simplify_type(tcx, ty, can_simplify_params)
85 ty::FnDef(def_id, _) | ty::Closure(def_id, _) => Some(ClosureSimplifiedType(def_id)),
86 ty::Generator(def_id, _, _) => Some(GeneratorSimplifiedType(def_id)),
87 ty::GeneratorWitness(ref tys) => {
88 Some(GeneratorWitnessSimplifiedType(tys.skip_binder().len()))
90 ty::Never => Some(NeverSimplifiedType),
91 ty::Tuple(ref tys) => Some(TupleSimplifiedType(tys.len())),
92 ty::FnPtr(ref f) => Some(FunctionSimplifiedType(f.skip_binder().inputs().len())),
93 ty::Projection(_) | ty::Param(_) => {
94 if can_simplify_params {
95 // In normalized types, projections don't unify with
96 // anything. when lazy normalization happens, this
97 // will change. It would still be nice to have a way
98 // to deal with known-not-to-unify-with-anything
99 // projections (e.g., the likes of <__S as Encoder>::Error).
100 Some(ParameterSimplifiedType)
105 ty::Opaque(def_id, _) => Some(OpaqueSimplifiedType(def_id)),
106 ty::Foreign(def_id) => Some(ForeignSimplifiedType(def_id)),
107 ty::Placeholder(..) | ty::Bound(..) | ty::Infer(_) | ty::Error(_) => None,
111 impl<D: Copy + Debug + Ord + Eq> SimplifiedTypeGen<D> {
112 pub fn map_def<U, F>(self, map: F) -> SimplifiedTypeGen<U>
115 U: Copy + Debug + Ord + Eq,
118 BoolSimplifiedType => BoolSimplifiedType,
119 CharSimplifiedType => CharSimplifiedType,
120 IntSimplifiedType(t) => IntSimplifiedType(t),
121 UintSimplifiedType(t) => UintSimplifiedType(t),
122 FloatSimplifiedType(t) => FloatSimplifiedType(t),
123 AdtSimplifiedType(d) => AdtSimplifiedType(map(d)),
124 StrSimplifiedType => StrSimplifiedType,
125 ArraySimplifiedType => ArraySimplifiedType,
126 PtrSimplifiedType => PtrSimplifiedType,
127 NeverSimplifiedType => NeverSimplifiedType,
128 MarkerTraitObjectSimplifiedType => MarkerTraitObjectSimplifiedType,
129 TupleSimplifiedType(n) => TupleSimplifiedType(n),
130 TraitSimplifiedType(d) => TraitSimplifiedType(map(d)),
131 ClosureSimplifiedType(d) => ClosureSimplifiedType(map(d)),
132 GeneratorSimplifiedType(d) => GeneratorSimplifiedType(map(d)),
133 GeneratorWitnessSimplifiedType(n) => GeneratorWitnessSimplifiedType(n),
134 OpaqueSimplifiedType(d) => OpaqueSimplifiedType(map(d)),
135 FunctionSimplifiedType(n) => FunctionSimplifiedType(n),
136 ParameterSimplifiedType => ParameterSimplifiedType,
137 ForeignSimplifiedType(d) => ForeignSimplifiedType(d),
142 impl<'a, D> HashStable<StableHashingContext<'a>> for SimplifiedTypeGen<D>
144 D: Copy + Debug + Ord + Eq + HashStable<StableHashingContext<'a>>,
146 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
147 mem::discriminant(self).hash_stable(hcx, hasher);
152 | ArraySimplifiedType
154 | NeverSimplifiedType
155 | ParameterSimplifiedType
156 | MarkerTraitObjectSimplifiedType => {
159 IntSimplifiedType(t) => t.hash_stable(hcx, hasher),
160 UintSimplifiedType(t) => t.hash_stable(hcx, hasher),
161 FloatSimplifiedType(t) => t.hash_stable(hcx, hasher),
162 AdtSimplifiedType(d) => d.hash_stable(hcx, hasher),
163 TupleSimplifiedType(n) => n.hash_stable(hcx, hasher),
164 TraitSimplifiedType(d) => d.hash_stable(hcx, hasher),
165 ClosureSimplifiedType(d) => d.hash_stable(hcx, hasher),
166 GeneratorSimplifiedType(d) => d.hash_stable(hcx, hasher),
167 GeneratorWitnessSimplifiedType(n) => n.hash_stable(hcx, hasher),
168 OpaqueSimplifiedType(d) => d.hash_stable(hcx, hasher),
169 FunctionSimplifiedType(n) => n.hash_stable(hcx, hasher),
170 ForeignSimplifiedType(d) => d.hash_stable(hcx, hasher),