|
7 | 7 | //! some of them support an alternate format that emits text, but that should
|
8 | 8 | //! not be used external to this module.
|
9 | 9 |
|
10 |
| -use std::borrow::Cow; |
11 | 10 | use std::cmp::Ordering;
|
12 | 11 | use std::fmt::{self, Display, Write};
|
13 | 12 | use std::iter::{self, once};
|
@@ -1390,50 +1389,47 @@ impl clean::FnDecl {
|
1390 | 1389 | }
|
1391 | 1390 |
|
1392 | 1391 | pub(crate) fn visibility_print_with_space(item: &clean::Item, cx: &Context<'_>) -> impl Display {
|
1393 |
| - use std::fmt::Write as _; |
1394 |
| - let vis: Cow<'static, str> = match item.visibility(cx.tcx()) { |
1395 |
| - None => "".into(), |
1396 |
| - Some(ty::Visibility::Public) => "pub ".into(), |
1397 |
| - Some(ty::Visibility::Restricted(vis_did)) => { |
1398 |
| - // FIXME(camelid): This may not work correctly if `item_did` is a module. |
1399 |
| - // However, rustdoc currently never displays a module's |
1400 |
| - // visibility, so it shouldn't matter. |
1401 |
| - let parent_module = find_nearest_parent_module(cx.tcx(), item.item_id.expect_def_id()); |
1402 |
| - |
1403 |
| - if vis_did.is_crate_root() { |
1404 |
| - "pub(crate) ".into() |
1405 |
| - } else if parent_module == Some(vis_did) { |
1406 |
| - // `pub(in foo)` where `foo` is the parent module |
1407 |
| - // is the same as no visibility modifier |
1408 |
| - "".into() |
1409 |
| - } else if parent_module.and_then(|parent| find_nearest_parent_module(cx.tcx(), parent)) |
1410 |
| - == Some(vis_did) |
1411 |
| - { |
1412 |
| - "pub(super) ".into() |
1413 |
| - } else { |
1414 |
| - let path = cx.tcx().def_path(vis_did); |
1415 |
| - debug!("path={path:?}"); |
1416 |
| - // modified from `resolved_path()` to work with `DefPathData` |
1417 |
| - let last_name = path.data.last().unwrap().data.get_opt_name().unwrap(); |
1418 |
| - let anchor = print_anchor(vis_did, last_name, cx); |
1419 |
| - |
1420 |
| - let mut s = "pub(in ".to_owned(); |
1421 |
| - for seg in &path.data[..path.data.len() - 1] { |
1422 |
| - let _ = write!(s, "{}::", seg.data.get_opt_name().unwrap()); |
1423 |
| - } |
1424 |
| - let _ = write!(s, "{anchor}) "); |
1425 |
| - s.into() |
1426 |
| - } |
1427 |
| - } |
1428 |
| - }; |
1429 |
| - |
1430 |
| - let is_doc_hidden = item.is_doc_hidden(); |
1431 | 1392 | fmt::from_fn(move |f| {
|
1432 |
| - if is_doc_hidden { |
| 1393 | + if item.is_doc_hidden() { |
1433 | 1394 | f.write_str("#[doc(hidden)] ")?;
|
1434 | 1395 | }
|
1435 | 1396 |
|
1436 |
| - f.write_str(&vis) |
| 1397 | + match item.visibility(cx.tcx()) { |
| 1398 | + None => {} |
| 1399 | + Some(ty::Visibility::Public) => f.write_str("pub ")?, |
| 1400 | + Some(ty::Visibility::Restricted(vis_did)) => { |
| 1401 | + // FIXME(camelid): This may not work correctly if `item_did` is a module. |
| 1402 | + // However, rustdoc currently never displays a module's |
| 1403 | + // visibility, so it shouldn't matter. |
| 1404 | + let parent_module = |
| 1405 | + find_nearest_parent_module(cx.tcx(), item.item_id.expect_def_id()); |
| 1406 | + |
| 1407 | + if vis_did.is_crate_root() { |
| 1408 | + f.write_str("pub(crate) ")?; |
| 1409 | + } else if parent_module == Some(vis_did) { |
| 1410 | + // `pub(in foo)` where `foo` is the parent module |
| 1411 | + // is the same as no visibility modifier; do nothing |
| 1412 | + } else if parent_module |
| 1413 | + .and_then(|parent| find_nearest_parent_module(cx.tcx(), parent)) |
| 1414 | + == Some(vis_did) |
| 1415 | + { |
| 1416 | + f.write_str("pub(super) ")?; |
| 1417 | + } else { |
| 1418 | + let path = cx.tcx().def_path(vis_did); |
| 1419 | + debug!("path={path:?}"); |
| 1420 | + // modified from `resolved_path()` to work with `DefPathData` |
| 1421 | + let last_name = path.data.last().unwrap().data.get_opt_name().unwrap(); |
| 1422 | + let anchor = print_anchor(vis_did, last_name, cx); |
| 1423 | + |
| 1424 | + f.write_str("pub(in ")?; |
| 1425 | + for seg in &path.data[..path.data.len() - 1] { |
| 1426 | + write!(f, "{}::", seg.data.get_opt_name().unwrap())?; |
| 1427 | + } |
| 1428 | + write!(f, "{anchor}) ")?; |
| 1429 | + } |
| 1430 | + } |
| 1431 | + } |
| 1432 | + Ok(()) |
1437 | 1433 | })
|
1438 | 1434 | }
|
1439 | 1435 |
|
|
0 commit comments