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

Error suggests adding '_ lifetime but named lifetime is needed #105227

Open
est31 opened this issue Dec 3, 2022 · 2 comments
Open

Error suggests adding '_ lifetime but named lifetime is needed #105227

est31 opened this issue Dec 3, 2022 · 2 comments
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@est31
Copy link
Member

est31 commented Dec 3, 2022

Code:

fn chars(v :(&str, &str)) -> impl Iterator<Item = char> {
	v.0.chars().chain(v.1.chars())
}

Gives:

error[[E0700]](https://doc.rust-lang.org/stable/error-index.html#E0700): hidden type for `impl Iterator<Item = char>` captures lifetime that does not appear in bounds
 --> src/lib.rs:2:2
  |
1 | fn chars(v :(&str, &str)) -> impl Iterator<Item = char> {
  |              ---- hidden type `std::iter::Chain<Chars<'_>, Chars<'_>>` captures the anonymous lifetime defined here
2 |     v.0.chars().chain(v.1.chars())
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
help: to declare that the `impl Trait` captures `'_`, you can add an explicit `'_` lifetime bound
  |
1 | fn chars(v :(&str, &str)) -> impl Iterator<Item = char> + '_ {
  |                                                         ++++

error[[E0700]](https://doc.rust-lang.org/stable/error-index.html#E0700): hidden type for `impl Iterator<Item = char>` captures lifetime that does not appear in bounds
 --> src/lib.rs:2:2
  |
1 | fn chars(v :(&str, &str)) -> impl Iterator<Item = char> {
  |                    ---- hidden type `std::iter::Chain<Chars<'_>, Chars<'_>>` captures the anonymous lifetime defined here
2 |     v.0.chars().chain(v.1.chars())
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
help: to declare that the `impl Trait` captures `'_`, you can add an explicit `'_` lifetime bound
  |
1 | fn chars(v :(&str, &str)) -> impl Iterator<Item = char> + '_ {
  |                                                         ++++

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

But if you add the '_ bound, it will still fail:

fn chars(v :(&str, &str)) -> impl Iterator<Item = char> + '_ {
	v.0.chars().chain(v.1.chars())
}
error[[E0106]](https://doc.rust-lang.org/nightly/error-index.html#E0106): missing lifetime specifier
 --> src/lib.rs:1:59
  |
1 | fn chars(v :(&str, &str)) -> impl Iterator<Item = char> + '_ {
  |             ------------                                  ^^ expected named lifetime parameter
  |
  = help: this function's return type contains a borrowed value, but the signature does not say which one of `v`'s 2 lifetimes it is borrowed from
help: consider introducing a named lifetime parameter
  |
1 | fn chars<'a>(v :(&'a str, &'a str)) -> impl Iterator<Item = char> + 'a {
  |         ++++      ++       ++                                       ~~

error: lifetime may not live long enough
 --> src/lib.rs:2:2
  |
1 | fn chars(v :(&str, &str)) -> impl Iterator<Item = char> + '_ {
  |              - let's call the lifetime of this reference `'1`
2 |     v.0.chars().chain(v.1.chars())
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ returning this value requires that `'1` must outlive `'static`

error: lifetime may not live long enough
 --> src/lib.rs:2:2
  |
1 | fn chars(v :(&str, &str)) -> impl Iterator<Item = char> + '_ {
  |                    - let's call the lifetime of this reference `'2`
2 |     v.0.chars().chain(v.1.chars())
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ returning this value requires that `'2` must outlive `'static`

For more information about this error, try `rustc --explain E0106`.
fn chars<'a>(v :(&'a str, &'a str)) -> impl Iterator<Item = char> + 'a {
	v.0.chars().chain(v.1.chars())
}

That suggestion does work, but you have to first apply the first (erroring) suggestion to get it.

@est31 est31 added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 3, 2022
@est31
Copy link
Member Author

est31 commented Dec 4, 2022

@rustbot label +T-compiler +D-invalid-suggestion

@rustbot rustbot added the D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. label Dec 4, 2022
@yanchen4791
Copy link
Contributor

@rustbot claim

bors added a commit to rust-lang-ci/rust that referenced this issue Jan 6, 2023
…bank

Suggest adding named lifetime when the return contains value borrowed from more than one lifetimes of function inputs

fix for rust-lang#105227.

The problem: The suggestion of adding an explicit `'_` lifetime bound is **incorrect** when the function's return type contains a value which could be borrowed from more than one lifetimes of the function's inputs. Instead, a named lifetime parameter can be introduced in such a case.

The solution: Checking the number of elided lifetimes in the function signature. If more than one lifetimes found in the function inputs when the suggestion of adding explicit `'_` lifetime, change it to using named lifetime parameter `'a` instead.
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 D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. 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

3 participants