Skip to content

Error E0311 help suggests adding an explicit lifetime bound on unknown anon type #142887

Open
@0xAdk

Description

@0xAdk

Code

If I copy the definition of rama::Service and impl that instead, the issue doesn't happen. So I failed to get a smaller example than this.

./odd_error.rs

#!/usr/bin/env -S cargo +nightly -Zscript -q
---
packag.edition = "2024"

[dependencies]
rama = "0.2.0"
---

fn main() {}

struct Foo;

impl<S, Request: 'static> rama::Service<S, Request> for Foo {
    type Response = ();
    type Error = ();

    fn serve(
        &self,
        _ctx: rama::Context<S>,
        _req: Request,
    ) -> impl Future<Output = Result<Self::Response, Self::Error>> {
        async { todo!() }
    }
}

Current output

./odd_error.rs

error[E0311]: the parameter type `S` may not live long enough
  --> odd_error.rs:22:10
   |
22 |     ) -> impl Future<Output = Result<Self::Response, Self::Error>> {
   |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...so that the type `impl Future<Output = Result<<Foo as Service<S, Request>>::Response, <Foo as Service<S, Request>>::Error>>` will meet its required lifetime bounds...
   |
  ::: ~/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/rama-core-0.2.0/src/service/svc.rs:19:9
   |
19 |         &self,
   |         - the parameter type `S` must be valid for the anonymous lifetime as defined here...
   |
note: ...that is required by this bound
  --> ~/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/rama-core-0.2.0/src/service/svc.rs:22:77
   |
22 |     ) -> impl Future<Output = Result<Self::Response, Self::Error>> + Send + '_;
   |                                                                             ^^
help: consider adding an explicit lifetime bound
   |
22 |     ) -> impl Future<Output = Result<Self::Response, Self::Error>> where S: anon {
   |                                                                    +++++++++++++

For more information about this error, try `rustc --explain E0311`.
error: could not compile `odd_error` (bin "odd_error") due to 1 previous error

Desired output

Copying the definition of rama::Service, rust gives the correct error:

error[E0311]: the parameter type `S` may not live long enough
  --> odd_error.rs:22:10
   |
22 |     ) -> impl Future<Output = Result<Self::Response, Self::Error>> {
   |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...so that the type `impl Future<Output = Result<<Foo as Service<S, Request>>::Response, <Foo as Service<S, Request>>::Error>>` will meet its required lifetime bounds...
...
32 |         &self,
   |         ----- the parameter type `S` must be valid for the anonymous lifetime defined here...
   |
note: ...that is required by this bound
  --> odd_error.rs:35:77
   |
35 |     ) -> impl Future<Output = Result<Self::Response, Self::Error>> + Send + '_;
   |                                                                             ^^
help: consider adding an explicit lifetime bound
   |
31 ~     fn serve<'a>(
32 ~         &'a self,
33 |         ctx: rama::Context<S>,
34 |         req: Request,
35 ~     ) -> impl Future<Output = Result<Self::Response, Self::Error>> + Send + 'a where S: 'a;
   |

For more information about this error, try `rustc --explain E0311`.
error: could not compile `odd_error` (bin "odd_error") due to 1 previous error

Rust Version

$ cargo +nightly rustc -- --version --verbose
rustc 1.89.0-nightly (d4e1159b8 2025-06-21)
binary: rustc
commit-hash: d4e1159b8c97478778b09a4cc1c7adce5653b8bf
commit-date: 2025-06-21
host: x86_64-unknown-linux-gnu
release: 1.89.0-nightly
LLVM version: 20.1.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions