Skip to content

Commit

Permalink
Rollup merge of rust-lang#93693 - rukai:91550, r=davidtwco
Browse files Browse the repository at this point in the history
Suggest deriving required supertraits

closes rust-lang#91550

I chose to just hardcode handling for PartialOrd and PartialEq because that should be robust enough and I dont know how to go about doing it generically

r? rust-lang/diagnostics
  • Loading branch information
matthiaskrgr authored Feb 16, 2022
2 parents cae0433 + 1973f27 commit 39aef92
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 25 deletions.
42 changes: 29 additions & 13 deletions compiler/rustc_typeck/src/check/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::lang_items::LangItem;
use rustc_hir::{ExprKind, Node, QPath};
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use rustc_middle::traits::util::supertraits;
use rustc_middle::ty::fast_reject::{simplify_type, SimplifyParams};
use rustc_middle::ty::print::with_crate_prefix;
use rustc_middle::ty::ToPolyTraitRef;
use rustc_middle::ty::{self, DefIdTree, ToPredicate, Ty, TyCtxt, TypeFoldable};
use rustc_span::lev_distance;
use rustc_span::symbol::{kw, sym, Ident};
Expand Down Expand Up @@ -1196,26 +1198,40 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
Some(adt) if adt.did.is_local() => adt,
_ => continue,
};
let can_derive = match self.tcx.get_diagnostic_name(trait_pred.def_id()) {
Some(sym::Default) => !adt.is_enum(),
Some(
if let Some(diagnostic_name) = self.tcx.get_diagnostic_name(trait_pred.def_id()) {
let can_derive = match diagnostic_name {
sym::Default => !adt.is_enum(),
sym::Eq
| sym::PartialEq
| sym::Ord
| sym::PartialOrd
| sym::Clone
| sym::Copy
| sym::Hash
| sym::Debug,
) => true,
_ => false,
};
if can_derive {
derives.push((
format!("{}", trait_pred.self_ty()),
self.tcx.def_span(adt.did),
format!("{}", trait_pred.trait_ref.print_only_trait_name()),
));
| sym::Debug => true,
_ => false,
};
if can_derive {
let self_name = trait_pred.self_ty().to_string();
let self_span = self.tcx.def_span(adt.did);
if let Some(poly_trait_ref) = pred.to_opt_poly_trait_pred() {
for super_trait in supertraits(self.tcx, poly_trait_ref.to_poly_trait_ref())
{
if let Some(parent_diagnostic_name) =
self.tcx.get_diagnostic_name(super_trait.def_id())
{
derives.push((
self_name.clone(),
self_span.clone(),
parent_diagnostic_name.to_string(),
));
}
}
}
derives.push((self_name, self_span, diagnostic_name.to_string()));
} else {
traits.push(self.tcx.def_span(trait_pred.def_id()));
}
} else {
traits.push(self.tcx.def_span(trait_pred.def_id()));
}
Expand Down
16 changes: 8 additions & 8 deletions src/test/ui/binop/issue-28837.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,9 @@ note: an implementation of `PartialOrd<_>` might be missing for `A`
|
LL | struct A;
| ^^^^^^^^^ must implement `PartialOrd<_>`
help: consider annotating `A` with `#[derive(PartialOrd)]`
help: consider annotating `A` with `#[derive(PartialEq, PartialOrd)]`
|
LL | #[derive(PartialOrd)]
LL | #[derive(PartialEq, PartialOrd)]
|

error[E0369]: binary operation `<=` cannot be applied to type `A`
Expand All @@ -290,9 +290,9 @@ note: an implementation of `PartialOrd<_>` might be missing for `A`
|
LL | struct A;
| ^^^^^^^^^ must implement `PartialOrd<_>`
help: consider annotating `A` with `#[derive(PartialOrd)]`
help: consider annotating `A` with `#[derive(PartialEq, PartialOrd)]`
|
LL | #[derive(PartialOrd)]
LL | #[derive(PartialEq, PartialOrd)]
|

error[E0369]: binary operation `>` cannot be applied to type `A`
Expand All @@ -308,9 +308,9 @@ note: an implementation of `PartialOrd<_>` might be missing for `A`
|
LL | struct A;
| ^^^^^^^^^ must implement `PartialOrd<_>`
help: consider annotating `A` with `#[derive(PartialOrd)]`
help: consider annotating `A` with `#[derive(PartialEq, PartialOrd)]`
|
LL | #[derive(PartialOrd)]
LL | #[derive(PartialEq, PartialOrd)]
|

error[E0369]: binary operation `>=` cannot be applied to type `A`
Expand All @@ -326,9 +326,9 @@ note: an implementation of `PartialOrd<_>` might be missing for `A`
|
LL | struct A;
| ^^^^^^^^^ must implement `PartialOrd<_>`
help: consider annotating `A` with `#[derive(PartialOrd)]`
help: consider annotating `A` with `#[derive(PartialEq, PartialOrd)]`
|
LL | #[derive(PartialOrd)]
LL | #[derive(PartialEq, PartialOrd)]
|

error: aborting due to 15 previous errors
Expand Down
29 changes: 29 additions & 0 deletions src/test/ui/derives/issue-91550.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use std::collections::HashSet;

/// natural case from the issue
struct Value(u32);

fn main() {
let hs = HashSet::<Value>::new();
hs.insert(Value(0)); //~ ERROR
}

/// synthetic cases
pub struct NoDerives;

struct Object<T>(T);
impl<T: Eq> Object<T> {
fn use_eq(&self) {}
}
impl<T: Ord> Object<T> {
fn use_ord(&self) {}
}
impl<T: Ord + PartialOrd> Object<T> {
fn use_ord_and_partial_ord(&self) {}
}

fn function(foo: Object<NoDerives>) {
foo.use_eq(); //~ ERROR
foo.use_ord(); //~ ERROR
foo.use_ord_and_partial_ord(); //~ ERROR
}
84 changes: 84 additions & 0 deletions src/test/ui/derives/issue-91550.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
error[E0599]: the method `insert` exists for struct `HashSet<Value>`, but its trait bounds were not satisfied
--> $DIR/issue-91550.rs:8:8
|
LL | struct Value(u32);
| ------------------
| |
| doesn't satisfy `Value: Eq`
| doesn't satisfy `Value: Hash`
...
LL | hs.insert(Value(0));
| ^^^^^^ method cannot be called on `HashSet<Value>` due to unsatisfied trait bounds
|
= note: the following trait bounds were not satisfied:
`Value: Eq`
`Value: Hash`
help: consider annotating `Value` with `#[derive(Eq, Hash, PartialEq)]`
|
LL | #[derive(Eq, Hash, PartialEq)]
|

error[E0599]: the method `use_eq` exists for struct `Object<NoDerives>`, but its trait bounds were not satisfied
--> $DIR/issue-91550.rs:26:9
|
LL | pub struct NoDerives;
| --------------------- doesn't satisfy `NoDerives: Eq`
LL |
LL | struct Object<T>(T);
| -------------------- method `use_eq` not found for this
...
LL | foo.use_eq();
| ^^^^^^ method cannot be called on `Object<NoDerives>` due to unsatisfied trait bounds
|
= note: the following trait bounds were not satisfied:
`NoDerives: Eq`
help: consider annotating `NoDerives` with `#[derive(Eq, PartialEq)]`
|
LL | #[derive(Eq, PartialEq)]
|

error[E0599]: the method `use_ord` exists for struct `Object<NoDerives>`, but its trait bounds were not satisfied
--> $DIR/issue-91550.rs:27:9
|
LL | pub struct NoDerives;
| --------------------- doesn't satisfy `NoDerives: Ord`
LL |
LL | struct Object<T>(T);
| -------------------- method `use_ord` not found for this
...
LL | foo.use_ord();
| ^^^^^^^ method cannot be called on `Object<NoDerives>` due to unsatisfied trait bounds
|
= note: the following trait bounds were not satisfied:
`NoDerives: Ord`
help: consider annotating `NoDerives` with `#[derive(Eq, Ord, PartialEq, PartialOrd)]`
|
LL | #[derive(Eq, Ord, PartialEq, PartialOrd)]
|

error[E0599]: the method `use_ord_and_partial_ord` exists for struct `Object<NoDerives>`, but its trait bounds were not satisfied
--> $DIR/issue-91550.rs:28:9
|
LL | pub struct NoDerives;
| ---------------------
| |
| doesn't satisfy `NoDerives: Ord`
| doesn't satisfy `NoDerives: PartialOrd`
LL |
LL | struct Object<T>(T);
| -------------------- method `use_ord_and_partial_ord` not found for this
...
LL | foo.use_ord_and_partial_ord();
| ^^^^^^^^^^^^^^^^^^^^^^^ method cannot be called on `Object<NoDerives>` due to unsatisfied trait bounds
|
= note: the following trait bounds were not satisfied:
`NoDerives: Ord`
`NoDerives: PartialOrd`
help: consider annotating `NoDerives` with `#[derive(Eq, Ord, PartialEq, PartialOrd)]`
|
LL | #[derive(Eq, Ord, PartialEq, PartialOrd)]
|

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0599`.
4 changes: 2 additions & 2 deletions src/test/ui/union/union-derive-clone.mirunsafeck.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ LL | let w = u.clone();
= note: the following trait bounds were not satisfied:
`CloneNoCopy: Copy`
which is required by `U5<CloneNoCopy>: Clone`
help: consider annotating `CloneNoCopy` with `#[derive(Copy)]`
help: consider annotating `CloneNoCopy` with `#[derive(Clone, Copy)]`
|
LL | #[derive(Copy)]
LL | #[derive(Clone, Copy)]
|

error[E0277]: the trait bound `U1: Copy` is not satisfied
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/union/union-derive-clone.thirunsafeck.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ LL | let w = u.clone();
= note: the following trait bounds were not satisfied:
`CloneNoCopy: Copy`
which is required by `U5<CloneNoCopy>: Clone`
help: consider annotating `CloneNoCopy` with `#[derive(Copy)]`
help: consider annotating `CloneNoCopy` with `#[derive(Clone, Copy)]`
|
LL | #[derive(Copy)]
LL | #[derive(Clone, Copy)]
|

error[E0277]: the trait bound `U1: Copy` is not satisfied
Expand Down

0 comments on commit 39aef92

Please sign in to comment.