From 8763b0498be5afd93d62db61225d61354d94c734 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Fri, 25 Oct 2024 22:54:42 +0000 Subject: [PATCH] Account for negative bounds in E0277 note and suggestion Do not suggest `#[derive(Copy)]` when we wanted a `!Copy` type. Do not say "`Copy` is not implemented for `T` but `Copy` is". Do not talk about `Trait` having no implementations when `!Trait` was desired. --- .../src/error_reporting/traits/fulfillment_errors.rs | 6 +++++- .../src/error_reporting/traits/suggestions.rs | 3 +++ .../traits/negative-bounds/on-unimplemented.stderr | 6 ------ tests/ui/traits/negative-bounds/simple.stderr | 12 ------------ 4 files changed, 8 insertions(+), 19 deletions(-) diff --git a/compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs b/compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs index 9923353db6735..ce33d819b4404 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs @@ -2545,12 +2545,16 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { && self.tcx.trait_impls_of(trait_def_id).is_empty() && !self.tcx.trait_is_auto(trait_def_id) && !self.tcx.trait_is_alias(trait_def_id) + && trait_predicate.polarity() == ty::PredicatePolarity::Positive { err.span_help( self.tcx.def_span(trait_def_id), crate::fluent_generated::trait_selection_trait_has_no_impls, ); - } else if !suggested && !unsatisfied_const { + } else if !suggested + && !unsatisfied_const + && trait_predicate.polarity() == ty::PredicatePolarity::Positive + { // Can't show anything else useful, try to find similar impls. let impl_candidates = self.find_similar_impl_candidates(trait_predicate); if !self.report_similar_impl_candidates( diff --git a/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs b/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs index b18c40f46c812..c768055edec1a 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs @@ -3701,6 +3701,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { err: &mut Diag<'_>, trait_pred: ty::PolyTraitPredicate<'tcx>, ) { + if trait_pred.polarity() == ty::PredicatePolarity::Negative { + return; + } let Some(diagnostic_name) = self.tcx.get_diagnostic_name(trait_pred.def_id()) else { return; }; diff --git a/tests/ui/traits/negative-bounds/on-unimplemented.stderr b/tests/ui/traits/negative-bounds/on-unimplemented.stderr index 07483e788e5b8..ed473d5791737 100644 --- a/tests/ui/traits/negative-bounds/on-unimplemented.stderr +++ b/tests/ui/traits/negative-bounds/on-unimplemented.stderr @@ -6,12 +6,6 @@ LL | fn hello() -> impl !Foo { LL | LL | NotFoo | ------ return type was inferred to be `NotFoo` here - | -help: this trait has no implementations, consider adding one - --> $DIR/on-unimplemented.rs:4:1 - | -LL | trait Foo {} - | ^^^^^^^^^ error: aborting due to 1 previous error diff --git a/tests/ui/traits/negative-bounds/simple.stderr b/tests/ui/traits/negative-bounds/simple.stderr index f8a43e605c477..499c19bb85468 100644 --- a/tests/ui/traits/negative-bounds/simple.stderr +++ b/tests/ui/traits/negative-bounds/simple.stderr @@ -28,18 +28,11 @@ error[E0277]: the trait bound `Copyable: !Copy` is not satisfied LL | not_copy::(); | ^^^^^^^^ the trait bound `Copyable: !Copy` is not satisfied | - = help: the trait `Copy` is not implemented for `Copyable` - but trait `Copy` is implemented for it note: required by a bound in `not_copy` --> $DIR/simple.rs:3:16 | LL | fn not_copy() {} | ^^^^^ required by this bound in `not_copy` -help: consider annotating `Copyable` with `#[derive(Copy)]` - | -LL + #[derive(Copy)] -LL | struct Copyable; - | error[E0277]: the trait bound `NotNecessarilyCopyable: !Copy` is not satisfied --> $DIR/simple.rs:37:16 @@ -52,11 +45,6 @@ note: required by a bound in `not_copy` | LL | fn not_copy() {} | ^^^^^ required by this bound in `not_copy` -help: consider annotating `NotNecessarilyCopyable` with `#[derive(Copy)]` - | -LL + #[derive(Copy)] -LL | struct NotNecessarilyCopyable; - | error: aborting due to 4 previous errors