Skip to content

GAT: confusing diagnostic when missing where clause  #84931

Closed
@BoxyUwU

Description

@BoxyUwU

Given the following code: playground

#![feature(generic_associated_types)]

trait StreamingIter {
    type Item<'a> where Self: 'a;
    fn next<'a>(&'a mut self) -> Option<Self::Item::<'a>>;
}

struct StreamingSliceIter<'a, T> {
    idx: usize,
    data: &'a mut [T],
}

impl<'b, T: 'b> StreamingIter for StreamingSliceIter<'b, T> {
    type Item<'a> = &'a mut T;
    fn next(&mut self) -> Option<&mut T> {
        self.idx += 1;
        Some(&mut self.data[self.idx - 1])
    }
}

The current output is:

error[E0309]: the parameter type `T` may not live long enough
  --> src/lib.rs:14:5
   |
13 | impl<'b, T: 'b> StreamingIter for StreamingSliceIter<'b, T> {
   |          -- help: consider adding an explicit lifetime bound...: `T: 'a +`
14 |     type Item<'a> = &'a mut T;
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^ ...so that the reference type `&'a mut T` does not outlive the data it points at

error: aborting due to previous error;

I'm not really sure what the correct output should be but it should mention that I forgot the where Self: 'a bound on the trait impl like I have it written in this compiling code. It should also not suggest adding adding a T: 'a bound because that bound is not writeable 😅

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-GATsArea: Generic associated types (GATs)A-diagnosticsArea: Messages for errors, warnings, and lintsD-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.F-generic_associated_types`#![feature(generic_associated_types)]` a.k.a. GATsT-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