Skip to content

Emit suggestion for where &T: Trait #82312

Closed
@estebank

Description

@estebank

Given:

fn foo<T>(x: T) where &T: std::io::Read {
    panic!();
}

we currently emit

error[E0637]: `&` without an explicit lifetime name cannot be used here
 --> src/main.rs:1:23
  |
1 | fn foo<T>(x: T) where &T: std::io::Read {
  |                       ^ explicit lifetime name needed here

error[E0310]: the parameter type `T` may not live long enough
 --> src/main.rs:1:27
  |
1 | fn foo<T>(x: T) where &T: std::io::Read {
  |        -                  ^^^^^^^^^^^^^ ...so that the reference type `&'static T` does not outlive the data it points at
  |        |
  |        help: consider adding an explicit lifetime bound...: `T: 'static`

'static suggestions are a lot of the time incorrect, but in this case what we can suggest is

fn foo<T>(x: T) where for<'a> &'a T: std::io::Read {

and both errors go away. Ideally we could silence the second error and have a structured suggestion for the first.

Taken from https://github.com/rust-lang/rust/pull/82194/files#r578364173

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.D-papercutDiagnostics: An error or lint that needs small tweaks.T-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