Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Fix compiler incorrectly rejecting Drop impls where parent struct uses HRTB #59497

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions src/librustc_typeck/check/dropck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rustc::infer::{self, InferOk, SuppressRegionErrors};
use rustc::middle::region;
use rustc::traits::{ObligationCause, TraitEngine, TraitEngineExt};
use rustc::ty::subst::{Subst, SubstsRef, UnpackedKind};
use rustc::ty::{self, Ty, TyCtxt};
use rustc::ty::{self, BoundRegion, Predicate, RegionKind, Ty, TyCtxt};
use crate::util::common::ErrorReported;

use syntax_pos::Span;
Expand All @@ -34,7 +34,9 @@ pub fn check_drop_impl<'a, 'tcx>(
drop_impl_did: DefId,
) -> Result<(), ErrorReported> {
let dtor_self_type = tcx.type_of(drop_impl_did);

let dtor_predicates = tcx.predicates_of(drop_impl_did);

match dtor_self_type.sty {
ty::Adt(adt_def, self_to_impl_substs) => {
ensure_drop_params_and_item_params_correspond(
Expand Down Expand Up @@ -215,7 +217,41 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'a, 'tcx>(
// the analysis together via the fulfill , rather than the
// repeated `contains` calls.

if !assumptions_in_impl_context.contains(&predicate) {
// Fixes #34426 by ignoring the `DefId` for late-bound regions.
//
// This solution is absolutely awful, and the correct solution
// is probably to find why `RegionKind::ReLateBound` generates
// different `DefId`s in different item blocks and fix it. I say
// that's probably the correct solution because
// `RegionKind::ReEarlyBound`s *also* have `DefId`s and they're
// identical in different item blocks, which indicates that
// something is up here.
//
// Alternatively, we could do the deep analysis of the Drop impl,
// which the above comment says not to do. However, as I'm writing
// this PR I don't know how I'd do that so this solution remains.
let predicate_matches = |p: &'_ &Predicate<'_>| {
match (p, predicate) {
(Predicate::Trait(p), Predicate::Trait(predicate)) => {
let predicate_substs = &*predicate.skip_binder().trait_ref.substs;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So... I have been told that skip_binder is always a signal that something with traits is going wrong. Digging around in the (4 years old) PR that created the "fulfill" comment above and in the docs around TraitRef I believe that using relate instead of == may do the right thing^TM for what you are doing here.

let p_substs = &*p.skip_binder().trait_ref.substs;

predicate_substs.len() == p_substs.len() &&
predicate_substs.iter().zip(p_substs.iter()).all(|(a, b)| {
match (a.unpack(), b.unpack()) {
(
UnpackedKind::Lifetime(RegionKind::ReLateBound(pr_i, BoundRegion::BrNamed(_, pr_n))),
UnpackedKind::Lifetime(RegionKind::ReLateBound(p_i, BoundRegion::BrNamed(_, p_n)))
) => (pr_i, pr_n) == (p_i, p_n),
_ => a == b
}
})
},
_ => &predicate == p
}
};

if !assumptions_in_impl_context.iter().find(predicate_matches).is_some() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: .iter().find(f).is_some() should be .iter().any(f)

let item_span = tcx.hir().span_by_hir_id(self_type_hir_id);
struct_span_err!(
tcx.sess,
Expand Down
48 changes: 48 additions & 0 deletions src/test/run-pass/drop/dropck-hrtb.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// run-pass
//! Regression test for #34426, regarding HRTB in drop impls

pub trait Lifetime<'a> {}
impl<'a> Lifetime<'a> for i32 {}

#[allow(dead_code)]
struct Foo<L>
where for<'a> L: Lifetime<'a>
{
l: L
}

impl<L> Drop for Foo<L>
where for<'a> L: Lifetime<'a>
{
fn drop(&mut self) {
println!("drop with hrtb");
}
}

pub trait Lifetime2<'a, 'b> {}
impl<'a, 'b> Lifetime2<'a, 'b> for i32 {}

#[allow(dead_code)]
struct Bar<L>
where for<'a, 'b> L: Lifetime2<'a, 'b>
{
l: L
}

impl<L> Drop for Bar<L>
where for<'a, 'b> L: Lifetime2<'a, 'b>
{
fn drop(&mut self) {
println!("drop with hrtb");
}
}

fn main() {
let _foo = Foo {
l: 0
};

let _bar = Bar {
l: 0
};
}