Skip to content

Commit cc4e21d

Browse files
authored
Rollup merge of rust-lang#99075 - danobi:dup_type_hint_sugg, r=petrochenkov
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 rust-lang#93506 .
2 parents da85ea2 + f0a99f9 commit cc4e21d

File tree

9 files changed

+13
-25
lines changed

9 files changed

+13
-25
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
@@ -2094,6 +2094,9 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
20942094
// |
20952095
// = note: cannot satisfy `_: Tt`
20962096

2097+
// Clear any more general suggestions in favor of our specific one
2098+
err.clear_suggestions();
2099+
20972100
err.span_suggestion_verbose(
20982101
span.shrink_to_hi(),
20992102
&format!(

src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ LL | #[derive(SessionDiagnostic)]
411411
|
412412
= help: normalized in stderr
413413
note: required by a bound in `DiagnosticBuilder::<'a, G>::set_arg`
414-
--> $COMPILER_DIR/rustc_errors/src/diagnostic_builder.rs:538:19
414+
--> $COMPILER_DIR/rustc_errors/src/diagnostic_builder.rs:539:19
415415
|
416416
LL | arg: impl IntoDiagnosticArg,
417417
| ^^^^^^^^^^^^^^^^^ required by this bound in `DiagnosticBuilder::<'a, G>::set_arg`

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)