Skip to content

Commit d01fd07

Browse files
committed
Fix incorrect trait bound restriction suggestion
Suggest ``` error[E0308]: mismatched types --> $DIR/restrict-assoc-type-of-generic-bound.rs:9:12 | LL | pub fn foo<A: MyTrait, B>(a: A) -> B { | - - expected `B` because of return type | | | expected this type parameter LL | return a.bar(); | ^^^^^^^ expected type parameter `B`, found associated type | = note: expected type parameter `B` found associated type `<A as MyTrait>::T` help: consider further restricting this bound | LL | pub fn foo<A: MyTrait<T = B>, B>(a: A) -> B { | +++++++ ``` instead of ``` error[E0308]: mismatched types --> $DIR/restrict-assoc-type-of-generic-bound.rs:9:12 | LL | pub fn foo<A: MyTrait, B>(a: A) -> B { | - - expected `B` because of return type | | | expected this type parameter LL | return a.bar(); | ^^^^^^^ expected type parameter `B`, found associated type | = note: expected type parameter `B` found associated type `<A as MyTrait>::T` help: consider further restricting this bound | LL | pub fn foo<A: MyTrait + <T = B>, B>(a: A) -> B { | +++++++++ ``` Fix #117501.
1 parent 75b064d commit d01fd07

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

compiler/rustc_middle/src/ty/diagnostics.rs

+2
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,8 @@ pub fn suggest_constraining_type_params<'a>(
274274
span,
275275
if span_to_replace.is_some() {
276276
constraint.clone()
277+
} else if constraint.starts_with("<") {
278+
constraint.to_string()
277279
} else if bound_list_non_empty {
278280
format!(" + {constraint}")
279281
} else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// run-rustfix
2+
pub trait MyTrait {
3+
type T;
4+
5+
fn bar(self) -> Self::T;
6+
}
7+
8+
pub fn foo<A: MyTrait<T = B>, B>(a: A) -> B {
9+
return a.bar(); //~ ERROR mismatched types
10+
}
11+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// run-rustfix
2+
pub trait MyTrait {
3+
type T;
4+
5+
fn bar(self) -> Self::T;
6+
}
7+
8+
pub fn foo<A: MyTrait, B>(a: A) -> B {
9+
return a.bar(); //~ ERROR mismatched types
10+
}
11+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/restrict-assoc-type-of-generic-bound.rs:9:12
3+
|
4+
LL | pub fn foo<A: MyTrait, B>(a: A) -> B {
5+
| - - expected `B` because of return type
6+
| |
7+
| expected this type parameter
8+
LL | return a.bar();
9+
| ^^^^^^^ expected type parameter `B`, found associated type
10+
|
11+
= note: expected type parameter `B`
12+
found associated type `<A as MyTrait>::T`
13+
help: consider further restricting this bound
14+
|
15+
LL | pub fn foo<A: MyTrait<T = B>, B>(a: A) -> B {
16+
| +++++++
17+
18+
error: aborting due to previous error
19+
20+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)