1
Fork 0

Fix rendering of reexported macros 2.0

This commit is contained in:
Guillaume Gomez 2021-07-05 10:40:21 +02:00
parent c5e344f774
commit d1ad40eac4
3 changed files with 45 additions and 8 deletions

View file

@ -546,17 +546,35 @@ fn build_static(cx: &mut DocContext<'_>, did: DefId, mutable: bool) -> clean::St
fn build_macro(cx: &mut DocContext<'_>, did: DefId, name: Symbol) -> clean::ItemKind {
let imported_from = cx.tcx.crate_name(did.krate);
match cx.enter_resolver(|r| r.cstore().load_macro_untracked(did, cx.sess())) {
LoadedMacro::MacroDef(def, _) => {
if let ast::ItemKind::MacroDef(ref def) = def.kind {
LoadedMacro::MacroDef(item_def, _) => {
if let ast::ItemKind::MacroDef(ref def) = item_def.kind {
let tts: Vec<_> = def.body.inner_tokens().into_trees().collect();
let matchers = tts.chunks(4).map(|arm| &arm[0]);
let source = if def.macro_rules {
format!(
"macro_rules! {} {{\n{}}}",
name,
utils::render_macro_arms(matchers, ";")
)
} else {
let vis = item_def.vis.clean(cx);
let source = format!(
"macro_rules! {} {{\n{}}}",
name,
utils::render_macro_arms(matchers, ";")
);
if matchers.len() <= 1 {
format!(
"{}macro {}{} {{\n ...\n}}",
vis.to_src_with_space(cx.tcx, did),
name,
matchers.map(utils::render_macro_matcher).collect::<String>(),
)
} else {
format!(
"{}macro {} {{\n{}}}",
vis.to_src_with_space(cx.tcx, did),
name,
utils::render_macro_arms(matchers, ";"),
)
}
};
clean::MacroItem(clean::Macro { source, imported_from: Some(imported_from) })
} else {
unreachable!()

View file

@ -10,6 +10,7 @@ crate mod types;
crate mod utils;
use rustc_ast as ast;
use rustc_ast_lowering::ResolverAstLowering;
use rustc_attr as attr;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_hir as hir;
@ -1696,6 +1697,23 @@ impl Clean<Visibility> for hir::Visibility<'_> {
}
}
impl Clean<Visibility> for ast::Visibility {
fn clean(&self, cx: &mut DocContext<'_>) -> Visibility {
match self.kind {
ast::VisibilityKind::Public => Visibility::Public,
ast::VisibilityKind::Inherited => Visibility::Inherited,
ast::VisibilityKind::Crate(_) => {
let krate = DefId::local(CRATE_DEF_INDEX);
Visibility::Restricted(krate)
}
ast::VisibilityKind::Restricted { id, .. } => {
let did = cx.enter_resolver(|r| r.local_def_id(id)).to_def_id();
Visibility::Restricted(did)
}
}
}
}
impl Clean<Visibility> for ty::Visibility {
fn clean(&self, _cx: &mut DocContext<'_>) -> Visibility {
match *self {

View file

@ -31,6 +31,7 @@ extern crate tracing;
// Dependencies listed in Cargo.toml do not need `extern crate`.
extern crate rustc_ast;
extern crate rustc_ast_lowering;
extern crate rustc_ast_pretty;
extern crate rustc_attr;
extern crate rustc_data_structures;