Rust: Confusing error message: "expected 2 lifetime parameters"

Created on 24 Apr 2019  路  9Comments  路  Source: rust-lang/rust

This code:

pub fn cli_args() -> clap::App<'_, '_> {
    clap_app! {foo =>
        (about: "foo the bar.")
    }
}

Gives me the following error:

error[E0106]: missing lifetime specifiers
  --> src/analysis.rs:10:32
   |
10 | pub fn cli_args() -> clap::App<'_, '_> {
   |                                ^^ expected 2 lifetime parameters

But I do have 2 lifetime parameters?

A-diagnostics A-lifetimes A-suggestion-diagnostics C-enhancement D-papercut T-compiler

Most helpful comment

There is always that compiler buzzword: _ambiguous_

me: _writes code_
rustc: 馃しZWJ馃

All 9 comments

CC https://github.com/rust-lang/rust/issues/60199 which shows how you can easily end with this code thanks to compiler suggestions.

I believe the parser is unifying the two lifetimes as one because they have the same name. @nikomatsakis was this intended? I was under the impression that multiple uses '_ would always introduce a new lifetime. The fix is either for changing the current behavior to accept this code, or fix the diagnostics and suggestions to explain the need for named lifetimes in this case.

In that case, the error just needs to be updated to say "only one elided lifetime is allowed" or something. But I'm very curious why that is the case.

Hmm... also what are the odds that the same error was reported less than a day ago? Is this a regression or has it always done this?

Changing the code I linked above to be Elide::FreshLateAnon(Cell::new(lifetime_count as u32)) with the following code

struct S<'a, 'b> {
    a: &'a usize,
    b: &'b usize,
}

fn foo() -> S<'_, '_> {
    unimplemented!();
}
fn main() {
}

emits the following output (and changes some existing tests' output the same way)

error[E0581]: return type references an anonymous lifetime which is not constrained by the fn input types
 --> file.rs:6:13
  |
6 | fn foo() -> S<'_, '_> {
  |             ^^^^^^^^^
  |
  = note: lifetimes appearing in an associated type are not considered constrained

Is this a regression or has it always done this?

It's always been the case since stabilization in 1.26, and probably since Jan 2017 when the code was originally written. My guess would be the "hidden lifetime parameters in types are deprecated" landing recently causing this type of code more likely to appear due to suggestions.


I believe that this can be solved as a simple diagnostics fix, but am intrigued at how to communicate why S<'_, '_> isn't valid (sure, we can say that it is hard to decide what lifetime goes with what, but it sounds like a cop-out).

There is always that compiler buzzword: _ambiguous_ :stuck_out_tongue:

There is always that compiler buzzword: _ambiguous_

me: _writes code_
rustc: 馃しZWJ馃

Similar issue, also because of clap.

The Rust compiler suggested this:

help: try adding a return type
... clap::app::App<'_, '_> {

If I understand the above discussion, then the compiler suggestion will fail, because of multiple elision. Can the compiler suggestion be adjusted?

@joelparkerhenderson we do so when possible, but it is considered acceptable (at the very least tolerated) if a suggestion gives you wrong code that a subsequent compilation will receive a new suggestion that _does_ get you to working code.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pedrohjordao picture pedrohjordao  路  3Comments

cuviper picture cuviper  路  3Comments

wthrowe picture wthrowe  路  3Comments

mcarton picture mcarton  路  3Comments

modsec picture modsec  路  3Comments