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.

if let Some(t) = self.ty
&& ty.has_infer()
{
ty = t;
Copy link
Member

@JohnTitor JohnTitor Mar 13, 2026

Choose a reason for hiding this comment

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

IIUC the logic on ambiguity.rs doesn't check what's the local type at all, right?
I wonder sometimes (inferred) self type isn't same as the local type when it has an outer type unrelated to the trait resolution, e.g. Box<T>, so overwriting seems a bit risky.
What's happen on suggestions for such a case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note that the overwriting is purely local and affecting the diagnostic: we're changing what will be included in the LetBinding ty field below not affecting the type associated to local.hir_id, for that we'd have to go through typeck.adjustments_mut().

Copy link
Member

Choose a reason for hiding this comment

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

I see, thank you for clarifying!

@rust-bors

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
   |            +++++
```
@rustbot
Copy link
Collaborator

rustbot commented Mar 16, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

Copy link
Member

@JohnTitor JohnTitor left a comment

Choose a reason for hiding this comment

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

@rust-bors
Copy link
Contributor

rust-bors bot commented Mar 17, 2026

📌 Commit 42709d4 has been approved by JohnTitor

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 17, 2026
@rust-bors
Copy link
Contributor

rust-bors bot commented Mar 18, 2026

⌛ Testing commit 42709d4 with merge 67c31c2...

Workflow: https://github.com/rust-lang/rust/actions/runs/23227589667

rust-bors bot pushed a commit that referenced this pull request Mar 18, 2026
When single impl can satisfy inference error, suggest type

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.
Zalathar added a commit to Zalathar/rust that referenced this pull request Mar 18, 2026
When single impl can satisfy inference error, suggest type

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 rust-lang#100802.
@Zalathar
Copy link
Member

@bors yield (to enclosing rollup)

@rust-bors
Copy link
Contributor

rust-bors bot commented Mar 18, 2026

Auto build was cancelled. Cancelled workflows:

The next pull request likely to be tested is #154030.

rust-bors bot pushed a commit that referenced this pull request Mar 18, 2026
Rollup of 3 pull requests

Successful merges:

 - #153727 (When single impl can satisfy inference error, suggest type)
 - #153998 (Move query-stack-frame spans into `QueryStackFrame`)
 - #154026 (Remove unused types `UnusedGenericParams` and `FiniteBitSet`)
@rust-bors
Copy link
Contributor

rust-bors bot commented Mar 18, 2026

⌛ Testing commit 42709d4 with merge 3047ff7...

Workflow: https://github.com/rust-lang/rust/actions/runs/23228345838

rust-bors bot pushed a commit that referenced this pull request Mar 18, 2026
When single impl can satisfy inference error, suggest type

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.
@Zalathar
Copy link
Member

Failed in rollup: #154030 (comment)

@bors r-

@rust-bors rust-bors bot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Mar 18, 2026
@rust-bors
Copy link
Contributor

rust-bors bot commented Mar 18, 2026

This pull request was unapproved.

This PR was contained in a rollup (#154030), which was unapproved.

Auto build was cancelled due to unapproval. Cancelled workflows:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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

5 participants