Skip to content

Commit

Permalink
Account for negative bounds in E0277 note and suggestion
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
estebank committed Oct 29, 2024
1 parent 66bb65d commit 8763b04
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
6 changes: 0 additions & 6 deletions tests/ui/traits/negative-bounds/on-unimplemented.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 0 additions & 12 deletions tests/ui/traits/negative-bounds/simple.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,11 @@ error[E0277]: the trait bound `Copyable: !Copy` is not satisfied
LL | not_copy::<Copyable>();
| ^^^^^^^^ 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<T: !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
Expand All @@ -52,11 +45,6 @@ note: required by a bound in `not_copy`
|
LL | fn not_copy<T: !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

Expand Down

0 comments on commit 8763b04

Please sign in to comment.