Skip to content

Suggest removing & when awaiting a reference to a future#154933

Merged
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
fru1tworld:fix-87211-ref-future-diagnostic
Apr 21, 2026
Merged

Suggest removing & when awaiting a reference to a future#154933
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
fru1tworld:fix-87211-ref-future-diagnostic

Conversation

@fru1tworld
Copy link
Copy Markdown
Contributor

@fru1tworld fru1tworld commented Apr 7, 2026

Fixes #87211

When .awaiting &impl Future, suggest removing the & instead of removing .await.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 7, 2026
@fru1tworld fru1tworld force-pushed the fix-87211-ref-future-diagnostic branch from f36f2b9 to 5afb519 Compare April 7, 2026 08:20
@fru1tworld fru1tworld marked this pull request as ready for review April 7, 2026 08:25
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 7, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 7, 2026

r? @JohnTitor

rustbot has assigned @JohnTitor.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 69 candidates
  • Random selection from 11 candidates

Comment thread compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs Outdated
@fru1tworld fru1tworld marked this pull request as draft April 7, 2026 12:03
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 7, 2026
@fru1tworld fru1tworld force-pushed the fix-87211-ref-future-diagnostic branch from bff3e3b to cdd3efa Compare April 7, 2026 12:08
@fru1tworld fru1tworld marked this pull request as ready for review April 7, 2026 12:12
@fru1tworld fru1tworld requested a review from chenyukang April 7, 2026 12:12
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 7, 2026
Comment thread tests/ui/async-await/await-ref-future.stderr Outdated
@JohnTitor JohnTitor assigned chenyukang and unassigned JohnTitor Apr 7, 2026
@fru1tworld fru1tworld marked this pull request as draft April 7, 2026 12:49
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 7, 2026
@fru1tworld fru1tworld force-pushed the fix-87211-ref-future-diagnostic branch from bc9d978 to 7e22d2d Compare April 7, 2026 23:17
@rust-log-analyzer

This comment has been minimized.

@fru1tworld fru1tworld force-pushed the fix-87211-ref-future-diagnostic branch from 7e22d2d to fe623c3 Compare April 7, 2026 23:33
@fru1tworld fru1tworld marked this pull request as ready for review April 7, 2026 23:37
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 7, 2026
@fru1tworld fru1tworld requested a review from chenyukang April 7, 2026 23:37
error[E0277]: `&impl Future<Output = ()>` is not a future
--> $DIR/await-ref-future.rs:9:9
|
LL | fut.await;
Copy link
Copy Markdown
Member

@chenyukang chenyukang Apr 8, 2026

Choose a reason for hiding this comment

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

View changes since the review

this test is the code from original issue, now suggest remove & not working for it.

as I referred, you may need to reuse the existing span-based borrow-removal logic (see suggest_remove_reference or extract it into a helper)

@chenyukang
Copy link
Copy Markdown
Member

nit:

it's not necessary to request review after each commit, reviewer can receive notification from your git push, and will review it when time available(it's normal to wait several days since a lot of us working in free time). https://rustc-dev-guide.rust-lang.org/contributing.html?highlight=reviewer#waiting-for-reviews

request review may makes them feel too pushy.

@fru1tworld fru1tworld force-pushed the fix-87211-ref-future-diagnostic branch from fe623c3 to 9aed706 Compare April 8, 2026 03:14
@fru1tworld
Copy link
Copy Markdown
Contributor Author

Updated to handle the additional cases:

  • let fut = &async_fn(); fut.await → suggest removing & from init
  • async fn bar(fut: &impl Future) → suggest removing & from param type (traces through async fn desugaring)
  • &dyn Future excluded (unsized)
  • Fallback help when specific span suggestion is unavailable

// In async fn, params are desugared: `let fut = __arg0;`
// with `LocalSource::AsyncFn`. Follow init back to the
// original parameter.
hir::Node::LetStmt(local)
Copy link
Copy Markdown
Member

@chenyukang chenyukang Apr 15, 2026

Choose a reason for hiding this comment

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

This is still a fairly ad hoc reimplementation of borrow-removal logic, and it misses cases that the more general machinery should handle. For example, let fut = &&my_async_fn(); fut.await; still falls back to help: remove the .await. I think the better direction is to extract the borrow-peeling/span-finding part of suggest_remove_reference above and reuse it here, instead of open special handling for cases such as AddrOf / LetStmt / AsyncFn cases again.

View changes since the review

@fru1tworld fru1tworld force-pushed the fix-87211-ref-future-diagnostic branch from 9aed706 to 1323164 Compare April 15, 2026 06:51
}

// Try removing `&` from the parameter type.
if let Some(param) = terminal_param {
Copy link
Copy Markdown
Member

@chenyukang chenyukang Apr 19, 2026

Choose a reason for hiding this comment

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

thanks, this version sees better.

this test should not suggest removing from parameter?

async fn ref_param_borrowed_expr(fut: &impl std::future::Future<Output = ()>) {
    (&fut).await; //~ ERROR is not a future
}

View changes since the review

@fru1tworld fru1tworld force-pushed the fix-87211-ref-future-diagnostic branch from 1323164 to e9856d7 Compare April 20, 2026 09:42
@fru1tworld
Copy link
Copy Markdown
Contributor Author

Fixed — guarded param & removal with peeled_refs.is_empty() so it's skipped when the expression itself has &. Added test for the case.

@chenyukang
Copy link
Copy Markdown
Member

Thanks!

@bors r=chenyukang

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Apr 21, 2026

📌 Commit e9856d7 has been approved by chenyukang

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 21, 2026
rust-bors Bot pushed a commit that referenced this pull request Apr 21, 2026
Rollup of 5 pull requests

Successful merges:

 - #153411 (Offload slice support)
 - #154557 (Make E0284 generic argument suggestions more explicit)
 - #154933 (Suggest removing `&` when awaiting a reference to a future)
 - #155524 (Fix LLVM offload install docs to use semicolon-separated CMake lists)
 - #155568 (Update books)
@rust-bors rust-bors Bot merged commit 03dc6a3 into rust-lang:main Apr 21, 2026
11 checks passed
@rustbot rustbot added this to the 1.97.0 milestone Apr 21, 2026
github-actions Bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request Apr 21, 2026
Rollup of 5 pull requests

Successful merges:

 - rust-lang/rust#153411 (Offload slice support)
 - rust-lang/rust#154557 (Make E0284 generic argument suggestions more explicit)
 - rust-lang/rust#154933 (Suggest removing `&` when awaiting a reference to a future)
 - rust-lang/rust#155524 (Fix LLVM offload install docs to use semicolon-separated CMake lists)
 - rust-lang/rust#155568 (Update books)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Confusing error: Immutable reference to future is not a future

5 participants