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

existential impl trait overlooks "hidden" lifetimes from projections #51525

Closed
nikomatsakis opened this issue Jun 12, 2018 · 2 comments · Fixed by #134408
Closed

existential impl trait overlooks "hidden" lifetimes from projections #51525

nikomatsakis opened this issue Jun 12, 2018 · 2 comments · Fixed by #134408
Labels
A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. A-lifetimes Area: Lifetimes / regions C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@nikomatsakis
Copy link
Contributor

In this example (play), we currently get an incorrect compilation error:

trait Foo<'a> {
    type Bar;
}

impl<'a> Foo<'a> for u32 {
    type Bar = &'a ();
}

fn baz<'a, T>() -> impl IntoIterator<Item = T::Bar>
where T: Foo<'a> { 
    None
}

fn main() { }

yields

error[E0308]: mismatched types
  --> src/main.rs:11:5
   |
11 |     None
   |     ^^^^ lifetime mismatch
   |
   = note: expected type `Foo<'static>`
              found type `Foo<'a>`

This does not seem correct. =) The problem is that T::Bar (at HIR lowering time) does not mention 'a. So we wind up mapping this -- because of the way the region desugaring works -- to <T as Foo<'static>>::Bar. If you manually write <T as Foo<'a>>::Bar in the original Rust source, the 'a becomes visible and is lowered correctly. In other words, this version compiles:

trait Foo<'a> {
    type Bar;
}

impl<'a> Foo<'a> for u32 {
    type Bar = &'a ();
}

fn baz<'a, T>() -> impl IntoIterator<Item = <T as Foo<'a>>::Bar>
where T: Foo<'a> { 
    None
}

fn main() { }
@nikomatsakis nikomatsakis added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. labels Jun 12, 2018
@nikomatsakis
Copy link
Contributor Author

cc @cramertj @oli-obk

@Dylan-DPC
Copy link
Member

This code now compiles, so requires a test to close this issue

@Dylan-DPC Dylan-DPC added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Dec 13, 2024
jieyouxu added a commit to jieyouxu/rust that referenced this issue Dec 17, 2024
…=compiler-errors

Regression test for RPIT inheriting lifetime from projection

Regression test to close rust-lang#51525
jieyouxu added a commit to jieyouxu/rust that referenced this issue Dec 17, 2024
…=compiler-errors

Regression test for RPIT inheriting lifetime from projection

Regression test to close rust-lang#51525
@bors bors closed this as completed in 53635e5 Dec 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. A-lifetimes Area: Lifetimes / regions C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants