Skip to content

Commit 1461ca3

Browse files
authored
Rollup merge of #141328 - azhogin:azhogin/async-drop-ice-for-empty-impl-fix, r=oli-obk
When AsyncDrop impl is empty, sync drop generated in elaborator Fixes #140974.
2 parents ad6fb06 + dc794f1 commit 1461ca3

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

compiler/rustc_mir_transform/src/elaborate_drop.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,29 @@ where
251251
span_bug!(span, "invalid `AsyncDrop` impl_source: {:?}", impl_source);
252252
}
253253
};
254-
let drop_fn_def_id = tcx.associated_item_def_ids(drop_trait)[0];
254+
// impl_item_refs may be empty if drop fn is not implemented in 'impl AsyncDrop for ...'
255+
// (#140974).
256+
// Such code will report error, so just generate sync drop here and return
257+
let Some(drop_fn_def_id) =
258+
tcx.associated_item_def_ids(drop_trait).into_iter().nth(0).copied()
259+
else {
260+
tcx.dcx().span_delayed_bug(
261+
self.elaborator.body().span,
262+
"AsyncDrop type without correct `async fn drop(...)`.",
263+
);
264+
self.elaborator.patch().patch_terminator(
265+
pin_obj_bb,
266+
TerminatorKind::Drop {
267+
place,
268+
target: succ,
269+
unwind: unwind.into_action(),
270+
replace: false,
271+
drop: None,
272+
async_fut: None,
273+
},
274+
);
275+
return pin_obj_bb;
276+
};
255277
let drop_fn = Ty::new_fn_def(tcx, drop_fn_def_id, trait_args);
256278
let sig = drop_fn.fn_sig(tcx);
257279
let sig = tcx.instantiate_bound_regions_with_erased(sig);

tests/crashes/140974.rs renamed to tests/ui/async-await/async-drop/elaborate-index-out-of-bounds.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
//@ known-bug: #140974
2-
//@edition:2021
1+
//@ edition: 2024
2+
// Ex-ICE: #140974
3+
#![crate_type = "lib"]
4+
#![allow(incomplete_features)]
35
#![feature(async_drop)]
46
use core::future::AsyncDrop;
57

@@ -10,5 +12,6 @@ impl Drop for HasIncompleteAsyncDrop {
1012
fn drop(&mut self) {}
1113
}
1214
impl AsyncDrop for HasIncompleteAsyncDrop {
15+
//~^ ERROR: not all trait items implemented, missing: `drop` [E0046]
1316
// not implemented yet..
1417
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0046]: not all trait items implemented, missing: `drop`
2+
--> $DIR/elaborate-index-out-of-bounds.rs:14:1
3+
|
4+
LL | impl AsyncDrop for HasIncompleteAsyncDrop {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ missing `drop` in implementation
6+
|
7+
= help: implement the missing item: `async fn drop(self: Pin<&mut Self>) { todo!() }`
8+
9+
error: aborting due to 1 previous error
10+
11+
For more information about this error, try `rustc --explain E0046`.

0 commit comments

Comments
 (0)