Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl Trait in return position implicitly captures non-'static type parameter despite + 'static #132364

Open
progval opened this issue Oct 30, 2024 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@progval
Copy link
Contributor

progval commented Oct 30, 2024

Code

trait MyTrait {}
struct MyStruct;
impl MyTrait for MyStruct {}

trait DeserTrait<'de> {}
struct DeserStruct;
impl<'de> DeserTrait<'de> for &'de DeserStruct {}

pub fn testfn<'de, D: DeserTrait<'de>>(_deserializer: D) -> impl MyTrait + 'static {
    MyStruct
}

fn test() -> impl Send {
    let deserializer = DeserStruct;
    let graph = testfn(&deserializer);
    graph
}

(playground)

Current output

error[E0597]: `deserializer` does not live long enough
  --> src/lib.rs:15:24
   |
14 |     let deserializer = DeserStruct;
   |         ------------ binding `deserializer` declared here
15 |     let graph = testfn(&deserializer);
   |                 -------^^^^^^^^^^^^^-
   |                 |      |
   |                 |      borrowed value does not live long enough
   |                 argument requires that `deserializer` is borrowed for `'static`
16 |     graph
17 | }
   | - `deserializer` dropped here while still borrowed

Desired output

I'm not sure, but adding + use<> to the function's return type explains what the issue is actually that impl Trait implicitly captured the trait bound:

error: `impl Trait` must mention all type parameters in scope in `use<...>`
 --> src/lib.rs:9:61
  |
9 | pub fn testfn<'de, D: DeserTrait<'de>>(_deserializer: D) -> impl MyTrait + use<> + 'static {
  |                    -                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |                    |
  |                    type parameter is implicitly captured by this `impl Trait`
  |
  = note: currently, all type parameters are required to be mentioned in the precise captures list

Rationale and extra context

It's impossible to debug (or google) otherwise, and I only figured it out because I remembered + use<> from the stabilization blog post

Other cases

No response

Rust Version

rustc 1.82.0

Anything else?

No response

@progval progval added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 30, 2024
@compiler-errors
Copy link
Member

This is a known limitation, see #116733 Limitation: Opaque hidden types.

@compiler-errors
Copy link
Member

compiler-errors commented Oct 30, 2024

I'd recommend using + use<> syntax, which doesn't have this problem. edit: Actually, not totally sure if that will work for your usecase.

@progval
Copy link
Contributor Author

progval commented Oct 30, 2024

I understand, but I wrote issue about the diagnostic that is unclear, not about limitations of the type system

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants