Rollup merge of #120801 - oli-obk:drop_recursion_ice, r=Nilstrieb

Avoid ICE in drop recursion check in case of invalid drop impls

fixes #120787
This commit is contained in:
Matthias Krüger 2024-02-08 20:35:01 +01:00 committed by GitHub
commit ed528515d0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 28 additions and 6 deletions

View File

@ -1313,6 +1313,7 @@ pub fn inputs(&self) -> Binder<'tcx, &'tcx [Ty<'tcx>]> {
self.map_bound_ref_unchecked(|fn_sig| fn_sig.inputs())
}
#[inline]
#[track_caller]
pub fn input(&self, index: usize) -> ty::Binder<'tcx, Ty<'tcx>> {
self.map_bound_ref(|fn_sig| fn_sig.inputs()[index])
}

View File

@ -72,15 +72,14 @@ pub fn check_drop_recursion<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
tcx.impl_of_method(def_id.to_def_id()).and_then(|def_id| tcx.impl_trait_ref(def_id))
&& let Some(drop_trait) = tcx.lang_items().drop_trait()
&& drop_trait == trait_ref.instantiate_identity().def_id
// avoid erroneous `Drop` impls from causing ICEs below
&& let sig = tcx.fn_sig(def_id).instantiate_identity()
&& sig.inputs().skip_binder().len() == 1
{
// It was. Now figure out for what type `Drop` is implemented and then
// check for recursion.
if let ty::Ref(_, dropped_ty, _) = tcx
.liberate_late_bound_regions(
def_id.to_def_id(),
tcx.fn_sig(def_id).instantiate_identity().input(0),
)
.kind()
if let ty::Ref(_, dropped_ty, _) =
tcx.liberate_late_bound_regions(def_id.to_def_id(), sig.input(0)).kind()
{
check_recursion(tcx, body, RecursiveDrop { drop_for: *dropped_ty });
}

View File

@ -0,0 +1,11 @@
// can't use build-fail, because this also fails check-fail, but
// the ICE from #120787 only reproduces on build-fail.
// compile-flags: --emit=mir
struct PrintOnDrop<'a>(&'a str);
impl Drop for PrintOnDrop<'_> {
fn drop() {} //~ ERROR method `drop` has a `&mut self` declaration in the trait
}
fn main() {}

View File

@ -0,0 +1,11 @@
error[E0186]: method `drop` has a `&mut self` declaration in the trait, but not in the impl
--> $DIR/recursion-check-on-erroneous-impl.rs:8:5
|
LL | fn drop() {}
| ^^^^^^^^^ expected `&mut self` in impl
|
= note: `drop` from trait: `fn(&mut Self)`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0186`.