Skip to content

Commit

Permalink
Rollup merge of #129034 - henryksloan:coroutine-must-use, r=joboet
Browse files Browse the repository at this point in the history
Add `#[must_use]` attribute to `Coroutine` trait

[Coroutines tracking issue](#43122)

Like closures (`FnOnce`, `AsyncFn`, etc.), coroutines are lazy and do nothing unless called (resumed). Closure traits like `FnOnce` have `#[must_use = "closures are lazy and do nothing unless called"]` to catch likely bugs for users of APIs that produce them. This PR adds such a `#[must_use]` attribute to `trait Coroutine`.
  • Loading branch information
matthiaskrgr authored Aug 13, 2024
2 parents f68a28d + 1e445f4 commit d4f5a89
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
1 change: 1 addition & 0 deletions library/core/src/ops/coroutine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pub enum CoroutineState<Y, R> {
#[lang = "coroutine"]
#[unstable(feature = "coroutine_trait", issue = "43122")]
#[fundamental]
#[must_use = "coroutines are lazy and do nothing unless resumed"]
pub trait Coroutine<R = ()> {
/// The type of value this coroutine yields.
///
Expand Down
5 changes: 3 additions & 2 deletions tests/ui/coroutine/issue-58888.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ impl Database {
}

fn check_connection(&self) -> impl Coroutine<Yield = (), Return = ()> + '_ {
#[coroutine] move || {
#[coroutine]
move || {
let iter = self.get_connection();
for i in iter {
yield i
Expand All @@ -23,5 +24,5 @@ impl Database {
}

fn main() {
Database.check_connection();
let _ = Database.check_connection();
}

0 comments on commit d4f5a89

Please sign in to comment.