Skip to content

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

Open
@est31

Description

@est31

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.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsD-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-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