]> git.lizzy.rs Git - rust.git/blob - src/librustc/ty/fast_reject.rs
Auto merge of #46024 - estebank:no-variant, r=petrochenkov
[rust.git] / src / librustc / ty / fast_reject.rs
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.
4 //
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.
10
11 use hir::def_id::DefId;
12 use ich::StableHashingContext;
13 use rustc_data_structures::stable_hasher::{StableHasher, StableHasherResult,
14                                            HashStable};
15 use std::fmt::Debug;
16 use std::hash::Hash;
17 use std::mem;
18 use syntax::ast;
19 use ty::{self, Ty, TyCtxt};
20
21 use self::SimplifiedTypeGen::*;
22
23 pub type SimplifiedType = SimplifiedTypeGen<DefId>;
24
25 /// See `simplify_type`
26 ///
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
34 {
35     BoolSimplifiedType,
36     CharSimplifiedType,
37     IntSimplifiedType(ast::IntTy),
38     UintSimplifiedType(ast::UintTy),
39     FloatSimplifiedType(ast::FloatTy),
40     AdtSimplifiedType(D),
41     StrSimplifiedType,
42     ArraySimplifiedType,
43     PtrSimplifiedType,
44     NeverSimplifiedType,
45     TupleSimplifiedType(usize),
46     TraitSimplifiedType(D),
47     ClosureSimplifiedType(D),
48     GeneratorSimplifiedType(D),
49     AnonSimplifiedType(D),
50     FunctionSimplifiedType(usize),
51     ParameterSimplifiedType,
52     ForeignSimplifiedType(DefId),
53 }
54
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.
58 ///
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>,
65                                      ty: Ty,
66                                      can_simplify_params: bool)
67                                      -> Option<SimplifiedType>
68 {
69     match ty.sty {
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()))
81         }
82         ty::TyRef(_, mt) => {
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)
87         }
88         ty::TyFnDef(def_id, _) |
89         ty::TyClosure(def_id, _) => {
90             Some(ClosureSimplifiedType(def_id))
91         }
92         ty::TyGenerator(def_id, _, _) => {
93             Some(GeneratorSimplifiedType(def_id))
94         }
95         ty::TyNever => Some(NeverSimplifiedType),
96         ty::TyTuple(ref tys, _) => {
97             Some(TupleSimplifiedType(tys.len()))
98         }
99         ty::TyFnPtr(ref f) => {
100             Some(FunctionSimplifiedType(f.skip_binder().inputs().len()))
101         }
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)
110             } else {
111                 None
112             }
113         }
114         ty::TyAnon(def_id, _) => {
115             Some(AnonSimplifiedType(def_id))
116         }
117         ty::TyForeign(def_id) => {
118             Some(ForeignSimplifiedType(def_id))
119         }
120         ty::TyInfer(_) | ty::TyError => None,
121     }
122 }
123
124 impl<D: Copy + Debug + Ord + Eq + Hash> SimplifiedTypeGen<D> {
125     pub fn map_def<U, F>(self, map: F) -> SimplifiedTypeGen<U>
126         where F: Fn(D) -> U,
127               U: Copy + Debug + Ord + Eq + Hash,
128     {
129         match self {
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),
148         }
149     }
150 }
151
152 impl<'gcx, D> HashStable<StableHashingContext<'gcx>> for SimplifiedTypeGen<D>
153     where D: Copy + Debug + Ord + Eq + Hash +
154              HashStable<StableHashingContext<'gcx>>,
155 {
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);
160         match *self {
161             BoolSimplifiedType |
162             CharSimplifiedType |
163             StrSimplifiedType |
164             ArraySimplifiedType |
165             PtrSimplifiedType |
166             NeverSimplifiedType |
167             ParameterSimplifiedType => {
168                 // nothing to do
169             }
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),
181         }
182     }
183 }