Skip to content

Commit 34e9e6d

Browse files
committed
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 .
1 parent 06754d8 commit 34e9e6d

File tree

8 files changed

+12
-24
lines changed

8 files changed

+12
-24
lines changed

compiler/rustc_errors/src/diagnostic.rs

+8
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,14 @@ impl Diagnostic {
614614
self
615615
}
616616

617+
/// Clear any existing suggestions.
618+
pub fn clear_suggestions(&mut self) -> &mut Self {
619+
if let Ok(suggestions) = &mut self.suggestions {
620+
suggestions.clear();
621+
}
622+
self
623+
}
624+
617625
/// Helper for pushing to `self.suggestions`, if available (not disable).
618626
fn push_suggestion(&mut self, suggestion: CodeSuggestion) {
619627
if let Ok(suggestions) = &mut self.suggestions {

compiler/rustc_errors/src/diagnostic_builder.rs

+1
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
461461
forward!(pub fn set_is_lint(&mut self,) -> &mut Self);
462462

463463
forward!(pub fn disable_suggestions(&mut self,) -> &mut Self);
464+
forward!(pub fn clear_suggestions(&mut self,) -> &mut Self);
464465

465466
forward!(pub fn multipart_suggestion(
466467
&mut self,

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -2074,6 +2074,9 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
20742074
// |
20752075
// = note: cannot satisfy `_: Tt`
20762076

2077+
// Clear any more general suggestions in favor of our specific one
2078+
err.clear_suggestions();
2079+
20772080
err.span_suggestion_verbose(
20782081
span.shrink_to_hi(),
20792082
&format!(

src/test/ui/inference/erase-type-params-in-label.stderr

-8
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ note: required by a bound in `foo`
1010
|
1111
LL | fn foo<T, K, W: Default, Z: Default>(t: T, k: K) -> Foo<T, K, W, Z> {
1212
| ^^^^^^^ required by this bound in `foo`
13-
help: consider giving `foo` an explicit type, where the type for type parameter `W` is specified
14-
|
15-
LL | let foo: Foo<i32, &str, W, Z> = foo(1, "");
16-
| ++++++++++++++++++++++
1713
help: consider specifying the type arguments in the function call
1814
|
1915
LL | let foo = foo::<T, K, W, Z>(1, "");
@@ -31,10 +27,6 @@ note: required by a bound in `bar`
3127
|
3228
LL | fn bar<T, K, Z: Default>(t: T, k: K) -> Bar<T, K, Z> {
3329
| ^^^^^^^ required by this bound in `bar`
34-
help: consider giving `bar` an explicit type, where the type for type parameter `Z` is specified
35-
|
36-
LL | let bar: Bar<i32, &str, Z> = bar(1, "");
37-
| +++++++++++++++++++
3830
help: consider specifying the type arguments in the function call
3931
|
4032
LL | let bar = bar::<T, K, Z>(1, "");

src/test/ui/inference/issue-71732.stderr

-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ note: required by a bound in `HashMap::<K, V, S>::get`
1313
|
1414
LL | K: Borrow<Q>,
1515
| ^^^^^^^^^ required by this bound in `HashMap::<K, V, S>::get`
16-
help: consider specifying the generic argument
17-
|
18-
LL | .get::<Q>(&"key".into())
19-
| +++++
2016
help: consider specifying the type argument in the function call
2117
|
2218
LL | .get::<Q>(&"key".into())

src/test/ui/traits/issue-77982.stderr

-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ note: required by a bound in `HashMap::<K, V, S>::get`
1313
|
1414
LL | K: Borrow<Q>,
1515
| ^^^^^^^^^ required by this bound in `HashMap::<K, V, S>::get`
16-
help: consider specifying the generic argument
17-
|
18-
LL | opts.get::<Q>(opt.as_ref());
19-
| +++++
2016
help: consider specifying the type argument in the function call
2117
|
2218
LL | opts.get::<Q>(opt.as_ref());

src/test/ui/traits/multidispatch-convert-ambig-dest.stderr

-4
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ LL | fn test<T,U>(_: T, _: U)
3030
| ---- required by a bound in this
3131
LL | where T : Convert<U>
3232
| ^^^^^^^^^^ required by this bound in `test`
33-
help: consider specifying the generic arguments
34-
|
35-
LL | test::<i32, U>(22, std::default::Default::default());
36-
| ++++++++++
3733
help: consider specifying the type arguments in the function call
3834
|
3935
LL | test::<T, U>(22, std::default::Default::default());

src/test/ui/type/type-annotation-needed.stderr

-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ note: required by a bound in `foo`
1010
|
1111
LL | fn foo<T: Into<String>>(x: i32) {}
1212
| ^^^^^^^^^^^^ required by this bound in `foo`
13-
help: consider specifying the generic argument
14-
|
15-
LL | foo::<T>(42);
16-
| +++++
1713
help: consider specifying the type argument in the function call
1814
|
1915
LL | foo::<T>(42);

0 commit comments

Comments
 (0)