@@ -43,7 +43,6 @@ use rustc_trait_selection::traits::{
4343 FulfillmentError , Obligation , ObligationCauseCode , supertraits,
4444} ;
4545use tracing:: { debug, info, instrument} ;
46-
4746use super :: probe:: { AutorefOrPtrAdjustment , IsSuggestion , Mode , ProbeScope } ;
4847use super :: { CandidateSource , MethodError , NoMatchData } ;
4948use 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 ( ) ) ;
0 commit comments