Implied region bounds do no satisfy explicit bounds

For example (playground):

trait Trait {
    fn bar<'a, 'b: 'a>(v: &'b &'a str);
}

impl Trait for () {
    fn bar<'a, 'b>(_v: &'b &'a str) {}
}

The &'b &'a str type is WF only if 'b: 'a, and so the fn body has an implied bound for that, but it still does not satisfy the trait:

error[E0195]: lifetime parameters or bounds on method `bar` do not match the trait declaration
 --> src/lib.rs:6:11
  |
2 |     fn bar<'a, 'b: 'a>(v: &'b &'a str);
  |           ------------ lifetimes in impl do not match this method in trait
...
6 |     fn bar<'a, 'b>(_v: &'b &'a str) {}
  |           ^^^^^^^^ lifetimes do not match method in trait

If we write it explicitly, it works (playground):

impl Trait for () {
    fn bar<'a, 'b>(_v: &'b &'a str)
    where
        &'b &'a str:,
    {
    }
}

Is this a bug? Deliberate design decision? Not decided?

Excuse me if I'm wrong, but isn't it kind of reverse? Like, &'b &'a str is only valid when 'a: 'b, so it works as intended.

3 Likes

Surprisingly though changing the bound to 'a: 'b still gives an error Rust Playground

1 Like

Well it looks like you're right but then it's even more strange because if I specify where &'b 'a str: it works in both cases even though it should only imply 'a: 'b.

Well I don't understand why we don't show an error in the second case of my original post, so maybe I'm somewhere wrong with my reasoning, but I still can reproduce with 'a: 'b so the question stands.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.