You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
#[must_use] on async fns works on returned Future instead of the awaited value #78149
A-async-awaitArea: Async & AwaitA-attributesArea: Attributes (`#[…]`, `#![…]`)A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.AsyncAwait-PolishAsync-await issues that are part of the "polish" areaAsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.C-bugCategory: This is a bug.P-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
I expected the compiler to warn about the unused return value in without_warning(), but the compiler emitted two warnings in with_warnings():
warning: unused implementer of `Future` that must be used
--> src/lib.rs:7:5
|
7 | foo();
| ^^^^^^
|
= note: `#[warn(unused_must_use)]` on by default
= note: futures do nothing unless you `.await` or poll them
warning: unused return value of `foo` that must be used
--> src/lib.rs:7:5
|
7 | foo();
| ^^^^^^
As the real return values of async fns are actually impl Futures, you may consider this as the expected behavior (I don't agree, and impl Futures are already #[must_use]). If that is the case, there should be a way to make the compiler warn on unused .awaited values (when type of the .awaited value is not #[must_use]).
I think this is indeed a bug, or at the very least unexpected behavior. It might be challenging to fix, since I don't know of a way to express the desired desugaring.
I agree that this will be challenging. The first problem as you said is that, in the desugaring, the #[must_use] is being applied to the function that returns the impl Future, and we don't have a clear place where it would be applied to mean "the value awaited by this future". So that's something we'd have to teach the lint somehow.
Second problem then would be that the lint would have to have the idea of a "must use future" (let's call it), and it would have to track when that future is awaited and figure out that the resulting value is itself #[must_use].
I'm pondering if there is a generalization that might make this easier and less special case, not clear.
A similar issue arises with fehler, withoutboats/fehler#45. In fact, it would be nice in general to be able to say, for a function which returns Result<Wombat,SomeError>, that the Wombat must be used. #[must_use] doesn't work there either, because it just demands use of the Result.
Another way to look at this is that both Result and Future have syntactic sugar for unpacking and implementing control flow effects, which makes them both special in that their contained type is somehow the "underlying" "real" return type of the function.
A-async-awaitArea: Async & AwaitA-attributesArea: Attributes (`#[…]`, `#![…]`)A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.AsyncAwait-PolishAsync-await issues that are part of the "polish" areaAsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.C-bugCategory: This is a bug.P-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
Activity
tmandry commentedon Oct 20, 2020
I think this is indeed a bug, or at the very least unexpected behavior. It might be challenging to fix, since I don't know of a way to express the desired desugaring.
nikomatsakis commentedon Oct 20, 2020
I agree that this will be challenging. The first problem as you said is that, in the desugaring, the
#[must_use]is being applied to the function that returns theimpl Future, and we don't have a clear place where it would be applied to mean "the value awaited by this future". So that's something we'd have to teach the lint somehow.Second problem then would be that the lint would have to have the idea of a "must use future" (let's call it), and it would have to track when that future is awaited and figure out that the resulting value is itself
#[must_use].I'm pondering if there is a generalization that might make this easier and less special case, not clear.
tmandry commentedon Oct 20, 2020
In the short term we could make a lint to warn people who use the attribute on async fns that it won't do anything.
camelid commentedon Oct 20, 2020
Assigning
P-mediumand removingI-prioritizeas discussed in the prioritization working group.11 remaining items
Rollup merge of rust-lang#89610 - guswynn:must_use_future, r=wesleywiser
Rollup merge of rust-lang#89610 - guswynn:must_use_future, r=wesleywiser
Rollup merge of rust-lang#89610 - guswynn:must_use_future, r=wesleywiser
Rollup merge of rust-lang#89610 - guswynn:must_use_future, r=wesleywiser
ijackson commentedon Nov 25, 2021
A similar issue arises with
fehler, withoutboats/fehler#45. In fact, it would be nice in general to be able to say, for a function which returnsResult<Wombat,SomeError>, that theWombatmust be used.#[must_use]doesn't work there either, because it just demands use of theResult.Another way to look at this is that both
ResultandFuturehave syntactic sugar for unpacking and implementing control flow effects, which makes them both special in that their contained type is somehow the "underlying" "real" return type of the function.remove must_use attributes on async functions
Consider `#[must_use]` annotation on `async fn` as also affecting the…
Rollup merge of rust-lang#100633 - estebank:must_use_async_fn_return,…
Rollup merge of rust-lang#100633 - estebank:must_use_async_fn_return,…
Rollup merge of rust-lang#100633 - estebank:must_use_async_fn_return,…
Rollup merge of rust-lang#100633 - estebank:must_use_async_fn_return,…