Skip to content

Commit 84e9397

Browse files
committed
Auto merge of #84074 - notriddle:rustdoc-macro-visibility, r=jyn514
rustdoc: clean up and test macro visibility print This fixes the overly-complex invariant mentioned in <#83237 (comment)>, where the macro source can't have any links in it only because the cache hasn't been populated yet.
2 parents b0c818c + 2dfd0bf commit 84e9397

File tree

3 files changed

+57
-12
lines changed

3 files changed

+57
-12
lines changed

src/librustdoc/clean/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2287,14 +2287,14 @@ impl Clean<Item> for (&hir::MacroDef<'_>, Option<Symbol>) {
22872287
if matchers.len() <= 1 {
22882288
format!(
22892289
"{}macro {}{} {{\n ...\n}}",
2290-
vis.print_with_space(cx.tcx, def_id, &cx.cache),
2290+
vis.to_src_with_space(cx.tcx, def_id),
22912291
name,
22922292
matchers.iter().map(|span| span.to_src(cx)).collect::<String>(),
22932293
)
22942294
} else {
22952295
format!(
22962296
"{}macro {} {{\n{}}}",
2297-
vis.print_with_space(cx.tcx, def_id, &cx.cache),
2297+
vis.to_src_with_space(cx.tcx, def_id),
22982298
name,
22992299
matchers
23002300
.iter()

src/librustdoc/html/format.rs

+38-10
Original file line numberDiff line numberDiff line change
@@ -1186,8 +1186,6 @@ impl clean::Visibility {
11861186
item_did: DefId,
11871187
cache: &'a Cache,
11881188
) -> impl fmt::Display + 'a + Captures<'tcx> {
1189-
use rustc_span::symbol::kw;
1190-
11911189
let to_print = match self {
11921190
clean::Public => "pub ".to_owned(),
11931191
clean::Inherited => String::new(),
@@ -1212,18 +1210,11 @@ impl clean::Visibility {
12121210
} else {
12131211
let path = tcx.def_path(vis_did);
12141212
debug!("path={:?}", path);
1215-
let first_name =
1216-
path.data[0].data.get_opt_name().expect("modules are always named");
12171213
// modified from `resolved_path()` to work with `DefPathData`
12181214
let last_name = path.data.last().unwrap().data.get_opt_name().unwrap();
12191215
let anchor = anchor(vis_did, &last_name.as_str(), cache).to_string();
12201216

1221-
let mut s = "pub(".to_owned();
1222-
if path.data.len() != 1
1223-
|| (first_name != kw::SelfLower && first_name != kw::Super)
1224-
{
1225-
s.push_str("in ");
1226-
}
1217+
let mut s = "pub(in ".to_owned();
12271218
for seg in &path.data[..path.data.len() - 1] {
12281219
s.push_str(&format!("{}::", seg.data.get_opt_name().unwrap()));
12291220
}
@@ -1234,6 +1225,43 @@ impl clean::Visibility {
12341225
};
12351226
display_fn(move |f| f.write_str(&to_print))
12361227
}
1228+
1229+
/// This function is the same as print_with_space, except that it renders no links.
1230+
/// It's used for macros' rendered source view, which is syntax highlighted and cannot have
1231+
/// any HTML in it.
1232+
crate fn to_src_with_space<'a, 'tcx: 'a>(
1233+
self,
1234+
tcx: TyCtxt<'tcx>,
1235+
item_did: DefId,
1236+
) -> impl fmt::Display + 'a + Captures<'tcx> {
1237+
let to_print = match self {
1238+
clean::Public => "pub ".to_owned(),
1239+
clean::Inherited => String::new(),
1240+
clean::Visibility::Restricted(vis_did) => {
1241+
// FIXME(camelid): This may not work correctly if `item_did` is a module.
1242+
// However, rustdoc currently never displays a module's
1243+
// visibility, so it shouldn't matter.
1244+
let parent_module = find_nearest_parent_module(tcx, item_did);
1245+
1246+
if vis_did.index == CRATE_DEF_INDEX {
1247+
"pub(crate) ".to_owned()
1248+
} else if parent_module == Some(vis_did) {
1249+
// `pub(in foo)` where `foo` is the parent module
1250+
// is the same as no visibility modifier
1251+
String::new()
1252+
} else if parent_module
1253+
.map(|parent| find_nearest_parent_module(tcx, parent))
1254+
.flatten()
1255+
== Some(vis_did)
1256+
{
1257+
"pub(super) ".to_owned()
1258+
} else {
1259+
format!("pub(in {}) ", tcx.def_path_str(vis_did))
1260+
}
1261+
}
1262+
};
1263+
display_fn(move |f| f.write_str(&to_print))
1264+
}
12371265
}
12381266

12391267
crate trait PrintWithSpace {

src/test/rustdoc/decl_macro.rs

+17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// compile-flags: --document-private-items
2+
13
#![feature(decl_macro)]
24

35
// @has decl_macro/macro.my_macro.html //pre 'pub macro my_macro() {'
@@ -37,3 +39,18 @@ pub macro my_macro_multi {
3739
pub macro by_example_single {
3840
($foo:expr) => {}
3941
}
42+
43+
mod a {
44+
mod b {
45+
// @has decl_macro/a/b/macro.by_example_vis.html //pre 'pub(super) macro by_example_vis($foo:expr) {'
46+
pub(in super) macro by_example_vis {
47+
($foo:expr) => {}
48+
}
49+
mod c {
50+
// @has decl_macro/a/b/c/macro.by_example_vis_named.html //pre 'pub(in a) macro by_example_vis_named($foo:expr) {'
51+
pub(in a) macro by_example_vis_named {
52+
($foo:expr) => {}
53+
}
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)