Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confusing error message: "expected 2 lifetime parameters" #60216

Closed
mark-i-m opened this issue Apr 23, 2019 · 10 comments
Closed

Confusing error message: "expected 2 lifetime parameters" #60216

mark-i-m opened this issue Apr 23, 2019 · 10 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@mark-i-m
Copy link
Member

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?

@nagisa nagisa added the A-diagnostics Area: Messages for errors, warnings, and lints label Apr 23, 2019
@estebank
Copy link
Contributor

CC #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.

@estebank estebank added the A-lifetimes Area: Lifetimes / regions label Apr 23, 2019
@estebank
Copy link
Contributor

It does seem like accepting only one '_ lifetime was intentional:

let elide = if lifetime_count == 1 {
Elide::Exact(possible_implied_output_region.unwrap())
} else {
Elide::Error(arg_lifetimes)
};

@mark-i-m
Copy link
Member Author

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.

@mark-i-m
Copy link
Member Author

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?

@estebank
Copy link
Contributor

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).

@mark-i-m
Copy link
Member Author

There is always that compiler buzzword: ambiguous 😛

@estebank
Copy link
Contributor

There is always that compiler buzzword: ambiguous

me: writes code
rustc: 🤷ZWJ🦀

@joelparkerhenderson
Copy link

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?

@estebank
Copy link
Contributor

@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.

@estebank estebank added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` labels Aug 28, 2019
@estebank estebank added the D-papercut Diagnostics: An error or lint that needs small tweaks. label Oct 8, 2019
@crlf0710 crlf0710 added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jun 11, 2020
@kadiwa4
Copy link
Contributor

kadiwa4 commented Jan 17, 2023

The error message is helpful now (nightly 2023-01-15, clap 2.34.0):

error[E0106]: missing lifetime specifiers
 --> lib.rs:3:32
  |
3 | pub fn cli_args() -> clap::App<'_, '_> {
  |                                ^^  ^^ expected named lifetime parameter
  |                                |
  |                                expected named lifetime parameter
  |
  = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
help: consider using the `'static` lifetime
  |
3 | pub fn cli_args() -> clap::App<'static, 'static> {
  |                                ~~~~~~~  ~~~~~~~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants