Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

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

Closed
memoryruins opened this issue Feb 7, 2021 · 2 comments · Fixed by #82272
Labels
A-GATs Area: Generic associated types (GATs) F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs requires-nightly This issue requires a nightly compiler in some way.

Comments

@memoryruins
Copy link
Contributor

memoryruins commented Feb 7, 2021

#![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
@jonas-schievink jonas-schievink added the F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs label Feb 7, 2021
@jackh726 jackh726 added the requires-nightly This issue requires a nightly compiler in some way. label Feb 8, 2021
@nikomatsakis
Copy link
Contributor

I believe that we should require an explicit lifetime parameter, but we should suggest (and accept) '_ here.

@nikomatsakis
Copy link
Contributor

The reasoning is that we are moving towards a model where you are required to "acknowledge" lifetime parameters (elided-lifetimes-in-paths), so why not make it required in newer code.

That said, for consistency, I think having it be consistent with any other parameter would also be ok.

@bors bors closed this as completed in ba8d7e2 May 11, 2021
@fmease fmease added the A-GATs Area: Generic associated types (GATs) label Nov 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-GATs Area: Generic associated types (GATs) F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs requires-nightly This issue requires a nightly compiler in some way.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants