@@ -1016,7 +1016,7 @@ impl LinkCollector<'_, '_> {
1016
1016
} else {
1017
1017
// `[char]` when a `char` module is in scope
1018
1018
let candidates = vec ! [ ( res, res. def_id( self . cx. tcx) ) , ( prim, None ) ] ;
1019
- ambiguity_error ( self . cx , diag_info, path_str, candidates) ;
1019
+ ambiguity_error ( self . cx , & diag_info, path_str, & candidates) ;
1020
1020
return None ;
1021
1021
}
1022
1022
}
@@ -1206,6 +1206,10 @@ impl LinkCollector<'_, '_> {
1206
1206
}
1207
1207
}
1208
1208
1209
+ if candidates. len ( ) > 1 && !ambiguity_error ( self . cx , & diag, & key. path_str , & candidates) {
1210
+ candidates = vec ! [ candidates[ 0 ] ] ;
1211
+ }
1212
+
1209
1213
if let & [ ( res, def_id) ] = candidates. as_slice ( ) {
1210
1214
let fragment = match ( & key. extra_fragment , def_id) {
1211
1215
( Some ( _) , Some ( def_id) ) => {
@@ -1221,9 +1225,6 @@ impl LinkCollector<'_, '_> {
1221
1225
return r;
1222
1226
}
1223
1227
1224
- if !candidates. is_empty ( ) {
1225
- ambiguity_error ( self . cx , diag, & key. path_str , candidates) ;
1226
- }
1227
1228
if cache_errors {
1228
1229
self . visited_links . insert ( key, None ) ;
1229
1230
}
@@ -1898,21 +1899,30 @@ fn report_malformed_generics(
1898
1899
}
1899
1900
1900
1901
/// Report an ambiguity error, where there were multiple possible resolutions.
1902
+ ///
1903
+ /// 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`.
1901
1905
fn ambiguity_error (
1902
1906
cx : & DocContext < ' _ > ,
1903
- diag_info : DiagnosticInfo < ' _ > ,
1907
+ diag_info : & DiagnosticInfo < ' _ > ,
1904
1908
path_str : & str ,
1905
- candidates : Vec < ( Res , Option < DefId > ) > ,
1906
- ) {
1909
+ candidates : & [ ( Res , Option < DefId > ) ] ,
1910
+ ) -> bool {
1907
1911
let mut msg = format ! ( "`{}` is " , path_str) ;
1908
1912
let kinds = candidates
1909
- . into_iter ( )
1913
+ . iter ( )
1910
1914
. map (
1911
1915
|( res, def_id) | {
1912
- if let Some ( def_id) = def_id { Res :: from_def_id ( cx. tcx , def_id) } else { res }
1916
+ if let Some ( def_id) = def_id { Res :: from_def_id ( cx. tcx , * def_id) } else { * res }
1913
1917
} ,
1914
1918
)
1915
1919
. collect :: < Vec < _ > > ( ) ;
1920
+ let descrs = kinds. iter ( ) . map ( |res| res. descr ( ) ) . collect :: < FxHashSet < & ' static str > > ( ) ;
1921
+ if descrs. len ( ) == 1 {
1922
+ // There is no way for users to disambiguate at this point, so better return the first
1923
+ // candidate and not show a warning.
1924
+ return false ;
1925
+ }
1916
1926
1917
1927
match kinds. as_slice ( ) {
1918
1928
[ res1, res2] => {
@@ -1936,7 +1946,7 @@ fn ambiguity_error(
1936
1946
}
1937
1947
}
1938
1948
1939
- report_diagnostic ( cx. tcx , BROKEN_INTRA_DOC_LINKS , & msg, & diag_info, |diag, sp| {
1949
+ report_diagnostic ( cx. tcx , BROKEN_INTRA_DOC_LINKS , & msg, diag_info, |diag, sp| {
1940
1950
if let Some ( sp) = sp {
1941
1951
diag. span_label ( sp, "ambiguous link" ) ;
1942
1952
} else {
@@ -1947,6 +1957,7 @@ fn ambiguity_error(
1947
1957
suggest_disambiguator ( res, diag, path_str, diag_info. ori_link , sp) ;
1948
1958
}
1949
1959
} ) ;
1960
+ true
1950
1961
}
1951
1962
1952
1963
/// In case of an ambiguity or mismatched disambiguator, suggest the correct
0 commit comments