]> git.lizzy.rs Git - rust.git/blob - src/librustc_middle/ty/fast_reject.rs
Rework `rustc_serialize`
[rust.git] / src / librustc_middle / ty / fast_reject.rs
1 use crate::ich::StableHashingContext;
2 use crate::ty::{self, Ty, TyCtxt};
3 use rustc_ast::ast;
4 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
5 use rustc_hir::def_id::DefId;
6 use std::fmt::Debug;
7 use std::hash::Hash;
8 use std::mem;
9
10 use self::SimplifiedTypeGen::*;
11
12 pub type SimplifiedType = SimplifiedTypeGen<DefId>;
13
14 /// See `simplify_type`
15 ///
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>
22 where
23     D: Copy + Debug + Ord + Eq,
24 {
25     BoolSimplifiedType,
26     CharSimplifiedType,
27     IntSimplifiedType(ast::IntTy),
28     UintSimplifiedType(ast::UintTy),
29     FloatSimplifiedType(ast::FloatTy),
30     AdtSimplifiedType(D),
31     StrSimplifiedType,
32     ArraySimplifiedType,
33     PtrSimplifiedType,
34     NeverSimplifiedType,
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),
47 }
48
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.
52 ///
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.
58 pub fn simplify_type(
59     tcx: TyCtxt<'_>,
60     ty: Ty<'_>,
61     can_simplify_params: bool,
62 ) -> Option<SimplifiedType> {
63     match ty.kind {
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))
76             }
77             _ => Some(MarkerTraitObjectSimplifiedType),
78         },
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)
84         }
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()))
89         }
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)
101             } else {
102                 None
103             }
104         }
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,
108     }
109 }
110
111 impl<D: Copy + Debug + Ord + Eq> SimplifiedTypeGen<D> {
112     pub fn map_def<U, F>(self, map: F) -> SimplifiedTypeGen<U>
113     where
114         F: Fn(D) -> U,
115         U: Copy + Debug + Ord + Eq,
116     {
117         match self {
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),
138         }
139     }
140 }
141
142 impl<'a, D> HashStable<StableHashingContext<'a>> for SimplifiedTypeGen<D>
143 where
144     D: Copy + Debug + Ord + Eq + HashStable<StableHashingContext<'a>>,
145 {
146     fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
147         mem::discriminant(self).hash_stable(hcx, hasher);
148         match *self {
149             BoolSimplifiedType
150             | CharSimplifiedType
151             | StrSimplifiedType
152             | ArraySimplifiedType
153             | PtrSimplifiedType
154             | NeverSimplifiedType
155             | ParameterSimplifiedType
156             | MarkerTraitObjectSimplifiedType => {
157                 // nothing to do
158             }
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),
171         }
172     }
173 }