1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use hir::def_id::DefId;
12 use ich::StableHashingContext;
13 use rustc_data_structures::stable_hasher::{StableHasher, StableHasherResult,
19 use ty::{self, Ty, TyCtxt};
21 use self::SimplifiedTypeGen::*;
23 pub type SimplifiedType = SimplifiedTypeGen<DefId>;
25 /// See `simplify_type`
27 /// Note that we keep this type generic over the type of identifier it uses
28 /// because we sometimes need to use SimplifiedTypeGen values as stable sorting
29 /// keys (in which case we use a DefPathHash as id-type) but in the general case
30 /// the non-stable but fast to construct DefId-version is the better choice.
31 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, RustcEncodable, RustcDecodable)]
32 pub enum SimplifiedTypeGen<D>
33 where D: Copy + Debug + Ord + Eq + Hash
37 IntSimplifiedType(ast::IntTy),
38 UintSimplifiedType(ast::UintTy),
39 FloatSimplifiedType(ast::FloatTy),
45 TupleSimplifiedType(usize),
46 TraitSimplifiedType(D),
47 ClosureSimplifiedType(D),
48 GeneratorSimplifiedType(D),
49 GeneratorWitnessSimplifiedType(usize),
50 OpaqueSimplifiedType(D),
51 FunctionSimplifiedType(usize),
52 ParameterSimplifiedType,
53 ForeignSimplifiedType(DefId),
56 /// Tries to simplify a type by dropping type parameters, deref'ing away any reference types, etc.
57 /// The idea is to get something simple that we can use to quickly decide if two types could unify
58 /// during method lookup.
60 /// If `can_simplify_params` is false, then we will fail to simplify type parameters entirely. This
61 /// is useful when those type parameters would be instantiated with fresh type variables, since
62 /// then we can't say much about whether two types would unify. Put another way,
63 /// `can_simplify_params` should be true if type parameters appear free in `ty` and `false` if they
64 /// are to be considered bound.
65 pub fn simplify_type<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
67 can_simplify_params: bool)
68 -> Option<SimplifiedType>
71 ty::Bool => Some(BoolSimplifiedType),
72 ty::Char => Some(CharSimplifiedType),
73 ty::Int(int_type) => Some(IntSimplifiedType(int_type)),
74 ty::Uint(uint_type) => Some(UintSimplifiedType(uint_type)),
75 ty::Float(float_type) => Some(FloatSimplifiedType(float_type)),
76 ty::Adt(def, _) => Some(AdtSimplifiedType(def.did)),
77 ty::Str => Some(StrSimplifiedType),
78 ty::Array(..) | ty::Slice(_) => Some(ArraySimplifiedType),
79 ty::RawPtr(_) => Some(PtrSimplifiedType),
80 ty::Dynamic(ref trait_info, ..) => {
81 trait_info.principal().map(|p| TraitSimplifiedType(p.def_id()))
83 ty::Ref(_, ty, _) => {
84 // since we introduce auto-refs during method lookup, we
85 // just treat &T and T as equivalent from the point of
86 // view of possibly unifying
87 simplify_type(tcx, ty, can_simplify_params)
89 ty::FnDef(def_id, _) |
90 ty::Closure(def_id, _) => {
91 Some(ClosureSimplifiedType(def_id))
93 ty::Generator(def_id, _, _) => {
94 Some(GeneratorSimplifiedType(def_id))
96 ty::GeneratorWitness(ref tys) => {
97 Some(GeneratorWitnessSimplifiedType(tys.skip_binder().len()))
99 ty::Never => Some(NeverSimplifiedType),
100 ty::Tuple(ref tys) => {
101 Some(TupleSimplifiedType(tys.len()))
103 ty::FnPtr(ref f) => {
104 Some(FunctionSimplifiedType(f.skip_binder().inputs().len()))
106 ty::Projection(_) | ty::Param(_) => {
107 if can_simplify_params {
108 // In normalized types, projections don't unify with
109 // anything. when lazy normalization happens, this
110 // will change. It would still be nice to have a way
111 // to deal with known-not-to-unify-with-anything
112 // projections (e.g. the likes of <__S as Encoder>::Error).
113 Some(ParameterSimplifiedType)
118 ty::Opaque(def_id, _) => {
119 Some(OpaqueSimplifiedType(def_id))
121 ty::Foreign(def_id) => {
122 Some(ForeignSimplifiedType(def_id))
124 ty::Infer(_) | ty::Error => None,
128 impl<D: Copy + Debug + Ord + Eq + Hash> SimplifiedTypeGen<D> {
129 pub fn map_def<U, F>(self, map: F) -> SimplifiedTypeGen<U>
131 U: Copy + Debug + Ord + Eq + Hash,
134 BoolSimplifiedType => BoolSimplifiedType,
135 CharSimplifiedType => CharSimplifiedType,
136 IntSimplifiedType(t) => IntSimplifiedType(t),
137 UintSimplifiedType(t) => UintSimplifiedType(t),
138 FloatSimplifiedType(t) => FloatSimplifiedType(t),
139 AdtSimplifiedType(d) => AdtSimplifiedType(map(d)),
140 StrSimplifiedType => StrSimplifiedType,
141 ArraySimplifiedType => ArraySimplifiedType,
142 PtrSimplifiedType => PtrSimplifiedType,
143 NeverSimplifiedType => NeverSimplifiedType,
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, 'gcx, D> HashStable<StableHashingContext<'a>> for SimplifiedTypeGen<D>
158 where D: Copy + Debug + Ord + Eq + Hash +
159 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 => {
175 IntSimplifiedType(t) => t.hash_stable(hcx, hasher),
176 UintSimplifiedType(t) => t.hash_stable(hcx, hasher),
177 FloatSimplifiedType(t) => t.hash_stable(hcx, hasher),
178 AdtSimplifiedType(d) => d.hash_stable(hcx, hasher),
179 TupleSimplifiedType(n) => n.hash_stable(hcx, hasher),
180 TraitSimplifiedType(d) => d.hash_stable(hcx, hasher),
181 ClosureSimplifiedType(d) => d.hash_stable(hcx, hasher),
182 GeneratorSimplifiedType(d) => d.hash_stable(hcx, hasher),
183 GeneratorWitnessSimplifiedType(n) => n.hash_stable(hcx, hasher),
184 OpaqueSimplifiedType(d) => d.hash_stable(hcx, hasher),
185 FunctionSimplifiedType(n) => n.hash_stable(hcx, hasher),
186 ForeignSimplifiedType(d) => d.hash_stable(hcx, hasher),