Provide better suggestions for inference errors on .collect()?#153925
Merged
rust-bors[bot] merged 1 commit intorust-lang:mainfrom Mar 16, 2026
Merged
Provide better suggestions for inference errors on .collect()?#153925rust-bors[bot] merged 1 commit intorust-lang:mainfrom
.collect()?#153925rust-bors[bot] merged 1 commit intorust-lang:mainfrom
Conversation
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()?; | ++++++++ ```
Collaborator
|
Some changes occurred in need_type_info.rs cc @lcnr |
Collaborator
|
r? @jieyouxu rustbot has assigned @jieyouxu. Use Why was this reviewer chosen?The reviewer was selected based on:
|
jieyouxu
approved these changes
Mar 16, 2026
Contributor
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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<_>>()?:On inference error in let binding, account for
?:Address #129269.