59
trait A<'self_>: 'self_ {
    type I;
}
trait AMut
where
    Self: for<'self_> A<'self_>,
    for<'self_> <Self as A<'self_>>::I: 'static,
{
    fn mutate_self(&mut self);
}

fn foo<X>(x: &mut X)
where
    X: 'static + for<'a> A<'a> + AMut,
    for<'a> <X as A<'a>>::I: 'static,
{
    x.mutate_self();
}

Playground

This errors out with

error[E0280]: the requirement `for<'self_> <Self as A<'self_>>::I: 'static` is not satisfied
  --> src/lib.rs:4:1
   |
4  |   trait AMut
   |   ^     ---- required by a bound in this
   |  _|
   | |
5  | | where
6  | |     Self: for<'self_> A<'self_>,
7  | |     for<'self_> <Self as A<'self_>>::I: 'static,
   | |                                         ------- required by this bound in `AMut`
8  | | {
9  | |     fn mutate_self(&mut self);
10 | | }
   | |_^

error[E0280]: the requirement `for<'self_> <X as A<'self_>>::I: 'static` is not satisfied
  --> src/lib.rs:14:34
   |
4  | trait AMut
   |       ---- required by a bound in this
...
7  |     for<'self_> <Self as A<'self_>>::I: 'static,
   |                                         ------- required by this bound in `AMut`
...
14 |     X: 'static + for<'a> A<'a> + AMut,
   |                                  ^^^^

I would've thought that the bound on line 15 would satisfy the bound on line 7. What am I missing?

4
  • 8
    I think it is this issue: github.com/rust-lang/rust/issues/27113. You can't have HKL on the lhs of a where clause
    – oli_obk
    Dec 8, 2015 at 10:05
  • 1
    @ker I'm willing to believe that. I just found out now that replacing the rhs 'static with a Send makes the bounds compile, though. Weird.
    – user
    Dec 8, 2015 at 10:11
  • 1
    @ker Ah, it compiles, but then actually trying to impl the trait for a type never works.
    – user
    Dec 9, 2015 at 0:37
  • For this particular case, adding a generic lifetime AMut and foo, instead of HKL, works.
    – rodrigo
    Oct 14, 2020 at 10:24

1 Answer 1

0

Have you tried like this?

trait A<'self_>: 'self_ {
    type I;
}
trait AMut<'a>
where
    Self: A<'a>,
    <Self as A<'a>>::I: 'static,
{
    fn mutate_self(&mut self);
}

fn foo<X>(x: &mut X)
where
    X: 'static + for<'a> A<'a> + for<'a> AMut<'a>,
    for<'a> <X as A<'a>>::I: 'static,
{
    x.mutate_self();
}
2
  • If I recall correctly, the point of my question back then was to mitigate the 'contagiousness' of lifetimes, as well as to understand why something that should've worked by the rules instead failed to. Your solution sidesteps the issue by giving rustc leeway to descend into corresponding binders rather than solving the types at the top level. The original error demonstrates a fundamental weakness in rustc's current implementation that chalk integration was/is supposed to fix IIUC (I've not kept up with the news).
    – user
    Jan 19 at 4:41
  • Hello @user, yes this is an old question :-) I don't remember exactly my reasoning. I found strange the "where: for" usage, which is generally used with closure HRTBs, Rust RFC 0387 and I proposed an solution. I can remove this idea if you think is not relevant.
    – Zeppi
    Jan 20 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.