Skip to content

task: don't let co_return {}; convert to with_stopped - #2474

Open
aarochu wants to merge 1 commit into
NVIDIA:mainfrom
aarochu:fix-task-co_return-empty-braces
Open

aarochu wants to merge 1 commit into
NVIDIA:mainfrom
aarochu:fix-task-co_return-empty-braces

Conversation

@aarochu

@aarochu aarochu commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Hey! This fixes #2472, where co_return {}; in a task<T> quietly completes the task with set_stopped instead of value-initializing the result.

What was going on

__promise_base had a non-template return_value(with_stopped). Since with_stopped is an empty aggregate, {} converts to it. For a T like std::expected<void, E>, std::optional<U> or std::string, {} only reaches T through a user-defined conversion, so that overload and the return_value(_Value&&) template are equally good, and the tie-break goes to the non-template. (task<int> was never affected: {} -> int is a standard conversion and wins outright.)

I checked that this is the cause rather than just trusting the pattern: deleting only that overload in a scratch copy of the headers makes co_return {} return a value for all of those types on GCC 15.2 and Clang 21.

The fix

The with_stopped overload is now a template, so a braced-init-list can't deduce it and {} goes to the value overload like it should. The two overloads are constrained on one shared predicate so they never compete, and co_return with_stopped{} / an lvalue with_stopped still stop the task.

I deliberately did not use the simpler "value overload excludes with_stopped" constraint from the issue. That would make co_return with_stopped{} ill-formed for a T that's constructible from with_stopped, which the existing stopped_as_value test covers. The predicate here keeps that case working: a T that accepts with_stopped still receives it as a plain value.

I left __promise_base<_Promise, void> alone. Its return_value isn't available on the compilers I could test with (co_return {} in a task<void> doesn't compile on GCC or Clang here), so I had no way to verify a change there.

Testing

  • New cases in test_task.cpp for co_return {} with std::optional<int> and std::string, plus one for co_return of an lvalue with_stopped.
  • With the fix reverted, only the new co_return {} test fails (sync_wait comes back empty); with it, all the [task] tests pass on Clang 21 (40 cases) and GCC 15.2 (39 cases, one is compiled out on GCC), and test_task_scheduler passes on both.
  • test_type_async_scope and examples/hello_coro still compile.
  • I only built and ran the task-related test files rather than the whole suite, and only on Linux with GCC/Clang, so CI is the real check for other toolchains.

`__promise_base::return_value(with_stopped)` took a non-template
`with_stopped` parameter. `with_stopped` is an empty aggregate, so `{}`
converts to it, and for any `_Ty` that `{}` only reaches through a
user-defined conversion (std::expected<void, E>, std::optional<U>,
std::string, ...) the value overload and the `with_stopped` overload are
indistinguishable. The non-template wins the tie, so `co_return {};`
silently completed the task with set_stopped instead of
value-initializing the result.

Make the `with_stopped` overload a template so a braced-init-list can't
deduce it, and make the two overloads mutually exclusive on a single
predicate so `co_return with_stopped{}` (and an lvalue `with_stopped`)
still stops the task, while a `_Ty` constructible from `with_stopped`
still receives it as an ordinary value.

Fixes NVIDIA#2472
@copy-pr-bot

copy-pr-bot Bot commented Nov 22, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@ericniebler

Copy link
Copy Markdown
Collaborator

/ok to test 31fd739

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

task: co_return {}; completes a task<T> with set_stopped since with_stopped was added

2 participants