1
Fork 0

rustdoc: Track macro_rules scopes during early doc link resolution

This way links referring to `macro_rules` items are resolved correctly
This commit is contained in:
Vadim Petrochenkov 2022-04-28 15:52:54 +03:00
parent 044e45c595
commit 8b21421873
6 changed files with 99 additions and 4 deletions

View file

@ -1267,13 +1267,15 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
self.insert_unused_macro(ident, def_id, item.id);
}
self.r.visibilities.insert(def_id, vis);
self.r.arenas.alloc_macro_rules_scope(MacroRulesScope::Binding(
let scope = self.r.arenas.alloc_macro_rules_scope(MacroRulesScope::Binding(
self.r.arenas.alloc_macro_rules_binding(MacroRulesBinding {
parent_macro_rules_scope: parent_scope.macro_rules,
binding,
ident,
}),
))
));
self.r.macro_rules_scopes.insert(def_id, scope);
scope
} else {
let module = parent_scope.module;
let vis = match item.kind {

View file

@ -145,7 +145,7 @@ enum ScopeSet<'a> {
pub struct ParentScope<'a> {
pub module: Module<'a>,
expansion: LocalExpnId,
macro_rules: MacroRulesScopeRef<'a>,
pub macro_rules: MacroRulesScopeRef<'a>,
derives: &'a [ast::Path],
}
@ -990,6 +990,8 @@ pub struct Resolver<'a> {
/// `macro_rules` scopes *produced* by expanding the macro invocations,
/// include all the `macro_rules` items and other invocations generated by them.
output_macro_rules_scopes: FxHashMap<LocalExpnId, MacroRulesScopeRef<'a>>,
/// `macro_rules` scopes produced by `macro_rules` item definitions.
macro_rules_scopes: FxHashMap<LocalDefId, MacroRulesScopeRef<'a>>,
/// Helper attributes that are in scope for the given expansion.
helper_attrs: FxHashMap<LocalExpnId, Vec<Ident>>,
/// Ready or in-progress results of resolving paths inside the `#[derive(...)]` attribute
@ -1361,6 +1363,7 @@ impl<'a> Resolver<'a> {
non_macro_attr: Lrc::new(SyntaxExtension::non_macro_attr(session.edition())),
invocation_parent_scopes: Default::default(),
output_macro_rules_scopes: Default::default(),
macro_rules_scopes: Default::default(),
helper_attrs: Default::default(),
derive_data: Default::default(),
local_macro_def_scopes: FxHashMap::default(),
@ -1919,6 +1922,11 @@ impl<'a> Resolver<'a> {
}
}
/// For rustdoc.
pub fn macro_rules_scope(&self, def_id: LocalDefId) -> MacroRulesScopeRef<'a> {
*self.macro_rules_scopes.get(&def_id).expect("not a `macro_rules` item")
}
/// Retrieves the span of the given `DefId` if `DefId` is in the local crate.
#[inline]
pub fn opt_span(&self, def_id: DefId) -> Option<Span> {