]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_trait_selection/src/traits/query/type_op/normalize.rs
Rollup merge of #102854 - semarie:openbsd-immutablestack, r=m-ou-se
[rust.git] / compiler / rustc_trait_selection / src / traits / query / type_op / normalize.rs
1 use crate::infer::canonical::{Canonicalized, CanonicalizedQueryResponse};
2 use crate::traits::query::Fallible;
3 use rustc_middle::ty::fold::TypeFoldable;
4 use rustc_middle::ty::{self, Lift, ParamEnvAnd, Ty, TyCtxt};
5 use std::fmt;
6
7 pub use rustc_middle::traits::query::type_op::Normalize;
8
9 impl<'tcx, T> super::QueryTypeOp<'tcx> for Normalize<T>
10 where
11     T: Normalizable<'tcx> + 'tcx,
12 {
13     type QueryResponse = T;
14
15     fn try_fast_path(_tcx: TyCtxt<'tcx>, key: &ParamEnvAnd<'tcx, Self>) -> Option<T> {
16         if !key.value.value.has_projections() { Some(key.value.value) } else { None }
17     }
18
19     fn perform_query(
20         tcx: TyCtxt<'tcx>,
21         canonicalized: Canonicalized<'tcx, ParamEnvAnd<'tcx, Self>>,
22     ) -> Fallible<CanonicalizedQueryResponse<'tcx, Self::QueryResponse>> {
23         T::type_op_method(tcx, canonicalized)
24     }
25 }
26
27 pub trait Normalizable<'tcx>: fmt::Debug + TypeFoldable<'tcx> + Lift<'tcx> + Copy {
28     fn type_op_method(
29         tcx: TyCtxt<'tcx>,
30         canonicalized: Canonicalized<'tcx, ParamEnvAnd<'tcx, Normalize<Self>>>,
31     ) -> Fallible<CanonicalizedQueryResponse<'tcx, Self>>;
32 }
33
34 impl<'tcx> Normalizable<'tcx> for Ty<'tcx> {
35     fn type_op_method(
36         tcx: TyCtxt<'tcx>,
37         canonicalized: Canonicalized<'tcx, ParamEnvAnd<'tcx, Normalize<Self>>>,
38     ) -> Fallible<CanonicalizedQueryResponse<'tcx, Self>> {
39         tcx.type_op_normalize_ty(canonicalized)
40     }
41 }
42
43 impl<'tcx> Normalizable<'tcx> for ty::Predicate<'tcx> {
44     fn type_op_method(
45         tcx: TyCtxt<'tcx>,
46         canonicalized: Canonicalized<'tcx, ParamEnvAnd<'tcx, Normalize<Self>>>,
47     ) -> Fallible<CanonicalizedQueryResponse<'tcx, Self>> {
48         tcx.type_op_normalize_predicate(canonicalized)
49     }
50 }
51
52 impl<'tcx> Normalizable<'tcx> for ty::PolyFnSig<'tcx> {
53     fn type_op_method(
54         tcx: TyCtxt<'tcx>,
55         canonicalized: Canonicalized<'tcx, ParamEnvAnd<'tcx, Normalize<Self>>>,
56     ) -> Fallible<CanonicalizedQueryResponse<'tcx, Self>> {
57         tcx.type_op_normalize_poly_fn_sig(canonicalized)
58     }
59 }
60
61 impl<'tcx> Normalizable<'tcx> for ty::FnSig<'tcx> {
62     fn type_op_method(
63         tcx: TyCtxt<'tcx>,
64         canonicalized: Canonicalized<'tcx, ParamEnvAnd<'tcx, Normalize<Self>>>,
65     ) -> Fallible<CanonicalizedQueryResponse<'tcx, Self>> {
66         tcx.type_op_normalize_fn_sig(canonicalized)
67     }
68 }