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