Skip to content

Commit bd3503d

Browse files
committed
add explanation comments
1 parent 0363988 commit bd3503d

2 files changed

Lines changed: 20 additions & 18 deletions

File tree

compiler/rustc_hir_typeck/src/method/suggest.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ use rustc_trait_selection::traits::{
4343
FulfillmentError, Obligation, ObligationCauseCode, supertraits,
4444
};
4545
use tracing::{debug, info, instrument};
46-
4746
use super::probe::{AutorefOrPtrAdjustment, IsSuggestion, Mode, ProbeScope};
4847
use super::{CandidateSource, MethodError, NoMatchData};
4948
use crate::errors::{self, CandidateTraitNote, NoAssociatedItem};
@@ -3283,16 +3282,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
32833282

32843283
/// Checks if we can suggest a derive macro for the unmet trait bound.
32853284
/// Returns Some(list_of_derives) if possible, or None if not.
3286-
fn check_derive(
3285+
fn consider_suggesting_derives_for_ty(
32873286
&self,
32883287
trait_pred: ty::TraitPredicate<'tcx>,
32893288
adt: ty::AdtDef<'tcx>,
32903289
) -> Option<Vec<(String, Span, Symbol)>> {
32913290
let diagnostic_name = self.tcx.get_diagnostic_name(trait_pred.def_id())?;
32923291

32933292
let can_derive = match diagnostic_name {
3294-
sym::Default => !adt.is_enum(),
3295-
sym::Eq
3293+
sym::Default
3294+
| sym::Eq
32963295
| sym::PartialEq
32973296
| sym::Ord
32983297
| sym::PartialOrd
@@ -3309,16 +3308,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
33093308

33103309
let trait_def_id = trait_pred.def_id();
33113310
let self_ty = trait_pred.self_ty();
3312-
let has_impl =
3313-
self.tcx.non_blanket_impls_for_ty(trait_def_id, self_ty).any(|impl_def_id| {
3314-
self.tcx
3315-
.type_of(impl_def_id)
3316-
.instantiate_identity()
3317-
.ty_adt_def()
3318-
.is_some_and(|def| def.did() == adt.did())
3319-
});
33203311

3321-
if has_impl {
3312+
// We need to check if there is already a manual implementation of the trait
3313+
// for this specific ADT to avoid suggesting `#[derive(..)]` that would conflict.
3314+
if self.tcx.non_blanket_impls_for_ty(trait_def_id, self_ty).any(|impl_def_id| {
3315+
self.tcx
3316+
.type_of(impl_def_id)
3317+
.instantiate_identity()
3318+
.ty_adt_def()
3319+
.is_some_and(|def| def.did() == adt.did())
3320+
}) {
33223321
return None;
33233322
}
33243323

@@ -3355,7 +3354,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
33553354
Some(adt) if adt.did().is_local() => adt,
33563355
_ => continue,
33573356
};
3358-
if let Some(new_derives) = self.check_derive(trait_pred, adt) {
3357+
if let Some(new_derives) = self.consider_suggesting_derives_for_ty(trait_pred, adt) {
33593358
derives.extend(new_derives);
33603359
} else {
33613360
traits.push(trait_pred.def_id());

tests/ui/suggestions/derive-trait-for-method-call.stderr

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@ LL | impl<X: Clone + Default + , Y: Clone + Default> Foo<X, Y> {
2525
| | | unsatisfied trait bound introduced here
2626
| | unsatisfied trait bound introduced here
2727
| unsatisfied trait bound introduced here
28-
note: the trait `Default` must be implemented
29-
--> $SRC_DIR/core/src/default.rs:LL:COL
30-
help: consider annotating `Enum` with `#[derive(Clone)]`
28+
help: consider annotating `CloneEnum` with `#[derive(Default)]`
3129
|
32-
LL + #[derive(Clone)]
30+
LL + #[derive(Default)]
31+
LL | enum CloneEnum {
32+
|
33+
help: consider annotating `Enum` with `#[derive(Clone, Default)]`
34+
|
35+
LL + #[derive(Clone, Default)]
3336
LL | enum Enum {
3437
|
3538

0 commit comments

Comments
 (0)