1 use crate::hir::def_id::DefId;
2 use crate::ich::StableHashingContext;
3 use rustc_data_structures::stable_hasher::{StableHasher, StableHasherResult,
9 use crate::ty::{self, Ty, TyCtxt};
11 use self::SimplifiedTypeGen::*;
13 pub type SimplifiedType = SimplifiedTypeGen<DefId>;
15 /// See `simplify_type`
17 /// Note that we keep this type generic over the type of identifier it uses
18 /// because we sometimes need to use SimplifiedTypeGen values as stable sorting
19 /// keys (in which case we use a DefPathHash as id-type) but in the general case
20 /// the non-stable but fast to construct DefId-version is the better choice.
21 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, RustcEncodable, RustcDecodable)]
22 pub enum SimplifiedTypeGen<D>
23 where D: Copy + Debug + Ord + Eq + Hash
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, ..) => {
74 match trait_info.principal_def_id() {
75 Some(principal_def_id) if !tcx.trait_is_auto(principal_def_id) => {
76 Some(TraitSimplifiedType(principal_def_id))
78 _ => Some(MarkerTraitObjectSimplifiedType)
81 ty::Ref(_, ty, _) => {
82 // since we introduce auto-refs during method lookup, we
83 // just treat &T and T as equivalent from the point of
84 // view of possibly unifying
85 simplify_type(tcx, ty, can_simplify_params)
87 ty::FnDef(def_id, _) |
88 ty::Closure(def_id, _) => {
89 Some(ClosureSimplifiedType(def_id))
91 ty::Generator(def_id, _, _) => {
92 Some(GeneratorSimplifiedType(def_id))
94 ty::GeneratorWitness(ref tys) => {
95 Some(GeneratorWitnessSimplifiedType(tys.skip_binder().len()))
97 ty::Never => Some(NeverSimplifiedType),
98 ty::Tuple(ref tys) => {
99 Some(TupleSimplifiedType(tys.len()))
101 ty::FnPtr(ref f) => {
102 Some(FunctionSimplifiedType(f.skip_binder().inputs().len()))
104 ty::UnnormalizedProjection(..) => bug!("only used with chalk-engine"),
105 ty::Projection(_) | ty::Param(_) => {
106 if can_simplify_params {
107 // In normalized types, projections don't unify with
108 // anything. when lazy normalization happens, this
109 // will change. It would still be nice to have a way
110 // to deal with known-not-to-unify-with-anything
111 // projections (e.g., the likes of <__S as Encoder>::Error).
112 Some(ParameterSimplifiedType)
117 ty::Opaque(def_id, _) => {
118 Some(OpaqueSimplifiedType(def_id))
120 ty::Foreign(def_id) => {
121 Some(ForeignSimplifiedType(def_id))
123 ty::Placeholder(..) | ty::Bound(..) | ty::Infer(_) | ty::Error => None,
127 impl<D: Copy + Debug + Ord + Eq + Hash> SimplifiedTypeGen<D> {
128 pub fn map_def<U, F>(self, map: F) -> SimplifiedTypeGen<U>
130 U: Copy + Debug + Ord + Eq + Hash,
133 BoolSimplifiedType => BoolSimplifiedType,
134 CharSimplifiedType => CharSimplifiedType,
135 IntSimplifiedType(t) => IntSimplifiedType(t),
136 UintSimplifiedType(t) => UintSimplifiedType(t),
137 FloatSimplifiedType(t) => FloatSimplifiedType(t),
138 AdtSimplifiedType(d) => AdtSimplifiedType(map(d)),
139 StrSimplifiedType => StrSimplifiedType,
140 ArraySimplifiedType => ArraySimplifiedType,
141 PtrSimplifiedType => PtrSimplifiedType,
142 NeverSimplifiedType => NeverSimplifiedType,
143 MarkerTraitObjectSimplifiedType => MarkerTraitObjectSimplifiedType,
144 TupleSimplifiedType(n) => TupleSimplifiedType(n),
145 TraitSimplifiedType(d) => TraitSimplifiedType(map(d)),
146 ClosureSimplifiedType(d) => ClosureSimplifiedType(map(d)),
147 GeneratorSimplifiedType(d) => GeneratorSimplifiedType(map(d)),
148 GeneratorWitnessSimplifiedType(n) => GeneratorWitnessSimplifiedType(n),
149 OpaqueSimplifiedType(d) => OpaqueSimplifiedType(map(d)),
150 FunctionSimplifiedType(n) => FunctionSimplifiedType(n),
151 ParameterSimplifiedType => ParameterSimplifiedType,
152 ForeignSimplifiedType(d) => ForeignSimplifiedType(d),
157 impl<'a, D> HashStable<StableHashingContext<'a>> for SimplifiedTypeGen<D>
159 D: Copy + Debug + Ord + Eq + Hash + HashStable<StableHashingContext<'a>>,
161 fn hash_stable<W: StableHasherResult>(&self,
162 hcx: &mut StableHashingContext<'a>,
163 hasher: &mut StableHasher<W>) {
164 mem::discriminant(self).hash_stable(hcx, hasher);
169 ArraySimplifiedType |
171 NeverSimplifiedType |
172 ParameterSimplifiedType |
173 MarkerTraitObjectSimplifiedType => {
176 IntSimplifiedType(t) => t.hash_stable(hcx, hasher),
177 UintSimplifiedType(t) => t.hash_stable(hcx, hasher),
178 FloatSimplifiedType(t) => t.hash_stable(hcx, hasher),
179 AdtSimplifiedType(d) => d.hash_stable(hcx, hasher),
180 TupleSimplifiedType(n) => n.hash_stable(hcx, hasher),
181 TraitSimplifiedType(d) => d.hash_stable(hcx, hasher),
182 ClosureSimplifiedType(d) => d.hash_stable(hcx, hasher),
183 GeneratorSimplifiedType(d) => d.hash_stable(hcx, hasher),
184 GeneratorWitnessSimplifiedType(n) => n.hash_stable(hcx, hasher),
185 OpaqueSimplifiedType(d) => d.hash_stable(hcx, hasher),
186 FunctionSimplifiedType(n) => n.hash_stable(hcx, hasher),
187 ForeignSimplifiedType(d) => d.hash_stable(hcx, hasher),