Skip to content

Independent lifetimes still considered overly constrained #74502

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

Closed
philip-peterson opened this issue Jul 19, 2020 · 3 comments
Closed

Independent lifetimes still considered overly constrained #74502

philip-peterson opened this issue Jul 19, 2020 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@philip-peterson
Copy link

philip-peterson commented Jul 19, 2020

I saw a confusing error message today which seems related to #60216, but not sure if it's the same.


error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
  --> cli/src/main.rs:27:14
   |
27 |             .arg(
   |              ^^^
   |
note: first, the lifetime cannot outlive the lifetime `'a` as defined on the function body at 24:20...
  --> cli/src/main.rs:24:20
   |
24 | fn with_build_args<'a, 'b, 'c, 'd>(app: Box<App<'a, 'b>>) -> Box<App<'c, 'd>> {
   |                    ^^
note: ...so that the types are compatible
  --> cli/src/main.rs:27:14
   |
27 |             .arg(
   |              ^^^
   = note: expected `clap::App<'_, '_>`
              found `clap::App<'a, 'b>`
note: but, the lifetime must be valid for the lifetime `'c` as defined on the function body at 24:28...
  --> cli/src/main.rs:24:28
   |
24 | fn with_build_args<'a, 'b, 'c, 'd>(app: Box<App<'a, 'b>>) -> Box<App<'c, 'd>> {
   |                            ^^
note: ...so that the expression is assignable
  --> cli/src/main.rs:25:5
   |
25 | /     Box::new(
26 | |         (*app)
27 | |             .arg(
28 | |                 Arg::with_name("release")
...  |
31 | |             )
32 | |     )
   | |_____^
   = note: expected `std::boxed::Box<clap::App<'c, 'd>>`
              found `std::boxed::Box<clap::App<'_, '_>>`

error: aborting due to previous error; 7 warnings emitted

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

The function in question takes four independent lifetimes which (in my mind) should be free to grow as large as the compiler would like.

fn with_build_args<'a, 'b, 'c, 'd>(app: Box<App<'a, 'b>>) -> Box<App<'c, 'd>> {
    Box::new(
        (*app)
            .arg(
                Arg::with_name("release")
                    .help("Whether to invoke `cargo build` using the --release flag")
                    .long("release")
            )
    )
}
@Lonami
Copy link
Contributor

Lonami commented Jul 19, 2020

The problem isn't about growing the lifetimes but the dependencies between them, as the compiler doesn't know the relationship between all of them four, and I don't think the compiler could infer it in this case.

@jyn514 jyn514 added A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. labels Jul 27, 2020
@JohnTitor JohnTitor added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 23, 2020
@kadiwa4
Copy link
Contributor

kadiwa4 commented Jan 17, 2023

The diagnostic has got shorter and I think it's good now (nightly 2023-01-15, clap 2.34.0):

error: lifetime may not live long enough
  --> lib.rs:4:5
   |
3  |   fn with_build_args<'a, 'b, 'c, 'd>(app: Box<App<'a, 'b>>) -> Box<App<'c, 'd>> {
   |                      --      -- lifetime `'c` defined here
   |                      |
   |                      lifetime `'a` defined here
4  | /     Box::new(
5  | |         (*app)
6  | |             .arg(
7  | |                 Arg::with_name("release")
...  |
10 | |             )
11 | |     )
   | |_____^ function was supposed to return data with lifetime `'c` but it is returning data with lifetime `'a`
   |
   = help: consider adding the following bound: `'a: 'c`

error: lifetime may not live long enough
  --> lib.rs:4:5
   |
3  |   fn with_build_args<'a, 'b, 'c, 'd>(app: Box<App<'a, 'b>>) -> Box<App<'c, 'd>> {
   |                          --      -- lifetime `'d` defined here
   |                          |
   |                          lifetime `'b` defined here
4  | /     Box::new(
5  | |         (*app)
6  | |             .arg(
7  | |                 Arg::with_name("release")
...  |
10 | |             )
11 | |     )
   | |_____^ function was supposed to return data with lifetime `'d` but it is returning data with lifetime `'b`
   |
   = help: consider adding the following bound: `'b: 'd`

help: the following changes may resolve your lifetime errors
  |
  = help: add bound `'a: 'c`
  = help: add bound `'b: 'd`

but perhaps it should tell you to replace 'c and 'd with 'a and 'b instead

@estebank
Copy link
Contributor

@philip-peterson feel free to reopen if there's more we could be doing here, but it does seem like the current output would help.

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 C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. 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