]> git.proxmox.com Git - rustc.git/blob - src/librustc/ty/fast_reject.rs
New upstream version 1.17.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 AdtSimplifiedType(DefId),
26 StrSimplifiedType,
27 ArraySimplifiedType,
28 PtrSimplifiedType,
29 NeverSimplifiedType,
30 TupleSimplifiedType(usize),
31 TraitSimplifiedType(DefId),
32 ClosureSimplifiedType(DefId),
33 AnonSimplifiedType(DefId),
34 FunctionSimplifiedType(usize),
35 ParameterSimplifiedType,
36 }
37
38 /// Tries to simplify a type by dropping type parameters, deref'ing away any reference types, etc.
39 /// The idea is to get something simple that we can use to quickly decide if two types could unify
40 /// during method lookup.
41 ///
42 /// If `can_simplify_params` is false, then we will fail to simplify type parameters entirely. This
43 /// is useful when those type parameters would be instantiated with fresh type variables, since
44 /// then we can't say much about whether two types would unify. Put another way,
45 /// `can_simplify_params` should be true if type parameters appear free in `ty` and `false` if they
46 /// are to be considered bound.
47 pub fn simplify_type<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
48 ty: Ty,
49 can_simplify_params: bool)
50 -> Option<SimplifiedType>
51 {
52 match ty.sty {
53 ty::TyBool => Some(BoolSimplifiedType),
54 ty::TyChar => Some(CharSimplifiedType),
55 ty::TyInt(int_type) => Some(IntSimplifiedType(int_type)),
56 ty::TyUint(uint_type) => Some(UintSimplifiedType(uint_type)),
57 ty::TyFloat(float_type) => Some(FloatSimplifiedType(float_type)),
58 ty::TyAdt(def, _) => Some(AdtSimplifiedType(def.did)),
59 ty::TyStr => Some(StrSimplifiedType),
60 ty::TyArray(..) | ty::TySlice(_) => Some(ArraySimplifiedType),
61 ty::TyRawPtr(_) => Some(PtrSimplifiedType),
62 ty::TyDynamic(ref trait_info, ..) => {
63 trait_info.principal().map(|p| TraitSimplifiedType(p.def_id()))
64 }
65 ty::TyRef(_, mt) => {
66 // since we introduce auto-refs during method lookup, we
67 // just treat &T and T as equivalent from the point of
68 // view of possibly unifying
69 simplify_type(tcx, mt.ty, can_simplify_params)
70 }
71 ty::TyClosure(def_id, _) => {
72 Some(ClosureSimplifiedType(def_id))
73 }
74 ty::TyNever => Some(NeverSimplifiedType),
75 ty::TyTuple(ref tys, _) => {
76 Some(TupleSimplifiedType(tys.len()))
77 }
78 ty::TyFnDef(.., ref f) | ty::TyFnPtr(ref f) => {
79 Some(FunctionSimplifiedType(f.skip_binder().inputs().len()))
80 }
81 ty::TyProjection(_) | ty::TyParam(_) => {
82 if can_simplify_params {
83 // In normalized types, projections don't unify with
84 // anything. when lazy normalization happens, this
85 // will change. It would still be nice to have a way
86 // to deal with known-not-to-unify-with-anything
87 // projections (e.g. the likes of <__S as Encoder>::Error).
88 Some(ParameterSimplifiedType)
89 } else {
90 None
91 }
92 }
93 ty::TyAnon(def_id, _) => {
94 Some(AnonSimplifiedType(def_id))
95 }
96 ty::TyInfer(_) | ty::TyError => None,
97 }
98 }