@@ -12,6 +12,7 @@ use rustc_hir::lang_items::LangItem;
12
12
use rustc_hir:: { AmbigArg , ItemKind } ;
13
13
use rustc_infer:: infer:: outlives:: env:: OutlivesEnvironment ;
14
14
use rustc_infer:: infer:: { self , InferCtxt , TyCtxtInferExt } ;
15
+ use rustc_lint_defs:: builtin:: SUPERTRAIT_ITEM_SHADOWING_DEFINITION ;
15
16
use rustc_macros:: LintDiagnostic ;
16
17
use rustc_middle:: mir:: interpret:: ErrorHandled ;
17
18
use rustc_middle:: query:: Providers ;
@@ -377,7 +378,10 @@ fn check_trait_item<'tcx>(
377
378
hir:: TraitItemKind :: Type ( _bounds, Some ( ty) ) => ( None , ty. span ) ,
378
379
_ => ( None , trait_item. span ) ,
379
380
} ;
381
+
380
382
check_dyn_incompatible_self_trait_by_name ( tcx, trait_item) ;
383
+ check_item_shadowed_by_supertrait ( tcx, def_id) ;
384
+
381
385
let mut res = check_associated_item ( tcx, def_id, span, method_sig) ;
382
386
383
387
if matches ! ( trait_item. kind, hir:: TraitItemKind :: Fn ( ..) ) {
@@ -892,6 +896,45 @@ fn check_dyn_incompatible_self_trait_by_name(tcx: TyCtxt<'_>, item: &hir::TraitI
892
896
}
893
897
}
894
898
899
+ fn check_item_shadowed_by_supertrait < ' tcx > ( tcx : TyCtxt < ' tcx > , trait_item_def_id : LocalDefId ) {
900
+ let item_name = tcx. item_name ( trait_item_def_id. to_def_id ( ) ) ;
901
+ let trait_def_id = tcx. local_parent ( trait_item_def_id) ;
902
+
903
+ let shadowed: Vec < _ > = traits:: supertrait_def_ids ( tcx, trait_def_id. to_def_id ( ) )
904
+ . skip ( 1 )
905
+ . flat_map ( |supertrait_def_id| {
906
+ tcx. associated_items ( supertrait_def_id) . filter_by_name_unhygienic ( item_name)
907
+ } )
908
+ . collect ( ) ;
909
+ if !shadowed. is_empty ( ) {
910
+ let shadowee = if let [ shadowed] = shadowed[ ..] {
911
+ errors:: SupertraitItemShadowee :: Labeled {
912
+ span : tcx. def_span ( shadowed. def_id ) ,
913
+ supertrait : tcx. item_name ( shadowed. trait_container ( tcx) . unwrap ( ) ) ,
914
+ }
915
+ } else {
916
+ let ( traits, spans) : ( Vec < _ > , Vec < _ > ) = shadowed
917
+ . iter ( )
918
+ . map ( |item| {
919
+ ( tcx. item_name ( item. trait_container ( tcx) . unwrap ( ) ) , tcx. def_span ( item. def_id ) )
920
+ } )
921
+ . unzip ( ) ;
922
+ errors:: SupertraitItemShadowee :: Several { traits : traits. into ( ) , spans : spans. into ( ) }
923
+ } ;
924
+
925
+ tcx. emit_node_span_lint (
926
+ SUPERTRAIT_ITEM_SHADOWING_DEFINITION ,
927
+ tcx. local_def_id_to_hir_id ( trait_item_def_id) ,
928
+ tcx. def_span ( trait_item_def_id) ,
929
+ errors:: SupertraitItemShadowing {
930
+ item : item_name,
931
+ subtrait : tcx. item_name ( trait_def_id. to_def_id ( ) ) ,
932
+ shadowee,
933
+ } ,
934
+ ) ;
935
+ }
936
+ }
937
+
895
938
fn check_impl_item < ' tcx > (
896
939
tcx : TyCtxt < ' tcx > ,
897
940
impl_item : & ' tcx hir:: ImplItem < ' tcx > ,
0 commit comments