From 0815067796a653ac9dbf95ff651d74074a4b3788 Mon Sep 17 00:00:00 2001 From: Gurinder Singh Date: Sat, 10 Feb 2024 16:05:39 +0530 Subject: [PATCH] Take empty `where` into account when suggesting predicates --- compiler/rustc_middle/src/ty/diagnostics.rs | 8 +++++++- tests/ui/trait-impl-bound-suggestions.fixed | 9 +++++++++ tests/ui/trait-impl-bound-suggestions.rs | 9 +++++++++ tests/ui/trait-impl-bound-suggestions.stderr | 18 +++++++++++++++++- 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_middle/src/ty/diagnostics.rs b/compiler/rustc_middle/src/ty/diagnostics.rs index 13cc5cbed443f..7cb326ce696ab 100644 --- a/compiler/rustc_middle/src/ty/diagnostics.rs +++ b/compiler/rustc_middle/src/ty/diagnostics.rs @@ -358,11 +358,17 @@ pub fn suggest_constraining_type_params<'a>( // trait Foo {... } // - insert: `where T: Zar` if matches!(param.kind, hir::GenericParamKind::Type { default: Some(_), .. }) { + // If we are here and the where clause span is of non-zero length + // it means we're dealing with an empty where clause like this: + // fn foo(x: X) where { ... } + // In that case we don't want to add another "where" (Fixes #120838) + let where_prefix = if generics.where_clause_span.is_empty() { " where" } else { "" }; + // Suggest a bound, but there is no existing `where` clause *and* the type param has a // default (``), so we suggest adding `where T: Bar`. suggestions.push(( generics.tail_span_for_predicate_suggestion(), - format!(" where {param_name}: {constraint}"), + format!("{where_prefix} {param_name}: {constraint}"), SuggestChangingConstraintsMessage::RestrictTypeFurther { ty: param_name }, )); continue; diff --git a/tests/ui/trait-impl-bound-suggestions.fixed b/tests/ui/trait-impl-bound-suggestions.fixed index 744e7bef04e97..fb11286a175e5 100644 --- a/tests/ui/trait-impl-bound-suggestions.fixed +++ b/tests/ui/trait-impl-bound-suggestions.fixed @@ -17,4 +17,13 @@ trait InsufficientlyConstrainedGeneric where X: std::marker::Copy { } } +// Regression test for #120838 +#[allow(dead_code)] +trait InsufficientlyConstrainedGenericWithEmptyWhere where X: std::marker::Copy { + fn return_the_constrained_type(&self, x: X) -> ConstrainedStruct { + //~^ ERROR the trait bound `X: Copy` is not satisfied + ConstrainedStruct { x } + } +} + pub fn main() { } diff --git a/tests/ui/trait-impl-bound-suggestions.rs b/tests/ui/trait-impl-bound-suggestions.rs index bf75175179efd..46130a5e76671 100644 --- a/tests/ui/trait-impl-bound-suggestions.rs +++ b/tests/ui/trait-impl-bound-suggestions.rs @@ -17,4 +17,13 @@ trait InsufficientlyConstrainedGeneric { } } +// Regression test for #120838 +#[allow(dead_code)] +trait InsufficientlyConstrainedGenericWithEmptyWhere where { + fn return_the_constrained_type(&self, x: X) -> ConstrainedStruct { + //~^ ERROR the trait bound `X: Copy` is not satisfied + ConstrainedStruct { x } + } +} + pub fn main() { } diff --git a/tests/ui/trait-impl-bound-suggestions.stderr b/tests/ui/trait-impl-bound-suggestions.stderr index c1f31e2b32e27..9883c5bda017f 100644 --- a/tests/ui/trait-impl-bound-suggestions.stderr +++ b/tests/ui/trait-impl-bound-suggestions.stderr @@ -14,6 +14,22 @@ help: consider further restricting type parameter `X` LL | trait InsufficientlyConstrainedGeneric where X: std::marker::Copy { | ++++++++++++++++++++++++++ -error: aborting due to 1 previous error +error[E0277]: the trait bound `X: Copy` is not satisfied + --> $DIR/trait-impl-bound-suggestions.rs:23:52 + | +LL | fn return_the_constrained_type(&self, x: X) -> ConstrainedStruct { + | ^^^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `X` + | +note: required by a bound in `ConstrainedStruct` + --> $DIR/trait-impl-bound-suggestions.rs:8:29 + | +LL | struct ConstrainedStruct { + | ^^^^ required by this bound in `ConstrainedStruct` +help: consider further restricting type parameter `X` + | +LL | trait InsufficientlyConstrainedGenericWithEmptyWhere where X: std::marker::Copy { + | ++++++++++++++++++++ + +error: aborting due to 2 previous errors For more information about this error, try `rustc --explain E0277`.