]> git.proxmox.com Git - rustc.git/blame - src/librustc/ty/fast_reject.rs
New upstream version 1.16.0+dfsg1
[rustc.git] / src / librustc / 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
54a0048b
SL
11use hir::def_id::DefId;
12use ty::{self, Ty, TyCtxt};
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),
9e0c209e 25 AdtSimplifiedType(DefId),
1a4d82fc 26 StrSimplifiedType,
c30ab7b3 27 ArraySimplifiedType,
1a4d82fc 28 PtrSimplifiedType,
5bcae85e 29 NeverSimplifiedType,
c34b1796 30 TupleSimplifiedType(usize),
e9174d1e 31 TraitSimplifiedType(DefId),
e9174d1e 32 ClosureSimplifiedType(DefId),
5bcae85e 33 AnonSimplifiedType(DefId),
c34b1796 34 FunctionSimplifiedType(usize),
1a4d82fc
JJ
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.
a7813a04
XL
47pub fn simplify_type<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
48 ty: Ty,
49 can_simplify_params: bool)
50 -> Option<SimplifiedType>
1a4d82fc
JJ
51{
52 match ty.sty {
62682a34
SL
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)),
9e0c209e 58 ty::TyAdt(def, _) => Some(AdtSimplifiedType(def.did)),
62682a34 59 ty::TyStr => Some(StrSimplifiedType),
c30ab7b3 60 ty::TyArray(..) | ty::TySlice(_) => Some(ArraySimplifiedType),
62682a34 61 ty::TyRawPtr(_) => Some(PtrSimplifiedType),
476ff2be
SL
62 ty::TyDynamic(ref trait_info, ..) => {
63 trait_info.principal().map(|p| TraitSimplifiedType(p.def_id()))
1a4d82fc 64 }
62682a34 65 ty::TyRef(_, mt) => {
1a4d82fc
JJ
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 }
62682a34 71 ty::TyClosure(def_id, _) => {
85aaf69f 72 Some(ClosureSimplifiedType(def_id))
1a4d82fc 73 }
5bcae85e 74 ty::TyNever => Some(NeverSimplifiedType),
62682a34 75 ty::TyTuple(ref tys) => {
1a4d82fc
JJ
76 Some(TupleSimplifiedType(tys.len()))
77 }
9e0c209e 78 ty::TyFnDef(.., ref f) | ty::TyFnPtr(ref f) => {
476ff2be 79 Some(FunctionSimplifiedType(f.sig.skip_binder().inputs().len()))
1a4d82fc 80 }
e9174d1e 81 ty::TyProjection(_) | ty::TyParam(_) => {
1a4d82fc 82 if can_simplify_params {
e9174d1e
SL
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).
1a4d82fc
JJ
88 Some(ParameterSimplifiedType)
89 } else {
90 None
91 }
92 }
5bcae85e
SL
93 ty::TyAnon(def_id, _) => {
94 Some(AnonSimplifiedType(def_id))
95 }
62682a34 96 ty::TyInfer(_) | ty::TyError => None,
1a4d82fc
JJ
97 }
98}