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

Somewhat confusing lifetime error #79281

Closed
camelid opened this issue Nov 21, 2020 · 3 comments
Closed

Somewhat confusing lifetime error #79281

camelid opened this issue Nov 21, 2020 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions C-bug Category: This is a bug. 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

@camelid
Copy link
Member

camelid commented Nov 21, 2020

This code (which comes from a rustup PR I'm working on -- rust-lang/rustup#2540) produces a somewhat confusing lifetime error:

    pub fn get_toolchains_from_glob(
        &self,
        pattern: &Pattern,
    ) -> Result<impl Iterator<Item = Toolchain<'_>>> {
        Ok(self
            .list_toolchains_iter()?
            .filter(|toolchain| pattern.matches(toolchain))
            .map(|toolchain| Toolchain::from(self, &toolchain).unwrap()))
    }

    fn list_toolchains_iter(&self) -> Result<Box<dyn Iterator<Item = String>>> {
        if utils::is_directory(&self.toolchains_dir) {
            Ok(Box::new(
                utils::read_dir("toolchains", &self.toolchains_dir)?
                    .filter_map(io::Result::ok)
                    .filter(|e| e.file_type().map(|f| !f.is_file()).unwrap_or(false))
                    .filter_map(|e| e.file_name().into_string().ok()),
            ))
        } else {
            Ok(Box::new(iter::empty()))
        }
    }

Errors:

error[E0623]: lifetime mismatch
   --> src/config.rs:370:17
    |
369 |         pattern: &Pattern,
    |                  -------- this parameter and the return type are declared with different lifetimes...
370 |     ) -> Result<impl Iterator<Item = Toolchain<'_>>> {
    |          -------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
    |                 |
    |                 ...but data from `pattern` is returned here

But isn't the "...but data from" error supposed to point to the code that returns data from pattern, not the return type annotation?

@camelid camelid added A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions C-bug Category: This is a bug. D-papercut Diagnostics: An error or lint that needs small tweaks. labels Nov 21, 2020
@jyn514 jyn514 added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Nov 21, 2020
@camelid
Copy link
Member Author

camelid commented Nov 21, 2020

Here's a fairly minimal repro (I don't think glob is the problem, but this was an easy way to trigger the error):

use glob::Pattern;

struct Foo<'a>(&'a str);

struct Bar(Vec<String>);

impl Bar {
    fn use_pattern(&self, pattern: &Pattern) -> impl Iterator<Item = Foo<'_>> {
        self.0
            .iter()
            .filter(|toolchain| pattern.matches(toolchain))
            .map(|toolchain| Foo(&toolchain))
    }
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0623]: lifetime mismatch
 --> src/lib.rs:8:49
  |
8 |     fn use_pattern(&self, pattern: &Pattern) -> impl Iterator<Item = Foo<'_>> {
  |                                    --------     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |                                    |            |
  |                                    |            ...but data from `pattern` is returned here
  |                                    this parameter and the return type are declared with different lifetimes...

error: aborting due to previous error

For more information about this error, try `rustc --explain E0623`.
error: could not compile `playground`

To learn more, run the command again with --verbose.

@BGR360
Copy link
Contributor

BGR360 commented Dec 29, 2021

It seems the gods of time have smiled upon this one:

Current output on nightly:

error[E0700]: hidden type for `impl Trait` captures lifetime that does not appear in bounds
 --> src/lib.rs:8:49
  |
8 |     fn use_pattern(&self, pattern: &Pattern) -> impl Iterator<Item = Foo<'_>> {
  |                                    --------     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |                                    |
  |                                    hidden type `Map<Filter<std::slice::Iter<'_, String>, [closure@src/lib.rs:11:21: 11:59]>, [closure@src/lib.rs:12:18: 12:45]>` captures the anonymous lifetime defined here
  |
help: to declare that the `impl Trait` captures `'_`, you can add an explicit `'_` lifetime bound
  |
8 |     fn use_pattern(&self, pattern: &Pattern) -> impl Iterator<Item = Foo<'_>> + '_ {
  |                                                                               ++++

@Dylan-DPC
Copy link
Member

The error now explains the problem so closing this.

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 C-bug Category: This is a bug. 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

4 participants