about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--compiler/rustc_middle/src/ty/diagnostics.rs2
-rw-r--r--tests/ui/trait-bounds/restrict-assoc-type-of-generic-bound.fixed11
-rw-r--r--tests/ui/trait-bounds/restrict-assoc-type-of-generic-bound.rs11
-rw-r--r--tests/ui/trait-bounds/restrict-assoc-type-of-generic-bound.stderr20
4 files changed, 44 insertions, 0 deletions
diff --git a/compiler/rustc_middle/src/ty/diagnostics.rs b/compiler/rustc_middle/src/ty/diagnostics.rs
index 7a782b2c249..77a50fa9276 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 00000000000..b3f5ad52db5
--- /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<A: MyTrait<T = B>, 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 00000000000..213abda7782
--- /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: MyTrait, 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.stderr b/tests/ui/trait-bounds/restrict-assoc-type-of-generic-bound.stderr
new file mode 100644
index 00000000000..61132efc414
--- /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: 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`.