Skip to content

Wrong where Self: 'a GAT bound suggested #90816

Open

Description

@vandenheuvel

Relates to #89970 and potentially to #90808.

#![feature(generic_associated_types)]

trait MyTrait {
    type AT<'a> where Self: 'a;
    
    fn selfreffing<'a>(&'a self) -> Self::AT<'a> { unimplemented!(); }
}

struct Inner;
impl MyTrait for Inner {
    type AT<'b> where Self: 'b = ();
}

struct Outer<'a, T>(&'a T);
impl<'a, T: MyTrait> MyTrait for Outer<'a, T> {
    type AT<'b> = T::AT<'b>;
    // type AT<'b> where T: 'b = T::AT<'b>; // suggested
    // type AT<'b> where Self: 'b = T::AT<'b>; // compiles
}

Playground

Activity

  1. vandenheuvel commented on Nov 12, 2021

    @vandenheuvel
    ContributorAuthor

    When switching to type AT<'b> = T::AT<'a>, the correct error is given, but the hint is not provided.

    #![feature(generic_associated_types)]
    
    trait MyTrait {
        type AT<'a> where Self: 'a;
        
        fn selfreffing<'a>(&'a self) -> Self::AT<'a> { unimplemented!(); }
    }
    
    struct Inner;
    impl MyTrait for Inner {
        type AT<'b> where Self: 'b = ();
    }
    
    struct Outer<'a, T>(&'a T);
    impl<'a, T: MyTrait> MyTrait for Outer<'a, T> {
        type AT<'b> = T::AT<'a>;
        // Should recommend type AT<'b> where Self: 'b = T::AT<'a>;
    }
    error[E0477]: the type `Outer<'a, T>` does not fulfill the required lifetime
      --> src/lib.rs:16:5
       |
    16 |     type AT<'b> = T::AT<'a>;
       |     ^^^^^^^^^^^^^^^^^^^^^^^^
       |
    note: type must outlive the lifetime `'b` as defined here
      --> src/lib.rs:16:13
       |
    16 |     type AT<'b> = T::AT<'a>;
       |             ^^
    
    For more information about this error, try `rustc --explain E0477`.
    

    Playground

  2. jackh726 commented on Dec 14, 2021

    @jackh726
    Member

    GATs issue triage: not blocking. I don't like that this diagnostic is just wrong. The "easy" fix here would be to disable the "consider adding a where clause" help if that where clause isn't on the trait, but a proper fix would be able to suggest Self: 'a. Either way, this is just poor diagnostics and doesn't miscompile.

  3. added a commit that references this issue on Sep 13, 2022
  4. added a commit that references this issue on Jan 24, 2023
  5. added
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Apr 5, 2023
  6. Dylan-DPC commented on May 3, 2023

    @Dylan-DPC
    Member

    Current output:

    error[[E0309]](https://doc.rust-lang.org/nightly/error_codes/E0309.html): the parameter type `T` may not live long enough
      --> src/lib.rs:16:19
       |
    16 |     type AT<'b> = T::AT<'b>;
       |                   ^^^^^^^^^- help: consider adding a where clause: `where T: 'b`
       |                   |
       |                   ...so that the type `T` will meet its required lifetime bounds...
       |
    note: ...that is required by this bound
      --> src/lib.rs:4:29
       |
    4  |     type AT<'a> where Self: 'a;
       |                             ^^
    
    For more information about this error, try `rustc --explain E0309`.
    
  7. added
    A-GATsArea: Generic associated types (GATs)
    and removed
    C-bugCategory: This is a bug.
    on Sep 24, 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)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