Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect trait bound restriction suggestion #117505

Merged
merged 1 commit into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`.
Loading