Skip to content

bad suggestion for type lifetime bounds with anonymous lifetimes #115375

Closed
@aliemjay

Description

@aliemjay

Given this code:

type Lt<'lt> = *mut &'lt u8;

fn outlives<'lt, T: 'lt>(_: T, _: Lt<'lt>) {}

pub fn test<A>(arg: A, lt: Lt<'_>, _: &str) {
    outlives(arg, lt);
    //~^ ERROR the parameter type `A` may not live long enough
}

The current error message is this: (note the invalid suggestion)

error[[E0311]](https://doc.rust-lang.org/nightly/error_codes/E0311.html): the parameter type `A` may not live long enough
 --> src/lib.rs:6:5
  |
6 |     outlives(arg, lt);
  |     ^^^^^^^^^^^^^^^^^
  |
note: the parameter type `A` must be valid for the anonymous lifetime defined here...
 --> src/lib.rs:5:28
  |
5 | pub fn test<A>(arg: A, lt: Lt<'_>, _: &str) {
  |                            ^^^^^^
note: ...so that the type `A` will meet its required lifetime bounds
 --> src/lib.rs:6:5
  |
6 |     outlives(arg, lt);
  |     ^^^^^^^^^^^^^^^^^
help: consider adding an explicit lifetime bound...
  |
5 | pub fn test<'a, A: 'a>(arg: A, lt: Lt<'_<'a>>, _: &'a str) {
  |             +++  ++++                   ++++       ++

For more information about this error, try `rustc --explain E0311`.

Two problems with this suggestion:

  • the last argument of type &str should not be touched by the suggestion.
  • the anonymous lifetime '_ should be correctly replaced by the named lifetime 'a.

The fixed suggestion should be:

help: consider adding an explicit lifetime bound...
  |
5 | pub fn test<'a, A: 'a>(arg: A, lt: Lt<'a>, _: &str) {
  |             +++  ++++                 ++

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-borrow-checkerArea: The borrow checkerA-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.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