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.
This commit is contained in:
Esteban Küber 2024-10-25 22:54:42 +00:00
parent 1a0c502183
commit 143b072c62
4 changed files with 8 additions and 19 deletions

View File

@ -2540,12 +2540,16 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
&& self.tcx.trait_impls_of(trait_def_id).is_empty() && self.tcx.trait_impls_of(trait_def_id).is_empty()
&& !self.tcx.trait_is_auto(trait_def_id) && !self.tcx.trait_is_auto(trait_def_id)
&& !self.tcx.trait_is_alias(trait_def_id) && !self.tcx.trait_is_alias(trait_def_id)
&& trait_predicate.polarity() == ty::PredicatePolarity::Positive
{ {
err.span_help( err.span_help(
self.tcx.def_span(trait_def_id), self.tcx.def_span(trait_def_id),
crate::fluent_generated::trait_selection_trait_has_no_impls, 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. // Can't show anything else useful, try to find similar impls.
let impl_candidates = self.find_similar_impl_candidates(trait_predicate); let impl_candidates = self.find_similar_impl_candidates(trait_predicate);
if !self.report_similar_impl_candidates( if !self.report_similar_impl_candidates(

View File

@ -3697,6 +3697,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
err: &mut Diag<'_>, err: &mut Diag<'_>,
trait_pred: ty::PolyTraitPredicate<'tcx>, 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 { let Some(diagnostic_name) = self.tcx.get_diagnostic_name(trait_pred.def_id()) else {
return; return;
}; };

View File

@ -6,12 +6,6 @@ LL | fn hello() -> impl !Foo {
LL | LL |
LL | NotFoo LL | NotFoo
| ------ return type was inferred to be `NotFoo` here | ------ 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 error: aborting due to 1 previous error

View File

@ -28,18 +28,11 @@ error[E0277]: the trait bound `Copyable: !Copy` is not satisfied
LL | not_copy::<Copyable>(); LL | not_copy::<Copyable>();
| ^^^^^^^^ the trait bound `Copyable: !Copy` is not satisfied | ^^^^^^^^ 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` note: required by a bound in `not_copy`
--> $DIR/simple.rs:3:16 --> $DIR/simple.rs:3:16
| |
LL | fn not_copy<T: !Copy>() {} LL | fn not_copy<T: !Copy>() {}
| ^^^^^ required by this bound in `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 error[E0277]: the trait bound `NotNecessarilyCopyable: !Copy` is not satisfied
--> $DIR/simple.rs:37:16 --> $DIR/simple.rs:37:16
@ -52,11 +45,6 @@ note: required by a bound in `not_copy`
| |
LL | fn not_copy<T: !Copy>() {} LL | fn not_copy<T: !Copy>() {}
| ^^^^^ required by this bound in `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 error: aborting due to 4 previous errors