Skip to content

Commit ae21240

Browse files
committed
Make implementation generic
1 parent 8610edd commit ae21240

File tree

5 files changed

+45
-37
lines changed

5 files changed

+45
-37
lines changed

compiler/rustc_typeck/src/check/method/suggest.rs

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,35 +1196,43 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11961196
Some(adt) if adt.did.is_local() => adt,
11971197
_ => continue,
11981198
};
1199-
let diagnostic_name = self.tcx.get_diagnostic_name(trait_pred.def_id());
1200-
let can_derive = match diagnostic_name {
1201-
Some(sym::Default) => !adt.is_enum(),
1202-
Some(
1199+
if let Some(diagnostic_name) = self.tcx.get_diagnostic_name(trait_pred.def_id()) {
1200+
let can_derive = match diagnostic_name {
1201+
sym::Default => !adt.is_enum(),
12031202
sym::Eq
12041203
| sym::PartialEq
12051204
| sym::Ord
12061205
| sym::PartialOrd
12071206
| sym::Clone
12081207
| sym::Copy
12091208
| sym::Hash
1210-
| sym::Debug,
1211-
) => true,
1212-
_ => false,
1213-
};
1214-
if can_derive {
1215-
let self_name = trait_pred.self_ty().to_string();
1216-
let self_span = self.tcx.def_span(adt.did);
1217-
if let Some(sym::Ord) = diagnostic_name {
1218-
derives.push((self_name.clone(), self_span.clone(), "PartialOrd".to_string()));
1219-
}
1220-
if let Some(sym::Eq) = diagnostic_name {
1221-
derives.push((self_name.clone(), self_span.clone(), "PartialEq".to_string()));
1209+
| sym::Debug => true,
1210+
_ => false,
1211+
};
1212+
if can_derive {
1213+
let self_name = trait_pred.self_ty().to_string();
1214+
let self_span = self.tcx.def_span(adt.did);
1215+
use crate::rustc_middle::ty::ToPolyTraitRef;
1216+
if let Some(poly_trait_ref) = pred.to_opt_poly_trait_pred() {
1217+
for super_trait in rustc_middle::traits::util::supertraits(
1218+
self.tcx,
1219+
poly_trait_ref.to_poly_trait_ref(),
1220+
) {
1221+
if let Some(parent_diagnostic_name) =
1222+
self.tcx.get_diagnostic_name(super_trait.def_id())
1223+
{
1224+
derives.push((
1225+
self_name.clone(),
1226+
self_span.clone(),
1227+
parent_diagnostic_name.to_string(),
1228+
));
1229+
}
1230+
}
1231+
}
1232+
derives.push((self_name, self_span, diagnostic_name.to_string()));
1233+
} else {
1234+
traits.push(self.tcx.def_span(trait_pred.def_id()));
12221235
}
1223-
derives.push((
1224-
self_name,
1225-
self_span,
1226-
trait_pred.trait_ref.print_only_trait_name().to_string(),
1227-
));
12281236
} else {
12291237
traits.push(self.tcx.def_span(trait_pred.def_id()));
12301238
}

src/test/ui/binop/issue-28837.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,9 @@ note: an implementation of `PartialOrd<_>` might be missing for `A`
272272
|
273273
LL | struct A;
274274
| ^^^^^^^^^ must implement `PartialOrd<_>`
275-
help: consider annotating `A` with `#[derive(PartialOrd)]`
275+
help: consider annotating `A` with `#[derive(PartialEq, PartialOrd)]`
276276
|
277-
LL | #[derive(PartialOrd)]
277+
LL | #[derive(PartialEq, PartialOrd)]
278278
|
279279

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

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

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

334334
error: aborting due to 15 previous errors

src/test/ui/derives/issue-91550.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ LL | foo.use_ord();
5151
|
5252
= note: the following trait bounds were not satisfied:
5353
`NoDerives: Ord`
54-
help: consider annotating `NoDerives` with `#[derive(Ord, PartialOrd)]`
54+
help: consider annotating `NoDerives` with `#[derive(Eq, Ord, PartialEq, PartialOrd)]`
5555
|
56-
LL | #[derive(Ord, PartialOrd)]
56+
LL | #[derive(Eq, Ord, PartialEq, PartialOrd)]
5757
|
5858

5959
error[E0599]: the method `use_ord_and_partial_ord` exists for struct `Object<NoDerives>`, but its trait bounds were not satisfied
@@ -74,9 +74,9 @@ LL | foo.use_ord_and_partial_ord();
7474
= note: the following trait bounds were not satisfied:
7575
`NoDerives: Ord`
7676
`NoDerives: PartialOrd`
77-
help: consider annotating `NoDerives` with `#[derive(Ord, PartialOrd)]`
77+
help: consider annotating `NoDerives` with `#[derive(Eq, Ord, PartialEq, PartialOrd)]`
7878
|
79-
LL | #[derive(Ord, PartialOrd)]
79+
LL | #[derive(Eq, Ord, PartialEq, PartialOrd)]
8080
|
8181

8282
error: aborting due to 4 previous errors

src/test/ui/union/union-derive-clone.mirunsafeck.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ LL | let w = u.clone();
1616
= note: the following trait bounds were not satisfied:
1717
`CloneNoCopy: Copy`
1818
which is required by `U5<CloneNoCopy>: Clone`
19-
help: consider annotating `CloneNoCopy` with `#[derive(Copy)]`
19+
help: consider annotating `CloneNoCopy` with `#[derive(Clone, Copy)]`
2020
|
21-
LL | #[derive(Copy)]
21+
LL | #[derive(Clone, Copy)]
2222
|
2323

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

src/test/ui/union/union-derive-clone.thirunsafeck.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ LL | let w = u.clone();
1616
= note: the following trait bounds were not satisfied:
1717
`CloneNoCopy: Copy`
1818
which is required by `U5<CloneNoCopy>: Clone`
19-
help: consider annotating `CloneNoCopy` with `#[derive(Copy)]`
19+
help: consider annotating `CloneNoCopy` with `#[derive(Clone, Copy)]`
2020
|
21-
LL | #[derive(Copy)]
21+
LL | #[derive(Clone, Copy)]
2222
|
2323

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

0 commit comments

Comments
 (0)