@@ -22,6 +22,7 @@ use rustc_resolve::rustdoc::{
2222 MalformedGenerics , has_primitive_or_keyword_docs, prepare_to_doc_link_resolution,
2323 source_span_for_markdown_range, strip_generics_from_path,
2424} ;
25+ use rustc_session:: config:: CrateType ;
2526use rustc_session:: lint:: Lint ;
2627use rustc_span:: BytePos ;
2728use rustc_span:: hygiene:: MacroKind ;
@@ -1174,7 +1175,6 @@ impl LinkCollector<'_, '_> {
11741175 #[ allow( rustc:: potential_query_instability) ]
11751176 pub ( crate ) fn resolve_ambiguities ( & mut self ) {
11761177 let mut ambiguous_links = mem:: take ( & mut self . ambiguous_links ) ;
1177-
11781178 for ( ( item_id, path_str) , info_items) in ambiguous_links. iter_mut ( ) {
11791179 for info in info_items {
11801180 info. resolved . retain ( |( res, _) | match res {
@@ -2232,15 +2232,35 @@ fn ambiguity_error(
22322232 emit_error : bool ,
22332233) -> bool {
22342234 let mut descrs = FxHashSet :: default ( ) ;
2235- let kinds = candidates
2235+ // proc macro can exist in multiple namespaces at once, so we need to compare `DefIds`
2236+ // to remove the candidate in the fn namespace.
2237+ let mut possible_proc_macro_id = None ;
2238+ let is_proc_macro_crate = cx. tcx . crate_types ( ) == & [ CrateType :: ProcMacro ] ;
2239+ let mut kinds = candidates
22362240 . iter ( )
2237- . map (
2238- |( res, def_id) | {
2239- if let Some ( def_id) = def_id { Res :: from_def_id ( cx. tcx , * def_id) } else { * res }
2240- } ,
2241- )
2242- . filter ( |res| descrs. insert ( res. descr ( ) ) )
2241+ . map ( |( res, def_id) | {
2242+ let r =
2243+ if let Some ( def_id) = def_id { Res :: from_def_id ( cx. tcx , * def_id) } else { * res } ;
2244+ if is_proc_macro_crate && let Res :: Def ( DefKind :: Macro ( _) , id) = r {
2245+ possible_proc_macro_id = Some ( id) ;
2246+ }
2247+ r
2248+ } )
22432249 . collect :: < Vec < _ > > ( ) ;
2250+ // In order to properly dedup proc macros, we have to do it in two passes:
2251+ // 1. Completing the full traversal to find the possible duplicate in the macro namespace,
2252+ // 2. Another full traversal to eliminate the candidate in the fn namespace.
2253+ //
2254+ // Thus, we have to do an iteration after collection is finished.
2255+ //
2256+ // As an optimization, we only deduplicate if we're in a proc-macro crate,
2257+ // and only if we already found something that looks like a proc macro.
2258+ if is_proc_macro_crate && let Some ( macro_id) = possible_proc_macro_id {
2259+ kinds. retain ( |res| !matches ! ( res, Res :: Def ( DefKind :: Fn , fn_id) if macro_id == * fn_id) ) ;
2260+ }
2261+
2262+ kinds. retain ( |res| descrs. insert ( res. descr ( ) ) ) ;
2263+
22442264 if descrs. len ( ) == 1 {
22452265 // There is no way for users to disambiguate at this point, so better return the first
22462266 // candidate and not show a warning.
0 commit comments