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

This commit is contained in:
Oli Scherer 2024-02-08 17:33:04 +00:00
parent 1280928a99
commit ad511ef92e
4 changed files with 28 additions and 6 deletions

View file

@ -1312,6 +1312,7 @@ impl<'tcx> PolyFnSig<'tcx> {
self.map_bound_ref_unchecked(|fn_sig| fn_sig.inputs()) self.map_bound_ref_unchecked(|fn_sig| fn_sig.inputs())
} }
#[inline] #[inline]
#[track_caller]
pub fn input(&self, index: usize) -> ty::Binder<'tcx, Ty<'tcx>> { pub fn input(&self, index: usize) -> ty::Binder<'tcx, Ty<'tcx>> {
self.map_bound_ref(|fn_sig| fn_sig.inputs()[index]) 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)) 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() && let Some(drop_trait) = tcx.lang_items().drop_trait()
&& drop_trait == trait_ref.instantiate_identity().def_id && 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 // It was. Now figure out for what type `Drop` is implemented and then
// check for recursion. // check for recursion.
if let ty::Ref(_, dropped_ty, _) = tcx if let ty::Ref(_, dropped_ty, _) =
.liberate_late_bound_regions( tcx.liberate_late_bound_regions(def_id.to_def_id(), sig.input(0)).kind()
def_id.to_def_id(),
tcx.fn_sig(def_id).instantiate_identity().input(0),
)
.kind()
{ {
check_recursion(tcx, body, RecursiveDrop { drop_for: *dropped_ty }); 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`.