Skip to content

Commit f63d2bc

Browse files
committed
Better suggestion span for missing type parameter
1 parent 89ecae5 commit f63d2bc

File tree

7 files changed

+43
-12
lines changed

7 files changed

+43
-12
lines changed

compiler/rustc_hir_analysis/src/errors.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -453,12 +453,11 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for MissingTypeParams {
453453
} else {
454454
// The user wrote `Iterator`, so we don't have a type we can suggest, but at
455455
// least we can clue them to the correct syntax `Iterator<Type>`.
456-
err.span_suggestion(
457-
self.span,
456+
err.span_suggestion_verbose(
457+
self.span.shrink_to_hi(),
458458
fluent::hir_analysis_suggestion,
459459
format!(
460-
"{}<{}>",
461-
snippet,
460+
"<{}>",
462461
self.missing_type_params
463462
.iter()
464463
.map(|n| n.to_string())

tests/ui/associated-types/issue-22560.stderr

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,13 @@ LL | trait Add<Rhs=Self> {
3535
| ------------------- type parameter `Rhs` must be specified for this
3636
...
3737
LL | type Test = dyn Add + Sub;
38-
| ^^^ help: set the type parameter to the desired type: `Add<Rhs>`
38+
| ^^^
3939
|
4040
= note: because of the default `Self` reference, type parameters must be specified on object types
41+
help: set the type parameter to the desired type
42+
|
43+
LL | type Test = dyn Add<Rhs> + Sub;
44+
| +++++
4145

4246
error[E0393]: the type parameter `Rhs` must be explicitly specified
4347
--> $DIR/issue-22560.rs:9:23
@@ -46,9 +50,13 @@ LL | trait Sub<Rhs=Self> {
4650
| ------------------- type parameter `Rhs` must be specified for this
4751
...
4852
LL | type Test = dyn Add + Sub;
49-
| ^^^ help: set the type parameter to the desired type: `Sub<Rhs>`
53+
| ^^^
5054
|
5155
= note: because of the default `Self` reference, type parameters must be specified on object types
56+
help: set the type parameter to the desired type
57+
|
58+
LL | type Test = dyn Add + Sub<Rhs>;
59+
| +++++
5260

5361
error: aborting due to 4 previous errors
5462

tests/ui/const-generics/generic_const_exprs/expected-type-of-closure-body-to-be-a-closure-or-coroutine-ice-113776.stderr

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,32 @@ error[E0393]: the type parameter `Rhs` must be explicitly specified
2020
--> $DIR/expected-type-of-closure-body-to-be-a-closure-or-coroutine-ice-113776.rs:16:27
2121
|
2222
LL | ) -> impl Iterator<Item = SubAssign> {
23-
| ^^^^^^^^^ help: set the type parameter to the desired type: `SubAssign<Rhs>`
23+
| ^^^^^^^^^
2424
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL
2525
|
2626
= note: type parameter `Rhs` must be specified for this
2727
|
2828
= note: because of the default `Self` reference, type parameters must be specified on object types
29+
help: set the type parameter to the desired type
30+
|
31+
LL | ) -> impl Iterator<Item = SubAssign<Rhs>> {
32+
| +++++
2933

3034
error[E0393]: the type parameter `Rhs` must be explicitly specified
3135
--> $DIR/expected-type-of-closure-body-to-be-a-closure-or-coroutine-ice-113776.rs:16:27
3236
|
3337
LL | ) -> impl Iterator<Item = SubAssign> {
34-
| ^^^^^^^^^ help: set the type parameter to the desired type: `SubAssign<Rhs>`
38+
| ^^^^^^^^^
3539
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL
3640
|
3741
= note: type parameter `Rhs` must be specified for this
3842
|
3943
= note: because of the default `Self` reference, type parameters must be specified on object types
4044
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
45+
help: set the type parameter to the desired type
46+
|
47+
LL | ) -> impl Iterator<Item = SubAssign<Rhs>> {
48+
| +++++
4149

4250
error[E0277]: `()` is not an iterator
4351
--> $DIR/expected-type-of-closure-body-to-be-a-closure-or-coroutine-ice-113776.rs:16:6

tests/ui/error-codes/E0393.stderr

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ LL | trait A<T=Self> {}
55
| --------------- type parameter `T` must be specified for this
66
LL |
77
LL | fn together_we_will_rule_the_galaxy(son: &dyn A) {}
8-
| ^ help: set the type parameter to the desired type: `A<T>`
8+
| ^
99
|
1010
= note: because of the default `Self` reference, type parameters must be specified on object types
11+
help: set the type parameter to the desired type
12+
|
13+
LL | fn together_we_will_rule_the_galaxy(son: &dyn A<T>) {}
14+
| +++
1115

1216
error: aborting due to 1 previous error
1317

tests/ui/issues/issue-21950.stderr

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@ LL | trait Add<Rhs=Self> {
1414
| ------------------- type parameter `Rhs` must be specified for this
1515
...
1616
LL | let x = &10 as &dyn Add;
17-
| ^^^ help: set the type parameter to the desired type: `Add<Rhs>`
17+
| ^^^
1818
|
1919
= note: because of the default `Self` reference, type parameters must be specified on object types
20+
help: set the type parameter to the desired type
21+
|
22+
LL | let x = &10 as &dyn Add<Rhs>;
23+
| +++++
2024

2125
error: aborting due to 2 previous errors
2226

tests/ui/issues/issue-22370.stderr

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ LL | trait A<T=Self> {}
55
| --------------- type parameter `T` must be specified for this
66
LL |
77
LL | fn f(a: &dyn A) {}
8-
| ^ help: set the type parameter to the desired type: `A<T>`
8+
| ^
99
|
1010
= note: because of the default `Self` reference, type parameters must be specified on object types
11+
help: set the type parameter to the desired type
12+
|
13+
LL | fn f(a: &dyn A<T>) {}
14+
| +++
1115

1216
error: aborting due to 1 previous error
1317

tests/ui/type/type-parameter-defaults-referencing-Self.stderr

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ LL | trait Foo<T=Self> {
55
| ----------------- type parameter `T` must be specified for this
66
...
77
LL | fn foo(x: &dyn Foo) { }
8-
| ^^^ help: set the type parameter to the desired type: `Foo<T>`
8+
| ^^^
99
|
1010
= note: because of the default `Self` reference, type parameters must be specified on object types
11+
help: set the type parameter to the desired type
12+
|
13+
LL | fn foo(x: &dyn Foo<T>) { }
14+
| +++
1115

1216
error: aborting due to 1 previous error
1317

0 commit comments

Comments
 (0)