-
-
Notifications
You must be signed in to change notification settings - Fork 14.7k
When single impl can satisfy inference error, suggest type #153727
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+145
−14
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2251,6 +2251,16 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { | |
| if candidates.is_empty() { | ||
| return false; | ||
| } | ||
| 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; | ||
| } | ||
|
Comment on lines
+2254
to
+2263
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This gives us the instead of |
||
| if let &[(cand, def_id)] = &candidates[..] { | ||
| if self.tcx.is_diagnostic_item(sym::FromResidual, cand.def_id) | ||
| && !self.tcx.features().enabled(sym::try_trait_v2) | ||
|
|
||
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
13 changes: 13 additions & 0 deletions
13
tests/ui/inference/cannot-infer-iterator-sum-return-type.fixed
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| //@ run-rustfix | ||
| fn main() { | ||
| let v = vec![1, 2]; | ||
| let sum: i32 = v //~ ERROR: type annotations needed | ||
| .iter() | ||
| .map(|val| *val) | ||
| .sum(); // `sum::<T>` needs `T` to be specified | ||
| // In this case any integer would fit, but we resolve to `i32` because that's what `{integer}` | ||
| // got coerced to. If the user needs further hinting that they can change the integer type, that | ||
| // can come from other suggestions. (#100802) | ||
| let bool = sum > 0; | ||
| assert_eq!(bool, true); | ||
| } |
13 changes: 13 additions & 0 deletions
13
tests/ui/inference/cannot-infer-iterator-sum-return-type.rs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| //@ run-rustfix | ||
| fn main() { | ||
| let v = vec![1, 2]; | ||
| let sum = v //~ ERROR: type annotations needed | ||
| .iter() | ||
| .map(|val| *val) | ||
| .sum(); // `sum::<T>` needs `T` to be specified | ||
| // In this case any integer would fit, but we resolve to `i32` because that's what `{integer}` | ||
| // got coerced to. If the user needs further hinting that they can change the integer type, that | ||
| // can come from other suggestions. (#100802) | ||
| let bool = sum > 0; | ||
| assert_eq!(bool, true); | ||
| } |
26 changes: 26 additions & 0 deletions
26
tests/ui/inference/cannot-infer-iterator-sum-return-type.stderr
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| 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: the type must implement `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 | ||
| | +++++ | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
||
| For more information about this error, try `rustc --explain E0283`. |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
LetBindingtyfield below not affecting the type associated tolocal.hir_id, for that we'd have to go throughtypeck.adjustments_mut().There was a problem hiding this comment.
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!