Skip to content

Commit 179cba4

Browse files
committed
rustdoc: Treat decl macros like other items when it comes to visibility
1 parent 3f1be1e commit 179cba4

File tree

8 files changed

+22
-80
lines changed

8 files changed

+22
-80
lines changed

src/librustdoc/clean/inline.rs

+5-13
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,7 @@ pub(crate) fn try_inline(
134134
})
135135
}
136136
Res::Def(DefKind::Macro(kind), did) => {
137-
let is_doc_hidden = cx.tcx.is_doc_hidden(did)
138-
|| attrs_without_docs
139-
.map(|(attrs, _)| attrs)
140-
.is_some_and(|attrs| utils::attrs_have_doc_flag(attrs.iter(), sym::hidden));
141-
let mac = build_macro(cx, did, name, import_def_id, kind, is_doc_hidden);
137+
let mac = build_macro(cx, did, name, kind);
142138

143139
let type_kind = match kind {
144140
MacroKind::Bang => ItemType::Macro,
@@ -740,18 +736,14 @@ fn build_macro(
740736
cx: &mut DocContext<'_>,
741737
def_id: DefId,
742738
name: Symbol,
743-
import_def_id: Option<LocalDefId>,
744739
macro_kind: MacroKind,
745-
is_doc_hidden: bool,
746740
) -> clean::ItemKind {
747741
match CStore::from_tcx(cx.tcx).load_macro_untracked(def_id, cx.tcx) {
748742
LoadedMacro::MacroDef { def, .. } => match macro_kind {
749-
MacroKind::Bang => {
750-
let vis = cx.tcx.visibility(import_def_id.map(|d| d.to_def_id()).unwrap_or(def_id));
751-
clean::MacroItem(clean::Macro {
752-
source: utils::display_macro_source(cx, name, &def, def_id, vis, is_doc_hidden),
753-
})
754-
}
743+
MacroKind::Bang => clean::MacroItem(clean::Macro {
744+
source: utils::display_macro_source(cx, name, &def),
745+
macro_rules: def.macro_rules,
746+
}),
755747
MacroKind::Derive | MacroKind::Attr => {
756748
clean::ProcMacroItem(clean::ProcMacro { kind: macro_kind, helpers: Vec::new() })
757749
}

src/librustdoc/clean/mod.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -2783,13 +2783,10 @@ fn clean_maybe_renamed_item<'tcx>(
27832783
fields: variant_data.fields().iter().map(|x| clean_field(x, cx)).collect(),
27842784
}),
27852785
ItemKind::Impl(impl_) => return clean_impl(impl_, item.owner_id.def_id, cx),
2786-
ItemKind::Macro(macro_def, MacroKind::Bang) => {
2787-
let ty_vis = cx.tcx.visibility(def_id);
2788-
MacroItem(Macro {
2789-
// FIXME this shouldn't be false
2790-
source: display_macro_source(cx, name, macro_def, def_id, ty_vis, false),
2791-
})
2792-
}
2786+
ItemKind::Macro(macro_def, MacroKind::Bang) => MacroItem(Macro {
2787+
source: display_macro_source(cx, name, macro_def),
2788+
macro_rules: macro_def.macro_rules,
2789+
}),
27932790
ItemKind::Macro(_, macro_kind) => clean_proc_macro(item, &mut name, macro_kind, cx),
27942791
// proc macros can have a name set by attributes
27952792
ItemKind::Fn(ref sig, generics, body_id) => {

src/librustdoc/clean/types.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2544,6 +2544,8 @@ pub(crate) struct ImportSource {
25442544
#[derive(Clone, Debug)]
25452545
pub(crate) struct Macro {
25462546
pub(crate) source: String,
2547+
/// Whether the macro was defined via `macro_rules!` as opposed to `macro`.
2548+
pub(crate) macro_rules: bool,
25472549
}
25482550

25492551
#[derive(Clone, Debug)]

src/librustdoc/clean/utils.rs

+2-11
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ use crate::clean::{
2424
clean_middle_ty, inline,
2525
};
2626
use crate::core::DocContext;
27-
use crate::html::format::visibility_to_src_with_space;
2827

2928
#[cfg(test)]
3029
mod tests;
@@ -620,9 +619,6 @@ pub(super) fn display_macro_source(
620619
cx: &mut DocContext<'_>,
621620
name: Symbol,
622621
def: &ast::MacroDef,
623-
def_id: DefId,
624-
vis: ty::Visibility<DefId>,
625-
is_doc_hidden: bool,
626622
) -> String {
627623
// Extract the spans of all matchers. They represent the "interface" of the macro.
628624
let matchers = def.body.tokens.chunks(4).map(|arm| &arm[0]);
@@ -632,18 +628,13 @@ pub(super) fn display_macro_source(
632628
} else {
633629
if matchers.len() <= 1 {
634630
format!(
635-
"{vis}macro {name}{matchers} {{\n ...\n}}",
636-
vis = visibility_to_src_with_space(Some(vis), cx.tcx, def_id, is_doc_hidden),
631+
"macro {name}{matchers} {{\n ...\n}}",
637632
matchers = matchers
638633
.map(|matcher| render_macro_matcher(cx.tcx, matcher))
639634
.collect::<String>(),
640635
)
641636
} else {
642-
format!(
643-
"{vis}macro {name} {{\n{arms}}}",
644-
vis = visibility_to_src_with_space(Some(vis), cx.tcx, def_id, is_doc_hidden),
645-
arms = render_macro_arms(cx.tcx, matchers, ","),
646-
)
637+
format!("macro {name} {{\n{arms}}}", arms = render_macro_arms(cx.tcx, matchers, ","))
647638
}
648639
}
649640
}

src/librustdoc/html/format.rs

-41
Original file line numberDiff line numberDiff line change
@@ -1616,47 +1616,6 @@ pub(crate) fn visibility_print_with_space<'a, 'tcx: 'a>(
16161616
})
16171617
}
16181618

1619-
/// This function is the same as print_with_space, except that it renders no links.
1620-
/// It's used for macros' rendered source view, which is syntax highlighted and cannot have
1621-
/// any HTML in it.
1622-
pub(crate) fn visibility_to_src_with_space<'a, 'tcx: 'a>(
1623-
visibility: Option<ty::Visibility<DefId>>,
1624-
tcx: TyCtxt<'tcx>,
1625-
item_did: DefId,
1626-
is_doc_hidden: bool,
1627-
) -> impl Display + 'a + Captures<'tcx> {
1628-
let vis: Cow<'static, str> = match visibility {
1629-
None => "".into(),
1630-
Some(ty::Visibility::Public) => "pub ".into(),
1631-
Some(ty::Visibility::Restricted(vis_did)) => {
1632-
// FIXME(camelid): This may not work correctly if `item_did` is a module.
1633-
// However, rustdoc currently never displays a module's
1634-
// visibility, so it shouldn't matter.
1635-
let parent_module = find_nearest_parent_module(tcx, item_did);
1636-
1637-
if vis_did.is_crate_root() {
1638-
"pub(crate) ".into()
1639-
} else if parent_module == Some(vis_did) {
1640-
// `pub(in foo)` where `foo` is the parent module
1641-
// is the same as no visibility modifier
1642-
"".into()
1643-
} else if parent_module.and_then(|parent| find_nearest_parent_module(tcx, parent))
1644-
== Some(vis_did)
1645-
{
1646-
"pub(super) ".into()
1647-
} else {
1648-
format!("pub(in {}) ", tcx.def_path_str(vis_did)).into()
1649-
}
1650-
}
1651-
};
1652-
display_fn(move |f| {
1653-
if is_doc_hidden {
1654-
f.write_str("#[doc(hidden)] ")?;
1655-
}
1656-
f.write_str(&vis)
1657-
})
1658-
}
1659-
16601619
pub(crate) trait PrintWithSpace {
16611620
fn print_with_space(&self) -> &str;
16621621
}

