From 9e7345be1f5201634e421aa0963c56a976f36da9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Thu, 2 Nov 2023 02:20:04 +0000 Subject: [PATCH] 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: 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 { | +++++++ ``` instead of ``` 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 { | +++++++++ ``` Fix #117501. --- compiler/rustc_middle/src/ty/diagnostics.rs | 2 ++ ...restrict-assoc-type-of-generic-bound.fixed | 11 ++++++++++ .../restrict-assoc-type-of-generic-bound.rs | 11 ++++++++++ ...estrict-assoc-type-of-generic-bound.stderr | 20 +++++++++++++++++++ 4 files changed, 44 insertions(+) create mode 100644 tests/ui/trait-bounds/restrict-assoc-type-of-generic-bound.fixed create mode 100644 tests/ui/trait-bounds/restrict-assoc-type-of-generic-bound.rs create mode 100644 tests/ui/trait-bounds/restrict-assoc-type-of-generic-bound.stderr 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, 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: 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: 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`.