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

Inapplicable "add explicit lifetime bound" suggestion for trait with GAT bound #115332

Open
detly opened this issue Aug 29, 2023 · 0 comments
Open
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-GATs Area: Generic associated types (GATs) D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@detly
Copy link

detly commented Aug 29, 2023

Code

pub trait HasGat {
    type Gat<'a>
    where
        Self: 'a;
}

pub trait NoGat: for<'a> HasGat<Gat<'a> = Self::At> {
    type At;
}

Current output

Compiling playground v0.0.1 (/playground)
error[E0311]: the parameter type `Self` may not live long enough
 --> src/lib.rs:7:33
  |
7 | pub trait NoGat: for<'a> HasGat<Gat<'a> = Self::At> {
  |                                 ^^^^^^^^^^^^^^^^^^
  |
note: the parameter type `Self` must be valid for the lifetime `'a` as defined here...
 --> src/lib.rs:7:22
  |
7 | pub trait NoGat: for<'a> HasGat<Gat<'a> = Self::At> {
  |                      ^^
  = help: consider adding an explicit lifetime bound `Self: 'a`...
note: ...so that the type `Self` will meet its required lifetime bounds...
 --> src/lib.rs:7:33
  |
7 | pub trait NoGat: for<'a> HasGat<Gat<'a> = Self::At> {
  |                                 ^^^^^^^^^^^^^^^^^^
note: ...that is required by this bound
 --> src/lib.rs:4:15
  |
4 |         Self: 'a;
  |               ^^

error[E0311]: the parameter type `Self` may not live long enough
 --> src/lib.rs:8:5
  |
8 |     type At;
  |     ^^^^^^^^
  |
note: the parameter type `Self` must be valid for the lifetime `'a` as defined here...
 --> src/lib.rs:7:22
  |
7 | pub trait NoGat: for<'a> HasGat<Gat<'a> = Self::At> {
  |                      ^^
  = help: consider adding an explicit lifetime bound `Self: 'a`...
note: ...so that the type `Self` will meet its required lifetime bounds...
 --> src/lib.rs:8:5
  |
8 |     type At;
  |     ^^^^^^^^
note: ...that is required by this bound
 --> src/lib.rs:4:15
  |
4 |         Self: 'a;
  |               ^^

For more information about this error, try `rustc --explain E0311`.
error: could not compile `playground` (lib) due to 2 previous errors

Desired output

No response

Rationale and extra context

The message contains the suggestion

consider adding an explicit lifetime bound Self: 'a...

...but there is nowhere I can see that would be syntactically valid to do this.

One solution is to have trait NoGat: 'static + for<'a> ..., but I don't know that it should be the suggestion here, because it's potentially stricter than necessary.

This seems related to #96465, but note that this is not about whether this bound is a reasonable one or not, or how to express it if it is, but that the "help" message is not applicable (and quite confusing when more complex types and traits are involved).

Other cases

No response

Anything else?

No response

@detly detly 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 Aug 29, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Aug 29, 2023
@fmease fmease added F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Oct 1, 2023
@fmease fmease added A-GATs Area: Generic associated types (GATs) and removed F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs labels Sep 24, 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) D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants