Skip to content

Commit f27e316

Browse files
committed
Subpart7 for async drop (major1) - library changes
1 parent e30e70d commit f27e316

File tree

7 files changed

+120
-5
lines changed

7 files changed

+120
-5
lines changed

compiler/rustc_feature/src/unstable.rs

+2
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,8 @@ declare_features! (
380380
(unstable, associated_const_equality, "1.58.0", Some(92827)),
381381
/// Allows associated type defaults.
382382
(unstable, associated_type_defaults, "1.2.0", Some(29661)),
383+
/// Allows implementing `AsyncDrop`.
384+
(incomplete, async_drop, "CURRENT_RUSTC_VERSION", Some(126482)),
383385
/// Allows async functions to be called from `dyn Trait`.
384386
(incomplete, async_fn_in_dyn_trait, "1.85.0", Some(133119)),
385387
/// Allows `#[track_caller]` on async functions.

compiler/rustc_interface/src/passes.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -979,11 +979,13 @@ fn run_required_analyses(tcx: TyCtxt<'_>) {
979979
tcx.ensure_ok().check_coroutine_obligations(
980980
tcx.typeck_root_def_id(def_id.to_def_id()).expect_local(),
981981
);
982-
// Eagerly check the unsubstituted layout for cycles.
983-
tcx.ensure_ok().layout_of(
984-
ty::TypingEnv::post_analysis(tcx, def_id.to_def_id())
985-
.as_query_input(tcx.type_of(def_id).instantiate_identity()),
986-
);
982+
if !tcx.is_templated_coroutine(def_id.to_def_id()) {
983+
// Eagerly check the unsubstituted layout for cycles.
984+
tcx.ensure_ok().layout_of(
985+
ty::TypingEnv::post_analysis(tcx, def_id.to_def_id())
986+
.as_query_input(tcx.type_of(def_id).instantiate_identity()),
987+
);
988+
}
987989
}
988990
});
989991
});

compiler/rustc_mir_transform/src/remove_zsts.rs

+7
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ fn trivially_zst<'tcx>(ty: Ty<'tcx>, tcx: TyCtxt<'tcx>) -> Option<bool> {
5959
| ty::RawPtr(..)
6060
| ty::Ref(..)
6161
| ty::FnPtr(..) => Some(false),
62+
ty::Coroutine(def_id, _) => {
63+
if tcx.is_templated_coroutine(*def_id) {
64+
Some(false)
65+
} else {
66+
None
67+
}
68+
}
6269
// check `layout_of` to see (including unreachable things we won't actually see)
6370
_ => None,
6471
}

library/core/src/future/async_drop.rs

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#![unstable(feature = "async_drop", issue = "126482")]
2+
3+
#[allow(unused_imports)]
4+
use core::future::Future;
5+
6+
#[allow(unused_imports)]
7+
use crate::pin::Pin;
8+
#[allow(unused_imports)]
9+
use crate::task::{Context, Poll};
10+
11+
/// Async version of Drop trait.
12+
///
13+
/// When a value is no longer needed, Rust will run a "destructor" on that value.
14+
/// The most common way that a value is no longer needed is when it goes out of
15+
/// scope. Destructors may still run in other circumstances, but we're going to
16+
/// focus on scope for the examples here. To learn about some of those other cases,
17+
/// please see [the reference] section on destructors.
18+
///
19+
/// [the reference]: https://doc.rust-lang.org/reference/destructors.html
20+
///
21+
/// ## `Copy` and ([`Drop`]|`AsyncDrop`) are exclusive
22+
///
23+
/// You cannot implement both [`Copy`] and ([`Drop`]|`AsyncDrop`) on the same type. Types that
24+
/// are `Copy` get implicitly duplicated by the compiler, making it very
25+
/// hard to predict when, and how often destructors will be executed. As such,
26+
/// these types cannot have destructors.
27+
#[cfg(not(bootstrap))]
28+
#[unstable(feature = "async_drop", issue = "126482")]
29+
#[lang = "async_drop"]
30+
pub trait AsyncDrop {
31+
/// Executes the async destructor for this type.
32+
///
33+
/// This method is called implicitly when the value goes out of scope,
34+
/// and cannot be called explicitly.
35+
///
36+
/// When this method has been called, `self` has not yet been deallocated.
37+
/// That only happens after the method is over.
38+
///
39+
/// # Panics
40+
#[allow(async_fn_in_trait)]
41+
async fn drop(self: Pin<&mut Self>);
42+
}
43+
44+
/// Async drop.
45+
#[cfg(not(bootstrap))]
46+
#[unstable(feature = "async_drop", issue = "126482")]
47+
#[lang = "async_drop_in_place"]
48+
pub async unsafe fn async_drop_in_place<T: ?Sized>(_to_drop: *mut T) {
49+
// Code here does not matter - this is replaced by the
50+
// real implementation by the compiler.
51+
}

library/core/src/future/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@
1212
use crate::ptr::NonNull;
1313
use crate::task::Context;
1414

15+
mod async_drop;
1516
mod future;
1617
mod into_future;
1718
mod join;
1819
mod pending;
1920
mod poll_fn;
2021
mod ready;
2122

23+
#[cfg(not(bootstrap))]
24+
#[unstable(feature = "async_drop", issue = "126482")]
25+
pub use async_drop::{AsyncDrop, async_drop_in_place};
2226
#[stable(feature = "into_future", since = "1.64.0")]
2327
pub use into_future::IntoFuture;
2428
#[stable(feature = "future_readiness_fns", since = "1.48.0")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//@ edition: 2021
2+
3+
use std::future::AsyncDrop; //~ ERROR use of unstable library feature 'async_drop'
4+
use std::pin::Pin;
5+
6+
struct Foo {}
7+
8+
impl Drop for Foo {
9+
fn drop(&mut self) {}
10+
}
11+
12+
impl AsyncDrop for Foo { //~ ERROR use of unstable library feature 'async_drop'
13+
async fn drop(self: Pin<&mut Self>) {} //~ ERROR use of unstable library feature 'async_drop'
14+
}
15+
16+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
error[E0658]: use of unstable library feature 'async_drop'
2+
--> $DIR/feature-gate-async-drop.rs:3:5
3+
|
4+
LL | use std::future::AsyncDrop;
5+
| ^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #126482 <https://github.com/rust-lang/rust/issues/126482> for more information
8+
= help: add `#![feature(async_drop)]` to the crate attributes to enable
9+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
10+
11+
error[E0658]: use of unstable library feature 'async_drop'
12+
--> $DIR/feature-gate-async-drop.rs:13:5
13+
|
14+
LL | async fn drop(self: Pin<&mut Self>) {}
15+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16+
|
17+
= note: see issue #126482 <https://github.com/rust-lang/rust/issues/126482> for more information
18+
= help: add `#![feature(async_drop)]` to the crate attributes to enable
19+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
20+
21+
error[E0658]: use of unstable library feature 'async_drop'
22+
--> $DIR/feature-gate-async-drop.rs:12:6
23+
|
24+
LL | impl AsyncDrop for Foo {
25+
| ^^^^^^^^^
26+
|
27+
= note: see issue #126482 <https://github.com/rust-lang/rust/issues/126482> for more information
28+
= help: add `#![feature(async_drop)]` to the crate attributes to enable
29+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
30+
31+
error: aborting due to 3 previous errors
32+
33+
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)