@@ -1015,8 +1015,8 @@ impl LinkCollector<'_, '_> {
10151015 res = prim;
10161016 } else {
10171017 // `[char]` when a `char` module is in scope
1018- let candidates = vec ! [ ( res, res. def_id( self . cx. tcx) ) , ( prim, None ) ] ;
1019- ambiguity_error ( self . cx , & diag_info, path_str, & candidates) ;
1018+ let candidates = & [ ( res, res. def_id ( self . cx . tcx ) ) , ( prim, None ) ] ;
1019+ ambiguity_error ( self . cx , & diag_info, path_str, candidates) ;
10201020 return None ;
10211021 }
10221022 }
@@ -1206,6 +1206,10 @@ impl LinkCollector<'_, '_> {
12061206 }
12071207 }
12081208
1209+ // If there are multiple items with the same "kind" (for example, both "associated types")
1210+ // and after removing duplicated kinds, only one remains, the `ambiguity_error` function
1211+ // won't emit an error. So at this point, we can just take the first candidate as it was
1212+ // the first retrieved and use it to generate the link.
12091213 if candidates. len ( ) > 1 && !ambiguity_error ( self . cx , & diag, & key. path_str , & candidates) {
12101214 candidates = vec ! [ candidates[ 0 ] ] ;
12111215 }
@@ -1901,29 +1905,31 @@ fn report_malformed_generics(
19011905/// Report an ambiguity error, where there were multiple possible resolutions.
19021906///
19031907/// If all `candidates` have the same kind, it's not possible to disambiguate so in this case,
1904- /// the function returns `false`. Otherwise, it'll emit the error and return `true`.
1908+ /// the function won't emit an error and will return `false`. Otherwise, it'll emit the error and
1909+ /// return `true`.
19051910fn ambiguity_error (
19061911 cx : & DocContext < ' _ > ,
19071912 diag_info : & DiagnosticInfo < ' _ > ,
19081913 path_str : & str ,
19091914 candidates : & [ ( Res , Option < DefId > ) ] ,
19101915) -> bool {
1911- let mut msg = format ! ( "`{}` is " , path_str ) ;
1916+ let mut descrs = FxHashSet :: default ( ) ;
19121917 let kinds = candidates
19131918 . iter ( )
19141919 . map (
19151920 |( res, def_id) | {
19161921 if let Some ( def_id) = def_id { Res :: from_def_id ( cx. tcx , * def_id) } else { * res }
19171922 } ,
19181923 )
1924+ . filter ( |res| descrs. insert ( res. descr ( ) ) )
19191925 . collect :: < Vec < _ > > ( ) ;
1920- let descrs = kinds. iter ( ) . map ( |res| res. descr ( ) ) . collect :: < FxHashSet < & ' static str > > ( ) ;
19211926 if descrs. len ( ) == 1 {
19221927 // There is no way for users to disambiguate at this point, so better return the first
19231928 // candidate and not show a warning.
19241929 return false ;
19251930 }
19261931
1932+ let mut msg = format ! ( "`{}` is " , path_str) ;
19271933 match kinds. as_slice ( ) {
19281934 [ res1, res2] => {
19291935 msg += & format ! (
0 commit comments