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