1 use crate::check::regionck::RegionCtxt;
3 use crate::hir::def_id::{DefId, LocalDefId};
4 use rustc_errors::{struct_span_err, ErrorReported};
5 use rustc_infer::infer::outlives::env::OutlivesEnvironment;
6 use rustc_infer::infer::{InferOk, RegionckMode, TyCtxtInferExt};
7 use rustc_infer::traits::TraitEngineExt as _;
8 use rustc_middle::ty::error::TypeError;
9 use rustc_middle::ty::relate::{Relate, RelateResult, TypeRelation};
10 use rustc_middle::ty::subst::{Subst, SubstsRef};
11 use rustc_middle::ty::{self, Predicate, Ty, TyCtxt};
13 use rustc_trait_selection::traits::error_reporting::InferCtxtExt;
14 use rustc_trait_selection::traits::query::dropck_outlives::AtExt;
15 use rustc_trait_selection::traits::{ObligationCause, TraitEngine, TraitEngineExt};
17 /// This function confirms that the `Drop` implementation identified by
18 /// `drop_impl_did` is not any more specialized than the type it is
19 /// attached to (Issue #8142).
23 /// 1. The self type must be nominal (this is already checked during
26 /// 2. The generic region/type parameters of the impl's self type must
27 /// all be parameters of the Drop impl itself (i.e., no
28 /// specialization like `impl Drop for Foo<i32>`), and,
30 /// 3. Any bounds on the generic parameters must be reflected in the
31 /// struct/enum definition for the nominal type itself (i.e.
32 /// cannot do `struct S<T>; impl<T:Clone> Drop for S<T> { ... }`).
34 pub fn check_drop_impl(tcx: TyCtxt<'_>, drop_impl_did: DefId) -> Result<(), ErrorReported> {
35 let dtor_self_type = tcx.type_of(drop_impl_did);
36 let dtor_predicates = tcx.predicates_of(drop_impl_did);
37 match dtor_self_type.kind {
38 ty::Adt(adt_def, self_to_impl_substs) => {
39 ensure_drop_params_and_item_params_correspond(
41 drop_impl_did.expect_local(),
46 ensure_drop_predicates_are_implied_by_item_defn(
49 adt_def.did.expect_local(),
54 // Destructors only work on nominal types. This was
55 // already checked by coherence, but compilation may
56 // not have been terminated.
57 let span = tcx.def_span(drop_impl_did);
58 tcx.sess.delay_span_bug(
60 &format!("should have been rejected by coherence check: {}", dtor_self_type),
67 fn ensure_drop_params_and_item_params_correspond<'tcx>(
69 drop_impl_did: LocalDefId,
70 drop_impl_ty: Ty<'tcx>,
72 ) -> Result<(), ErrorReported> {
73 let drop_impl_hir_id = tcx.hir().as_local_hir_id(drop_impl_did);
75 // check that the impl type can be made to match the trait type.
77 tcx.infer_ctxt().enter(|ref infcx| {
78 let impl_param_env = tcx.param_env(self_type_did);
80 let mut fulfillment_cx = TraitEngine::new(tcx);
82 let named_type = tcx.type_of(self_type_did);
84 let drop_impl_span = tcx.def_span(drop_impl_did);
85 let fresh_impl_substs =
86 infcx.fresh_substs_for_item(drop_impl_span, drop_impl_did.to_def_id());
87 let fresh_impl_self_ty = drop_impl_ty.subst(tcx, fresh_impl_substs);
89 let cause = &ObligationCause::misc(drop_impl_span, drop_impl_hir_id);
90 match infcx.at(cause, impl_param_env).eq(named_type, fresh_impl_self_ty) {
91 Ok(InferOk { obligations, .. }) => {
92 fulfillment_cx.register_predicate_obligations(infcx, obligations);
95 let item_span = tcx.def_span(self_type_did);
96 let self_descr = tcx.def_kind(self_type_did).descr(self_type_did);
101 "`Drop` impls cannot be specialized"
106 "use the same sequence of generic type, lifetime and const parameters \
107 as the {} definition",
112 return Err(ErrorReported);
116 if let Err(ref errors) = fulfillment_cx.select_all_or_error(&infcx) {
117 // this could be reached when we get lazy normalization
118 infcx.report_fulfillment_errors(errors, None, false);
119 return Err(ErrorReported);
122 // NB. It seems a bit... suspicious to use an empty param-env
123 // here. The correct thing, I imagine, would be
124 // `OutlivesEnvironment::new(impl_param_env)`, which would
125 // allow region solving to take any `a: 'b` relations on the
126 // impl into account. But I could not create a test case where
127 // it did the wrong thing, so I chose to preserve existing
128 // behavior, since it ought to be simply more
129 // conservative. -nmatsakis
130 let outlives_env = OutlivesEnvironment::new(infcx.tcx, ty::ParamEnv::empty());
132 infcx.resolve_regions_and_report_errors(
133 drop_impl_did.to_def_id(),
135 RegionckMode::default(),
141 /// Confirms that every predicate imposed by dtor_predicates is
142 /// implied by assuming the predicates attached to self_type_did.
143 fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>(
145 dtor_predicates: ty::GenericPredicates<'tcx>,
146 self_type_did: LocalDefId,
147 self_to_impl_substs: SubstsRef<'tcx>,
148 ) -> Result<(), ErrorReported> {
149 let mut result = Ok(());
151 // Here is an example, analogous to that from
152 // `compare_impl_method`.
154 // Consider a struct type:
156 // struct Type<'c, 'b:'c, 'a> {
157 // x: &'a Contents // (contents are irrelevant;
158 // y: &'c Cell<&'b Contents>, // only the bounds matter for our purposes.)
163 // impl<'z, 'y:'z, 'x:'y> Drop for P<'z, 'y, 'x> {
164 // fn drop(&mut self) { self.y.set(self.x); } // (only legal if 'x: 'y)
167 // We start out with self_to_impl_substs, that maps the generic
168 // parameters of Type to that of the Drop impl.
170 // self_to_impl_substs = {'c => 'z, 'b => 'y, 'a => 'x}
172 // Applying this to the predicates (i.e., assumptions) provided by the item
173 // definition yields the instantiated assumptions:
177 // We then check all of the predicates of the Drop impl:
181 // and ensure each is in the list of instantiated
182 // assumptions. Here, `'y:'z` is present, but `'x:'y` is
183 // absent. So we report an error that the Drop impl injected a
184 // predicate that is not present on the struct definition.
186 let self_type_hir_id = tcx.hir().as_local_hir_id(self_type_did);
188 // We can assume the predicates attached to struct/enum definition
190 let generic_assumptions = tcx.predicates_of(self_type_did);
192 let assumptions_in_impl_context = generic_assumptions.instantiate(tcx, &self_to_impl_substs);
193 let assumptions_in_impl_context = assumptions_in_impl_context.predicates;
195 let self_param_env = tcx.param_env(self_type_did);
197 // An earlier version of this code attempted to do this checking
198 // via the traits::fulfill machinery. However, it ran into trouble
199 // since the fulfill machinery merely turns outlives-predicates
200 // 'a:'b and T:'b into region inference constraints. It is simpler
201 // just to look for all the predicates directly.
203 assert_eq!(dtor_predicates.parent, None);
204 for &(predicate, predicate_sp) in dtor_predicates.predicates {
205 // (We do not need to worry about deep analysis of type
206 // expressions etc because the Drop impls are already forced
207 // to take on a structure that is roughly an alpha-renaming of
208 // the generic parameters of the item definition.)
210 // This path now just checks *all* predicates via an instantiation of
211 // the `SimpleEqRelation`, which simply forwards to the `relate` machinery
212 // after taking care of anonymizing late bound regions.
214 // However, it may be more efficient in the future to batch
215 // the analysis together via the fulfill (see comment above regarding
216 // the usage of the fulfill machinery), rather than the
217 // repeated `.iter().any(..)` calls.
219 // This closure is a more robust way to check `Predicate` equality
220 // than simple `==` checks (which were the previous implementation).
221 // It relies on `ty::relate` for `TraitPredicate` and `ProjectionPredicate`
222 // (which implement the Relate trait), while delegating on simple equality
223 // for the other `Predicate`.
224 // This implementation solves (Issue #59497) and (Issue #58311).
225 // It is unclear to me at the moment whether the approach based on `relate`
226 // could be extended easily also to the other `Predicate`.
227 let predicate_matches_closure = |p: Predicate<'tcx>| {
228 let mut relator: SimpleEqRelation<'tcx> = SimpleEqRelation::new(tcx, self_param_env);
230 predicate.ignore_qualifiers(tcx).skip_binder().kind(),
231 p.ignore_qualifiers(tcx).skip_binder().kind(),
233 (&ty::PredicateKind::Trait(a, _), &ty::PredicateKind::Trait(b, _)) => {
234 relator.relate(ty::Binder::bind(a), ty::Binder::bind(b)).is_ok()
236 (&ty::PredicateKind::Projection(a), &ty::PredicateKind::Projection(b)) => {
237 relator.relate(ty::Binder::bind(a), ty::Binder::bind(b)).is_ok()
243 if !assumptions_in_impl_context.iter().copied().any(predicate_matches_closure) {
244 let item_span = tcx.hir().span(self_type_hir_id);
245 let self_descr = tcx.def_kind(self_type_did).descr(self_type_did.to_def_id());
250 "`Drop` impl requires `{}` but the {} it is implemented for does not",
254 .span_note(item_span, "the implementor must specify the same requirement")
256 result = Err(ErrorReported);
263 /// This function is not only checking that the dropck obligations are met for
264 /// the given type, but it's also currently preventing non-regular recursion in
265 /// types from causing stack overflows (dropck_no_diverge_on_nonregular_*.rs).
266 crate fn check_drop_obligations<'a, 'tcx>(
267 rcx: &mut RegionCtxt<'a, 'tcx>,
271 ) -> Result<(), ErrorReported> {
272 debug!("check_drop_obligations typ: {:?}", ty);
274 let cause = &ObligationCause::misc(span, body_id);
275 let infer_ok = rcx.infcx.at(cause, rcx.fcx.param_env).dropck_outlives(ty);
276 debug!("dropck_outlives = {:#?}", infer_ok);
277 rcx.fcx.register_infer_ok_obligations(infer_ok);
282 // This is an implementation of the TypeRelation trait with the
283 // aim of simply comparing for equality (without side-effects).
284 // It is not intended to be used anywhere else other than here.
285 crate struct SimpleEqRelation<'tcx> {
287 param_env: ty::ParamEnv<'tcx>,
290 impl<'tcx> SimpleEqRelation<'tcx> {
291 fn new(tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> SimpleEqRelation<'tcx> {
292 SimpleEqRelation { tcx, param_env }
296 impl TypeRelation<'tcx> for SimpleEqRelation<'tcx> {
297 fn tcx(&self) -> TyCtxt<'tcx> {
301 fn param_env(&self) -> ty::ParamEnv<'tcx> {
305 fn tag(&self) -> &'static str {
306 "dropck::SimpleEqRelation"
309 fn a_is_expected(&self) -> bool {
313 fn relate_with_variance<T: Relate<'tcx>>(
318 ) -> RelateResult<'tcx, T> {
319 // Here we ignore variance because we require drop impl's types
320 // to be *exactly* the same as to the ones in the struct definition.
324 fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
325 debug!("SimpleEqRelation::tys(a={:?}, b={:?})", a, b);
326 ty::relate::super_relate_tys(self, a, b)
333 ) -> RelateResult<'tcx, ty::Region<'tcx>> {
334 debug!("SimpleEqRelation::regions(a={:?}, b={:?})", a, b);
336 // We can just equate the regions because LBRs have been
337 // already anonymized.
341 // I'm not sure is this `TypeError` is the right one, but
342 // it should not matter as it won't be checked (the dropck
343 // will emit its own, more informative and higher-level errors
344 // in case anything goes wrong).
345 Err(TypeError::RegionsPlaceholderMismatch)
351 a: &'tcx ty::Const<'tcx>,
352 b: &'tcx ty::Const<'tcx>,
353 ) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> {
354 debug!("SimpleEqRelation::consts(a={:?}, b={:?})", a, b);
355 ty::relate::super_relate_consts(self, a, b)
362 ) -> RelateResult<'tcx, ty::Binder<T>>
366 debug!("SimpleEqRelation::binders({:?}: {:?}", a, b);
368 // Anonymizing the LBRs is necessary to solve (Issue #59497).
369 // After we do so, it should be totally fine to skip the binders.
370 let anon_a = self.tcx.anonymize_late_bound_regions(&a);
371 let anon_b = self.tcx.anonymize_late_bound_regions(&b);
372 self.relate(anon_a.skip_binder(), anon_b.skip_binder())?;