]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_traits/src/type_op.rs
Rollup merge of #87685 - notriddle:lazy-from-docs, r=dtolnay
[rust.git] / compiler / rustc_traits / src / type_op.rs
1 use rustc_hir as hir;
2 use rustc_hir::def_id::DefId;
3 use rustc_infer::infer::at::ToTrace;
4 use rustc_infer::infer::canonical::{Canonical, QueryResponse};
5 use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
6 use rustc_infer::traits::TraitEngineExt as _;
7 use rustc_middle::ty::query::Providers;
8 use rustc_middle::ty::subst::{GenericArg, Subst, UserSelfTy, UserSubsts};
9 use rustc_middle::ty::{self, FnSig, Lift, PolyFnSig, Ty, TyCtxt, TypeFoldable, Variance};
10 use rustc_middle::ty::{ParamEnv, ParamEnvAnd, Predicate, ToPredicate};
11 use rustc_span::DUMMY_SP;
12 use rustc_trait_selection::infer::InferCtxtBuilderExt;
13 use rustc_trait_selection::infer::InferCtxtExt;
14 use rustc_trait_selection::traits::query::normalize::AtExt;
15 use rustc_trait_selection::traits::query::type_op::ascribe_user_type::AscribeUserType;
16 use rustc_trait_selection::traits::query::type_op::eq::Eq;
17 use rustc_trait_selection::traits::query::type_op::normalize::Normalize;
18 use rustc_trait_selection::traits::query::type_op::prove_predicate::ProvePredicate;
19 use rustc_trait_selection::traits::query::type_op::subtype::Subtype;
20 use rustc_trait_selection::traits::query::{Fallible, NoSolution};
21 use rustc_trait_selection::traits::{Normalized, Obligation, ObligationCause, TraitEngine};
22 use std::fmt;
23
24 crate fn provide(p: &mut Providers) {
25     *p = Providers {
26         type_op_ascribe_user_type,
27         type_op_eq,
28         type_op_prove_predicate,
29         type_op_subtype,
30         type_op_normalize_ty,
31         type_op_normalize_predicate,
32         type_op_normalize_fn_sig,
33         type_op_normalize_poly_fn_sig,
34         ..*p
35     };
36 }
37
38 fn type_op_ascribe_user_type<'tcx>(
39     tcx: TyCtxt<'tcx>,
40     canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, AscribeUserType<'tcx>>>,
41 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, ()>>, NoSolution> {
42     tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, |infcx, fulfill_cx, key| {
43         let (param_env, AscribeUserType { mir_ty, def_id, user_substs }) = key.into_parts();
44
45         debug!(
46             "type_op_ascribe_user_type: mir_ty={:?} def_id={:?} user_substs={:?}",
47             mir_ty, def_id, user_substs
48         );
49
50         let mut cx = AscribeUserTypeCx { infcx, param_env, fulfill_cx };
51         cx.relate_mir_and_user_ty(mir_ty, def_id, user_substs)?;
52
53         Ok(())
54     })
55 }
56
57 struct AscribeUserTypeCx<'me, 'tcx> {
58     infcx: &'me InferCtxt<'me, 'tcx>,
59     param_env: ParamEnv<'tcx>,
60     fulfill_cx: &'me mut dyn TraitEngine<'tcx>,
61 }
62
63 impl AscribeUserTypeCx<'me, 'tcx> {
64     fn normalize<T>(&mut self, value: T) -> T
65     where
66         T: TypeFoldable<'tcx>,
67     {
68         self.infcx
69             .partially_normalize_associated_types_in(
70                 ObligationCause::misc(DUMMY_SP, hir::CRATE_HIR_ID),
71                 self.param_env,
72                 value,
73             )
74             .into_value_registering_obligations(self.infcx, self.fulfill_cx)
75     }
76
77     fn relate<T>(&mut self, a: T, variance: Variance, b: T) -> Result<(), NoSolution>
78     where
79         T: ToTrace<'tcx>,
80     {
81         self.infcx
82             .at(&ObligationCause::dummy(), self.param_env)
83             .relate(a, variance, b)?
84             .into_value_registering_obligations(self.infcx, self.fulfill_cx);
85         Ok(())
86     }
87
88     fn prove_predicate(&mut self, predicate: Predicate<'tcx>) {
89         self.fulfill_cx.register_predicate_obligation(
90             self.infcx,
91             Obligation::new(ObligationCause::dummy(), self.param_env, predicate),
92         );
93     }
94
95     fn tcx(&self) -> TyCtxt<'tcx> {
96         self.infcx.tcx
97     }
98
99     fn subst<T>(&self, value: T, substs: &[GenericArg<'tcx>]) -> T
100     where
101         T: TypeFoldable<'tcx>,
102     {
103         value.subst(self.tcx(), substs)
104     }
105
106     fn relate_mir_and_user_ty(
107         &mut self,
108         mir_ty: Ty<'tcx>,
109         def_id: DefId,
110         user_substs: UserSubsts<'tcx>,
111     ) -> Result<(), NoSolution> {
112         let UserSubsts { user_self_ty, substs } = user_substs;
113         let tcx = self.tcx();
114
115         let ty = tcx.type_of(def_id);
116         let ty = self.subst(ty, substs);
117         debug!("relate_type_and_user_type: ty of def-id is {:?}", ty);
118         let ty = self.normalize(ty);
119
120         self.relate(mir_ty, Variance::Invariant, ty)?;
121
122         // Prove the predicates coming along with `def_id`.
123         //
124         // Also, normalize the `instantiated_predicates`
125         // because otherwise we wind up with duplicate "type
126         // outlives" error messages.
127         let instantiated_predicates =
128             self.tcx().predicates_of(def_id).instantiate(self.tcx(), substs);
129         for instantiated_predicate in instantiated_predicates.predicates {
130             let instantiated_predicate = self.normalize(instantiated_predicate);
131             self.prove_predicate(instantiated_predicate);
132         }
133
134         if let Some(UserSelfTy { impl_def_id, self_ty }) = user_self_ty {
135             let impl_self_ty = self.tcx().type_of(impl_def_id);
136             let impl_self_ty = self.subst(impl_self_ty, &substs);
137             let impl_self_ty = self.normalize(impl_self_ty);
138
139             self.relate(self_ty, Variance::Invariant, impl_self_ty)?;
140
141             self.prove_predicate(
142                 ty::PredicateKind::WellFormed(impl_self_ty.into()).to_predicate(self.tcx()),
143             );
144         }
145
146         // In addition to proving the predicates, we have to
147         // prove that `ty` is well-formed -- this is because
148         // the WF of `ty` is predicated on the substs being
149         // well-formed, and we haven't proven *that*. We don't
150         // want to prove the WF of types from  `substs` directly because they
151         // haven't been normalized.
152         //
153         // FIXME(nmatsakis): Well, perhaps we should normalize
154         // them?  This would only be relevant if some input
155         // type were ill-formed but did not appear in `ty`,
156         // which...could happen with normalization...
157         self.prove_predicate(ty::PredicateKind::WellFormed(ty.into()).to_predicate(self.tcx()));
158         Ok(())
159     }
160 }
161
162 fn type_op_eq<'tcx>(
163     tcx: TyCtxt<'tcx>,
164     canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Eq<'tcx>>>,
165 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, ()>>, NoSolution> {
166     tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, |infcx, fulfill_cx, key| {
167         let (param_env, Eq { a, b }) = key.into_parts();
168         infcx
169             .at(&ObligationCause::dummy(), param_env)
170             .eq(a, b)?
171             .into_value_registering_obligations(infcx, fulfill_cx);
172         Ok(())
173     })
174 }
175
176 fn type_op_normalize<T>(
177     infcx: &InferCtxt<'_, 'tcx>,
178     fulfill_cx: &mut dyn TraitEngine<'tcx>,
179     key: ParamEnvAnd<'tcx, Normalize<T>>,
180 ) -> Fallible<T>
181 where
182     T: fmt::Debug + TypeFoldable<'tcx> + Lift<'tcx>,
183 {
184     let (param_env, Normalize { value }) = key.into_parts();
185     let Normalized { value, obligations } =
186         infcx.at(&ObligationCause::dummy(), param_env).normalize(value)?;
187     fulfill_cx.register_predicate_obligations(infcx, obligations);
188     Ok(value)
189 }
190
191 fn type_op_normalize_ty(
192     tcx: TyCtxt<'tcx>,
193     canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Normalize<Ty<'tcx>>>>,
194 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, Ty<'tcx>>>, NoSolution> {
195     tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, type_op_normalize)
196 }
197
198 fn type_op_normalize_predicate(
199     tcx: TyCtxt<'tcx>,
200     canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Normalize<Predicate<'tcx>>>>,
201 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, Predicate<'tcx>>>, NoSolution> {
202     tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, type_op_normalize)
203 }
204
205 fn type_op_normalize_fn_sig(
206     tcx: TyCtxt<'tcx>,
207     canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Normalize<FnSig<'tcx>>>>,
208 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, FnSig<'tcx>>>, NoSolution> {
209     tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, type_op_normalize)
210 }
211
212 fn type_op_normalize_poly_fn_sig(
213     tcx: TyCtxt<'tcx>,
214     canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Normalize<PolyFnSig<'tcx>>>>,
215 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, PolyFnSig<'tcx>>>, NoSolution> {
216     tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, type_op_normalize)
217 }
218
219 fn type_op_subtype<'tcx>(
220     tcx: TyCtxt<'tcx>,
221     canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Subtype<'tcx>>>,
222 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, ()>>, NoSolution> {
223     tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, |infcx, fulfill_cx, key| {
224         let (param_env, Subtype { sub, sup }) = key.into_parts();
225         infcx
226             .at(&ObligationCause::dummy(), param_env)
227             .sup(sup, sub)?
228             .into_value_registering_obligations(infcx, fulfill_cx);
229         Ok(())
230     })
231 }
232
233 fn type_op_prove_predicate<'tcx>(
234     tcx: TyCtxt<'tcx>,
235     canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, ProvePredicate<'tcx>>>,
236 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, ()>>, NoSolution> {
237     tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, |infcx, fulfill_cx, key| {
238         let (param_env, ProvePredicate { predicate }) = key.into_parts();
239         fulfill_cx.register_predicate_obligation(
240             infcx,
241             Obligation::new(ObligationCause::dummy(), param_env, predicate),
242         );
243         Ok(())
244     })
245 }