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

Inspect does not feed properly into map #81685

Open
Xavientois opened this issue Feb 2, 2021 · 1 comment
Open

Inspect does not feed properly into map #81685

Xavientois opened this issue Feb 2, 2021 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-inference Area: Type inference A-typesystem Area: The type system C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked.

Comments

@Xavientois
Copy link
Contributor

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, { ... } ```

@Xavientois Xavientois added the C-bug Category: This is a bug. label Feb 2, 2021
@jonas-schievink jonas-schievink added the E-needs-mcve Call for participation: This issue has a repro, but needs a Minimal Complete and Verifiable Example label Feb 3, 2021
@SNCPlay42
Copy link
Contributor

Reduced:

fn validate_filename<S>(_: S) {}

fn main() {
    let _: Vec<_> = Some(()).into_iter().inspect(validate_filename).collect();
}

If I change it to

fn validate_filename<S>(_: S) {}

fn should_be_iterator<I: Iterator>(_: I) {}

fn main() {
    should_be_iterator(Some(()).into_iter().inspect(validate_filename));
}

I get this output:

error[E0308]: mismatched types
 --> src/main.rs:6:45
  |
6 |     should_be_iterator(Some(()).into_iter().inspect(validate_filename));
  |                                             ^^^^^^^ one type is more general than the other
  |
  = note: expected type `FnOnce<(&(),)>`
             found type `FnOnce<(&(),)>`

So I think this is related to/another version of #75791.

@rustbot label -E-needs-mcve

@rustbot rustbot removed the E-needs-mcve Call for participation: This issue has a repro, but needs a Minimal Complete and Verifiable Example label Mar 28, 2021
@ChrisDenton ChrisDenton added the needs-triage-legacy Old issue that were never triaged. Remove this label once the issue has been sufficiently triaged. label Jul 16, 2023
@jieyouxu jieyouxu added A-typesystem Area: The type system A-diagnostics Area: Messages for errors, warnings, and lints A-inference Area: Type inference D-confusing Diagnostics: Confusing error or lint that should be reworked. and removed needs-triage-legacy Old issue that were never triaged. Remove this label once the issue has been sufficiently triaged. labels Feb 18, 2024
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-inference Area: Type inference A-typesystem Area: The type system C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked.
Projects
None yet
Development

No branches or pull requests

6 participants