Skip to content

E0195 on where Self: 'a bounds when they are unconditionally satisfied by Self #84021

Open
@ExpHP

Description

@ExpHP
trait Test {
    fn boo<'ctx>() where Self: 'ctx;
}

impl<A, B> Test for (A, B) {
    fn boo<'ctx>() where A: 'ctx, B: 'ctx { }    // ok
}

impl Test for () {
    fn boo<'ctx>() { }   //  [E0195]: lifetime parameters or bounds on method `boo` do not match the trait declaration
}

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=9d4125a483581bb47d66f002f1074fda

I expected this to succeed. And in fact, the first impl (the generic one) succeeds! (i.e. the compiler is able to successfully determine that Self: 'ctx given A: 'ctx and B: 'ctx)

However, an error is emitted on the impl for ():

error[E0195]: lifetime parameters or bounds on method `boo` do not match the trait declaration
  --> src/lib.rs:10:11
   |
2  |     fn boo<'ctx>() where Self: 'ctx;
   |           ------ lifetimes in impl do not match this method in trait
...
10 |     fn boo<'ctx>() { }
   |           ^^^^^^ lifetimes do not match method in trait

error: aborting due to previous error

In order to make this one compile, we need to add where (): 'ctx to the impl, which is, well, a bit silly!

impl Test for () {
    fn boo<'ctx>() where (): 'ctx { }   //  ok (workaround)
}

Addendum: In fact, any mention of the lifetime inside where bounds causes compilation to succeed.

impl Test for () {
    fn boo<'ctx>() where i32: 'ctx { }  // ok
}

impl Test for () {
    fn boo<'ctx>() where &'ctx i32: Clone { }  // also ok
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regionsA-trait-systemArea: Trait systemC-bugCategory: This is a bug.D-confusingDiagnostics: Confusing error or lint that should be reworked.T-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