rust-lang / rust-lang/rust

Bug when using `.flatten()` method that has `Item = &'a T` when calling async function inside loop

Open
#126,044 5 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-async-await A-auto-traits A-diagnostics A-trait-system AsyncAwait-Triaged C-bug D-confusing fixed-by-higher-ranked-assumptions
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=f560d75b3eb32ab6d2e0e51590b0337f

I expected to see this happen: code compiles

Instead, this happened: error: implementation of std::marker::Send is not general enough

Meta

rustc --version --verbose:

rustc 1.77.1 (7cf61ebde 2024-03-27)
binary: rustc
commit-hash: 7cf61ebde7b22796c69757901dd346d0fe70bd97
commit-date: 2024-03-27
host: x86_64-unknown-linux-gnu
release: 1.77.1
LLVM version: 17.0.6

Bug exists on beta and nightly versions also.

Backtrace

error: implementation of `std::marker::Send` is not general enough
  --> src/main.rs:63:5
   |
63 |     tokio::spawn(join_with_cancellation(f, cancellation.clone()));
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ implementation of `std::marker::Send` is not general enough
   |
   = note: `std::marker::Send` would have to be implemented for the type `&CancellationToken`
   = note: ...but `std::marker::Send` is actually implemented for the type `&'0 CancellationToken`, for some specific lifetime `'0`

I have working example: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=19183aa875e3dadf501f3b6bdfb61ce4.

It seems like the reason is combination of .flatten() method that has Item = &'a T when calling async function inside loop. If you try to remove .await from the line 33, it compiles. If you try to remove .flatten() as in second link - it compiles.

Also, the error seems kinda random. The pattern is as follows:

   = note: ...but `std::marker::Send` is actually implemented for the type `&'0 T`, for some specific lifetime `'0`

But every time I change the code, the compiler shows another T type in error hint.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure from the Rust Playground links, starting at src/main.rs:63 and the async call near line 33. Compare the versions with and without .flatten() and .await, then trace the resulting Send lifetime diagnostic; done means the original example compiles correctly or the remaining behavior is isolated with a focused compiler regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.