Skip to content

Somewhat confusing lifetime error #79281

@camelid

Description

@camelid

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsC-bugCategory: This is a bug.D-papercutDiagnostics: An error or lint that needs small tweaks.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