@@ -914,9 +914,6 @@ fn iterate_trait_method_candidates(
914
914
let db = table. db ;
915
915
let env = table. trait_env . clone ( ) ;
916
916
let self_is_array = matches ! ( self_ty. kind( Interner ) , chalk_ir:: TyKind :: Array ( ..) ) ;
917
- // if ty is `dyn Trait`, the trait doesn't need to be in scope
918
- let inherent_trait =
919
- self_ty. dyn_trait ( ) . into_iter ( ) . flat_map ( |t| all_super_traits ( db. upcast ( ) , t) ) ;
920
917
let env_traits = matches ! ( self_ty. kind( Interner ) , TyKind :: Placeholder ( _) )
921
918
// if we have `T: Trait` in the param env, the trait doesn't need to be in scope
922
919
. then ( || {
@@ -925,7 +922,7 @@ fn iterate_trait_method_candidates(
925
922
} )
926
923
. into_iter ( )
927
924
. flatten ( ) ;
928
- let traits = inherent_trait . chain ( env_traits) . chain ( traits_in_scope. iter ( ) . copied ( ) ) ;
925
+ let traits = env_traits. chain ( traits_in_scope. iter ( ) . copied ( ) ) ;
929
926
930
927
let canonical_self_ty = table. canonicalize ( self_ty. clone ( ) ) . value ;
931
928
@@ -990,6 +987,23 @@ fn iterate_inherent_methods(
990
987
VisibleFromModule :: None => ( None , None ) ,
991
988
} ;
992
989
990
+ // For trait object types, methods of the trait and its super traits are considered inherent
991
+ // methods. This matters because these trait methods have higher priority than the other
992
+ // traits' methods, which would be considered in `iterate_trait_method_candidates()` after this
993
+ // function.
994
+ let inherent_traits =
995
+ self_ty. dyn_trait ( ) . into_iter ( ) . flat_map ( |t| all_super_traits ( db. upcast ( ) , t) ) ;
996
+ for t in inherent_traits {
997
+ let data = db. trait_data ( t) ;
998
+ for & ( _, item) in data. items . iter ( ) {
999
+ // We don't pass `visible_from_module` as all trait items should be visible from the
1000
+ // trait object.
1001
+ if is_valid_candidate ( table, name, receiver_ty, item, self_ty, None ) {
1002
+ callback ( receiver_adjustments. clone ( ) . unwrap_or_default ( ) , item) ?;
1003
+ }
1004
+ }
1005
+ }
1006
+
993
1007
if let Some ( block_id) = block {
994
1008
if let Some ( impls) = db. inherent_impls_in_block ( block_id) {
995
1009
impls_for_self_ty (
0 commit comments