]> git.proxmox.com Git - rustc.git/blob - src/librustc/traits/query/type_op/prove_predicate.rs
New upstream version 1.31.0~beta.4+dfsg1
[rustc.git] / src / librustc / traits / query / type_op / prove_predicate.rs
1 // Copyright 2016 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 infer::canonical::{Canonical, Canonicalized, CanonicalizedQueryResponse, QueryResponse};
12 use traits::query::Fallible;
13 use ty::{ParamEnvAnd, Predicate, TyCtxt};
14
15 #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
16 pub struct ProvePredicate<'tcx> {
17 pub predicate: Predicate<'tcx>,
18 }
19
20 impl<'tcx> ProvePredicate<'tcx> {
21 pub fn new(predicate: Predicate<'tcx>) -> Self {
22 ProvePredicate { predicate }
23 }
24 }
25
26 impl<'gcx: 'tcx, 'tcx> super::QueryTypeOp<'gcx, 'tcx> for ProvePredicate<'tcx> {
27 type QueryResponse = ();
28
29 fn try_fast_path(
30 tcx: TyCtxt<'_, 'gcx, 'tcx>,
31 key: &ParamEnvAnd<'tcx, Self>,
32 ) -> Option<Self::QueryResponse> {
33 // Proving Sized, very often on "obviously sized" types like
34 // `&T`, accounts for about 60% percentage of the predicates
35 // we have to prove. No need to canonicalize and all that for
36 // such cases.
37 if let Predicate::Trait(trait_ref) = key.value.predicate {
38 if let Some(sized_def_id) = tcx.lang_items().sized_trait() {
39 if trait_ref.def_id() == sized_def_id {
40 if trait_ref.skip_binder().self_ty().is_trivially_sized(tcx) {
41 return Some(());
42 }
43 }
44 }
45 }
46
47 None
48 }
49
50 fn perform_query(
51 tcx: TyCtxt<'_, 'gcx, 'tcx>,
52 canonicalized: Canonicalized<'gcx, ParamEnvAnd<'tcx, Self>>,
53 ) -> Fallible<CanonicalizedQueryResponse<'gcx, ()>> {
54 tcx.type_op_prove_predicate(canonicalized)
55 }
56
57 fn shrink_to_tcx_lifetime(
58 v: &'a CanonicalizedQueryResponse<'gcx, ()>,
59 ) -> &'a Canonical<'tcx, QueryResponse<'tcx, ()>> {
60 v
61 }
62 }
63
64 BraceStructTypeFoldableImpl! {
65 impl<'tcx> TypeFoldable<'tcx> for ProvePredicate<'tcx> {
66 predicate,
67 }
68 }
69
70 BraceStructLiftImpl! {
71 impl<'a, 'tcx> Lift<'tcx> for ProvePredicate<'a> {
72 type Lifted = ProvePredicate<'tcx>;
73 predicate,
74 }
75 }
76
77 impl_stable_hash_for! {
78 struct ProvePredicate<'tcx> { predicate }
79 }