Skip to content

Commit 84f259e

Browse files
Unify macro source display
1 parent c70250d commit 84f259e

File tree

3 files changed

+48
-58
lines changed

3 files changed

+48
-58
lines changed

src/librustdoc/clean/inline.rs

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -543,39 +543,15 @@ fn build_static(cx: &mut DocContext<'_>, did: DefId, mutable: bool) -> clean::St
543543
}
544544
}
545545

546-
fn build_macro(cx: &mut DocContext<'_>, did: DefId, name: Symbol) -> clean::ItemKind {
547-
let imported_from = cx.tcx.crate_name(did.krate);
548-
match cx.enter_resolver(|r| r.cstore().load_macro_untracked(did, cx.sess())) {
546+
fn build_macro(cx: &mut DocContext<'_>, def_id: DefId, name: Symbol) -> clean::ItemKind {
547+
let imported_from = cx.tcx.crate_name(def_id.krate);
548+
match cx.enter_resolver(|r| r.cstore().load_macro_untracked(def_id, cx.sess())) {
549549
LoadedMacro::MacroDef(item_def, _) => {
550550
if let ast::ItemKind::MacroDef(ref def) = item_def.kind {
551-
let tts: Vec<_> = def.body.inner_tokens().into_trees().collect();
552-
let matchers = tts.chunks(4).map(|arm| &arm[0]);
553-
let source = if def.macro_rules {
554-
format!(
555-
"macro_rules! {} {{\n{}}}",
556-
name,
557-
utils::render_macro_arms(matchers, ";")
558-
)
559-
} else {
560-
let vis = item_def.vis.clean(cx);
561-
562-
if matchers.len() <= 1 {
563-
format!(
564-
"{}macro {}{} {{\n ...\n}}",
565-
vis.to_src_with_space(cx.tcx, did),
566-
name,
567-
matchers.map(utils::render_macro_matcher).collect::<String>(),
568-
)
569-
} else {
570-
format!(
571-
"{}macro {} {{\n{}}}",
572-
vis.to_src_with_space(cx.tcx, did),
573-
name,
574-
utils::render_macro_arms(matchers, ";"),
575-
)
576-
}
577-
};
578-
clean::MacroItem(clean::Macro { source, imported_from: Some(imported_from) })
551+
clean::MacroItem(clean::Macro {
552+
source: utils::display_macro_source(cx, name, def, def_id, item_def.vis),
553+
imported_from: Some(imported_from),
554+
})
579555
} else {
580556
unreachable!()
581557
}

src/librustdoc/clean/mod.rs

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2175,37 +2175,15 @@ impl Clean<Item> for (&hir::MacroDef<'_>, Option<Symbol>) {
21752175
fn clean(&self, cx: &mut DocContext<'_>) -> Item {
21762176
let (item, renamed) = self;
21772177
let name = renamed.unwrap_or(item.ident.name);
2178-
let tts = item.ast.body.inner_tokens().trees().collect::<Vec<_>>();
2179-
// Extract the macro's matchers. They represent the "interface" of the macro.
2180-
let matchers = tts.chunks(4).map(|arm| &arm[0]);
2181-
2182-
let source = if item.ast.macro_rules {
2183-
format!("macro_rules! {} {{\n{}}}", name, render_macro_arms(matchers, ";"))
2184-
} else {
2185-
let vis = item.vis.clean(cx);
2186-
let def_id = item.def_id.to_def_id();
2187-
2188-
if matchers.len() <= 1 {
2189-
format!(
2190-
"{}macro {}{} {{\n ...\n}}",
2191-
vis.to_src_with_space(cx.tcx, def_id),
2192-
name,
2193-
matchers.map(render_macro_matcher).collect::<String>(),
2194-
)
2195-
} else {
2196-
format!(
2197-
"{}macro {} {{\n{}}}",
2198-
vis.to_src_with_space(cx.tcx, def_id),
2199-
name,
2200-
render_macro_arms(matchers, ","),
2201-
)
2202-
}
2203-
};
2178+
let def_id = item.def_id.to_def_id();
22042179

22052180
Item::from_hir_id_and_parts(
22062181
item.hir_id(),
22072182
Some(name),
2208-
MacroItem(Macro { source, imported_from: None }),
2183+
MacroItem(Macro {
2184+
source: display_macro_source(cx, name, &item.ast, def_id, &item.vis),
2185+
imported_from: None,
2186+
}),
22092187
cx,
22102188
)
22112189
}

src/librustdoc/clean/utils.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ use crate::clean::blanket_impl::BlanketImplFinder;
33
use crate::clean::{
44
inline, Clean, Crate, Generic, GenericArg, GenericArgs, ImportSource, Item, ItemKind, Lifetime,
55
Path, PathSegment, PolyTrait, Primitive, PrimitiveType, ResolvedPath, Type, TypeBinding,
6+
Visibility,
67
};
78
use crate::core::DocContext;
89
use crate::formats::item_type::ItemType;
910

11+
use rustc_ast as ast;
1012
use rustc_ast::tokenstream::TokenTree;
1113
use rustc_hir as hir;
1214
use rustc_hir::def::{DefKind, Res};
@@ -577,3 +579,37 @@ pub(super) fn render_macro_arms<'a>(
577579
pub(super) fn render_macro_matcher(matcher: &TokenTree) -> String {
578580
rustc_ast_pretty::pprust::tt_to_string(matcher)
579581
}
582+
583+
pub(super) fn display_macro_source(
584+
cx: &mut DocContext<'_>,
585+
name: Symbol,
586+
def: &ast::MacroDef,
587+
def_id: DefId,
588+
vis: impl Clean<Visibility>,
589+
) -> String {
590+
let tts: Vec<_> = def.body.inner_tokens().into_trees().collect();
591+
// Extract the spans of all matchers. They represent the "interface" of the macro.
592+
let matchers = tts.chunks(4).map(|arm| &arm[0]);
593+
594+
if def.macro_rules {
595+
format!("macro_rules! {} {{\n{}}}", name, render_macro_arms(matchers, ";"))
596+
} else {
597+
let vis = vis.clean(cx);
598+
599+
if matchers.len() <= 1 {
600+
format!(
601+
"{}macro {}{} {{\n ...\n}}",
602+
vis.to_src_with_space(cx.tcx, def_id),
603+
name,
604+
matchers.map(render_macro_matcher).collect::<String>(),
605+
)
606+
} else {
607+
format!(
608+
"{}macro {} {{\n{}}}",
609+
vis.to_src_with_space(cx.tcx, def_id),
610+
name,
611+
render_macro_arms(matchers, ","),
612+
)
613+
}
614+
}
615+
}

0 commit comments

Comments
 (0)