From f63d2bc657f2158bc37c747bb0835a9d02b864d1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Esteban=20K=C3=BCber?= <esteban@kuber.com.ar>
Date: Wed, 3 Jul 2024 21:02:27 +0000
Subject: [PATCH] Better suggestion span for missing type parameter

---
 compiler/rustc_hir_analysis/src/errors.rs            |  7 +++----
 tests/ui/associated-types/issue-22560.stderr         | 12 ++++++++++--
 ...dy-to-be-a-closure-or-coroutine-ice-113776.stderr | 12 ++++++++++--
 tests/ui/error-codes/E0393.stderr                    |  6 +++++-
 tests/ui/issues/issue-21950.stderr                   |  6 +++++-
 tests/ui/issues/issue-22370.stderr                   |  6 +++++-
 .../type-parameter-defaults-referencing-Self.stderr  |  6 +++++-
 7 files changed, 43 insertions(+), 12 deletions(-)

diff --git a/compiler/rustc_hir_analysis/src/errors.rs b/compiler/rustc_hir_analysis/src/errors.rs
index 3ffb51fa992..c2d86efd517 100644
--- a/compiler/rustc_hir_analysis/src/errors.rs
+++ b/compiler/rustc_hir_analysis/src/errors.rs
@@ -453,12 +453,11 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for MissingTypeParams {
             } else {
                 // The user wrote `Iterator`, so we don't have a type we can suggest, but at
                 // least we can clue them to the correct syntax `Iterator<Type>`.
-                err.span_suggestion(
-                    self.span,
+                err.span_suggestion_verbose(
+                    self.span.shrink_to_hi(),
                     fluent::hir_analysis_suggestion,
                     format!(
-                        "{}<{}>",
-                        snippet,
+                        "<{}>",
                         self.missing_type_params
                             .iter()
                             .map(|n| n.to_string())
diff --git a/tests/ui/associated-types/issue-22560.stderr b/tests/ui/associated-types/issue-22560.stderr
index 46e6e3951a5..834040490f9 100644
--- a/tests/ui/associated-types/issue-22560.stderr
+++ b/tests/ui/associated-types/issue-22560.stderr
@@ -35,9 +35,13 @@ LL | trait Add<Rhs=Self> {
    | ------------------- type parameter `Rhs` must be specified for this
 ...
 LL | type Test = dyn Add + Sub;
-   |                 ^^^ help: set the type parameter to the desired type: `Add<Rhs>`
+   |                 ^^^
    |
    = note: because of the default `Self` reference, type parameters must be specified on object types
+help: set the type parameter to the desired type
+   |
+LL | type Test = dyn Add<Rhs> + Sub;
+   |                    +++++
 
 error[E0393]: the type parameter `Rhs` must be explicitly specified
   --> $DIR/issue-22560.rs:9:23
@@ -46,9 +50,13 @@ LL | trait Sub<Rhs=Self> {
    | ------------------- type parameter `Rhs` must be specified for this
 ...
 LL | type Test = dyn Add + Sub;
-   |                       ^^^ help: set the type parameter to the desired type: `Sub<Rhs>`
+   |                       ^^^
    |
    = note: because of the default `Self` reference, type parameters must be specified on object types
+help: set the type parameter to the desired type
+   |
+LL | type Test = dyn Add + Sub<Rhs>;
+   |                          +++++
 
 error: aborting due to 4 previous errors
 
diff --git a/tests/ui/const-generics/generic_const_exprs/expected-type-of-closure-body-to-be-a-closure-or-coroutine-ice-113776.stderr b/tests/ui/const-generics/generic_const_exprs/expected-type-of-closure-body-to-be-a-closure-or-coroutine-ice-113776.stderr
index be79450a3ce..416a9381124 100644
--- a/tests/ui/const-generics/generic_const_exprs/expected-type-of-closure-body-to-be-a-closure-or-coroutine-ice-113776.stderr
+++ b/tests/ui/const-generics/generic_const_exprs/expected-type-of-closure-body-to-be-a-closure-or-coroutine-ice-113776.stderr
@@ -20,24 +20,32 @@ error[E0393]: the type parameter `Rhs` must be explicitly specified
   --> $DIR/expected-type-of-closure-body-to-be-a-closure-or-coroutine-ice-113776.rs:16:27
    |
 LL | ) -> impl Iterator<Item = SubAssign> {
-   |                           ^^^^^^^^^ help: set the type parameter to the desired type: `SubAssign<Rhs>`
+   |                           ^^^^^^^^^
   --> $SRC_DIR/core/src/ops/arith.rs:LL:COL
    |
    = note: type parameter `Rhs` must be specified for this
    |
    = note: because of the default `Self` reference, type parameters must be specified on object types
+help: set the type parameter to the desired type
+   |
+LL | ) -> impl Iterator<Item = SubAssign<Rhs>> {
+   |                                    +++++
 
 error[E0393]: the type parameter `Rhs` must be explicitly specified
   --> $DIR/expected-type-of-closure-body-to-be-a-closure-or-coroutine-ice-113776.rs:16:27
    |
 LL | ) -> impl Iterator<Item = SubAssign> {
-   |                           ^^^^^^^^^ help: set the type parameter to the desired type: `SubAssign<Rhs>`
+   |                           ^^^^^^^^^
   --> $SRC_DIR/core/src/ops/arith.rs:LL:COL
    |
    = note: type parameter `Rhs` must be specified for this
    |
    = note: because of the default `Self` reference, type parameters must be specified on object types
    = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
+help: set the type parameter to the desired type
+   |
+LL | ) -> impl Iterator<Item = SubAssign<Rhs>> {
+   |                                    +++++
 
 error[E0277]: `()` is not an iterator
   --> $DIR/expected-type-of-closure-body-to-be-a-closure-or-coroutine-ice-113776.rs:16:6
diff --git a/tests/ui/error-codes/E0393.stderr b/tests/ui/error-codes/E0393.stderr
index 4083fa23e87..489398b7be5 100644
--- a/tests/ui/error-codes/E0393.stderr
+++ b/tests/ui/error-codes/E0393.stderr
@@ -5,9 +5,13 @@ LL | trait A<T=Self> {}
    | --------------- type parameter `T` must be specified for this
 LL |
 LL | fn together_we_will_rule_the_galaxy(son: &dyn A) {}
-   |                                               ^ help: set the type parameter to the desired type: `A<T>`
+   |                                               ^
    |
    = note: because of the default `Self` reference, type parameters must be specified on object types
+help: set the type parameter to the desired type
+   |
+LL | fn together_we_will_rule_the_galaxy(son: &dyn A<T>) {}
+   |                                                +++
 
 error: aborting due to 1 previous error
 
diff --git a/tests/ui/issues/issue-21950.stderr b/tests/ui/issues/issue-21950.stderr
index e498565d4e6..584370c7178 100644
--- a/tests/ui/issues/issue-21950.stderr
+++ b/tests/ui/issues/issue-21950.stderr
@@ -14,9 +14,13 @@ LL | trait Add<Rhs=Self> {
    | ------------------- type parameter `Rhs` must be specified for this
 ...
 LL |     let x = &10 as &dyn Add;
-   |                         ^^^ help: set the type parameter to the desired type: `Add<Rhs>`
+   |                         ^^^
    |
    = note: because of the default `Self` reference, type parameters must be specified on object types
+help: set the type parameter to the desired type
+   |
+LL |     let x = &10 as &dyn Add<Rhs>;
+   |                            +++++
 
 error: aborting due to 2 previous errors
 
diff --git a/tests/ui/issues/issue-22370.stderr b/tests/ui/issues/issue-22370.stderr
index 977cfe06bb8..3dc060963f9 100644
--- a/tests/ui/issues/issue-22370.stderr
+++ b/tests/ui/issues/issue-22370.stderr
@@ -5,9 +5,13 @@ LL | trait A<T=Self> {}
    | --------------- type parameter `T` must be specified for this
 LL |
 LL | fn f(a: &dyn A) {}
-   |              ^ help: set the type parameter to the desired type: `A<T>`
+   |              ^
    |
    = note: because of the default `Self` reference, type parameters must be specified on object types
+help: set the type parameter to the desired type
+   |
+LL | fn f(a: &dyn A<T>) {}
+   |               +++
 
 error: aborting due to 1 previous error
 
diff --git a/tests/ui/type/type-parameter-defaults-referencing-Self.stderr b/tests/ui/type/type-parameter-defaults-referencing-Self.stderr
index 16d08b26722..c81405f03f8 100644
--- a/tests/ui/type/type-parameter-defaults-referencing-Self.stderr
+++ b/tests/ui/type/type-parameter-defaults-referencing-Self.stderr
@@ -5,9 +5,13 @@ LL | trait Foo<T=Self> {
    | ----------------- type parameter `T` must be specified for this
 ...
 LL | fn foo(x: &dyn Foo) { }
-   |                ^^^ help: set the type parameter to the desired type: `Foo<T>`
+   |                ^^^
    |
    = note: because of the default `Self` reference, type parameters must be specified on object types
+help: set the type parameter to the desired type
+   |
+LL | fn foo(x: &dyn Foo<T>) { }
+   |                   +++
 
 error: aborting due to 1 previous error