Skip to content

Inspect does not feed properly into map #81685

Open
@Xavientois

Description

@Xavientois

I tried this code:

pub fn compile<S>(filenames: &[S]) -> String
  where
      S: AsRef<str> + AsRef<Path> + Display,
  {
      let source_strings: Vec<_> = filenames
          .iter()
          .inspect(file::validate_filename)
          .map(file::open_file)
          .map(byte_reader::read_bytes)
          .collect();

     // -- snip
  }

I expected to see this happen: For the code to compile, and for the inspect to not break the iterator like it does. It works when I write it like this:

pub fn compile<S>(filenames: &[S]) -> String
  where
      S: AsRef<str> + AsRef<Path> + Display,
  {
      let source_strings: Vec<_> = filenames
          .iter()
          .inspect(|f| file::validate_filename(f))
          .map(file::open_file)
          .map(byte_reader::read_bytes)
          .collect();

     // -- snip
  }

Instead, this happened: It gives me a compiler error:

error[E0599]: no method named `map` found for struct `Inspect<std::slice::Iter<'_, S>, fn(&&S) {validate_filename::<&&S>}>` in the current scope
    --> src/compile.rs:12:10
     |
12   |         .map(file::open_file)
     |          ^^^ method not found in `Inspect<std::slice::Iter<'_, S>, fn(&&S) {validate_filename::<&&S>}>`
     |
    ::: /Users/rrampersad/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/iter/adapters/mod.rs:2743:1
     |
2743 | pub struct Inspect<I, F> {
     | ------------------------ doesn't satisfy `_: Iterator`
     |
     = note: the method `map` exists but the following trait bounds were not satisfied:
             `<fn(&&S) {validate_filename::<&&S>} as FnOnce<(&&S,)>>::Output = ()`
             which is required by `Inspect<std::slice::Iter<'_, S>, fn(&&S) {validate_filename::<&&S>}>: Iterator`
             `fn(&&S) {validate_filename::<&&S>}: FnMut<(&&S,)>`
             which is required by `Inspect<std::slice::Iter<'_, S>, fn(&&S) {validate_filename::<&&S>}>: Iterator`
             `Inspect<std::slice::Iter<'_, S>, fn(&&S) {validate_filename::<&&S>}>: Iterator`
             which is required by `&mut Inspect<std::slice::Iter<'_, S>, fn(&&S) {validate_filename::<&&S>}>: Iterator`

error[E0599]: no method named `map` found for struct `Inspect<std::slice::Iter<'_, S>, fn(&&S) {validate_filename::<&&S>}>` in the current scope
    --> src/compile.rs:12:10
     |
12   |         .map(file::open_file)
     |          ^^^ method not found in `Inspect<std::slice::Iter<'_, S>, fn(&&S) {validate_filename::<&&S>}>`
     |
    ::: /Users/rrampersad/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/iter/adapters/mod.rs:2743:1
     |
2743 | pub struct Inspect<I, F> {
     | ------------------------ doesn't satisfy `_: Iterator`
     |
     = note: the method `map` exists but the following trait bounds were not satisfied:
             `<fn(&&S) {validate_filename::<&&S>} as FnOnce<(&&S,)>>::Output = ()`
             which is required by `Inspect<std::slice::Iter<'_, S>, fn(&&S) {validate_filename::<&&S>}>: Iterator`
             `fn(&&S) {validate_filename::<&&S>}: FnMut<(&&S,)>`
             which is required by `Inspect<std::slice::Iter<'_, S>, fn(&&S) {validate_filename::<&&S>}>: Iterator`
             `Inspect<std::slice::Iter<'_, S>, fn(&&S) {validate_filename::<&&S>}>: Iterator`
             which is required by `&mut Inspect<std::slice::Iter<'_, S>, fn(&&S) {validate_filename::<&&S>}>: Iterator`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0599`.
error: aborting due to previous error

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

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

Meta

rustc --version --verbose:

rustc 1.49.0 (e1884a8e3 2020-12-29)
binary: rustc
commit-hash: e1884a8e3c3e813aada8254edfa120e85bf5ffca
commit-date: 2020-12-29
host: x86_64-apple-darwin
release: 1.49.0
Signature of `validate_filename`

``` pub fn validate_filename(filename: S) where S: AsRef + Display, { ... } ```

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-inferenceArea: Type inferenceA-type-systemArea: Type systemC-bugCategory: This is a bug.D-confusingDiagnostics: Confusing error or lint that should be reworked.T-typesRelevant to the types 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