]> git.proxmox.com Git - rustc.git/blame - src/librustc/middle/ty/fast_reject.rs
Imported Upstream version 1.8.0+dfsg1
[rustc.git] / src / librustc / middle / ty / fast_reject.rs
CommitLineData
1a4d82fc
JJ
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
e9174d1e 11use middle::def_id::DefId;
1a4d82fc 12use middle::ty::{self, Ty};
b039eaaf 13use syntax::ast;
1a4d82fc
JJ
14
15use self::SimplifiedType::*;
16
17/// See `simplify_type
9cc50fc6 18#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
1a4d82fc
JJ
19pub enum SimplifiedType {
20 BoolSimplifiedType,
21 CharSimplifiedType,
b039eaaf
SL
22 IntSimplifiedType(ast::IntTy),
23 UintSimplifiedType(ast::UintTy),
24 FloatSimplifiedType(ast::FloatTy),
e9174d1e 25 EnumSimplifiedType(DefId),
1a4d82fc
JJ
26 StrSimplifiedType,
27 VecSimplifiedType,
28 PtrSimplifiedType,
c34b1796 29 TupleSimplifiedType(usize),
e9174d1e
SL
30 TraitSimplifiedType(DefId),
31 StructSimplifiedType(DefId),
32 ClosureSimplifiedType(DefId),
c34b1796 33 FunctionSimplifiedType(usize),
1a4d82fc
JJ
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.
46pub fn simplify_type(tcx: &ty::ctxt,
47 ty: Ty,
48 can_simplify_params: bool)
49 -> Option<SimplifiedType>
50{
51 match ty.sty {
62682a34
SL
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)),
e9174d1e 57 ty::TyEnum(def, _) => Some(EnumSimplifiedType(def.did)),
62682a34
SL
58 ty::TyStr => Some(StrSimplifiedType),
59 ty::TyArray(..) | ty::TySlice(_) => Some(VecSimplifiedType),
60 ty::TyRawPtr(_) => Some(PtrSimplifiedType),
61 ty::TyTrait(ref trait_info) => {
1a4d82fc
JJ
62 Some(TraitSimplifiedType(trait_info.principal_def_id()))
63 }
e9174d1e
SL
64 ty::TyStruct(def, _) => {
65 Some(StructSimplifiedType(def.did))
1a4d82fc 66 }
62682a34 67 ty::TyRef(_, mt) => {
1a4d82fc
JJ
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 }
62682a34 73 ty::TyBox(_) => {
1a4d82fc 74 // treat like we would treat `Box`
c1a9b12d
SL
75 match tcx.lang_items.require_owned_box() {
76 Ok(def_id) => Some(StructSimplifiedType(def_id)),
77 Err(msg) => tcx.sess.fatal(&msg),
78 }
1a4d82fc 79 }
62682a34 80 ty::TyClosure(def_id, _) => {
85aaf69f 81 Some(ClosureSimplifiedType(def_id))
1a4d82fc 82 }
62682a34 83 ty::TyTuple(ref tys) => {
1a4d82fc
JJ
84 Some(TupleSimplifiedType(tys.len()))
85 }
62682a34 86 ty::TyBareFn(_, ref f) => {
1a4d82fc
JJ
87 Some(FunctionSimplifiedType(f.sig.0.inputs.len()))
88 }
e9174d1e 89 ty::TyProjection(_) | ty::TyParam(_) => {
1a4d82fc 90 if can_simplify_params {
e9174d1e
SL
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).
1a4d82fc
JJ
96 Some(ParameterSimplifiedType)
97 } else {
98 None
99 }
100 }
62682a34 101 ty::TyInfer(_) | ty::TyError => None,
1a4d82fc
JJ
102 }
103}