Skip to content

Commit 04bc9d1

Browse files
authored
Rollup merge of rust-lang#141031 - azhogin:azhogin/async-drop-dependency-fix, r=oli-obk
Async drop fix for dropee from another crate (rust-lang#140858) Fixes rust-lang#140858. For `AsyncDestructor` impl def id was wrongly kept as a LocalDefId, which causes crash when dropee is declared in another crate. Also, potential problem found: when user crate drops type with async drop in dependency crate, and user crate doesn't enable `feature(async_drop)`, then sync drop version will be used. Is it a problem? Do we need some notification about such situations?
2 parents 642cd65 + 7b2dcf2 commit 04bc9d1

File tree

5 files changed

+65
-2
lines changed

5 files changed

+65
-2
lines changed

compiler/rustc_middle/src/ty/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1185,7 +1185,7 @@ pub struct Destructor {
11851185
#[derive(Copy, Clone, Debug, HashStable, Encodable, Decodable)]
11861186
pub struct AsyncDestructor {
11871187
/// The `DefId` of the `impl AsyncDrop`
1188-
pub impl_did: LocalDefId,
1188+
pub impl_did: DefId,
11891189
}
11901190

11911191
#[derive(Clone, Copy, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]

compiler/rustc_middle/src/ty/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ impl<'tcx> TyCtxt<'tcx> {
465465
dtor_candidate = Some(impl_did);
466466
}
467467

468-
Some(ty::AsyncDestructor { impl_did: dtor_candidate? })
468+
Some(ty::AsyncDestructor { impl_did: dtor_candidate?.into() })
469469
}
470470

471471
/// Returns the set of types that are required to be alive in
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//@ edition:2021
2+
3+
#![feature(async_drop)]
4+
#![allow(incomplete_features)]
5+
6+
pub struct HasDrop;
7+
impl Drop for HasDrop{
8+
fn drop(&mut self) {
9+
println!("Sync drop");
10+
}
11+
}
12+
13+
pub struct MongoDrop;
14+
impl MongoDrop {
15+
pub async fn new() -> Result<Self, HasDrop> {
16+
Ok(Self)
17+
}
18+
}
19+
impl Drop for MongoDrop{
20+
fn drop(&mut self) {
21+
println!("Sync drop");
22+
}
23+
}
24+
impl std::future::AsyncDrop for MongoDrop {
25+
async fn drop(self: std::pin::Pin<&mut Self>) {
26+
println!("Async drop");
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//@ run-pass
2+
//@ check-run-results
3+
//@ aux-build:async-drop-dep.rs
4+
//@ edition:2021
5+
6+
#![feature(async_drop)]
7+
#![allow(incomplete_features)]
8+
9+
extern crate async_drop_dep;
10+
11+
use async_drop_dep::MongoDrop;
12+
use std::pin::pin;
13+
use std::task::{Context, Poll, Waker};
14+
use std::future::Future;
15+
16+
async fn asyncdrop() {
17+
let _ = MongoDrop::new().await;
18+
}
19+
20+
pub fn block_on<T>(fut: impl Future<Output = T>) -> T {
21+
let mut fut = pin!(fut);
22+
let ctx = &mut Context::from_waker(Waker::noop());
23+
24+
loop {
25+
match fut.as_mut().poll(ctx) {
26+
Poll::Pending => {}
27+
Poll::Ready(t) => break t,
28+
}
29+
}
30+
}
31+
32+
fn main() {
33+
let _ = block_on(asyncdrop());
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Async drop

0 commit comments

Comments
 (0)