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

Replace unstable Waker::noop() with Waker::NOOP. #122924

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion library/alloc/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ fn raw_waker<W: Wake + Send + Sync + 'static>(waker: Arc<W>) -> RawWaker {
/// // cast the Rc<Task> into a `LocalWaker`
/// let local_waker: LocalWaker = task.clone().into();
/// // Build the context using `ContextBuilder`
/// let mut cx = ContextBuilder::from_waker(Waker::noop())
/// let mut cx = ContextBuilder::from_waker(Waker::NOOP)
/// .local_waker(&local_waker)
/// .build();
///
Expand Down
15 changes: 5 additions & 10 deletions library/core/src/task/wake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ impl fmt::Debug for Context<'_> {
/// use std::future::Future;
///
/// let local_waker = LocalWaker::noop();
/// let waker = Waker::noop();
/// let waker = Waker::NOOP;
///
/// let mut cx = ContextBuilder::from_waker(&waker)
/// .local_waker(&local_waker)
Expand Down Expand Up @@ -465,7 +465,7 @@ impl Waker {
Waker { waker }
}

/// Returns a reference to a `Waker` that does nothing when used.
/// A reference to a `Waker` that does nothing when used.
///
/// This is mostly useful for writing tests that need a [`Context`] to poll
/// some futures, but are not expecting those futures to wake the waker or
Expand All @@ -481,18 +481,13 @@ impl Waker {
/// use std::future::Future;
/// use std::task;
///
/// let mut cx = task::Context::from_waker(task::Waker::noop());
/// let mut cx = task::Context::from_waker(task::Waker::NOOP);
///
/// let mut future = Box::pin(async { 10 });
/// assert_eq!(future.as_mut().poll(&mut cx), task::Poll::Ready(10));
/// ```
#[inline]
#[must_use]
#[unstable(feature = "noop_waker", issue = "98286")]
pub const fn noop() -> &'static Waker {
const WAKER: &Waker = &Waker { waker: RawWaker::NOOP };
WAKER
}
pub const NOOP: &'static Waker = &Waker { waker: RawWaker::NOOP };
Copy link
Member

@RalfJung RalfJung Mar 23, 2024

Choose a reason for hiding this comment

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

FWIW, since constants get promoted, it'd now also be possible to change this back to have type Waker and use it as

let ctx = &mut Context::from_waker(&Waker::NOOP);

This would be basically un-doing #119984. The let that motivated #119984 are no longer needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I already tried that initially and the compiler rejects it.

I prepared a change which replaced Waker::noop() with Waker::NOOP. Unfortunately, it seems that this doesn't enable &NOOP usage:

let mut cx = &mut Context::from_waker(&Waker::NOOP);
cx.waker().wake_by_ref();
error[E0716]: temporary value dropped while borrowed
   --> src/wake.rs:404:44
    |
404 |     let mut cx = &mut Context::from_waker(&Waker::NOOP);
    |                                            ^^^^^^^^^^^ - temporary value is freed at the end of this statement

If I understand correctly now, this is not possible because the Waker has a destructor. Therefore, since use via constant promotion is not possible, it seems wise to offer a &Waker value, in which case making it a constant rather than a const fn doesn't provide any advantages (but still might be a choice of API aesthetics).

Copy link
Member

Choose a reason for hiding this comment

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

Ah, true, if it has a destructor it cannot get promoted. Good point.


