Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lifetime bounds not propagated for associated type #77654

Closed
Swatinem opened this issue Oct 7, 2020 · 3 comments
Closed

Lifetime bounds not propagated for associated type #77654

Swatinem opened this issue Oct 7, 2020 · 3 comments
Labels
A-associated-items Area: Associated items (types, constants & functions) A-lifetimes Area: Lifetimes / regions C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Swatinem
Copy link
Contributor

Swatinem commented Oct 7, 2020

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]
@Swatinem Swatinem added the C-bug Category: This is a bug. label Oct 7, 2020
@jonas-schievink jonas-schievink added A-associated-items Area: Associated items (types, constants & functions) A-lifetimes Area: Lifetimes / regions T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 7, 2020
@estk
Copy link
Contributor

estk commented Jun 28, 2021

I have encountered this

@ocstl
Copy link

ocstl commented Dec 12, 2021

This seems to have been fixed by #87281.

@Swatinem
Copy link
Contributor Author

Indeed, the playground example now works fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items (types, constants & functions) A-lifetimes Area: Lifetimes / regions C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants