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