Skip to content

GAT in path suggests wrong lifetime name when elided lifetime is rejected #103815

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

Closed
kupiakos opened this issue Oct 31, 2022 · 0 comments · Fixed by #104048
Closed

GAT in path suggests wrong lifetime name when elided lifetime is rejected #103815

kupiakos opened this issue Oct 31, 2022 · 0 comments · Fixed by #104048
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-GATs Area: Generic associated types (GATs) F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@kupiakos
Copy link
Contributor

Usage of GATs must specify lifetime generic parameters even if they can be inferred, unlike structs and type aliases. Personally, I think this is the wrong choice, though I recognize the linting folks disagree. In any case, the diagnostic output for when '_ is necessary isn't tested in enough scenarios.

Given the following code: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=dec1ee3c77e71e10d6217f53ebb0f86f

// Copyright 2022 Google LLC.
// SPDX-License-Identifier: Apache-2.0

struct MyElement;
struct MyContainer {
    data: Vec<MyElement>,
}

trait MyIndex<Key> {
    type Output<'a> where Self : 'a;
    fn my_index(&self, k: Key) -> Self::Output<'_>;
}

impl MyIndex<usize> for MyContainer {
    type Output<'a> = &'a MyElement;

    fn my_index(&self, n: usize) -> Self::Output {
        &self.data[n]
    }
}

The current output is:

error[E0107]: missing generics for associated type `MyIndex::Output`
  --> src/lib.rs:36:43
   |
36 |     fn my_index(&self, n: usize) -> Self::Output {
   |                                           ^^^^^^ expected 1 lifetime argument
   |
note: associated type defined here, with 1 lifetime parameter: `'a`
  --> src/lib.rs:10:10
   |
10 |     type Output<'a> where Self : 'a;
   |          ^^^^^^ --
help: add missing lifetime argument
   |
36 |     fn my_index(&self, n: usize) -> Self::Output<'a> {
   |                                           ~~~~~~~~~~

Ideally the output should look like:

error[E0107]: missing generics for associated type `MyIndex::Output`
  --> src/lib.rs:36:43
   |
36 |     fn my_index(&self, n: usize) -> Self::Output {
   |                                           ^^^^^^ expected 1 lifetime argument
   |
note: associated type defined here, with 1 lifetime parameter: `'a`
  --> src/lib.rs:10:10
   |
10 |     type Output<'a> where Self : 'a;
   |          ^^^^^^ --
help: add missing lifetime argument
   |
36 |     fn my_index(&self, n: usize) -> Self::Output<'_> {
   |                                           ~~~~~~~~~~

The only difference here is that it suggests using '_ before suggesting it use the lifetime parameter declared in the type definition. It really shouldn't consider any of the generic params in the GAT definition. Note that if I replace it with fn my_index<'b>(&'b self, n: usize) -> Self::Output or fn my_index<'b>(&self, n: usize) -> Self::Output, it suggests using 'b, as expected.

@kupiakos kupiakos added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 31, 2022
@jackh726 jackh726 added the F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs label Nov 2, 2022
@bors bors closed this as completed in 454784a Nov 27, 2022
flip1995 pushed a commit to flip1995/rust-clippy that referenced this issue Dec 1, 2022
Separate lifetime ident from lifetime resolution in HIR

Drive-by: change how suggested generic args are computed.
Fixes rust-lang/rust#103815

I recommend reviewing commit-by-commit.
RalfJung pushed a commit to RalfJung/rust-analyzer that referenced this issue Apr 20, 2024
Separate lifetime ident from lifetime resolution in HIR

Drive-by: change how suggested generic args are computed.
Fixes rust-lang/rust#103815

I recommend reviewing commit-by-commit.
RalfJung pushed a commit to RalfJung/rust-analyzer that referenced this issue Apr 27, 2024
Separate lifetime ident from lifetime resolution in HIR

Drive-by: change how suggested generic args are computed.
Fixes rust-lang/rust#103815

I recommend reviewing commit-by-commit.
@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-diagnostics Area: Messages for errors, warnings, and lints A-GATs Area: Generic associated types (GATs) F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants