Skip to content

Commit be9d961

Browse files
committed
More accurate span for type parameter suggestion
After: ``` error[E0229]: associated item constraints are not allowed here --> $DIR/impl-block-params-declared-in-wrong-spot-issue-113073.rs:3:10 | LL | impl Foo<T: Default> for String {} | ^^^^^^^^^^ associated item constraint not allowed here | help: declare the type parameter right after the `impl` keyword | LL - impl Foo<T: Default> for String {} LL + impl<T: Default> Foo<T> for String {} | ``` Before: ``` error[E0229]: associated item constraints are not allowed here --> $DIR/impl-block-params-declared-in-wrong-spot-issue-113073.rs:3:10 | LL | impl Foo<T: Default> for String {} | ^^^^^^^^^^ associated item constraint not allowed here | help: declare the type parameter right after the `impl` keyword | LL | impl<T: Default> Foo<T> for String {} | ++++++++++++ ~ ```
1 parent 032be6f commit be9d961

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1340,11 +1340,13 @@ pub fn prohibit_assoc_item_constraint(
13401340
format!("<{lifetimes}{type_with_constraints}>"),
13411341
)
13421342
};
1343-
let suggestions =
1344-
vec![param_decl, (constraint.span, format!("{}", matching_param.name))];
1343+
let suggestions = vec![
1344+
param_decl,
1345+
(constraint.span.with_lo(constraint.ident.span.hi()), String::new()),
1346+
];
13451347

13461348
err.multipart_suggestion_verbose(
1347-
format!("declare the type parameter right after the `impl` keyword"),
1349+
"declare the type parameter right after the `impl` keyword",
13481350
suggestions,
13491351
Applicability::MaybeIncorrect,
13501352
);

tests/ui/generics/impl-block-params-declared-in-wrong-spot-issue-113073.stderr

+18-12
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ LL | impl Foo<T: Default> for String {}
1414
|
1515
help: declare the type parameter right after the `impl` keyword
1616
|
17-
LL | impl<T: Default> Foo<T> for String {}
18-
| ++++++++++++ ~
17+
LL - impl Foo<T: Default> for String {}
18+
LL + impl<T: Default> Foo<T> for String {}
19+
|
1920

2021
error[E0229]: associated item constraints are not allowed here
2122
--> $DIR/impl-block-params-declared-in-wrong-spot-issue-113073.rs:7:10
@@ -25,8 +26,9 @@ LL | impl Foo<T: 'a + Default> for u8 {}
2526
|
2627
help: declare the type parameter right after the `impl` keyword
2728
|
28-
LL | impl<'a, T: 'a + Default> Foo<T> for u8 {}
29-
| +++++++++++++++++++++ ~
29+
LL - impl Foo<T: 'a + Default> for u8 {}
30+
LL + impl<'a, T: 'a + Default> Foo<T> for u8 {}
31+
|
3032

3133
error[E0229]: associated item constraints are not allowed here
3234
--> $DIR/impl-block-params-declared-in-wrong-spot-issue-113073.rs:13:13
@@ -36,8 +38,9 @@ LL | impl<T> Foo<T: Default> for u16 {}
3638
|
3739
help: declare the type parameter right after the `impl` keyword
3840
|
39-
LL | impl<T, T: Default> Foo<T> for u16 {}
40-
| ++++++++++++ ~
41+
LL - impl<T> Foo<T: Default> for u16 {}
42+
LL + impl<T, T: Default> Foo<T> for u16 {}
43+
|
4144

4245
error[E0229]: associated item constraints are not allowed here
4346
--> $DIR/impl-block-params-declared-in-wrong-spot-issue-113073.rs:17:14
@@ -47,8 +50,9 @@ LL | impl<'a> Foo<T: 'a + Default> for u32 {}
4750
|
4851
help: declare the type parameter right after the `impl` keyword
4952
|
50-
LL | impl<'a, 'a, T: 'a + Default> Foo<T> for u32 {}
51-
| +++++++++++++++++++++ ~
53+
LL - impl<'a> Foo<T: 'a + Default> for u32 {}
54+
LL + impl<'a, 'a, T: 'a + Default> Foo<T> for u32 {}
55+
|
5256

5357
error[E0229]: associated item constraints are not allowed here
5458
--> $DIR/impl-block-params-declared-in-wrong-spot-issue-113073.rs:23:10
@@ -58,8 +62,9 @@ LL | impl Bar<T: Default, K: Default> for String {}
5862
|
5963
help: declare the type parameter right after the `impl` keyword
6064
|
61-
LL | impl<T: Default> Bar<T, K: Default> for String {}
62-
| ++++++++++++ ~
65+
LL - impl Bar<T: Default, K: Default> for String {}
66+
LL + impl<T: Default> Bar<T, K: Default> for String {}
67+
|
6368

6469
error[E0107]: trait takes 2 generic arguments but 1 generic argument was supplied
6570
--> $DIR/impl-block-params-declared-in-wrong-spot-issue-113073.rs:27:9
@@ -87,8 +92,9 @@ LL | impl<T> Bar<T, K: Default> for u8 {}
8792
|
8893
help: declare the type parameter right after the `impl` keyword
8994
|
90-
LL | impl<T, K: Default> Bar<T, K> for u8 {}
91-
| ++++++++++++ ~
95+
LL - impl<T> Bar<T, K: Default> for u8 {}
96+
LL + impl<T, K: Default> Bar<T, K> for u8 {}
97+
|
9298

9399
error: aborting due to 8 previous errors
94100

0 commit comments

Comments
 (0)