rust-lang / rust-lang/rust

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

Open
#142,887 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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

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

Start by running the supplied odd_error.rs reproducer with the stated nightly Rust version and compare the current E0311 diagnostic with the desired output. Read the referenced rama-core-0.2.0/src/service/svc.rs signature and use the copied Service definition as the comparison case. Done means the compiler reports the lifetime defined at the source signature and suggests an explicit named lifetime bound instead of anon.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.