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