rust-lang / futures-rs

Zero-cost asynchronous programming in Rust
https://rust-lang.github.io/futures-rs/
Apache License 2.0
5.38k stars 622 forks source link

returning a future from a function with an `impl Trait` argument references that argument even if not used in the future, causing lifetime errors #2861

Closed pushrax closed 4 months ago

pushrax commented 4 months ago

Here are two equivalent snippets; the latter just inlines stream_files into stream_file_range:

The first snippet fails to compile with

error[E0597]: `file_ids` does not live long enough
  --> src/main.rs:13:18
   |
12 |     let file_ids = BTreeSet::from([1, 2, 3]);
   |         -------- binding `file_ids` declared here
13 |     stream_files(file_ids.range(range).copied())
   |     -------------^^^^^^^^-----------------------
   |     |            |
   |     |            borrowed value does not live long enough
   |     argument requires that `file_ids` is borrowed for `'static`
14 | }
   | - `file_ids` dropped here while still borrowed

The latter snippet compiles and works as expected.

This seems to indicate that futures::stream::iter is somehow pulling the file_ids_iter argument into its future and forcing it to a 'static lifetime (despite it being moved by the time iter is called, and not referenced anywhere).

I'm not sure if this is a bug/limitation in Rust or futures-rs, or if I'm missing something.

cc @morgangallant

pushrax commented 4 months ago

further minimized the example

pushrax commented 4 months ago

closing as the most minimal example doesn't use futures-rs at all