Skip to content

Confusing hint on missing parentheses after collect<> #67680

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
CohenArthur opened this issue Dec 28, 2019 · 3 comments · Fixed by #134154
Closed

Confusing hint on missing parentheses after collect<> #67680

CohenArthur opened this issue Dec 28, 2019 · 3 comments · Fixed by #134154
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@CohenArthur
Copy link
Contributor

CohenArthur commented Dec 28, 2019

The following code produces a quite unhelpful error from the compiler:

use std::io;                                               
use std::fs;
use std::path::{Path, PathBuf};

pub fn build<P: AsRef<Path>>(path: P) -> Result<Vec<PathBuf>, io::Error> {
    let entries = fs::read_dir(path)?;

    let filevec = entries.map(| res | res.map(| e | e.path()))
                         .collect::<Result<Vec<_>, io::Error>>?;

    Ok(filevec)
}

The error in question being .collect::<Result<Vec<_>, io::Error>>?; instead of .collect::<Result<Vec<_>, io::Error>>()?;.

Compiler output:

   Compiling playground v0.0.1 (/playground)
error: field expressions may not have generic arguments
 --> src/lib.rs:9:36
  |
9 |                          .collect::<Result<Vec<_>, io::Error>>?;
  |                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

error: could not compile `playground`.

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

It's quite a simple mistake, but I got confused by the error message and tried different type-related fixes which didn't work (obviously).

This might be related to #54521

@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 28, 2019
@estebank
Copy link
Contributor

Current full output:

error: field expressions cannot have generic arguments
  --> src/lib.rs:11:36
   |
11 |                          .collect::<Result<Vec<_>, io::Error>>?;
   |                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0615]: attempted to take value of method `collect` on type `std::iter::Map<std::fs::ReadDir, [closure@src/lib.rs:10:31: 10:62]>`
  --> src/lib.rs:11:27
   |
11 |                          .collect::<Result<Vec<_>, io::Error>>?;
   |                           ^^^^^^^ method, not a field
   |
help: use parentheses to call the method
   |
11 |                          .collect::<Result<Vec<_>, io::Error>>()?;
   |                                                               ^^

@estebank estebank added P-low Low priority D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. and removed D-confusing Diagnostics: Confusing error or lint that should be reworked. labels Jan 5, 2023
@estebank
Copy link
Contributor

estebank commented Jan 5, 2023

No change, only outstanding work is to emit only one of the two errors along with the existing suggestion on E0615.

@oriongonza
Copy link
Contributor

@rustbot claim

jieyouxu added a commit to jieyouxu/rust that referenced this issue Dec 12, 2024
…mpiler-errors

suppress field expr with generics error message if it's a method

Don't emit "field expressions may not have generic arguments" if it's a method call without `()`

r? estebank
Fixes rust-lang#67680

Is this the best way to go? It's by far the simplest I could come up with.
@bors bors closed this as completed in 1d78422 Dec 12, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Dec 12, 2024
Rollup merge of rust-lang#134154 - dev-ardi:field-expr-generics, r=compiler-errors

suppress field expr with generics error message if it's a method

Don't emit "field expressions may not have generic arguments" if it's a method call without `()`

r? estebank
Fixes rust-lang#67680

Is this the best way to go? It's by far the simplest I could come up with.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants