Skip to content

Commit

Permalink
Fix incorrect trait bound restriction suggestion
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
estebank committed Nov 2, 2023
1 parent 75b064d commit 9e7345b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
2 changes: 2 additions & 0 deletions compiler/rustc_middle/src/ty/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ pub fn suggest_constraining_type_params<'a>(
span,
if span_to_replace.is_some() {
constraint.clone()
} else if constraint.starts_with("<") {
constraint.to_string()
} else if bound_list_non_empty {
format!(" + {constraint}")
} else {
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/trait-bounds/restrict-assoc-type-of-generic-bound.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// run-rustfix
pub trait MyTrait {
type T;

fn bar(self) -> Self::T;
}

pub fn foo<A: MyTrait<T = B>, B>(a: A) -> B {
return a.bar(); //~ ERROR mismatched types
}
fn main() {}
11 changes: 11 additions & 0 deletions tests/ui/trait-bounds/restrict-assoc-type-of-generic-bound.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// run-rustfix
pub trait MyTrait {
type T;

fn bar(self) -> Self::T;
}

pub fn foo<A: MyTrait, B>(a: A) -> B {
return a.bar(); //~ ERROR mismatched types
}
fn main() {}
20 changes: 20 additions & 0 deletions tests/ui/trait-bounds/restrict-assoc-type-of-generic-bound.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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 {
| +++++++

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.

0 comments on commit 9e7345b

Please sign in to comment.