Skip to content

Commit aed1171

Browse files
authored
Rollup merge of #141677 - azhogin:azhogin/async-drop-unexpected-type-instead-of-drop-fn-fix, r=oli-obk
Async drop - type instead of async drop fn, fixes #140484 Fixes: #140484 Fixes: #140500 Fixes ICE, when type is provided in AsyncDrop trait instead of `async fn drop()`. Fixes ICE, when async drop fn has wrong signature.
2 parents 55f7571 + f023a69 commit aed1171

File tree

7 files changed

+91
-30
lines changed

7 files changed

+91
-30
lines changed

compiler/rustc_mir_transform/src/elaborate_drop.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::{fmt, iter, mem};
22

33
use rustc_abi::{FIRST_VARIANT, FieldIdx, VariantIdx};
4+
use rustc_hir::def::DefKind;
45
use rustc_hir::lang_items::LangItem;
56
use rustc_index::Idx;
67
use rustc_middle::mir::*;
@@ -254,8 +255,19 @@ where
254255
// impl_item_refs may be empty if drop fn is not implemented in 'impl AsyncDrop for ...'
255256
// (#140974).
256257
// 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()
258+
let Some(drop_fn_def_id) = tcx
259+
.associated_item_def_ids(drop_trait)
260+
.first()
261+
.and_then(|def_id| {
262+
if tcx.def_kind(def_id) == DefKind::AssocFn
263+
&& tcx.check_args_compatible(*def_id, trait_args)
264+
{
265+
Some(def_id)
266+
} else {
267+
None
268+
}
269+
})
270+
.copied()
259271
else {
260272
tcx.dcx().span_delayed_bug(
261273
self.elaborator.body().span,

tests/crashes/140484.rs

Lines changed: 0 additions & 14 deletions
This file was deleted.

tests/crashes/140500.rs

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//@ edition: 2024
2+
// ex-ice: #140500
3+
#![crate_type = "lib"]
4+
#![feature(async_drop)]
5+
#![expect(incomplete_features)]
6+
use std::future::AsyncDrop;
7+
struct A;
8+
impl Drop for A {
9+
fn drop(&mut self) {}
10+
}
11+
impl AsyncDrop for A {
12+
fn drop(_wrong: impl Sized) {} //~ ERROR: method `drop` has a `self: Pin<&mut Self>` declaration in the trait, but not in the impl
13+
}
14+
async fn bar() {
15+
A;
16+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0186]: method `drop` has a `self: Pin<&mut Self>` declaration in the trait, but not in the impl
2+
--> $DIR/type-parameter.rs:12:5
3+
|
4+
LL | fn drop(_wrong: impl Sized) {}
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `self: Pin<&mut Self>` in impl
6+
|
7+
= note: `drop` from trait: `fn(Pin<&mut Self>) -> impl Future<Output = ()>`
8+
9+
error: aborting due to 1 previous error
10+
11+
For more information about this error, try `rustc --explain E0186`.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Ex-ice: #140484
2+
//@ edition: 2024
3+
#![crate_type = "lib"]
4+
#![allow(incomplete_features)]
5+
#![allow(non_camel_case_types)]
6+
#![feature(async_drop)]
7+
use std::future::AsyncDrop;
8+
struct a;
9+
impl Drop for a { //~ ERROR: not all trait items implemented, missing: `drop`
10+
fn b() {} //~ ERROR: method `b` is not a member of trait `Drop`
11+
}
12+
impl AsyncDrop for a { //~ ERROR: not all trait items implemented, missing: `drop`
13+
type c = ();
14+
//~^ ERROR: type `c` is not a member of trait `AsyncDrop`
15+
}
16+
async fn bar() {
17+
a;
18+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
error[E0407]: method `b` is not a member of trait `Drop`
2+
--> $DIR/unexpected-sort.rs:10:5
3+
|
4+
LL | fn b() {}
5+
| ^^^^^^^^^ not a member of trait `Drop`
6+
7+
error[E0437]: type `c` is not a member of trait `AsyncDrop`
8+
--> $DIR/unexpected-sort.rs:13:5
9+
|
10+
LL | type c = ();
11+
| ^^^^^^^^^^^^ not a member of trait `AsyncDrop`
12+
13+
error[E0046]: not all trait items implemented, missing: `drop`
14+
--> $DIR/unexpected-sort.rs:9:1
15+
|
16+
LL | impl Drop for a {
17+
| ^^^^^^^^^^^^^^^ missing `drop` in implementation
18+
|
19+
= help: implement the missing item: `fn drop(&mut self) { todo!() }`
20+
21+
error[E0046]: not all trait items implemented, missing: `drop`
22+
--> $DIR/unexpected-sort.rs:12:1
23+
|
24+
LL | impl AsyncDrop for a {
25+
| ^^^^^^^^^^^^^^^^^^^^ missing `drop` in implementation
26+
|
27+
= help: implement the missing item: `async fn drop(self: Pin<&mut Self>) { todo!() }`
28+
29+
error: aborting due to 4 previous errors
30+
31+
Some errors have detailed explanations: E0046, E0407, E0437.
32+
For more information about an error, try `rustc --explain E0046`.

0 commit comments

Comments
 (0)