Skip to content

GAT: elided lifetimes in paths errors with error[E0107]: missing generics for associated type #81862

Closed
@memoryruins

Description

@memoryruins
#![allow(incomplete_features)]
#![feature(generic_associated_types)]

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

Expectation from the rfc:

Lastly, lifetimes can be elided in associated type constructors in the same manner that they can be elided in other type constructors.

Instead, this happened:

error[E0107]: missing generics for associated type `StreamingIterator::Item`
 --> src/main.rs:6:40
  |
6 |     fn next(&mut self) -> Option<Self::Item>;
  |                                        ^^^^ expected 1 lifetime argument
  |
note: associated type defined here, with 1 lifetime parameter: `'a`
 --> src/main.rs:5:10
  |
5 |     type Item<'a>;
  |          ^^^^ --
help: use angle brackets to add missing lifetime argument
  |
6 |     fn next(&mut self) -> Option<Self::Item<'a>>;
  |                                            ^^^^

If we follow the diagnostic and change it to

fn next(&mut self) -> Option<Self::Item<'a>>;

It will error with the following:

   Compiling playground v0.0.1 (/playground)
error[E0261]: use of undeclared lifetime name `'a`
 --> src/main.rs:6:45
  |
6 |     fn next(&mut self) -> Option<Self::Item<'a>>;
  |                                             ^^ undeclared lifetime
  |
  = help: if you want to experiment with in-band lifetime bindings, add `#![feature(in_band_lifetimes)]` to the crate attributes
help: consider introducing lifetime `'a` here
  |
4 | trait StreamingIterator<'a> {
  |                        ^^^^
help: consider introducing lifetime `'a` here
  |
6 |     fn next<'a>(&mut self) -> Option<Self::Item<'a>>;
  |            ^^^^

If we follow only the suggestion of fn next<'a>(&mut self) -> Option<Self::Item<'a>> or instead enable the in-band lifetime feature, then it will compile.

An alternative to the compiler suggestions would be to use a placeholder lifetime,

fn next(&mut self) -> Option<Self::Item<'_>>;

which is the preferred style over elided lifetimes in paths today.

  • Should eliding lifetimes in paths still be expected to work?
  • If not, could the initial diagnostic be improved?

Meta

rustc --version --verbose:

rustc 1.52.0-nightly (a73c2e555 2021-02-06)
binary: rustc
commit-hash: a73c2e555c26ef0c8b98c91c97a7d24b7017267f
commit-date: 2021-02-06
host: x86_64-pc-windows-msvc
release: 1.52.0-nightly
LLVM version: 11.0.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-GATsArea: Generic associated types (GATs)F-generic_associated_types`#![feature(generic_associated_types)]` a.k.a. GATsrequires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions