Skip to content

HRTB with nested lifetimes and explicit lifetime bounds fails. #95921

Open
@aliemjay

Description

@aliemjay

I tried this code:

trait Service<Req> {}

impl<'a, Req: 'a> Service<&'a Req> for u8 {}

fn main() {
    fn impl_check() where u8: for<'a, 'b> Service<&'a &'b u8> {}
    impl_check()
}

I expected to see this happen: code compiles fine

Instead, this happened: Implementation is not general enough Error

Removing the explicit lifetime bound on the impl fixes the problem:

- impl<'a, Req: 'a> Service<&'a Req> for u8 {}
+ impl<'a, Req> Service<&'a Req> for u8 {}

Meta

Reproduced on both the current stable and nightly branches.

rustc --version --verbose:

1.62.0-nightly (2022-04-09 8bf93e9b6791acee3a59)
Error Output

error: implementation of `Service` is not general enough
 [--> src/lib.rs:7:5
](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021#)  |
7 |     impl_check()
  |     ^^^^^^^^^^ implementation of `Service` is not general enough
  |
  = note: `u8` must implement `Service<&'0 &'1 u8>`, for any two lifetimes `'0` and `'1`...
  = note: ...but it actually implements `Service<&'2 &'b u8>`, for some specific lifetime `'2`

@rustbot label A-typesystem, A-lifetimes, A-traits

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-higher-rankedArea: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs)A-lifetimesArea: Lifetimes / regionsA-trait-systemArea: Trait systemA-type-systemArea: Type systemC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types 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