Skip to content

elided_lifetimes_in_paths lint is broken #51902

Closed
@reuvenpo

Description

@reuvenpo

Using the latest stable compiler(1.27) (and probably earlier ones too) given the snippet:

#![deny(elided_lifetimes_in_paths)]

struct Bar<'a> {
    a: &'a i32,
}

fn main() {
    let bar = Bar::<'_> {a: &42};
}

we get the error:

error: hidden lifetime parameters are deprecated, try `Foo<'_>`
 --> src/main.rs:8:21
  |
8 |     let bar = Bar::<'_> {a: &42};
  |                     ^^
  |
note: lint level defined here
 --> src/main.rs:1:9
  |
1 | #![deny(elided_lifetimes_in_paths)]
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

This makes no sense, both because the name Foo is constant in the error message, and both because the lifetime parameter suggested doesn't fix anything. (again, because the error message is just a constant)
https://doc.rust-lang.org/nightly/nightly-rustc/rustc/lint/builtin/static.ELIDED_LIFETIMES_IN_PATHS.html

Note, this does compile:

#![deny(elided_lifetimes_in_paths)]

struct Bar<'a> {
    a: &'a i32,
}

fn fun<'a>() {
    let bar = Bar::<'a> {a: &42};
}

fn main() {
    fun();
}

but since main can't have any lifetime parameters, Bar can no be initialized directly in main, and fun is defined with a lifetime parameter that is only used internally, rather than giving any information about how the function is to be used.

while experimenting with the same code, THIS COMPILED:

// #![deny(elided_lifetimes_in_paths)]  // Note this is commented out, as in, unrelated

struct Foo {
    a: i32
}

struct Bar<'a> {
    a: &'a Foo,
}

fn fun<'a>() -> Bar<'a> {
    let bar = Bar::<'a> {a: &Foo{a: 42}};
    bar
}

fn main() {
    fun();
}

EDIT: This last example compiles because of the "rvalue static promotion" feature, which i was not aware of when opening the issue. Had that Foo been a mutable local variable, this would correctly fail. Learn something new every day :)

See also #51903.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regionsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions