When single impl can satisfy inference error, suggest type#153727
Open
estebank wants to merge 1 commit intorust-lang:mainfrom
Open
When single impl can satisfy inference error, suggest type#153727estebank wants to merge 1 commit intorust-lang:mainfrom
estebank wants to merge 1 commit intorust-lang:mainfrom
Conversation
Collaborator
|
Some changes occurred in need_type_info.rs cc @lcnr |
Collaborator
|
r? @JohnTitor rustbot has assigned @JohnTitor. Use Why was this reviewer chosen?The reviewer was selected based on:
|
estebank
commented
Mar 11, 2026
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; | ||
| } |
Contributor
Author
There was a problem hiding this comment.
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
This comment has been minimized.
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 | +++++ ```
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 where a return type must be known, like when calling
Iterator::sum::<T>()without specifyingT, look for allTthat would satisfySum<S>. If only one, suggest it.Fix #100802.