Skip to content

Multiple anonymous lifetimes bounds upset compiler #60199

Closed
@imp

Description

@imp

TL;DR Rust is unable to compile its own hint with anonymous lifetimes
(Playground)

#![warn(rust_2018_idioms)]

use clap::{App, SubCommand};

fn app() -> App {
    SubCommand::with_name("app")
}

fn main () {
    let _matches = app().get_matches();
}

Rust doesn't like it as App is defined with two lifetimes - App<'a, 'b>
and indeed the hint is dead on:

   Compiling playground v0.0.1 (/playground)
warning: hidden lifetime parameters in types are deprecated
 --> src/lib.rs:5:13
  |
5 | fn app() -> App {
  |             ^^^- help: indicate the anonymous lifetimes: `<'_, '_>`
  |
note: lint level defined here
 --> src/lib.rs:1:9
  |
1 | #![warn(rust_2018_idioms)]
  |         ^^^^^^^^^^^^^^^^
  = note: #[warn(elided_lifetimes_in_paths)] implied by #[warn(rust_2018_idioms)]

So we do as suggested and add two anonymous lifetimes to App<'_, '_>

#![warn(rust_2018_idioms)]

use clap::{App, SubCommand};

fn app() -> App<'_, '_> {
    SubCommand::with_name("app")
}

fn main () {
    let _matches = app().get_matches();
}

This time it is even worth as now compiler coudn't tell these two lifetimes apart

   Compiling playground v0.0.1 (/playground)
error[E0106]: missing lifetime specifiers
 --> src/lib.rs:5:17
  |
5 | fn app() -> App<'_, '_> {
  |                 ^^ expected 2 lifetime parameters

error: aborting due to previous error

So it is either anonymous liftime logic should be fixed to work similar to the type hints, or the help message should avoid suggesting incorrect syntax.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-edition-2018Area: The 2018 editionA-lifetimesArea: Lifetimes / regionsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.L-elided_lifetimes_in_pathsLint: elided_lifetimes_in_pathsT-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