Skip to content

When single impl can satisfy inference error, suggest type#153727

Open
estebank wants to merge 1 commit intorust-lang:mainfrom
estebank:issue-100802
Open

When single impl can satisfy inference error, suggest type#153727
estebank wants to merge 1 commit intorust-lang:mainfrom
estebank:issue-100802

Conversation

@estebank
Copy link
Contributor

@estebank estebank commented Mar 11, 2026

When encountering an inference error where a return type must be known, like when calling Iterator::sum::<T>() without specifying T, look for all T that would satisfy Sum<S>. If only one, suggest it.

error[E0283]: type annotations needed
  --> $DIR/cannot-infer-iterator-sum-return-type.rs:4:9
   |
LL |     let sum = v
   |         ^^^
...
LL |         .sum(); // `sum::<T>` needs `T` to be specified
   |          --- type must be known at this point
   |
   = note: cannot satisfy `_: Sum<i32>`
help: the trait `Sum` is implemented for `i32`
  --> $SRC_DIR/core/src/iter/traits/accum.rs:LL:COL
  ::: $SRC_DIR/core/src/iter/traits/accum.rs:LL:COL
   |
   = note: in this macro invocation
note: required by a bound in `std::iter::Iterator::sum`
  --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
   = note: this error originates in the macro `integer_sum_product` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider giving `sum` an explicit type, where the type for type parameter `S` is specified
   |
LL |     let sum: i32 = v
   |            +++++

Fix #100802.

@rustbot
Copy link
Collaborator

rustbot commented Mar 11, 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 11, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 11, 2026

r? @JohnTitor

rustbot has assigned @JohnTitor.
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, types
  • compiler, types expanded to 69 candidates
  • Random selection from 16 candidates

Comment on lines +2259 to +2268
let mut specific_candidates = candidates.clone();
specific_candidates.retain(|(tr, _)| {
tr.with_replaced_self_ty(self.tcx, trait_pred.skip_binder().self_ty())
== trait_pred.skip_binder().trait_ref
});
if !specific_candidates.is_empty() {
// We have found a subset of impls that fully satisfy the expected trait, only
// mention those types.
candidates = specific_candidates;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This gives us the

help: the trait `Sum` is implemented for `i32`
    --> library/core/src/iter/traits/accum.rs:48:9
     |
  48 |         impl Sum for $a {
     |         ^^^^^^^^^^^^^^^
...
 204 | integer_sum_product! { i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize }
     | ---------------------------------------------------------------------------- in this macro invocation

instead of

     = help: the following types implement trait `Sum<A>`:
               `Duration` implements `Sum<&'a Duration>`
               `Duration` implements `Sum`
               `Option<T>` implements `Sum<Option<U>>`
               `Result<T, E>` implements `Sum<Result<U, E>>`
               `Saturating<u128>` implements `Sum<&'a Saturating<u128>>`
               `Saturating<u128>` implements `Sum`
               `Saturating<u16>` implements `Sum<&'a Saturating<u16>>`
               `Saturating<u16>` implements `Sum`
             and 88 others

@rust-log-analyzer

This comment has been minimized.

When encountering an inference error where a return type must be known, like when calling `Iterator::sum::<T>()` without specifying `T`, look for all `T` that would satisfy `Sum<S>`. If only one, suggest it.

```
error[E0283]: type annotations needed
  --> $DIR/cannot-infer-iterator-sum-return-type.rs:4:9
   |
LL |     let sum = v
   |         ^^^
...
LL |         .sum(); // `sum::<T>` needs `T` to be specified
   |          --- type must be known at this point
   |
   = note: cannot satisfy `_: Sum<i32>`
help: the trait `Sum` is implemented for `i32`
  --> $SRC_DIR/core/src/iter/traits/accum.rs:LL:COL
  ::: $SRC_DIR/core/src/iter/traits/accum.rs:LL:COL
   |
   = note: in this macro invocation
note: required by a bound in `std::iter::Iterator::sum`
  --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
   = note: this error originates in the macro `integer_sum_product` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider giving `sum` an explicit type, where the type for type parameter `S` is specified
   |
LL |     let sum: i32 = v
   |            +++++
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Iterator::sum requires type annotations in seemingly simple cases

4 participants