]> git.proxmox.com Git - rustc.git/blob - src/librustc/middle/fast_reject.rs
b33e5a802f1582f6a639431938abaf3c026d8709
[rustc.git] / src / librustc / middle / 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 middle::ty::{self, Ty};
12 use syntax::ast;
13
14 use self::SimplifiedType::*;
15
16 /// See `simplify_type
17 #[derive(Clone, Copy, PartialEq, Eq, Hash)]
18 pub enum SimplifiedType {
19 BoolSimplifiedType,
20 CharSimplifiedType,
21 IntSimplifiedType(ast::IntTy),
22 UintSimplifiedType(ast::UintTy),
23 FloatSimplifiedType(ast::FloatTy),
24 EnumSimplifiedType(ast::DefId),
25 StrSimplifiedType,
26 VecSimplifiedType,
27 PtrSimplifiedType,
28 TupleSimplifiedType(uint),
29 TraitSimplifiedType(ast::DefId),
30 StructSimplifiedType(ast::DefId),
31 ClosureSimplifiedType(ast::DefId),
32 FunctionSimplifiedType(uint),
33 ParameterSimplifiedType,
34 }
35
36 /// Tries to simplify a type by dropping type parameters, deref'ing away any reference types, etc.
37 /// The idea is to get something simple that we can use to quickly decide if two types could unify
38 /// during method lookup.
39 ///
40 /// If `can_simplify_params` is false, then we will fail to simplify type parameters entirely. This
41 /// is useful when those type parameters would be instantiated with fresh type variables, since
42 /// then we can't say much about whether two types would unify. Put another way,
43 /// `can_simplify_params` should be true if type parameters appear free in `ty` and `false` if they
44 /// are to be considered bound.
45 pub fn simplify_type(tcx: &ty::ctxt,
46 ty: Ty,
47 can_simplify_params: bool)
48 -> Option<SimplifiedType>
49 {
50 match ty.sty {
51 ty::ty_bool => Some(BoolSimplifiedType),
52 ty::ty_char => Some(CharSimplifiedType),
53 ty::ty_int(int_type) => Some(IntSimplifiedType(int_type)),
54 ty::ty_uint(uint_type) => Some(UintSimplifiedType(uint_type)),
55 ty::ty_float(float_type) => Some(FloatSimplifiedType(float_type)),
56 ty::ty_enum(def_id, _) => Some(EnumSimplifiedType(def_id)),
57 ty::ty_str => Some(StrSimplifiedType),
58 ty::ty_vec(..) => Some(VecSimplifiedType),
59 ty::ty_ptr(_) => Some(PtrSimplifiedType),
60 ty::ty_trait(ref trait_info) => {
61 Some(TraitSimplifiedType(trait_info.principal_def_id()))
62 }
63 ty::ty_struct(def_id, _) => {
64 Some(StructSimplifiedType(def_id))
65 }
66 ty::ty_rptr(_, mt) => {
67 // since we introduce auto-refs during method lookup, we
68 // just treat &T and T as equivalent from the point of
69 // view of possibly unifying
70 simplify_type(tcx, mt.ty, can_simplify_params)
71 }
72 ty::ty_uniq(_) => {
73 // treat like we would treat `Box`
74 let def_id = tcx.lang_items.owned_box().unwrap();
75 Some(StructSimplifiedType(def_id))
76 }
77 ty::ty_closure(def_id, _, _) => {
78 Some(ClosureSimplifiedType(def_id))
79 }
80 ty::ty_tup(ref tys) => {
81 Some(TupleSimplifiedType(tys.len()))
82 }
83 ty::ty_bare_fn(_, ref f) => {
84 Some(FunctionSimplifiedType(f.sig.0.inputs.len()))
85 }
86 ty::ty_projection(_) => {
87 None
88 }
89 ty::ty_param(_) => {
90 if can_simplify_params {
91 Some(ParameterSimplifiedType)
92 } else {
93 None
94 }
95 }
96 ty::ty_open(_) | ty::ty_infer(_) | ty::ty_err => None,
97 }
98 }
99