Skip to content

Provide better suggestions for inference errors on .collect()?#153925

Merged
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
estebank:issue-129269
Mar 16, 2026
Merged

Provide better suggestions for inference errors on .collect()?#153925
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
estebank:issue-129269

Conversation

@estebank
Copy link
Contributor

@estebank estebank commented Mar 15, 2026

When encountering an inference error when calling .collect() followed by ?, detect the return type of the enclosing function and suggest .collect::<Result<_, _>>()? instead of .collect::<Vec<_>>()?:

error[E0283]: type annotations needed
  --> $DIR/question-mark-type-infer.rs:10:21
   |
LL |     l.iter().map(f).collect()?
   |                     ^^^^^^^ cannot infer type of the type parameter `B` declared on the method `collect`
   |
   = note: cannot satisfy `_: FromIterator<Result<i32, ()>>`
note: required by a bound in `collect`
  --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
help: consider specifying the generic argument
   |
LL |     l.iter().map(f).collect::<Result<_, _>>()?
   |                            ++++++++++++++++

On inference error in let binding, account for ?:

error[E0282]: type annotations needed
  --> $DIR/question-mark-type-inference-in-chain.rs:31:9
   |
LL |     let mut tags = lines.iter().map(|e| parse(e)).collect()?;
   |         ^^^^^^^^
...
LL |     tags.sort();
   |     ---- type must be known at this point
   |
help: consider giving `tags` an explicit type
   |
LL |     let mut tags: Vec<_> = lines.iter().map(|e| parse(e)).collect()?;
   |                 ++++++++

Address #129269.

When encountering an inference error when calling `.collect()` followed by `?`, detect the return type of the enclosing function and suggest `.collect::<Result<_, _>>()?` instead of `.collect::<Vec<_>>()?`:

```
error[E0283]: type annotations needed
  --> $DIR/question-mark-type-infer.rs:10:21
   |
LL |     l.iter().map(f).collect()?
   |                     ^^^^^^^ cannot infer type of the type parameter `B` declared on the method `collect`
   |
   = note: cannot satisfy `_: FromIterator<Result<i32, ()>>`
note: required by a bound in `collect`
  --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
help: consider specifying the generic argument
   |
LL |     l.iter().map(f).collect::<Result<_, _>>()?
   |                            ++++++++++++++++
```

On inference error in let binding, account for `?`:
```
error[E0282]: type annotations needed
  --> $DIR/question-mark-type-inference-in-chain.rs:31:9
   |
LL |     let mut tags = lines.iter().map(|e| parse(e)).collect()?;
   |         ^^^^^^^^
...
LL |     tags.sort();
   |     ---- type must be known at this point
   |
help: consider giving `tags` an explicit type
   |
LL |     let mut tags: Vec<_> = lines.iter().map(|e| parse(e)).collect()?;
   |                 ++++++++
```
@rustbot
Copy link
Collaborator

rustbot commented Mar 15, 2026

Some changes occurred in need_type_info.rs

cc @lcnr

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 15, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 15, 2026

r? @jieyouxu

rustbot has assigned @jieyouxu.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 69 candidates
  • Random selection from 15 candidates

Copy link
Member

@jieyouxu jieyouxu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, thanks
@bors r+ rollup

View changes since this review

@rust-bors
Copy link
Contributor

rust-bors bot commented Mar 16, 2026

📌 Commit 774f232 has been approved by jieyouxu

It is now in the queue for this repository.

@rust-bors rust-bors bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 16, 2026
Zalathar added a commit to Zalathar/rust that referenced this pull request Mar 16, 2026
Provide better suggestions for inference errors on `.collect()?`

When encountering an inference error when calling `.collect()` followed by `?`, detect the return type of the enclosing function and suggest `.collect::<Result<_, _>>()?` instead of `.collect::<Vec<_>>()?`:

```
error[E0283]: type annotations needed
  --> $DIR/question-mark-type-infer.rs:10:21
   |
LL |     l.iter().map(f).collect()?
   |                     ^^^^^^^ cannot infer type of the type parameter `B` declared on the method `collect`
   |
   = note: cannot satisfy `_: FromIterator<Result<i32, ()>>`
note: required by a bound in `collect`
  --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
help: consider specifying the generic argument
   |
LL |     l.iter().map(f).collect::<Result<_, _>>()?
   |                            ++++++++++++++++
```

On inference error in let binding, account for `?`:
```
error[E0282]: type annotations needed
  --> $DIR/question-mark-type-inference-in-chain.rs:31:9
   |
LL |     let mut tags = lines.iter().map(|e| parse(e)).collect()?;
   |         ^^^^^^^^
...
LL |     tags.sort();
   |     ---- type must be known at this point
   |
help: consider giving `tags` an explicit type
   |
LL |     let mut tags: Vec<_> = lines.iter().map(|e| parse(e)).collect()?;
   |                 ++++++++
```

Address rust-lang#129269.
rust-bors bot pushed a commit that referenced this pull request Mar 16, 2026
Rollup of 7 pull requests

Successful merges:

 - #153639 (Remove `QueryInfo`.)
 - #153570 (MaybeUninit: mention common mistakes in assume_init docs; link to validity invariant docs)
 - #153793 (Add overview documentation for `std::mem`.)
 - #153922 (rustc_mir_build only depends on rustc_lint_defs, not rustc_lint)
 - #153923 (run_make_support: Print the command and output even if it succeeds)
 - #153925 (Provide better suggestions for inference errors on `.collect()?`)
 - #153928 (remove several redundant tests)
rust-bors bot pushed a commit that referenced this pull request Mar 16, 2026
Rollup of 8 pull requests

Successful merges:

 - #153639 (Remove `QueryInfo`.)
 - #153570 (MaybeUninit: mention common mistakes in assume_init docs; link to validity invariant docs)
 - #153724 (Show named lifetime in closure upvar diagnostics)
 - #153793 (Add overview documentation for `std::mem`.)
 - #153922 (rustc_mir_build only depends on rustc_lint_defs, not rustc_lint)
 - #153923 (run_make_support: Print the command and output even if it succeeds)
 - #153925 (Provide better suggestions for inference errors on `.collect()?`)
 - #153928 (remove several redundant tests)
@rust-bors rust-bors bot merged commit 7514258 into rust-lang:main Mar 16, 2026
11 checks passed
@rustbot rustbot added this to the 1.96.0 milestone Mar 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants