Files
rust/tests/ui/inference
Stuart Cook 7514258efd Rollup merge of #153925 - estebank:issue-129269, r=jieyouxu
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.
2026-03-16 15:01:36 +11:00
..
2025-07-25 20:38:54 +05:00
2025-07-25 20:38:54 +05:00
2024-03-03 16:30:48 -03:00
2024-02-13 18:08:25 -03:00
2025-02-03 10:39:32 -05:00
2025-09-19 11:30:27 +02:00
2025-08-05 19:34:46 +05:00
2025-08-09 16:27:20 +05:00
2024-02-29 08:46:00 +08:00
2024-02-29 08:46:00 +08:00