]> git.lizzy.rs Git - rust.git/blob - src/librustc/traits/query/type_op/normalize.rs
Retire BraceStructLiftImpl.
[rust.git] / src / librustc / traits / query / type_op / normalize.rs
1 use crate::infer::canonical::{Canonicalized, CanonicalizedQueryResponse};
2 use std::fmt;
3 use crate::traits::query::Fallible;
4 use crate::ty::fold::TypeFoldable;
5 use crate::ty::{self, Lift, ParamEnvAnd, Ty, TyCtxt};
6
7 #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, TypeFoldable, Lift)]
8 pub struct Normalize<T> {
9     pub value: T,
10 }
11
12 impl<'tcx, T> Normalize<T>
13 where
14     T: fmt::Debug + TypeFoldable<'tcx>,
15 {
16     pub fn new(value: T) -> Self {
17         Self { value }
18     }
19 }
20
21 impl<'tcx, T> super::QueryTypeOp<'tcx> for Normalize<T>
22 where
23     T: Normalizable<'tcx> + 'tcx,
24 {
25     type QueryResponse = T;
26
27     fn try_fast_path(_tcx: TyCtxt<'tcx>, key: &ParamEnvAnd<'tcx, Self>) -> Option<T> {
28         if !key.value.value.has_projections() {
29             Some(key.value.value)
30         } else {
31             None
32         }
33     }
34
35     fn perform_query(
36         tcx: TyCtxt<'tcx>,
37         canonicalized: Canonicalized<'tcx, ParamEnvAnd<'tcx, Self>>,
38     ) -> Fallible<CanonicalizedQueryResponse<'tcx, Self::QueryResponse>> {
39         T::type_op_method(tcx, canonicalized)
40     }
41 }
42
43 pub trait Normalizable<'tcx>: fmt::Debug + TypeFoldable<'tcx> + Lift<'tcx> + Copy {
44     fn type_op_method(
45         tcx: TyCtxt<'tcx>,
46         canonicalized: Canonicalized<'tcx, ParamEnvAnd<'tcx, Normalize<Self>>>,
47     ) -> Fallible<CanonicalizedQueryResponse<'tcx, Self>>;
48 }
49
50 impl Normalizable<'tcx> for Ty<'tcx> {
51     fn type_op_method(
52         tcx: TyCtxt<'tcx>,
53         canonicalized: Canonicalized<'tcx, ParamEnvAnd<'tcx, Normalize<Self>>>,
54     ) -> Fallible<CanonicalizedQueryResponse<'tcx, Self>> {
55         tcx.type_op_normalize_ty(canonicalized)
56     }
57 }
58
59 impl Normalizable<'tcx> for ty::Predicate<'tcx> {
60     fn type_op_method(
61         tcx: TyCtxt<'tcx>,
62         canonicalized: Canonicalized<'tcx, ParamEnvAnd<'tcx, Normalize<Self>>>,
63     ) -> Fallible<CanonicalizedQueryResponse<'tcx, Self>> {
64         tcx.type_op_normalize_predicate(canonicalized)
65     }
66 }
67
68 impl Normalizable<'tcx> for ty::PolyFnSig<'tcx> {
69     fn type_op_method(
70         tcx: TyCtxt<'tcx>,
71         canonicalized: Canonicalized<'tcx, ParamEnvAnd<'tcx, Normalize<Self>>>,
72     ) -> Fallible<CanonicalizedQueryResponse<'tcx, Self>> {
73         tcx.type_op_normalize_poly_fn_sig(canonicalized)
74     }
75 }
76
77 impl Normalizable<'tcx> for ty::FnSig<'tcx> {
78     fn type_op_method(
79         tcx: TyCtxt<'tcx>,
80         canonicalized: Canonicalized<'tcx, ParamEnvAnd<'tcx, Normalize<Self>>>,
81     ) -> Fallible<CanonicalizedQueryResponse<'tcx, Self>> {
82         tcx.type_op_normalize_fn_sig(canonicalized)
83     }
84 }
85
86 impl_stable_hash_for! {
87     impl<T> for struct Normalize<T> {
88         value
89     }
90 }