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 /// Tries to simplify a type by only returning the outermost injective¹ layer, if one exists.
59 /// The idea is to get something simple that we can use to quickly decide if two types could unify,
60 /// for example during method lookup.
62 /// A special case here are parameters and projections. Projections can be normalized to
63 /// a different type, meaning that `<T as Trait>::Assoc` and `u8` can be unified, even though
64 /// their outermost layer is different while parameters like `T` of impls are later replaced
65 /// with an inference variable, which then also allows unification with other types.
67 /// When using `SimplifyParams::Yes`, we still return a simplified type for params and projections²,
68 /// the reasoning for this can be seen at the places doing this.
71 /// ¹ meaning that if two outermost layers are different, then the whole types are also different.
72 /// ² FIXME(@lcnr): this seems like it can actually end up being unsound with the way it's used during
73 /// candidate selection. We do not consider non blanket impls for `<_ as Trait>::Assoc` even
74 /// though `_` can be inferred to a concrete type later at which point a concrete impl
75 /// could actually apply. After experimenting for about an hour I wasn't able to cause any issues
76 /// this way so I am not going to change this until we actually find an issue as I am really
77 /// interesting in getting an actual test for this.
81 can_simplify_params: SimplifyParams,
82 ) -> Option<SimplifiedType> {
84 ty::Bool => Some(BoolSimplifiedType),
85 ty::Char => Some(CharSimplifiedType),
86 ty::Int(int_type) => Some(IntSimplifiedType(int_type)),
87 ty::Uint(uint_type) => Some(UintSimplifiedType(uint_type)),
88 ty::Float(float_type) => Some(FloatSimplifiedType(float_type)),
89 ty::Adt(def, _) => Some(AdtSimplifiedType(def.did)),
90 ty::Str => Some(StrSimplifiedType),
91 ty::Array(..) => Some(ArraySimplifiedType),
92 ty::Slice(..) => Some(SliceSimplifiedType),
93 ty::RawPtr(ptr) => Some(PtrSimplifiedType(ptr.mutbl)),
94 ty::Dynamic(ref trait_info, ..) => match trait_info.principal_def_id() {
95 Some(principal_def_id) if !tcx.trait_is_auto(principal_def_id) => {
96 Some(TraitSimplifiedType(principal_def_id))
98 _ => Some(MarkerTraitObjectSimplifiedType),
100 ty::Ref(_, _, mutbl) => Some(RefSimplifiedType(mutbl)),
101 ty::FnDef(def_id, _) | ty::Closure(def_id, _) => Some(ClosureSimplifiedType(def_id)),
102 ty::Generator(def_id, _, _) => Some(GeneratorSimplifiedType(def_id)),
103 ty::GeneratorWitness(ref tys) => {
104 Some(GeneratorWitnessSimplifiedType(tys.skip_binder().len()))
106 ty::Never => Some(NeverSimplifiedType),
107 ty::Tuple(ref tys) => Some(TupleSimplifiedType(tys.len())),
108 ty::FnPtr(ref f) => Some(FunctionSimplifiedType(f.skip_binder().inputs().len())),
109 ty::Projection(_) | ty::Param(_) => {
110 if can_simplify_params == SimplifyParams::Yes {
111 // In normalized types, projections don't unify with
112 // anything. when lazy normalization happens, this
113 // will change. It would still be nice to have a way
114 // to deal with known-not-to-unify-with-anything
115 // projections (e.g., the likes of <__S as Encoder>::Error).
116 Some(ParameterSimplifiedType)
121 ty::Opaque(def_id, _) => Some(OpaqueSimplifiedType(def_id)),
122 ty::Foreign(def_id) => Some(ForeignSimplifiedType(def_id)),
123 ty::Placeholder(..) | ty::Bound(..) | ty::Infer(_) | ty::Error(_) => None,
127 impl<D: Copy + Debug + Ord + Eq> SimplifiedTypeGen<D> {
128 pub fn def(self) -> Option<D> {
131 | ForeignSimplifiedType(d)
132 | TraitSimplifiedType(d)
133 | ClosureSimplifiedType(d)
134 | GeneratorSimplifiedType(d)
135 | OpaqueSimplifiedType(d) => Some(d),
140 pub fn map_def<U, F>(self, map: F) -> SimplifiedTypeGen<U>
143 U: Copy + Debug + Ord + Eq,
146 BoolSimplifiedType => BoolSimplifiedType,
147 CharSimplifiedType => CharSimplifiedType,
148 IntSimplifiedType(t) => IntSimplifiedType(t),
149 UintSimplifiedType(t) => UintSimplifiedType(t),
150 FloatSimplifiedType(t) => FloatSimplifiedType(t),
151 AdtSimplifiedType(d) => AdtSimplifiedType(map(d)),
152 ForeignSimplifiedType(d) => ForeignSimplifiedType(map(d)),
153 StrSimplifiedType => StrSimplifiedType,
154 ArraySimplifiedType => ArraySimplifiedType,
155 SliceSimplifiedType => SliceSimplifiedType,
156 RefSimplifiedType(m) => RefSimplifiedType(m),
157 PtrSimplifiedType(m) => PtrSimplifiedType(m),
158 NeverSimplifiedType => NeverSimplifiedType,
159 MarkerTraitObjectSimplifiedType => MarkerTraitObjectSimplifiedType,
160 TupleSimplifiedType(n) => TupleSimplifiedType(n),
161 TraitSimplifiedType(d) => TraitSimplifiedType(map(d)),
162 ClosureSimplifiedType(d) => ClosureSimplifiedType(map(d)),
163 GeneratorSimplifiedType(d) => GeneratorSimplifiedType(map(d)),
164 GeneratorWitnessSimplifiedType(n) => GeneratorWitnessSimplifiedType(n),
165 OpaqueSimplifiedType(d) => OpaqueSimplifiedType(map(d)),
166 FunctionSimplifiedType(n) => FunctionSimplifiedType(n),
167 ParameterSimplifiedType => ParameterSimplifiedType,
172 impl<'a, D> HashStable<StableHashingContext<'a>> for SimplifiedTypeGen<D>
174 D: Copy + Debug + Ord + Eq + HashStable<StableHashingContext<'a>>,
176 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
177 mem::discriminant(self).hash_stable(hcx, hasher);
182 | ArraySimplifiedType
183 | SliceSimplifiedType
184 | NeverSimplifiedType
185 | ParameterSimplifiedType
186 | MarkerTraitObjectSimplifiedType => {
189 RefSimplifiedType(m) | PtrSimplifiedType(m) => m.hash_stable(hcx, hasher),
190 IntSimplifiedType(t) => t.hash_stable(hcx, hasher),
191 UintSimplifiedType(t) => t.hash_stable(hcx, hasher),
192 FloatSimplifiedType(t) => t.hash_stable(hcx, hasher),
193 AdtSimplifiedType(d) => d.hash_stable(hcx, hasher),
194 TupleSimplifiedType(n) => n.hash_stable(hcx, hasher),
195 TraitSimplifiedType(d) => d.hash_stable(hcx, hasher),
196 ClosureSimplifiedType(d) => d.hash_stable(hcx, hasher),
197 GeneratorSimplifiedType(d) => d.hash_stable(hcx, hasher),
198 GeneratorWitnessSimplifiedType(n) => n.hash_stable(hcx, hasher),
199 OpaqueSimplifiedType(d) => d.hash_stable(hcx, hasher),
200 FunctionSimplifiedType(n) => n.hash_stable(hcx, hasher),
201 ForeignSimplifiedType(d) => d.hash_stable(hcx, hasher),