Skip to content

Lifetime bounds not propagated for associated type #77654

Closed
@Swatinem

Description

@Swatinem

When defining a trait with a lifetime bound and an associated type.
When constraining that associated type by another bound, that is not properly propagated when calling another function.

This came up here: getsentry/symbolic#277

Minified example:

pub trait MyTrait<'a> {
    type E;    
}

pub fn fails<'a, O>(o: &O) where O: MyTrait<'a>, O::E: 'static {
    original(o)
}

pub fn fails_too<'a, O>(o: &O) where O: MyTrait<'a>, <O as MyTrait<'a>>::E: 'static {
    original(o)
}

pub fn works<'a, O, E>(o: &O) where O: MyTrait<'a, E = E>, E: 'static {
    original(o)
}

fn original<'a, O>(_o: &O) where O: MyTrait<'a>, O::E: 'static {}

playground link

I expected to see this happen:
All the three given functions should forward correctly.

Instead, this happened:
The first two functions give me the following error, and the suggestion is misleading as well, since the second function contains literally the trait bound thats in the suggestion.

error[E0310]: the associated type `<O as MyTrait<'_>>::E` may not live long enough
 --> src/lib.rs:6:5
  |
6 |     original(o)
  |     ^^^^^^^^
  |
  = help: consider adding an explicit lifetime bound `<O as MyTrait<'_>>::E: 'static`...
  = note: ...so that the type `<O as MyTrait<'_>>::E` will meet its required lifetime bounds

error[E0310]: the associated type `<O as MyTrait<'_>>::E` may not live long enough
  --> src/lib.rs:10:5
   |
10 |     original(o)
   |     ^^^^^^^^
   |
   = help: consider adding an explicit lifetime bound `<O as MyTrait<'_>>::E: 'static`...
   = note: ...so that the type `<O as MyTrait<'_>>::E` will meet its required lifetime bounds

Meta

rustc --version --verbose:

1.46
1.49.0-nightly (2020-10-06 98edd1fbf8a68977a2a7) [from the playground]

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-lifetimesArea: Lifetimes / regionsC-bugCategory: This is a bug.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