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)]
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 AnonSimplifiedType(D),
50 FunctionSimplifiedType(usize),
51 ParameterSimplifiedType,
52 ForeignSimplifiedType(DefId),
55 /// Tries to simplify a type by dropping type parameters, deref'ing away any reference types, etc.
56 /// The idea is to get something simple that we can use to quickly decide if two types could unify
57 /// during method lookup.
59 /// If `can_simplify_params` is false, then we will fail to simplify type parameters entirely. This
60 /// is useful when those type parameters would be instantiated with fresh type variables, since
61 /// then we can't say much about whether two types would unify. Put another way,
62 /// `can_simplify_params` should be true if type parameters appear free in `ty` and `false` if they
63 /// are to be considered bound.
64 pub fn simplify_type<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
66 can_simplify_params: bool)
67 -> Option<SimplifiedType>
70 ty::TyBool => Some(BoolSimplifiedType),
71 ty::TyChar => Some(CharSimplifiedType),
72 ty::TyInt(int_type) => Some(IntSimplifiedType(int_type)),
73 ty::TyUint(uint_type) => Some(UintSimplifiedType(uint_type)),
74 ty::TyFloat(float_type) => Some(FloatSimplifiedType(float_type)),
75 ty::TyAdt(def, _) => Some(AdtSimplifiedType(def.did)),
76 ty::TyStr => Some(StrSimplifiedType),
77 ty::TyArray(..) | ty::TySlice(_) => Some(ArraySimplifiedType),
78 ty::TyRawPtr(_) => Some(PtrSimplifiedType),
79 ty::TyDynamic(ref trait_info, ..) => {
80 trait_info.principal().map(|p| TraitSimplifiedType(p.def_id()))
83 // since we introduce auto-refs during method lookup, we
84 // just treat &T and T as equivalent from the point of
85 // view of possibly unifying
86 simplify_type(tcx, mt.ty, can_simplify_params)
88 ty::TyFnDef(def_id, _) |
89 ty::TyClosure(def_id, _) => {
90 Some(ClosureSimplifiedType(def_id))
92 ty::TyGenerator(def_id, _, _) => {
93 Some(GeneratorSimplifiedType(def_id))
95 ty::TyNever => Some(NeverSimplifiedType),
96 ty::TyTuple(ref tys, _) => {
97 Some(TupleSimplifiedType(tys.len()))
99 ty::TyFnPtr(ref f) => {
100 Some(FunctionSimplifiedType(f.skip_binder().inputs().len()))
102 ty::TyProjection(_) | ty::TyParam(_) => {
103 if can_simplify_params {
104 // In normalized types, projections don't unify with
105 // anything. when lazy normalization happens, this
106 // will change. It would still be nice to have a way
107 // to deal with known-not-to-unify-with-anything
108 // projections (e.g. the likes of <__S as Encoder>::Error).
109 Some(ParameterSimplifiedType)
114 ty::TyAnon(def_id, _) => {
115 Some(AnonSimplifiedType(def_id))
117 ty::TyForeign(def_id) => {
118 Some(ForeignSimplifiedType(def_id))
120 ty::TyInfer(_) | ty::TyError => None,
124 impl<D: Copy + Debug + Ord + Eq + Hash> SimplifiedTypeGen<D> {
125 pub fn map_def<U, F>(self, map: F) -> SimplifiedTypeGen<U>
127 U: Copy + Debug + Ord + Eq + Hash,
130 BoolSimplifiedType => BoolSimplifiedType,
131 CharSimplifiedType => CharSimplifiedType,
132 IntSimplifiedType(t) => IntSimplifiedType(t),
133 UintSimplifiedType(t) => UintSimplifiedType(t),
134 FloatSimplifiedType(t) => FloatSimplifiedType(t),
135 AdtSimplifiedType(d) => AdtSimplifiedType(map(d)),
136 StrSimplifiedType => StrSimplifiedType,
137 ArraySimplifiedType => ArraySimplifiedType,
138 PtrSimplifiedType => PtrSimplifiedType,
139 NeverSimplifiedType => NeverSimplifiedType,
140 TupleSimplifiedType(n) => TupleSimplifiedType(n),
141 TraitSimplifiedType(d) => TraitSimplifiedType(map(d)),
142 ClosureSimplifiedType(d) => ClosureSimplifiedType(map(d)),
143 GeneratorSimplifiedType(d) => GeneratorSimplifiedType(map(d)),
144 AnonSimplifiedType(d) => AnonSimplifiedType(map(d)),
145 FunctionSimplifiedType(n) => FunctionSimplifiedType(n),
146 ParameterSimplifiedType => ParameterSimplifiedType,
147 ForeignSimplifiedType(d) => ForeignSimplifiedType(d),
152 impl<'gcx, D> HashStable<StableHashingContext<'gcx>> for SimplifiedTypeGen<D>
153 where D: Copy + Debug + Ord + Eq + Hash +
154 HashStable<StableHashingContext<'gcx>>,
156 fn hash_stable<W: StableHasherResult>(&self,
157 hcx: &mut StableHashingContext<'gcx>,
158 hasher: &mut StableHasher<W>) {
159 mem::discriminant(self).hash_stable(hcx, hasher);
164 ArraySimplifiedType |
166 NeverSimplifiedType |
167 ParameterSimplifiedType => {
170 IntSimplifiedType(t) => t.hash_stable(hcx, hasher),
171 UintSimplifiedType(t) => t.hash_stable(hcx, hasher),
172 FloatSimplifiedType(t) => t.hash_stable(hcx, hasher),
173 AdtSimplifiedType(d) => d.hash_stable(hcx, hasher),
174 TupleSimplifiedType(n) => n.hash_stable(hcx, hasher),
175 TraitSimplifiedType(d) => d.hash_stable(hcx, hasher),
176 ClosureSimplifiedType(d) => d.hash_stable(hcx, hasher),
177 GeneratorSimplifiedType(d) => d.hash_stable(hcx, hasher),
178 AnonSimplifiedType(d) => d.hash_stable(hcx, hasher),
179 FunctionSimplifiedType(n) => n.hash_stable(hcx, hasher),
180 ForeignSimplifiedType(d) => d.hash_stable(hcx, hasher),