]> git.proxmox.com Git - rustc.git/blob - src/librustc/ty/fast_reject.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.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 EnumSimplifiedType(DefId),
26 StrSimplifiedType,
27 VecSimplifiedType,
28 PtrSimplifiedType,
29 TupleSimplifiedType(usize),
30 TraitSimplifiedType(DefId),
31 StructSimplifiedType(DefId),
32 ClosureSimplifiedType(DefId),
33 FunctionSimplifiedType(usize),
34 ParameterSimplifiedType,
35 }
36
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.
40 ///
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: &TyCtxt,
47 ty: Ty,
48 can_simplify_params: bool)
49 -> Option<SimplifiedType>
50 {
51 match ty.sty {
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()))
63 }
64 ty::TyStruct(def, _) => {
65 Some(StructSimplifiedType(def.did))
66 }
67 ty::TyRef(_, mt) => {
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)
72 }
73 ty::TyBox(_) => {
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),
78 }
79 }
80 ty::TyClosure(def_id, _) => {
81 Some(ClosureSimplifiedType(def_id))
82 }
83 ty::TyTuple(ref tys) => {
84 Some(TupleSimplifiedType(tys.len()))
85 }
86 ty::TyFnDef(_, _, ref f) | ty::TyFnPtr(ref f) => {
87 Some(FunctionSimplifiedType(f.sig.0.inputs.len()))
88 }
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)
97 } else {
98 None
99 }
100 }
101 ty::TyInfer(_) | ty::TyError => None,
102 }
103 }