When single impl can satisfy inference error, suggest type#153727
When single impl can satisfy inference error, suggest type#153727estebank wants to merge 1 commit intorust-lang:mainfrom
Conversation
|
Some changes occurred in need_type_info.rs cc @lcnr |
|
r? @JohnTitor rustbot has assigned @JohnTitor. Use Why was this reviewer chosen?The reviewer was selected based on:
|
| 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; | ||
| } |
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.
| if let Some(t) = self.ty | ||
| && ty.has_infer() | ||
| { | ||
| ty = t; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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().
There was a problem hiding this comment.
I see, thank you for clarifying!
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 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. |
|
⌛ Testing commit 42709d4 with merge 67c31c2... Workflow: https://github.com/rust-lang/rust/actions/runs/23227589667 |
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.
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.
|
@bors yield (to enclosing rollup) |
|
Auto build was cancelled. Cancelled workflows: The next pull request likely to be tested is #154030. |
|
⌛ Testing commit 42709d4 with merge 3047ff7... Workflow: https://github.com/rust-lang/rust/actions/runs/23228345838 |
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.
|
Failed in rollup: #154030 (comment) @bors r- |
|
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: |
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.