Skip to content

On missing HRLT annotation, we sometimes provide a slightly incorrect suggestion #72404

Closed
@estebank

Description

@estebank

Given the following code:

struct X<'a>(&'a ());
struct S<'a>(&'a dyn Fn(&X) -> &X);

fn main() {
    let x = S(&|x| {
        println!("hi");
        x
    });
    x.0(&X(&()));
}

we currently give the following output

error[E0106]: missing lifetime specifier
 --> src/main.rs:2:32
  |
2 | struct S<'a>(&'a dyn Fn(&X) -> &X);
  |                         --     ^ expected named lifetime parameter
  |
  = help: this function's return type contains a borrowed value, but the signature does not say which one of argument 1's 2 lifetimes it is borrowed from
  = note: for more information on higher-ranked polymorphism, visit https://doc.rust-lang.org/nomicon/hrtb.html
help: consider making the bound lifetime-generic with a new `'a` lifetime
  |
2 | struct S<'a>(&'a dyn for<'a> Fn(&'a X) -> &'a X);
  |                      ^^^^^^^    ^^^^^     ^^^
help: consider introducing a named lifetime parameter
  |
2 | struct S<'a, 'a>(&'a dyn Fn(&'a X) -> &'a X);
  |          ^^^                ^^^^^     ^^^

error[E0106]: missing lifetime specifier
 --> src/main.rs:2:33
  |
2 | struct S<'a>(&'a dyn Fn(&X) -> &X);
  |                         --      ^ expected named lifetime parameter
  |
  = help: this function's return type contains a borrowed value, but the signature does not say which one of argument 1's 2 lifetimes it is borrowed from
  = note: for more information on higher-ranked polymorphism, visit https://doc.rust-lang.org/nomicon/hrtb.html
help: consider making the bound lifetime-generic with a new `'a` lifetime
  |
2 | struct S<'a>(&'a dyn for<'a> Fn(&'a X) -> &X<'a>);
  |                      ^^^^^^^    ^^^^^      ^^^^^
help: consider introducing a named lifetime parameter
  |
2 | struct S<'a, 'a>(&'a dyn Fn(&'a X) -> &X<'a>);
  |          ^^^                ^^^^^      ^^^^^

Beyond the verbosity, one of the suggestions offers struct S<'a, 'a>, which is not ideal.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-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-papercutDiagnostics: An error or lint that needs small tweaks.P-lowLow priorityT-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