-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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( | ||
|
@@ -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; | ||
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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: |
||
let item_span = tcx.hir().span_by_hir_id(self_type_hir_id); | ||
struct_span_err!( | ||
tcx.sess, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 aroundTraitRef
I believe that usingrelate
instead of==
may do the right thing^TM for what you are doing here.