Fix duplicated type annotation suggestion

Before, there was more or less duplicated suggestions to add type hints.
Fix by clearing more generic suggestions when a more specific suggestion
is possible.

This fixes #93506 .
This commit is contained in:
Daniel Xu 2022-07-08 21:03:03 -05:00
parent 06754d8852
commit 34e9e6dff1
8 changed files with 12 additions and 24 deletions

View File

@ -614,6 +614,14 @@ pub fn disable_suggestions(&mut self) -> &mut Self {
self self
} }
/// Clear any existing suggestions.
pub fn clear_suggestions(&mut self) -> &mut Self {
if let Ok(suggestions) = &mut self.suggestions {
suggestions.clear();
}
self
}
/// Helper for pushing to `self.suggestions`, if available (not disable). /// Helper for pushing to `self.suggestions`, if available (not disable).
fn push_suggestion(&mut self, suggestion: CodeSuggestion) { fn push_suggestion(&mut self, suggestion: CodeSuggestion) {
if let Ok(suggestions) = &mut self.suggestions { if let Ok(suggestions) = &mut self.suggestions {

View File

@ -461,6 +461,7 @@ pub fn span_labels(
forward!(pub fn set_is_lint(&mut self,) -> &mut Self); forward!(pub fn set_is_lint(&mut self,) -> &mut Self);
forward!(pub fn disable_suggestions(&mut self,) -> &mut Self); forward!(pub fn disable_suggestions(&mut self,) -> &mut Self);
forward!(pub fn clear_suggestions(&mut self,) -> &mut Self);
forward!(pub fn multipart_suggestion( forward!(pub fn multipart_suggestion(
&mut self, &mut self,

View File

@ -2074,6 +2074,9 @@ fn maybe_report_ambiguity(
// | // |
// = note: cannot satisfy `_: Tt` // = note: cannot satisfy `_: Tt`
// Clear any more general suggestions in favor of our specific one
err.clear_suggestions();
err.span_suggestion_verbose( err.span_suggestion_verbose(
span.shrink_to_hi(), span.shrink_to_hi(),
&format!( &format!(

View File

@ -10,10 +10,6 @@ note: required by a bound in `foo`
| |
LL | fn foo<T, K, W: Default, Z: Default>(t: T, k: K) -> Foo<T, K, W, Z> { LL | fn foo<T, K, W: Default, Z: Default>(t: T, k: K) -> Foo<T, K, W, Z> {
| ^^^^^^^ required by this bound in `foo` | ^^^^^^^ required by this bound in `foo`
help: consider giving `foo` an explicit type, where the type for type parameter `W` is specified
|
LL | let foo: Foo<i32, &str, W, Z> = foo(1, "");
| ++++++++++++++++++++++
help: consider specifying the type arguments in the function call help: consider specifying the type arguments in the function call
| |
LL | let foo = foo::<T, K, W, Z>(1, ""); LL | let foo = foo::<T, K, W, Z>(1, "");
@ -31,10 +27,6 @@ note: required by a bound in `bar`
| |
LL | fn bar<T, K, Z: Default>(t: T, k: K) -> Bar<T, K, Z> { LL | fn bar<T, K, Z: Default>(t: T, k: K) -> Bar<T, K, Z> {
| ^^^^^^^ required by this bound in `bar` | ^^^^^^^ required by this bound in `bar`
help: consider giving `bar` an explicit type, where the type for type parameter `Z` is specified
|
LL | let bar: Bar<i32, &str, Z> = bar(1, "");
| +++++++++++++++++++
help: consider specifying the type arguments in the function call help: consider specifying the type arguments in the function call
| |
LL | let bar = bar::<T, K, Z>(1, ""); LL | let bar = bar::<T, K, Z>(1, "");

View File

@ -13,10 +13,6 @@ note: required by a bound in `HashMap::<K, V, S>::get`
| |
LL | K: Borrow<Q>, LL | K: Borrow<Q>,
| ^^^^^^^^^ required by this bound in `HashMap::<K, V, S>::get` | ^^^^^^^^^ required by this bound in `HashMap::<K, V, S>::get`
help: consider specifying the generic argument
|
LL | .get::<Q>(&"key".into())
| +++++
help: consider specifying the type argument in the function call help: consider specifying the type argument in the function call
| |
LL | .get::<Q>(&"key".into()) LL | .get::<Q>(&"key".into())

View File

@ -13,10 +13,6 @@ note: required by a bound in `HashMap::<K, V, S>::get`
| |
LL | K: Borrow<Q>, LL | K: Borrow<Q>,
| ^^^^^^^^^ required by this bound in `HashMap::<K, V, S>::get` | ^^^^^^^^^ required by this bound in `HashMap::<K, V, S>::get`
help: consider specifying the generic argument
|
LL | opts.get::<Q>(opt.as_ref());
| +++++
help: consider specifying the type argument in the function call help: consider specifying the type argument in the function call
| |
LL | opts.get::<Q>(opt.as_ref()); LL | opts.get::<Q>(opt.as_ref());

View File

@ -30,10 +30,6 @@ LL | fn test<T,U>(_: T, _: U)
| ---- required by a bound in this | ---- required by a bound in this
LL | where T : Convert<U> LL | where T : Convert<U>
| ^^^^^^^^^^ required by this bound in `test` | ^^^^^^^^^^ required by this bound in `test`
help: consider specifying the generic arguments
|
LL | test::<i32, U>(22, std::default::Default::default());
| ++++++++++
help: consider specifying the type arguments in the function call help: consider specifying the type arguments in the function call
| |
LL | test::<T, U>(22, std::default::Default::default()); LL | test::<T, U>(22, std::default::Default::default());

View File

@ -10,10 +10,6 @@ note: required by a bound in `foo`
| |
LL | fn foo<T: Into<String>>(x: i32) {} LL | fn foo<T: Into<String>>(x: i32) {}
| ^^^^^^^^^^^^ required by this bound in `foo` | ^^^^^^^^^^^^ required by this bound in `foo`
help: consider specifying the generic argument
|
LL | foo::<T>(42);
| +++++
help: consider specifying the type argument in the function call help: consider specifying the type argument in the function call
| |
LL | foo::<T>(42); LL | foo::<T>(42);