]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_traits/src/type_op.rs
Rollup merge of #102587 - Enselic:rustc-unix_sigpipe, r=jackh726
[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::{DefiningAnchor, InferCtxt, TyCtxtInferExt};
6 use rustc_infer::traits::{ObligationCauseCode, TraitEngineExt as _};
7 use rustc_middle::ty::query::Providers;
8 use rustc_middle::ty::{
9     self, EarlyBinder, FnSig, Lift, PolyFnSig, Ty, TyCtxt, TypeFoldable, Variance,
10 };
11 use rustc_middle::ty::{GenericArg, UserSelfTy, UserSubsts};
12 use rustc_middle::ty::{ParamEnv, ParamEnvAnd, Predicate, ToPredicate};
13 use rustc_span::{Span, DUMMY_SP};
14 use rustc_trait_selection::infer::InferCtxtBuilderExt;
15 use rustc_trait_selection::infer::InferCtxtExt;
16 use rustc_trait_selection::traits::query::normalize::AtExt;
17 use rustc_trait_selection::traits::query::type_op::ascribe_user_type::AscribeUserType;
18 use rustc_trait_selection::traits::query::type_op::eq::Eq;
19 use rustc_trait_selection::traits::query::type_op::normalize::Normalize;
20 use rustc_trait_selection::traits::query::type_op::prove_predicate::ProvePredicate;
21 use rustc_trait_selection::traits::query::type_op::subtype::Subtype;
22 use rustc_trait_selection::traits::query::{Fallible, NoSolution};
23 use rustc_trait_selection::traits::{Normalized, Obligation, ObligationCause, TraitEngine};
24 use std::fmt;
25 use std::iter::zip;
26
27 pub(crate) fn provide(p: &mut Providers) {
28     *p = Providers {
29         type_op_ascribe_user_type,
30         type_op_eq,
31         type_op_prove_predicate,
32         type_op_subtype,
33         type_op_normalize_ty,
34         type_op_normalize_predicate,
35         type_op_normalize_fn_sig,
36         type_op_normalize_poly_fn_sig,
37         ..*p
38     };
39 }
40
41 fn type_op_ascribe_user_type<'tcx>(
42     tcx: TyCtxt<'tcx>,
43     canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, AscribeUserType<'tcx>>>,
44 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, ()>>, NoSolution> {
45     tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, |infcx, fulfill_cx, key| {
46         type_op_ascribe_user_type_with_span(infcx, fulfill_cx, key, None)
47     })
48 }
49
50 /// The core of the `type_op_ascribe_user_type` query: for diagnostics purposes in NLL HRTB errors,
51 /// this query can be re-run to better track the span of the obligation cause, and improve the error
52 /// message. Do not call directly unless you're in that very specific context.
53 pub fn type_op_ascribe_user_type_with_span<'a, 'tcx: 'a>(
54     infcx: &'a InferCtxt<'tcx>,
55     fulfill_cx: &'a mut dyn TraitEngine<'tcx>,
56     key: ParamEnvAnd<'tcx, AscribeUserType<'tcx>>,
57     span: Option<Span>,
58 ) -> Result<(), NoSolution> {
59     let (param_env, AscribeUserType { mir_ty, def_id, user_substs }) = key.into_parts();
60     debug!(
61         "type_op_ascribe_user_type: mir_ty={:?} def_id={:?} user_substs={:?}",
62         mir_ty, def_id, user_substs
63     );
64
65     let mut cx = AscribeUserTypeCx { infcx, param_env, span: span.unwrap_or(DUMMY_SP), fulfill_cx };
66     cx.relate_mir_and_user_ty(mir_ty, def_id, user_substs)?;
67     Ok(())
68 }
69
70 struct AscribeUserTypeCx<'me, 'tcx> {
71     infcx: &'me InferCtxt<'tcx>,
72     param_env: ParamEnv<'tcx>,
73     span: Span,
74     fulfill_cx: &'me mut dyn TraitEngine<'tcx>,
75 }
76
77 impl<'me, 'tcx> AscribeUserTypeCx<'me, 'tcx> {
78     fn normalize<T>(&mut self, value: T) -> T
79     where
80         T: TypeFoldable<'tcx>,
81     {
82         self.normalize_with_cause(value, ObligationCause::misc(self.span, hir::CRATE_HIR_ID))
83     }
84
85     fn normalize_with_cause<T>(&mut self, value: T, cause: ObligationCause<'tcx>) -> T
86     where
87         T: TypeFoldable<'tcx>,
88     {
89         self.infcx
90             .partially_normalize_associated_types_in(cause, self.param_env, value)
91             .into_value_registering_obligations(self.infcx, self.fulfill_cx)
92     }
93
94     fn relate<T>(&mut self, a: T, variance: Variance, b: T) -> Result<(), NoSolution>
95     where
96         T: ToTrace<'tcx>,
97     {
98         self.infcx
99             .at(&ObligationCause::dummy_with_span(self.span), self.param_env)
100             .relate(a, variance, b)?
101             .into_value_registering_obligations(self.infcx, self.fulfill_cx);
102         Ok(())
103     }
104
105     fn prove_predicate(&mut self, predicate: Predicate<'tcx>, cause: ObligationCause<'tcx>) {
106         self.fulfill_cx.register_predicate_obligation(
107             self.infcx,
108             Obligation::new(cause, self.param_env, predicate),
109         );
110     }
111
112     fn tcx(&self) -> TyCtxt<'tcx> {
113         self.infcx.tcx
114     }
115
116     fn subst<T>(&self, value: T, substs: &[GenericArg<'tcx>]) -> T
117     where
118         T: TypeFoldable<'tcx>,
119     {
120         EarlyBinder(value).subst(self.tcx(), substs)
121     }
122
123     #[instrument(level = "debug", skip(self))]
124     fn relate_mir_and_user_ty(
125         &mut self,
126         mir_ty: Ty<'tcx>,
127         def_id: DefId,
128         user_substs: UserSubsts<'tcx>,
129     ) -> Result<(), NoSolution> {
130         let UserSubsts { user_self_ty, substs } = user_substs;
131         let tcx = self.tcx();
132
133         let ty = tcx.type_of(def_id);
134         let ty = self.subst(ty, substs);
135         let ty = self.normalize(ty);
136         debug!("relate_type_and_user_type: ty of def-id is {:?}", ty);
137
138         self.relate(mir_ty, Variance::Invariant, ty)?;
139
140         // Prove the predicates coming along with `def_id`.
141         //
142         // Also, normalize the `instantiated_predicates`
143         // because otherwise we wind up with duplicate "type
144         // outlives" error messages.
145         let instantiated_predicates =
146             self.tcx().predicates_of(def_id).instantiate(self.tcx(), substs);
147
148         let cause = ObligationCause::dummy_with_span(self.span);
149
150         debug!(?instantiated_predicates);
151         for (instantiated_predicate, predicate_span) in
152             zip(instantiated_predicates.predicates, instantiated_predicates.spans)
153         {
154             let span = if self.span == DUMMY_SP { predicate_span } else { self.span };
155             let cause = ObligationCause::new(
156                 span,
157                 hir::CRATE_HIR_ID,
158                 ObligationCauseCode::AscribeUserTypeProvePredicate(predicate_span),
159             );
160             let instantiated_predicate =
161                 self.normalize_with_cause(instantiated_predicate, cause.clone());
162             self.prove_predicate(instantiated_predicate, cause);
163         }
164
165         if let Some(UserSelfTy { impl_def_id, self_ty }) = user_self_ty {
166             let impl_self_ty = self.tcx().type_of(impl_def_id);
167             let impl_self_ty = self.subst(impl_self_ty, &substs);
168             let impl_self_ty = self.normalize(impl_self_ty);
169
170             self.relate(self_ty, Variance::Invariant, impl_self_ty)?;
171
172             self.prove_predicate(
173                 ty::Binder::dummy(ty::PredicateKind::WellFormed(impl_self_ty.into()))
174                     .to_predicate(self.tcx()),
175                 cause.clone(),
176             );
177         }
178
179         // In addition to proving the predicates, we have to
180         // prove that `ty` is well-formed -- this is because
181         // the WF of `ty` is predicated on the substs being
182         // well-formed, and we haven't proven *that*. We don't
183         // want to prove the WF of types from  `substs` directly because they
184         // haven't been normalized.
185         //
186         // FIXME(nmatsakis): Well, perhaps we should normalize
187         // them?  This would only be relevant if some input
188         // type were ill-formed but did not appear in `ty`,
189         // which...could happen with normalization...
190         self.prove_predicate(
191             ty::Binder::dummy(ty::PredicateKind::WellFormed(ty.into())).to_predicate(self.tcx()),
192             cause,
193         );
194         Ok(())
195     }
196 }
197
198 fn type_op_eq<'tcx>(
199     tcx: TyCtxt<'tcx>,
200     canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Eq<'tcx>>>,
201 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, ()>>, NoSolution> {
202     tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, |infcx, fulfill_cx, key| {
203         let (param_env, Eq { a, b }) = key.into_parts();
204         infcx
205             .at(&ObligationCause::dummy(), param_env)
206             .eq(a, b)?
207             .into_value_registering_obligations(infcx, fulfill_cx);
208         Ok(())
209     })
210 }
211
212 fn type_op_normalize<'tcx, T>(
213     infcx: &InferCtxt<'tcx>,
214     fulfill_cx: &mut dyn TraitEngine<'tcx>,
215     key: ParamEnvAnd<'tcx, Normalize<T>>,
216 ) -> Fallible<T>
217 where
218     T: fmt::Debug + TypeFoldable<'tcx> + Lift<'tcx>,
219 {
220     let (param_env, Normalize { value }) = key.into_parts();
221     let Normalized { value, obligations } =
222         infcx.at(&ObligationCause::dummy(), param_env).normalize(value)?;
223     fulfill_cx.register_predicate_obligations(infcx, obligations);
224     Ok(value)
225 }
226
227 fn type_op_normalize_ty<'tcx>(
228     tcx: TyCtxt<'tcx>,
229     canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Normalize<Ty<'tcx>>>>,
230 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, Ty<'tcx>>>, NoSolution> {
231     tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, type_op_normalize)
232 }
233
234 fn type_op_normalize_predicate<'tcx>(
235     tcx: TyCtxt<'tcx>,
236     canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Normalize<Predicate<'tcx>>>>,
237 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, Predicate<'tcx>>>, NoSolution> {
238     tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, type_op_normalize)
239 }
240
241 fn type_op_normalize_fn_sig<'tcx>(
242     tcx: TyCtxt<'tcx>,
243     canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Normalize<FnSig<'tcx>>>>,
244 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, FnSig<'tcx>>>, NoSolution> {
245     tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, type_op_normalize)
246 }
247
248 fn type_op_normalize_poly_fn_sig<'tcx>(
249     tcx: TyCtxt<'tcx>,
250     canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Normalize<PolyFnSig<'tcx>>>>,
251 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, PolyFnSig<'tcx>>>, NoSolution> {
252     tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, type_op_normalize)
253 }
254
255 fn type_op_subtype<'tcx>(
256     tcx: TyCtxt<'tcx>,
257     canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Subtype<'tcx>>>,
258 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, ()>>, NoSolution> {
259     tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, |infcx, fulfill_cx, key| {
260         let (param_env, Subtype { sub, sup }) = key.into_parts();
261         infcx
262             .at(&ObligationCause::dummy(), param_env)
263             .sup(sup, sub)?
264             .into_value_registering_obligations(infcx, fulfill_cx);
265         Ok(())
266     })
267 }
268
269 fn type_op_prove_predicate<'tcx>(
270     tcx: TyCtxt<'tcx>,
271     canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, ProvePredicate<'tcx>>>,
272 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, ()>>, NoSolution> {
273     // HACK This bubble is required for this test to pass:
274     // impl-trait/issue-99642.rs
275     tcx.infer_ctxt().with_opaque_type_inference(DefiningAnchor::Bubble).enter_canonical_trait_query(
276         &canonicalized,
277         |infcx, fulfill_cx, key| {
278             type_op_prove_predicate_with_cause(infcx, fulfill_cx, key, ObligationCause::dummy());
279             Ok(())
280         },
281     )
282 }
283
284 /// The core of the `type_op_prove_predicate` query: for diagnostics purposes in NLL HRTB errors,
285 /// this query can be re-run to better track the span of the obligation cause, and improve the error
286 /// message. Do not call directly unless you're in that very specific context.
287 pub fn type_op_prove_predicate_with_cause<'a, 'tcx: 'a>(
288     infcx: &'a InferCtxt<'tcx>,
289     fulfill_cx: &'a mut dyn TraitEngine<'tcx>,
290     key: ParamEnvAnd<'tcx, ProvePredicate<'tcx>>,
291     cause: ObligationCause<'tcx>,
292 ) {
293     let (param_env, ProvePredicate { predicate }) = key.into_parts();
294     fulfill_cx.register_predicate_obligation(infcx, Obligation::new(cause, param_env, predicate));
295 }