@@ -806,6 +806,20 @@ fn trait_impls_for<'a>(
806806 iter. collect ( )
807807}
808808
809+ /// Check for resolve collisions between a trait and its derive.
810+ ///
811+ /// These are common and we should just resolve to the trait in that case.
812+ fn is_derive_trait_collision < T > ( ns : & PerNS < Result < Vec < ( Res , T ) > , ResolutionFailure < ' _ > > > ) -> bool {
813+ if let ( & Ok ( ref type_ns) , & Ok ( ref macro_ns) ) = ( & ns. type_ns , & ns. macro_ns ) {
814+ type_ns. iter ( ) . any ( |( res, _) | matches ! ( res, Res :: Def ( DefKind :: Trait , _) ) )
815+ && macro_ns
816+ . iter ( )
817+ . any ( |( res, _) | matches ! ( res, Res :: Def ( DefKind :: Macro ( MacroKind :: Derive ) , _) ) )
818+ } else {
819+ false
820+ }
821+ }
822+
809823impl < ' a , ' tcx > DocVisitor for LinkCollector < ' a , ' tcx > {
810824 fn visit_item ( & mut self , item : & Item ) {
811825 self . resolve_links ( item) ;
@@ -1313,9 +1327,22 @@ impl LinkCollector<'_, '_> {
13131327 disambiguator,
13141328 candidates. into_iter ( ) . filter_map ( |res| res. err ( ) ) . collect ( ) ,
13151329 ) ;
1330+ } else if len == 1 {
1331+ candidates. into_iter ( ) . filter_map ( |res| res. ok ( ) ) . flatten ( ) . collect :: < Vec < _ > > ( )
1332+ } else {
1333+ let has_derive_trait_collision = is_derive_trait_collision ( & candidates) ;
1334+ if len == 2 && has_derive_trait_collision {
1335+ candidates. type_ns . unwrap ( )
1336+ } else {
1337+ // If we're reporting an ambiguity, don't mention the namespaces that failed
1338+ let mut candidates = candidates. map ( |candidate| candidate. ok ( ) ) ;
1339+ // If there a collision between a trait and a derive, we ignore the derive.
1340+ if has_derive_trait_collision {
1341+ candidates. macro_ns = None ;
1342+ }
1343+ candidates. into_iter ( ) . filter_map ( |res| res) . flatten ( ) . collect :: < Vec < _ > > ( )
1344+ }
13161345 }
1317-
1318- candidates. into_iter ( ) . filter_map ( |res| res. ok ( ) ) . flatten ( ) . collect :: < Vec < _ > > ( )
13191346 }
13201347 }
13211348 }
0 commit comments