Skip to content

Intervening type alias of GAT to causes incorrect E0581 #85533

Closed

Description

@elidupree

This code [playground]:

#![feature(generic_associated_types)]

trait Trait {
    type Assoc<'a>;
}

type AssocAlias<'a, T> = <T as Trait>::Assoc<'a>;

fn works<'a, T: Trait>(entity: T::Assoc<'a>) -> &'a i32 {&5}
fn fails<'a, T: Trait>(entity: AssocAlias<'a, T>) -> &'a i32 {&5}

Produces the following error on 1.54.0-nightly (2021-05-19 f94942d):

error[E0581]: return type references lifetime `'a`, which is not constrained by the fn input types
  --> src/lib.rs:10:54
   |
10 | fn fails<'a, T: Trait>(entity: AssocAlias<'a, T>) -> &'a i32 {&5}
   |            

The version that uses T::Assoc directly works, but the one using the type alias (which should be identical) fails.

Activity

  1. jackh726 commented on Jul 21, 2021

    @jackh726
    Member

    This is a variation of #47511 and it isn't a GAT issue.

    Essentially, the issue is that because of the way we assign a lifetime as late-bound or early-bound, we end up assigning 'a as early bound in the first and late bound in the second. Then later once the type alias is expanded, we see that it's not actually constrained (where that requirement isn't there for early bound regions).

    Although it's kind of not great, there are a couple workarounds:

    fn fails<'a, 'b: 'a, T: Trait>(entity: AssocAlias<'a, T>) -> &'a i32 {&5}
    fn fails<'a, T: Trait + 'a>(entity: AssocAlias<'a, T>) -> &'a i32 {&5}

    Not going to remove the GAT label here (since I think this is an important consideration), but this is non-blocking for stabilization.

  2. jackh726 commented on Oct 16, 2021

    @jackh726
    Member

    GATs issue triage: not blocking. As mentioned previously, this is caused by us treating regions in type alias substs as constrained, but not in projections - leading to one function assigning a region as early-bound and the other as late-bound. Not a GATs issue, but keeping label for now, since we probably want to think about this diagnostics-wise at least at some point.

  3. added a commit that references this issue on Sep 13, 2022
  4. added 3 commits that reference this issue on Nov 8, 2022
    254942c
    9d8ac54
    f162e3a
  5. added a commit that references this issue on Jan 24, 2023
  6. added
    A-GATsArea: Generic associated types (GATs)
    on Nov 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-GATsArea: Generic associated types (GATs)C-bugCategory: This is a bug.F-generic_associated_types`#![feature(generic_associated_types)]` a.k.a. GATs

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions