Skip to content

Commit

Permalink
Unrolled build for rust-lang#120801
Browse files Browse the repository at this point in the history
Rollup merge of rust-lang#120801 - oli-obk:drop_recursion_ice, r=Nilstrieb

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

fixes rust-lang#120787
  • Loading branch information
rust-timer committed Feb 8, 2024
2 parents c29082f + ad511ef commit 8863327
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1313,6 +1313,7 @@ impl<'tcx> PolyFnSig<'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])
}
Expand Down
11 changes: 5 additions & 6 deletions compiler/rustc_mir_build/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/drop/recursion-check-on-erroneous-impl.rs
Original file line number Diff line number Diff line change
@@ -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() {}
11 changes: 11 additions & 0 deletions tests/ui/drop/recursion-check-on-erroneous-impl.stderr
Original file line number Diff line number Diff line change
@@ -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`.

0 comments on commit 8863327

Please sign in to comment.