/// Get a reference to the underlying [`RawWaker`].
#[inline]
Expand Down Expand Up @@ -697,7 +692,7 @@ impl LocalWaker {
/// use std::future::Future;
/// use std::task::{ContextBuilder, LocalWaker, Waker, Poll};
///
/// let mut cx = ContextBuilder::from_waker(Waker::noop())
/// let mut cx = ContextBuilder::from_waker(Waker::NOOP)
/// .local_waker(LocalWaker::noop())
/// .build();
///
Expand Down
2 changes: 1 addition & 1 deletion library/core/tests/async_iter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn into_async_iter() {
let async_iter = async_iter::from_iter(0..3);
let mut async_iter = pin!(async_iter.into_async_iter());

let mut cx = &mut core::task::Context::from_waker(core::task::Waker::noop());
let mut cx = &mut core::task::Context::from_waker(core::task::Waker::NOOP);

assert_eq!(async_iter.as_mut().poll_next(&mut cx), Poll::Ready(Some(0)));
assert_eq!(async_iter.as_mut().poll_next(&mut cx), Poll::Ready(Some(1)));
Expand Down
2 changes: 1 addition & 1 deletion src/tools/miri/tests/pass/async-closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::task::*;

pub fn block_on<T>(fut: impl Future<Output = T>) -> T {
let mut fut = pin!(fut);
let ctx = &mut Context::from_waker(Waker::noop());
let ctx = &mut Context::from_waker(Waker::NOOP);

loop {
match fut.as_mut().poll(ctx) {
Expand Down
2 changes: 1 addition & 1 deletion src/tools/miri/tests/pass/async-fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ async fn uninhabited_variant() {
fn run_fut<T>(fut: impl Future<Output = T>) -> T {
use std::task::{Context, Poll, Waker};

let mut context = Context::from_waker(Waker::noop());
let mut context = Context::from_waker(Waker::NOOP);

let mut pinned = Box::pin(fut);
loop {
Expand Down
2 changes: 1 addition & 1 deletion src/tools/miri/tests/pass/dyn-star.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ fn dispatch_on_pin_mut() {
let mut fut = async_main();

// Poll loop, just to test the future...
let ctx = &mut Context::from_waker(Waker::noop());
let ctx = &mut Context::from_waker(Waker::NOOP);

loop {
match unsafe { Pin::new_unchecked(&mut fut).poll(ctx) } {
Expand Down
4 changes: 2 additions & 2 deletions src/tools/miri/tests/pass/future-self-referential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl Future for DoStuff {
}

fn run_fut<T>(fut: impl Future<Output = T>) -> T {
let mut context = Context::from_waker(Waker::noop());
let mut context = Context::from_waker(Waker::NOOP);

let mut pinned = pin!(fut);
loop {
Expand All @@ -89,7 +89,7 @@ fn run_fut<T>(fut: impl Future<Output = T>) -> T {
}

fn self_referential_box() {
let cx = &mut Context::from_waker(Waker::noop());
let cx = &mut Context::from_waker(Waker::NOOP);

async fn my_fut() -> i32 {
let val = 10;
Expand Down
2 changes: 1 addition & 1 deletion src/tools/miri/tests/pass/issues/issue-miri-2068.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::task::{Context, Poll, Waker};

pub fn fuzzing_block_on<O, F: Future<Output = O>>(fut: F) -> O {
let mut fut = std::pin::pin!(fut);
let mut context = Context::from_waker(Waker::noop());
let mut context = Context::from_waker(Waker::NOOP);
loop {
match fut.as_mut().poll(&mut context) {
Poll::Ready(v) => return v,
Expand Down
2 changes: 1 addition & 1 deletion src/tools/miri/tests/pass/move-data-across-await-point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn data_moved() {
fn run_fut<T>(fut: impl Future<Output = T>) -> T {
use std::task::{Context, Poll, Waker};

let mut context = Context::from_waker(Waker::noop());
let mut context = Context::from_waker(Waker::NOOP);

let mut pinned = Box::pin(fut);
loop {
Expand Down
2 changes: 1 addition & 1 deletion tests/coverage/async.coverage
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
LL| | #[coverage(off)]
LL| | pub fn block_on<F: Future>(mut future: F) -> F::Output {
LL| | let mut future = pin!(future);
LL| | let mut context = Context::from_waker(Waker::noop());
LL| | let mut context = Context::from_waker(Waker::NOOP);
LL| |
LL| | loop {
LL| | if let Poll::Ready(val) = future.as_mut().poll(&mut context) {
Expand Down
2 changes: 1 addition & 1 deletion tests/coverage/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ mod executor {
#[coverage(off)]
pub fn block_on<F: Future>(mut future: F) -> F::Output {
let mut future = pin!(future);
let mut context = Context::from_waker(Waker::noop());
let mut context = Context::from_waker(Waker::NOOP);

loop {
if let Poll::Ready(val) = future.as_mut().poll(&mut context) {
Expand Down
2 changes: 1 addition & 1 deletion tests/coverage/async2.coverage
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
LL| | #[coverage(off)]
LL| | pub fn block_on<F: Future>(mut future: F) -> F::Output {
LL| | let mut future = pin!(future);
LL| | let mut context = Context::from_waker(Waker::noop());
LL| | let mut context = Context::from_waker(Waker::NOOP);
LL| |
LL| | loop {
LL| | if let Poll::Ready(val) = future.as_mut().poll(&mut context) {
Expand Down
2 changes: 1 addition & 1 deletion tests/coverage/async2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ mod executor {
#[coverage(off)]
pub fn block_on<F: Future>(mut future: F) -> F::Output {
let mut future = pin!(future);
let mut context = Context::from_waker(Waker::noop());
let mut context = Context::from_waker(Waker::NOOP);

loop {
if let Poll::Ready(val) = future.as_mut().poll(&mut context) {
Expand Down
2 changes: 1 addition & 1 deletion tests/coverage/async_block.coverage
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
LL| | #[coverage(off)]
LL| | pub fn block_on<F: Future>(mut future: F) -> F::Output {
LL| | let mut future = pin!(future);
LL| | let mut context = Context::from_waker(Waker::noop());
LL| | let mut context = Context::from_waker(Waker::NOOP);
LL| |
LL| | loop {
LL| | if let Poll::Ready(val) = future.as_mut().poll(&mut context) {
Expand Down
2 changes: 1 addition & 1 deletion tests/coverage/async_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ mod executor {
#[coverage(off)]
pub fn block_on<F: Future>(mut future: F) -> F::Output {
let mut future = pin!(future);
let mut context = Context::from_waker(Waker::noop());
let mut context = Context::from_waker(Waker::NOOP);

loop {
if let Poll::Ready(val) = future.as_mut().poll(&mut context) {
Expand Down
2 changes: 1 addition & 1 deletion tests/coverage/closure_macro_async.coverage
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
LL| | #[coverage(off)]
LL| | pub fn block_on<F: Future>(mut future: F) -> F::Output {
LL| | let mut future = pin!(future);
LL| | let mut context = Context::from_waker(Waker::noop());
LL| | let mut context = Context::from_waker(Waker::NOOP);
LL| |
LL| | loop {
LL| | if let Poll::Ready(val) = future.as_mut().poll(&mut context) {
Expand Down
2 changes: 1 addition & 1 deletion tests/coverage/closure_macro_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ mod executor {
#[coverage(off)]
pub fn block_on<F: Future>(mut future: F) -> F::Output {
let mut future = pin!(future);
let mut context = Context::from_waker(Waker::noop());
let mut context = Context::from_waker(Waker::NOOP);

loop {
if let Poll::Ready(val) = future.as_mut().poll(&mut context) {
Expand Down
2 changes: 1 addition & 1 deletion tests/mir-opt/async_closure_shims.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::task::*;

pub fn block_on<T>(fut: impl Future<Output = T>) -> T {
let mut fut = pin!(fut);
let ctx = &mut Context::from_waker(Waker::noop());
let ctx = &mut Context::from_waker(Waker::NOOP);

loop {
match fut.as_mut().poll(ctx) {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/async-await/async-closures/auxiliary/block-on.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::task::*;
pub fn block_on<T>(fut: impl Future<Output = T>) -> T {
let mut fut = pin!(fut);
// Poll loop, just to test the future...
let ctx = &mut Context::from_waker(Waker::noop());
let ctx = &mut Context::from_waker(Waker::NOOP);

loop {
match unsafe { fut.as_mut().poll(ctx) } {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/async-await/async-fn/auxiliary/block-on.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::task::*;
pub fn block_on<T>(fut: impl Future<Output = T>) -> T {
let mut fut = pin!(fut);
// Poll loop, just to test the future...
let ctx = &mut Context::from_waker(Waker::noop());
let ctx = &mut Context::from_waker(Waker::NOOP);

loop {
match unsafe { fut.as_mut().poll(ctx) } {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/async-await/for-await-passthrough.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async fn real_main() {

fn main() {
let future = real_main();
let mut cx = &mut core::task::Context::from_waker(std::task::Waker::noop());
let mut cx = &mut core::task::Context::from_waker(std::task::Waker::NOOP);
let mut future = core::pin::pin!(future);
while let core::task::Poll::Pending = future.as_mut().poll(&mut cx) {}
}
2 changes: 1 addition & 1 deletion tests/ui/async-await/for-await.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async fn real_main() {

fn main() {
let future = real_main();
let mut cx = &mut core::task::Context::from_waker(std::task::Waker::noop());
let mut cx = &mut core::task::Context::from_waker(std::task::Waker::NOOP);
let mut future = core::pin::pin!(future);
while let core::task::Poll::Pending = future.as_mut().poll(&mut cx) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn main() {
let mut fut = pin!(async_main());

// Poll loop, just to test the future...
let ctx = &mut Context::from_waker(Waker::noop());
let ctx = &mut Context::from_waker(Waker::NOOP);

loop {
match fut.as_mut().poll(ctx) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn main() {
let mut fut = pin!(async_main());

// Poll loop, just to test the future...
let ctx = &mut Context::from_waker(Waker::noop());
let ctx = &mut Context::from_waker(Waker::NOOP);

loop {
match fut.as_mut().poll(ctx) {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/coroutine/async-gen-yield-ty-is-unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ async gen fn gen_fn() -> &'static str {

pub fn main() {
let async_iterator = pin!(gen_fn());
let ctx = &mut Context::from_waker(Waker::noop());
let ctx = &mut Context::from_waker(Waker::NOOP);
async_iterator.poll_next(ctx);
}
2 changes: 1 addition & 1 deletion tests/ui/coroutine/async_gen_fn_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fn main() {
let mut fut = pin!(async_main());

// Poll loop, just to test the future...
let ctx = &mut Context::from_waker(Waker::noop());
let ctx = &mut Context::from_waker(Waker::NOOP);

loop {
match fut.as_mut().poll(ctx) {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/dyn-star/dispatch-on-pin-mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn main() {
let mut fut = pin!(async_main());

// Poll loop, just to test the future...
let ctx = &mut Context::from_waker(Waker::noop());
let ctx = &mut Context::from_waker(Waker::NOOP);

loop {
match fut.as_mut().poll(ctx) {
Expand Down
Loading