mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-07 01:05:39 +03:00
7514258efd
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/rust#129269.