Skip to content

Commit 892c674

Browse files
committed
diagnostics: don't suggest #[derive] if impl already exists
1 parent da476f1 commit 892c674

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

Lines changed: 18 additions & 2 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};
@@ -3311,7 +3310,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
33113310
| sym::Debug => true,
33123311
_ => false,
33133312
};
3314-
if can_derive {
3313+
3314+
let trait_def_id = trait_pred.def_id();
3315+
let adt_def_id = adt.did();
3316+
3317+
let has_impl = self.tcx.trait_impls_of(trait_def_id)
3318+
.non_blanket_impls()
3319+
.values()
3320+
.flatten()
3321+
.any(|impl_def_id| {
3322+
let impl_self_ty = self.tcx.type_of(impl_def_id).instantiate_identity();
3323+
3324+
match impl_self_ty.kind() {
3325+
ty::Adt(def, _) => def.did() == adt_def_id,
3326+
_ => false,
3327+
}
3328+
});
3329+
3330+
if can_derive && !has_impl {
33153331
let self_name = trait_pred.self_ty().to_string();
33163332
let self_span = self.tcx.def_span(adt.did());
33173333
for super_trait in

0 commit comments

Comments
 (0)