From 16e2b9f66201e0d0437028842c3e565424cd5fc6 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Tue, 21 Feb 2023 05:16:35 +0000 Subject: [PATCH] Place binder correctly for arbitrary trait bound suggestion --- compiler/rustc_middle/src/ty/diagnostics.rs | 4 ++-- ...correct-binder-for-arbitrary-bound-sugg.rs | 16 ++++++++++++++ ...ect-binder-for-arbitrary-bound-sugg.stderr | 22 +++++++++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 tests/ui/suggestions/correct-binder-for-arbitrary-bound-sugg.rs create mode 100644 tests/ui/suggestions/correct-binder-for-arbitrary-bound-sugg.stderr diff --git a/compiler/rustc_middle/src/ty/diagnostics.rs b/compiler/rustc_middle/src/ty/diagnostics.rs index 3ca17e7273eb9..ae0bb4949c743 100644 --- a/compiler/rustc_middle/src/ty/diagnostics.rs +++ b/compiler/rustc_middle/src/ty/diagnostics.rs @@ -117,7 +117,7 @@ pub fn suggest_arbitrary_trait_bound<'tcx>( } let param_name = trait_pred.skip_binder().self_ty().to_string(); - let mut constraint = trait_pred.print_modifiers_and_trait_path().to_string(); + let mut constraint = trait_pred.to_string(); if let Some((name, term)) = associated_ty { // FIXME: this case overlaps with code in TyCtxt::note_and_explain_type_err. @@ -144,7 +144,7 @@ pub fn suggest_arbitrary_trait_bound<'tcx>( this requirement", if generics.where_clause_span.is_empty() { "introducing a" } else { "extending the" }, ), - format!("{} {}: {}", generics.add_where_or_trailing_comma(), param_name, constraint), + format!("{} {constraint}", generics.add_where_or_trailing_comma()), Applicability::MaybeIncorrect, ); true diff --git a/tests/ui/suggestions/correct-binder-for-arbitrary-bound-sugg.rs b/tests/ui/suggestions/correct-binder-for-arbitrary-bound-sugg.rs new file mode 100644 index 0000000000000..e56c8622ece03 --- /dev/null +++ b/tests/ui/suggestions/correct-binder-for-arbitrary-bound-sugg.rs @@ -0,0 +1,16 @@ +trait Foo +where + for<'a> &'a Self: Bar, +{ +} + +impl Foo for () {} + +trait Bar {} + +impl Bar for &() {} + +fn foo() {} +//~^ ERROR the trait bound `for<'a> &'a T: Bar` is not satisfied + +fn main() {} diff --git a/tests/ui/suggestions/correct-binder-for-arbitrary-bound-sugg.stderr b/tests/ui/suggestions/correct-binder-for-arbitrary-bound-sugg.stderr new file mode 100644 index 0000000000000..2298e7f4e0c3c --- /dev/null +++ b/tests/ui/suggestions/correct-binder-for-arbitrary-bound-sugg.stderr @@ -0,0 +1,22 @@ +error[E0277]: the trait bound `for<'a> &'a T: Bar` is not satisfied + --> $DIR/correct-binder-for-arbitrary-bound-sugg.rs:13:11 + | +LL | fn foo() {} + | ^^^ the trait `for<'a> Bar` is not implemented for `&'a T` + | +note: required by a bound in `Foo` + --> $DIR/correct-binder-for-arbitrary-bound-sugg.rs:3:23 + | +LL | trait Foo + | --- required by a bound in this trait +LL | where +LL | for<'a> &'a Self: Bar, + | ^^^ required by this bound in `Foo` +help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement + | +LL | fn foo() where for<'a> &'a T: Bar {} + | ++++++++++++++++++++++++ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`.