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

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

Merged
merged 1 commit into from
Feb 8, 2024
Merged
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
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 @@ -1312,6 +1312,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);
Copy link
Member

Choose a reason for hiding this comment

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

could have made this a ZST without the lifetime^^
not that it matters

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh yea, forgot to minimize


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`.
Loading