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 middle::ty::{self, Ty};
14 use self::SimplifiedType::*;
16 /// See `simplify_type
17 #[derive(Clone, Copy, PartialEq, Eq, Hash)]
18 pub enum SimplifiedType {
21 IntSimplifiedType(ast::IntTy),
22 UintSimplifiedType(ast::UintTy),
23 FloatSimplifiedType(ast::FloatTy),
24 EnumSimplifiedType(ast::DefId),
28 TupleSimplifiedType(uint),
29 TraitSimplifiedType(ast::DefId),
30 StructSimplifiedType(ast::DefId),
31 UnboxedClosureSimplifiedType(ast::DefId),
32 FunctionSimplifiedType(uint),
33 ParameterSimplifiedType,
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.
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,
47 can_simplify_params: bool)
48 -> Option<SimplifiedType>
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()))
63 ty::ty_struct(def_id, _) => {
64 Some(StructSimplifiedType(def_id))
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)
73 // treat like we would treat `Box`
74 let def_id = tcx.lang_items.owned_box().unwrap();
75 Some(StructSimplifiedType(def_id))
77 ty::ty_unboxed_closure(def_id, _, _) => {
78 Some(UnboxedClosureSimplifiedType(def_id))
80 ty::ty_tup(ref tys) => {
81 Some(TupleSimplifiedType(tys.len()))
83 ty::ty_closure(ref f) => {
84 Some(FunctionSimplifiedType(f.sig.0.inputs.len()))
86 ty::ty_bare_fn(_, ref f) => {
87 Some(FunctionSimplifiedType(f.sig.0.inputs.len()))
89 ty::ty_projection(_) => {
93 if can_simplify_params {
94 Some(ParameterSimplifiedType)
99 ty::ty_open(_) | ty::ty_infer(_) | ty::ty_err => None,