1 use crate::mir::Mutability;
2 use crate::ty::{self, Ty, TyCtxt};
3 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
4 use rustc_hir::def_id::DefId;
5 use rustc_query_system::ich::StableHashingContext;
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>
27 IntSimplifiedType(ty::IntTy),
28 UintSimplifiedType(ty::UintTy),
29 FloatSimplifiedType(ty::FloatTy),
31 ForeignSimplifiedType(D),
35 RefSimplifiedType(Mutability),
36 PtrSimplifiedType(Mutability),
38 TupleSimplifiedType(usize),
39 /// A trait object, all of whose components are markers
40 /// (e.g., `dyn Send + Sync`).
41 MarkerTraitObjectSimplifiedType,
42 TraitSimplifiedType(D),
43 ClosureSimplifiedType(D),
44 GeneratorSimplifiedType(D),
45 GeneratorWitnessSimplifiedType(usize),
46 OpaqueSimplifiedType(D),
47 FunctionSimplifiedType(usize),
48 ParameterSimplifiedType,
51 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
52 pub enum SimplifyParams {
57 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
58 pub enum StripReferences {
63 /// Tries to simplify a type by only returning the outermost injective¹ layer, if one exists.
65 /// The idea is to get something simple that we can use to quickly decide if two types could unify,
66 /// for example during method lookup.
68 /// A special case here are parameters and projections. Projections can be normalized to
69 /// a different type, meaning that `<T as Trait>::Assoc` and `u8` can be unified, even though
70 /// their outermost layer is different while parameters like `T` of impls are later replaced
71 /// with an inference variable, which then also allows unification with other types.
73 /// When using `SimplifyParams::Yes`, we still return a simplified type for params and projections²,
74 /// the reasoning for this can be seen at the places doing this.
76 /// For diagnostics we strip references with `StripReferences::Yes`. This is currently the best
77 /// way to skip some unhelpful suggestions.
79 /// ¹ meaning that if two outermost layers are different, then the whole types are also different.
80 /// ² FIXME(@lcnr): this seems like it can actually end up being unsound with the way it's used during
81 /// candidate selection. We do not consider non blanket impls for `<_ as Trait>::Assoc` even
82 /// though `_` can be inferred to a concrete type later at which point a concrete impl
83 /// could actually apply. After experimenting for about an hour I wasn't able to cause any issues
84 /// this way so I am not going to change this until we actually find an issue as I am really
85 /// interesting in getting an actual test for this.
89 can_simplify_params: SimplifyParams,
90 strip_references: StripReferences,
91 ) -> Option<SimplifiedType> {
93 ty::Bool => Some(BoolSimplifiedType),
94 ty::Char => Some(CharSimplifiedType),
95 ty::Int(int_type) => Some(IntSimplifiedType(int_type)),
96 ty::Uint(uint_type) => Some(UintSimplifiedType(uint_type)),
97 ty::Float(float_type) => Some(FloatSimplifiedType(float_type)),
98 ty::Adt(def, _) => Some(AdtSimplifiedType(def.did)),
99 ty::Str => Some(StrSimplifiedType),
100 ty::Array(..) => Some(ArraySimplifiedType),
101 ty::Slice(..) => Some(SliceSimplifiedType),
102 ty::RawPtr(ptr) => Some(PtrSimplifiedType(ptr.mutbl)),
103 ty::Dynamic(ref trait_info, ..) => match trait_info.principal_def_id() {
104 Some(principal_def_id) if !tcx.trait_is_auto(principal_def_id) => {
105 Some(TraitSimplifiedType(principal_def_id))
107 _ => Some(MarkerTraitObjectSimplifiedType),
109 ty::Ref(_, ty, mutbl) => {
110 if strip_references == StripReferences::Yes {
111 // For diagnostics, when recommending similar impls we want to
112 // recommend impls even when there is a reference mismatch,
113 // so we treat &T and T equivalently in that case.
114 simplify_type(tcx, ty, can_simplify_params, strip_references)
116 Some(RefSimplifiedType(mutbl))
119 ty::FnDef(def_id, _) | ty::Closure(def_id, _) => Some(ClosureSimplifiedType(def_id)),
120 ty::Generator(def_id, _, _) => Some(GeneratorSimplifiedType(def_id)),
121 ty::GeneratorWitness(ref tys) => {
122 Some(GeneratorWitnessSimplifiedType(tys.skip_binder().len()))
124 ty::Never => Some(NeverSimplifiedType),
125 ty::Tuple(ref tys) => Some(TupleSimplifiedType(tys.len())),
126 ty::FnPtr(ref f) => Some(FunctionSimplifiedType(f.skip_binder().inputs().len())),
127 ty::Projection(_) | ty::Param(_) => {
128 if can_simplify_params == SimplifyParams::Yes {
129 // In normalized types, projections don't unify with
130 // anything. when lazy normalization happens, this
131 // will change. It would still be nice to have a way
132 // to deal with known-not-to-unify-with-anything
133 // projections (e.g., the likes of <__S as Encoder>::Error).
134 Some(ParameterSimplifiedType)
139 ty::Opaque(def_id, _) => Some(OpaqueSimplifiedType(def_id)),
140 ty::Foreign(def_id) => Some(ForeignSimplifiedType(def_id)),
141 ty::Placeholder(..) | ty::Bound(..) | ty::Infer(_) | ty::Error(_) => None,
145 impl<D: Copy + Debug + Ord + Eq> SimplifiedTypeGen<D> {
146 pub fn def(self) -> Option<D> {
149 | ForeignSimplifiedType(d)
150 | TraitSimplifiedType(d)
151 | ClosureSimplifiedType(d)
152 | GeneratorSimplifiedType(d)
153 | OpaqueSimplifiedType(d) => Some(d),
158 pub fn map_def<U, F>(self, map: F) -> SimplifiedTypeGen<U>
161 U: Copy + Debug + Ord + Eq,
164 BoolSimplifiedType => BoolSimplifiedType,
165 CharSimplifiedType => CharSimplifiedType,
166 IntSimplifiedType(t) => IntSimplifiedType(t),
167 UintSimplifiedType(t) => UintSimplifiedType(t),
168 FloatSimplifiedType(t) => FloatSimplifiedType(t),
169 AdtSimplifiedType(d) => AdtSimplifiedType(map(d)),
170 ForeignSimplifiedType(d) => ForeignSimplifiedType(map(d)),
171 StrSimplifiedType => StrSimplifiedType,
172 ArraySimplifiedType => ArraySimplifiedType,
173 SliceSimplifiedType => SliceSimplifiedType,
174 RefSimplifiedType(m) => RefSimplifiedType(m),
175 PtrSimplifiedType(m) => PtrSimplifiedType(m),
176 NeverSimplifiedType => NeverSimplifiedType,
177 MarkerTraitObjectSimplifiedType => MarkerTraitObjectSimplifiedType,
178 TupleSimplifiedType(n) => TupleSimplifiedType(n),
179 TraitSimplifiedType(d) => TraitSimplifiedType(map(d)),
180 ClosureSimplifiedType(d) => ClosureSimplifiedType(map(d)),
181 GeneratorSimplifiedType(d) => GeneratorSimplifiedType(map(d)),
182 GeneratorWitnessSimplifiedType(n) => GeneratorWitnessSimplifiedType(n),
183 OpaqueSimplifiedType(d) => OpaqueSimplifiedType(map(d)),
184 FunctionSimplifiedType(n) => FunctionSimplifiedType(n),
185 ParameterSimplifiedType => ParameterSimplifiedType,
190 impl<'a, D> HashStable<StableHashingContext<'a>> for SimplifiedTypeGen<D>
192 D: Copy + Debug + Ord + Eq + HashStable<StableHashingContext<'a>>,
194 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
195 mem::discriminant(self).hash_stable(hcx, hasher);
200 | ArraySimplifiedType
201 | SliceSimplifiedType
202 | NeverSimplifiedType
203 | ParameterSimplifiedType
204 | MarkerTraitObjectSimplifiedType => {
207 RefSimplifiedType(m) | PtrSimplifiedType(m) => m.hash_stable(hcx, hasher),
208 IntSimplifiedType(t) => t.hash_stable(hcx, hasher),
209 UintSimplifiedType(t) => t.hash_stable(hcx, hasher),
210 FloatSimplifiedType(t) => t.hash_stable(hcx, hasher),
211 AdtSimplifiedType(d) => d.hash_stable(hcx, hasher),
212 TupleSimplifiedType(n) => n.hash_stable(hcx, hasher),
213 TraitSimplifiedType(d) => d.hash_stable(hcx, hasher),
214 ClosureSimplifiedType(d) => d.hash_stable(hcx, hasher),
215 GeneratorSimplifiedType(d) => d.hash_stable(hcx, hasher),
216 GeneratorWitnessSimplifiedType(n) => n.hash_stable(hcx, hasher),
217 OpaqueSimplifiedType(d) => d.hash_stable(hcx, hasher),
218 FunctionSimplifiedType(n) => n.hash_stable(hcx, hasher),
219 ForeignSimplifiedType(d) => d.hash_stable(hcx, hasher),