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

Missing explicit lifetime in associated type suggests introducing invalid higher-ranked lifetime #122025

Open
ilyvion opened this issue Mar 5, 2024 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ilyvion
Copy link

ilyvion commented Mar 5, 2024

Code

trait T {
    type Assoc;
}

fn foo<F>(t: F) where F: T<Assoc=&str> {}

Current output

error[E0637]: `&` without an explicit lifetime name cannot be used here
 --> src/main.rs:5:34
  |
5 | fn foo<F>(t: F) where F: T<Assoc=&str> {}
  |                                  ^ explicit lifetime name needed here
  |
help: consider introducing a higher-ranked lifetime here
  |
5 | fn foo<F>(t: F) where F: for<'a> T<Assoc=&'a str> {}
  |                          +++++++          ++

Desired output

error[E0637]: `&` without an explicit lifetime name cannot be used here
 --> src/main.rs:5:34
  |
5 | fn foo<F>(t: F) where F: T<Assoc=&str> {}
  |                                  ^ explicit lifetime name needed here
  |
help: consider adding an explicit lifetime here
  |
5 | fn foo<'a, F>(t: F) where F: T<Assoc=&'a str> {}
  |        ++                             ++

Rationale and extra context

If you try to follow the suggested help text, it just gets you another error:

error[E0582]: binding for associated type `Assoc` references lifetime `'a`, which does not appear in the trait input types
  --> src/lib.rs:17:36
   |
5  | fn foo<F>(t: F) where F: for<'a> T<Assoc=&'a str> {}
   |                                    ^^^^^^^^^^^^^

i.e. it's not the correct course of action here

Other cases

No response

Rust Version

❯ rustc --version --verbose
rustc 1.76.0 (07dca489a 2024-02-04)
binary: rustc
commit-hash: 07dca489ac2d933c78d3c5158e3f43beefeb02ce
commit-date: 2024-02-04
host: x86_64-pc-windows-msvc
release: 1.76.0
LLVM version: 17.0.6

But also verified to behave the same on nightly (2024-03-04 d18480b84fdbf1efc34f) on the playground

Anything else?

No response

@ilyvion ilyvion 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 Mar 5, 2024
@jieyouxu jieyouxu added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue labels Mar 5, 2024
@veera-sivarajan
Copy link
Contributor

@rustbot claim

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-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue 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