Skip to content

Unclear lifetime error from GAT and HRTB interaction #105507

Closed

Description

@Imberflur

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=3d9d42dd1968a2f14a49b84c6a39300c

struct Wrapper<'a, T: ?Sized>(&'a T);

trait Project {
    type Projected<'a> where Self: 'a;
    fn project(this: Wrapper<'_, Self>) -> Self::Projected<'_>;
}
trait MyTrait {}
trait ProjectedMyTrait {}

impl<T> Project for Option<T> {
    type Projected<'a> = Option<Wrapper<'a, T>> where T: 'a;
    fn project(this: Wrapper<'_, Self>) -> Self::Projected<'_> {
        this.0.as_ref().map(Wrapper)
    }
}

impl<T: MyTrait> MyTrait for Option<Wrapper<'_, T>> {}

impl<T: ProjectedMyTrait> MyTrait for Wrapper<'_, T> {}

impl<T> ProjectedMyTrait for T
where
    T: Project,
    for<'a> T::Projected<'a>: MyTrait,
{}

fn require_trait(_: impl MyTrait) {}
fn foo<T: MyTrait>(wrap: Wrapper<'_, Option<T>>) {
    require_trait(wrap)
}

The current output is:

error: `T` does not live long enough
  --> src/lib.rs:31:5
   |
31 |     require_trait(wrap)
   |     ^^^^^^^^^^^^^^^^^^^

Ideally the output would somehow point out that part of the cause is the for <'a> clause essentially leading to a T: 'static requirement.

When I encountered this error, the relevant for<'a> and GAT trait implementation were not nearby so it was very difficult to tell what the issue was.

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Dec 9, 2022
  2. yanchen4791 commented on Jan 6, 2023

    @yanchen4791

    @rustbot claim

  3. added 2 commits that reference this issue on Jan 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-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