diff --git a/compiler/rustc_middle/src/ty/diagnostics.rs b/compiler/rustc_middle/src/ty/diagnostics.rs index 7a782b2c24985..77a50fa9276db 100644 --- a/compiler/rustc_middle/src/ty/diagnostics.rs +++ b/compiler/rustc_middle/src/ty/diagnostics.rs @@ -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 { diff --git a/tests/ui/trait-bounds/restrict-assoc-type-of-generic-bound.fixed b/tests/ui/trait-bounds/restrict-assoc-type-of-generic-bound.fixed new file mode 100644 index 0000000000000..b3f5ad52db593 --- /dev/null +++ b/tests/ui/trait-bounds/restrict-assoc-type-of-generic-bound.fixed @@ -0,0 +1,11 @@ +// run-rustfix +pub trait MyTrait { + type T; + + fn bar(self) -> Self::T; +} + +pub fn foo, B>(a: A) -> B { + return a.bar(); //~ ERROR mismatched types +} +fn main() {} diff --git a/tests/ui/trait-bounds/restrict-assoc-type-of-generic-bound.rs b/tests/ui/trait-bounds/restrict-assoc-type-of-generic-bound.rs new file mode 100644 index 0000000000000..213abda778292 --- /dev/null +++ b/tests/ui/trait-bounds/restrict-assoc-type-of-generic-bound.rs @@ -0,0 +1,11 @@ +// run-rustfix +pub trait MyTrait { + type T; + + fn bar(self) -> Self::T; +} + +pub fn foo(a: A) -> B { + return a.bar(); //~ ERROR mismatched types +} +fn main() {} diff --git a/tests/ui/trait-bounds/restrict-assoc-type-of-generic-bound.stderr b/tests/ui/trait-bounds/restrict-assoc-type-of-generic-bound.stderr new file mode 100644 index 0000000000000..61132efc414d8 --- /dev/null +++ b/tests/ui/trait-bounds/restrict-assoc-type-of-generic-bound.stderr @@ -0,0 +1,20 @@ +error[E0308]: mismatched types + --> $DIR/restrict-assoc-type-of-generic-bound.rs:9:12 + | +LL | pub fn foo(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 `::T` +help: consider further restricting this bound + | +LL | pub fn foo, B>(a: A) -> B { + | +++++++ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0308`.