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 Some(TraitSimplifiedType(trait_info.principal().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::UnnormalizedProjection(..) => bug!("only used with chalk-engine"),
107 ty::Projection(_) | ty::Param(_) => {
108 if can_simplify_params {
109 // In normalized types, projections don't unify with
110 // anything. when lazy normalization happens, this
111 // will change. It would still be nice to have a way
112 // to deal with known-not-to-unify-with-anything
113 // projections (e.g. the likes of <__S as Encoder>::Error).
114 Some(ParameterSimplifiedType)
119 ty::Opaque(def_id, _) => {
120 Some(OpaqueSimplifiedType(def_id))
122 ty::Foreign(def_id) => {
123 Some(ForeignSimplifiedType(def_id))
125 ty::Placeholder(..) | ty::Bound(..) | ty::Infer(_) | ty::Error => None,
129 impl<D: Copy + Debug + Ord + Eq + Hash> SimplifiedTypeGen<D> {
130 pub fn map_def<U, F>(self, map: F) -> SimplifiedTypeGen<U>
132 U: Copy + Debug + Ord + Eq + Hash,
135 BoolSimplifiedType => BoolSimplifiedType,
136 CharSimplifiedType => CharSimplifiedType,
137 IntSimplifiedType(t) => IntSimplifiedType(t),
138 UintSimplifiedType(t) => UintSimplifiedType(t),
139 FloatSimplifiedType(t) => FloatSimplifiedType(t),
140 AdtSimplifiedType(d) => AdtSimplifiedType(map(d)),
141 StrSimplifiedType => StrSimplifiedType,
142 ArraySimplifiedType => ArraySimplifiedType,
143 PtrSimplifiedType => PtrSimplifiedType,
144 NeverSimplifiedType => NeverSimplifiedType,
145 TupleSimplifiedType(n) => TupleSimplifiedType(n),
146 TraitSimplifiedType(d) => TraitSimplifiedType(map(d)),
147 ClosureSimplifiedType(d) => ClosureSimplifiedType(map(d)),
148 GeneratorSimplifiedType(d) => GeneratorSimplifiedType(map(d)),
149 GeneratorWitnessSimplifiedType(n) => GeneratorWitnessSimplifiedType(n),
150 OpaqueSimplifiedType(d) => OpaqueSimplifiedType(map(d)),
151 FunctionSimplifiedType(n) => FunctionSimplifiedType(n),
152 ParameterSimplifiedType => ParameterSimplifiedType,
153 ForeignSimplifiedType(d) => ForeignSimplifiedType(d),
158 impl<'a, 'gcx, D> HashStable<StableHashingContext<'a>> for SimplifiedTypeGen<D>
159 where D: Copy + Debug + Ord + Eq + Hash +
160 HashStable<StableHashingContext<'a>>,
162 fn hash_stable<W: StableHasherResult>(&self,
163 hcx: &mut StableHashingContext<'a>,
164 hasher: &mut StableHasher<W>) {
165 mem::discriminant(self).hash_stable(hcx, hasher);
170 ArraySimplifiedType |
172 NeverSimplifiedType |
173 ParameterSimplifiedType => {
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),