@@ -15,6 +15,7 @@ use rustc_middle::ty::{DefIdTree, Visibility};
1515use rustc_resolve:: { ParentScope , Resolver } ;
1616use rustc_session:: config:: Externs ;
1717use rustc_session:: Session ;
18+ use rustc_span:: symbol:: sym;
1819use rustc_span:: { Symbol , SyntaxContext } ;
1920
2021use std:: collections:: hash_map:: Entry ;
@@ -216,6 +217,8 @@ impl<'ra> EarlyDocLinkResolver<'_, 'ra> {
216217 ns : Namespace ,
217218 parent_scope : & ParentScope < ' ra > ,
218219 ) -> bool {
220+ // FIXME: This caching may be incorrect in case of multiple `macro_rules`
221+ // items with the same name in the same module.
219222 self . doc_link_resolutions
220223 . entry ( ( Symbol :: intern ( path_str) , ns, parent_scope. module . def_id ( ) ) )
221224 . or_insert_with_key ( |( path, ns, _) | {
@@ -307,18 +310,30 @@ impl Visitor<'_> for EarlyDocLinkResolver<'_, '_> {
307310 let module_def_id = self . resolver . local_def_id ( item. id ) . to_def_id ( ) ;
308311 let module = self . resolver . expect_module ( module_def_id) ;
309312 let old_module = mem:: replace ( & mut self . parent_scope . module , module) ;
313+ let old_macro_rules = self . parent_scope . macro_rules ;
310314 self . resolve_doc_links_local ( & item. attrs ) ; // Inner attribute scope
311315 self . process_module_children_or_reexports ( module_def_id) ;
312316 visit:: walk_item ( self , item) ;
317+ if item
318+ . attrs
319+ . iter ( )
320+ . all ( |attr| !attr. has_name ( sym:: macro_use) && !attr. has_name ( sym:: macro_escape) )
321+ {
322+ self . parent_scope . macro_rules = old_macro_rules;
323+ }
313324 self . parent_scope . module = old_module;
314325 } else {
315- match item. kind {
326+ match & item. kind {
316327 ItemKind :: Trait ( ..) => {
317328 self . all_traits . push ( self . resolver . local_def_id ( item. id ) . to_def_id ( ) ) ;
318329 }
319330 ItemKind :: Impl ( box ast:: Impl { of_trait : Some ( ..) , .. } ) => {
320331 self . all_trait_impls . push ( self . resolver . local_def_id ( item. id ) . to_def_id ( ) ) ;
321332 }
333+ ItemKind :: MacroDef ( macro_def) if macro_def. macro_rules => {
334+ self . parent_scope . macro_rules =
335+ self . resolver . macro_rules_scope ( self . resolver . local_def_id ( item. id ) ) ;
336+ }
322337 _ => { }
323338 }
324339 visit:: walk_item ( self , item) ;
@@ -345,6 +360,12 @@ impl Visitor<'_> for EarlyDocLinkResolver<'_, '_> {
345360 visit:: walk_field_def ( self , field)
346361 }
347362
363+ fn visit_block ( & mut self , block : & ast:: Block ) {
364+ let old_macro_rules = self . parent_scope . macro_rules ;
365+ visit:: walk_block ( self , block) ;
366+ self . parent_scope . macro_rules = old_macro_rules;
367+ }
368+
348369 // NOTE: if doc-comments are ever allowed on other nodes (e.g. function parameters),
349370 // then this will have to implement other visitor methods too.
350371}
0 commit comments