Skip to content

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

Open
@ilyvion

Description

@ilyvion

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.S-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions