Skip to content

Unclear lifetime error from GAT and HRTB interaction #105507

New issue

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

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

Already on GitHub? # to your account

Closed
Imberflur opened this issue Dec 9, 2022 · 1 comment
Closed

Unclear lifetime error from GAT and HRTB interaction #105507

Imberflur opened this issue Dec 9, 2022 · 1 comment
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Imberflur
Copy link

Imberflur commented Dec 9, 2022

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.

@Imberflur Imberflur added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 9, 2022
@yanchen4791
Copy link
Contributor

@rustbot claim

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 18, 2023
…tebank

Add 'static lifetime suggestion when GAT implied 'static requirement from HRTB

Fix for issue rust-lang#105507

The problem:
When generic associated types (GATs) are from higher-ranked trait bounds (HRTB), they are implied 'static requirement (see
[Implied 'static requirement from higher-ranked trait bounds](https://blog.rust-lang.org/2022/10/28/gats-stabilization.html#implied-static-requirement-from-higher-ranked-trait-bounds) for more details). If the user did not explicitly specify the `'static` lifetime when using the GAT, the current error message will only point out the type `does not live long enough` where the type is used, but not where the GAT is specified and how to fix the problem.

The solution:
Add notes at the span where the problematic GATs are specified and suggestions of how to fix the problem by adding `'static` lifetime at the right spans.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 18, 2023
…tebank

Add 'static lifetime suggestion when GAT implied 'static requirement from HRTB

Fix for issue rust-lang#105507

The problem:
When generic associated types (GATs) are from higher-ranked trait bounds (HRTB), they are implied 'static requirement (see
[Implied 'static requirement from higher-ranked trait bounds](https://blog.rust-lang.org/2022/10/28/gats-stabilization.html#implied-static-requirement-from-higher-ranked-trait-bounds) for more details). If the user did not explicitly specify the `'static` lifetime when using the GAT, the current error message will only point out the type `does not live long enough` where the type is used, but not where the GAT is specified and how to fix the problem.

The solution:
Add notes at the span where the problematic GATs are specified and suggestions of how to fix the problem by adding `'static` lifetime at the right spans.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints 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

2 participants