From 18276b2dbd4528edd7f41a0b3345438542167723 Mon Sep 17 00:00:00 2001 From: Rune Tynan Date: Thu, 26 Nov 2020 21:17:13 -0500 Subject: [PATCH] Apply review: use from_hir, add macro source fix. --- src/librustdoc/clean/mod.rs | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index f417eb6740cd..d9a3c16fb535 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -2320,22 +2320,29 @@ impl Clean for (&hir::MacroDef<'_>, Option) { let tts = item.ast.body.inner_tokens().trees().collect::>(); // Extract the spans of all matchers. They represent the "interface" of the macro. let matchers = tts.chunks(4).map(|arm| arm[0].span()).collect::>(); + let source = if item.ast.macro_rules { + format!( + "macro_rules! {} {{\n{}}}", + name, + matchers + .iter() + .map(|span| { format!(" {} => {{ ... }};\n", span.to_src(cx)) }) + .collect::(), + ) + } else { + // This code currently assumes that there will only be one or zero matchers, as syntax + // for multiple is not currently defined. + format!( + "pub macro {}({}) {{\n\t...\n}}", + name, + matchers.iter().map(|span| span.to_src(cx)).collect::(), + ) + }; - Item::from_def_id_and_parts( - cx.tcx.hir().local_def_id(item.hir_id).to_def_id(), + Item::from_hir_id_and_parts( + item.hir_id, Some(name.clean(cx)), - MacroItem(Macro { - // FIXME(#76761): Make this respect `macro_rules!` vs `pub macro` - source: format!( - "macro_rules! {} {{\n{}}}", - name, - matchers - .iter() - .map(|span| { format!(" {} => {{ ... }};\n", span.to_src(cx)) }) - .collect::(), - ), - imported_from: None, - }), + MacroItem(Macro { source, imported_from: None }), cx, ) }