src/librustdoc/html/highlight.rs

-7
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,6 @@ pub(crate) fn render_example_with_highlighting(
5858
write_footer(out, playground_button);
5959
}
6060

61-
/// Highlights `src` as an item-decl, returning the HTML output.
62-
pub(crate) fn render_item_decl_with_highlighting(src: &str, out: &mut Buffer) {
63-
write!(out, "<pre class=\"rust item-decl\">");
64-
write_code(out, src, None, None);
65-
write!(out, "</pre>");
66-
}
67-
6861
fn write_header(
6962
out: &mut Buffer,
7063
class: &str,

src/librustdoc/html/render/print_item.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1745,7 +1745,13 @@ fn item_variants(
17451745
}
17461746

17471747
fn item_macro(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean::Macro) {
1748-
highlight::render_item_decl_with_highlighting(&t.source, w);
1748+
wrap_item(w, |w| {
1749+
// FIXME: Also print `#[doc(hidden)]` for `macro_rules!` if it `is_doc_hidden`.
1750+
if !t.macro_rules {
1751+
write!(w, "{}", visibility_print_with_space(it, cx));
1752+
}
1753+
highlight::write_code(w, &t.source, None, None);
1754+
});
17491755
write!(w, "{}", document(cx, it, None, HeadingOffset::H2))
17501756
}
17511757

tests/rustdoc/decl_macro.rs

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ mod a {
4848
}
4949
mod c {
5050
//@ has decl_macro/a/b/c/macro.by_example_vis_named.html //pre 'pub(in a) macro by_example_vis_named($foo:expr) {'
51+
// Regression test for <https://github.com/rust-lang/rust/issues/83000>:
52+
//@ has - '//pre[@class="rust item-decl"]//a[@class="mod"]/@href' '../../index.html'
5153
pub(in a) macro by_example_vis_named {
5254
($foo:expr) => {}
5355
}

0 commit comments

Comments
 (0)