Skip to content

Commit e948e8d

Browse files
Fix reexports visibility
1 parent 84f259e commit e948e8d

File tree

3 files changed

+30
-33
lines changed

3 files changed

+30
-33
lines changed

src/librustdoc/clean/inline.rs

+25-12
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type Attrs<'hir> = rustc_middle::ty::Attributes<'hir>;
4141
crate fn try_inline(
4242
cx: &mut DocContext<'_>,
4343
parent_module: DefId,
44+
import_def_id: Option<DefId>,
4445
res: Res,
4546
name: Symbol,
4647
attrs: Option<Attrs<'_>>,
@@ -108,7 +109,7 @@ crate fn try_inline(
108109
clean::ConstantItem(build_const(cx, did))
109110
}
110111
Res::Def(DefKind::Macro(kind), did) => {
111-
let mac = build_macro(cx, did, name);
112+
let mac = build_macro(cx, did, name, import_def_id);
112113

113114
let type_kind = match kind {
114115
MacroKind::Bang => ItemType::Macro,
@@ -123,14 +124,13 @@ crate fn try_inline(
123124

124125
let (attrs, cfg) = merge_attrs(cx, Some(parent_module), load_attrs(cx, did), attrs_clone);
125126
cx.inlined.insert(did.into());
126-
ret.push(clean::Item::from_def_id_and_attrs_and_parts(
127-
did,
128-
Some(name),
129-
kind,
130-
box attrs,
131-
cx,
132-
cfg,
133-
));
127+
let mut item =
128+
clean::Item::from_def_id_and_attrs_and_parts(did, Some(name), kind, box attrs, cx, cfg);
129+
if let Some(import_def_id) = import_def_id {
130+
// The visibility needs to reflect the one from the reexport and not from the "source" DefId.
131+
item.visibility = cx.tcx.visibility(import_def_id).clean(cx);
132+
}
133+
ret.push(item);
134134
Some(ret)
135135
}
136136

@@ -509,7 +509,9 @@ fn build_module(
509509
)),
510510
cfg: None,
511511
});
512-
} else if let Some(i) = try_inline(cx, did, item.res, item.ident.name, None, visited) {
512+
} else if let Some(i) =
513+
try_inline(cx, did, None, item.res, item.ident.name, None, visited)
514+
{
513515
items.extend(i)
514516
}
515517
}
@@ -543,13 +545,24 @@ fn build_static(cx: &mut DocContext<'_>, did: DefId, mutable: bool) -> clean::St
543545
}
544546
}
545547

546-
fn build_macro(cx: &mut DocContext<'_>, def_id: DefId, name: Symbol) -> clean::ItemKind {
548+
fn build_macro(
549+
cx: &mut DocContext<'_>,
550+
def_id: DefId,
551+
name: Symbol,
552+
import_def_id: Option<DefId>,
553+
) -> clean::ItemKind {
547554
let imported_from = cx.tcx.crate_name(def_id.krate);
548555
match cx.enter_resolver(|r| r.cstore().load_macro_untracked(def_id, cx.sess())) {
549556
LoadedMacro::MacroDef(item_def, _) => {
550557
if let ast::ItemKind::MacroDef(ref def) = item_def.kind {
551558
clean::MacroItem(clean::Macro {
552-
source: utils::display_macro_source(cx, name, def, def_id, item_def.vis),
559+
source: utils::display_macro_source(
560+
cx,
561+
name,
562+
def,
563+
def_id,
564+
cx.tcx.visibility(import_def_id.unwrap_or(def_id)),
565+
),
553566
imported_from: Some(imported_from),
554567
})
555568
} else {

src/librustdoc/clean/mod.rs

+5-20
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ crate mod types;
1010
crate mod utils;
1111

1212
use rustc_ast as ast;
13-
use rustc_ast_lowering::ResolverAstLowering;
1413
use rustc_attr as attr;
1514
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1615
use rustc_hir as hir;
@@ -1697,23 +1696,6 @@ impl Clean<Visibility> for hir::Visibility<'_> {
16971696
}
16981697
}
16991698

1700-
impl Clean<Visibility> for ast::Visibility {
1701-
fn clean(&self, cx: &mut DocContext<'_>) -> Visibility {
1702-
match self.kind {
1703-
ast::VisibilityKind::Public => Visibility::Public,
1704-
ast::VisibilityKind::Inherited => Visibility::Inherited,
1705-
ast::VisibilityKind::Crate(_) => {
1706-
let krate = DefId::local(CRATE_DEF_INDEX);
1707-
Visibility::Restricted(krate)
1708-
}
1709-
ast::VisibilityKind::Restricted { id, .. } => {
1710-
let did = cx.enter_resolver(|r| r.local_def_id(id)).to_def_id();
1711-
Visibility::Restricted(did)
1712-
}
1713-
}
1714-
}
1715-
}
1716-
17171699
impl Clean<Visibility> for ty::Visibility {
17181700
fn clean(&self, _cx: &mut DocContext<'_>) -> Visibility {
17191701
match *self {
@@ -2015,6 +1997,7 @@ fn clean_extern_crate(
20151997
if let Some(items) = inline::try_inline(
20161998
cx,
20171999
cx.tcx.parent_module(krate.hir_id()).to_def_id(),
2000+
Some(krate.def_id.to_def_id()),
20182001
res,
20192002
name,
20202003
Some(attrs),
@@ -2070,7 +2053,7 @@ fn clean_use_statement(
20702053
// forcefully don't inline if this is not public or if the
20712054
// #[doc(no_inline)] attribute is present.
20722055
// Don't inline doc(hidden) imports so they can be stripped at a later stage.
2073-
let mut denied = !import.vis.node.is_pub()
2056+
let mut denied = (!import.vis.node.is_pub() && !cx.render_options.document_private)
20742057
|| pub_underscore
20752058
|| attrs.iter().any(|a| {
20762059
a.has_name(sym::doc)
@@ -2106,17 +2089,19 @@ fn clean_use_statement(
21062089
}
21072090
if !denied {
21082091
let mut visited = FxHashSet::default();
2092+
let import_def_id = import.def_id.to_def_id();
21092093

21102094
if let Some(mut items) = inline::try_inline(
21112095
cx,
21122096
cx.tcx.parent_module(import.hir_id()).to_def_id(),
2097+
Some(import_def_id),
21132098
path.res,
21142099
name,
21152100
Some(attrs),
21162101
&mut visited,
21172102
) {
21182103
items.push(Item::from_def_id_and_parts(
2119-
import.def_id.to_def_id(),
2104+
import_def_id,
21202105
None,
21212106
ImportItem(Import::new_simple(name, resolve_use_source(cx, path), false)),
21222107
cx,

src/librustdoc/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ extern crate tracing;
3131
// Dependencies listed in Cargo.toml do not need `extern crate`.
3232

3333
extern crate rustc_ast;
34-
extern crate rustc_ast_lowering;
3534
extern crate rustc_ast_pretty;
3635
extern crate rustc_attr;
3736
extern crate rustc_data_structures;

0 commit comments

Comments
 (0)