]> git.lizzy.rs Git - rust.git/blob - src/librustc/ty/fast_reject.rs
ee1544d2d996d3df5905d29c6838ec2b4439e6e5
[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 ty::{self, Ty, TyCtxt};
13 use syntax::ast;
14
15 use self::SimplifiedType::*;
16
17 /// See `simplify_type
18 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
19 pub enum SimplifiedType {
20     BoolSimplifiedType,
21     CharSimplifiedType,
22     IntSimplifiedType(ast::IntTy),
23     UintSimplifiedType(ast::UintTy),
24     FloatSimplifiedType(ast::FloatTy),
25     AdtSimplifiedType(DefId),
26     StrSimplifiedType,
27     VecSimplifiedType,
28     PtrSimplifiedType,
29     NeverSimplifiedType,
30     TupleSimplifiedType(usize),
31     TraitSimplifiedType(DefId),
32     ClosureSimplifiedType(DefId),
33     AnonSimplifiedType(DefId),
34     FunctionSimplifiedType(usize),
35     ParameterSimplifiedType,
36 }
37
38 /// Tries to simplify a type by dropping type parameters, deref'ing away any reference types, etc.
39 /// The idea is to get something simple that we can use to quickly decide if two types could unify
40 /// during method lookup.
41 ///
42 /// If `can_simplify_params` is false, then we will fail to simplify type parameters entirely. This
43 /// is useful when those type parameters would be instantiated with fresh type variables, since
44 /// then we can't say much about whether two types would unify. Put another way,
45 /// `can_simplify_params` should be true if type parameters appear free in `ty` and `false` if they
46 /// are to be considered bound.
47 pub fn simplify_type<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
48                                      ty: Ty,
49                                      can_simplify_params: bool)
50                                      -> Option<SimplifiedType>
51 {
52     match ty.sty {
53         ty::TyBool => Some(BoolSimplifiedType),
54         ty::TyChar => Some(CharSimplifiedType),
55         ty::TyInt(int_type) => Some(IntSimplifiedType(int_type)),
56         ty::TyUint(uint_type) => Some(UintSimplifiedType(uint_type)),
57         ty::TyFloat(float_type) => Some(FloatSimplifiedType(float_type)),
58         ty::TyAdt(def, _) => Some(AdtSimplifiedType(def.did)),
59         ty::TyStr => Some(StrSimplifiedType),
60         ty::TyArray(..) | ty::TySlice(_) => Some(VecSimplifiedType),
61         ty::TyRawPtr(_) => Some(PtrSimplifiedType),
62         ty::TyTrait(ref trait_info) => {
63             Some(TraitSimplifiedType(trait_info.principal.def_id()))
64         }
65         ty::TyRef(_, mt) => {
66             // since we introduce auto-refs during method lookup, we
67             // just treat &T and T as equivalent from the point of
68             // view of possibly unifying
69             simplify_type(tcx, mt.ty, can_simplify_params)
70         }
71         ty::TyBox(_) => {
72             // treat like we would treat `Box`
73             match tcx.lang_items.require_owned_box() {
74                 Ok(def_id) => Some(AdtSimplifiedType(def_id)),
75                 Err(msg) => tcx.sess.fatal(&msg),
76             }
77         }
78         ty::TyClosure(def_id, _) => {
79             Some(ClosureSimplifiedType(def_id))
80         }
81         ty::TyNever => Some(NeverSimplifiedType),
82         ty::TyTuple(ref tys) => {
83             Some(TupleSimplifiedType(tys.len()))
84         }
85         ty::TyFnDef(.., ref f) | ty::TyFnPtr(ref f) => {
86             Some(FunctionSimplifiedType(f.sig.0.inputs.len()))
87         }
88         ty::TyProjection(_) | ty::TyParam(_) => {
89             if can_simplify_params {
90                 // In normalized types, projections don't unify with
91                 // anything. when lazy normalization happens, this
92                 // will change. It would still be nice to have a way
93                 // to deal with known-not-to-unify-with-anything
94                 // projections (e.g. the likes of <__S as Encoder>::Error).
95                 Some(ParameterSimplifiedType)
96             } else {
97                 None
98             }
99         }
100         ty::TyAnon(def_id, _) => {
101             Some(AnonSimplifiedType(def_id))
102         }
103         ty::TyInfer(_) | ty::TyError => None,
104     }
105 }