Skip to content

Commit 9da4eac

Browse files
committed
auto merge of #11391 : alexcrichton/rust/rustdoc-inline, r=brson
If a reexport comes from a non-public module, then the documentation for the reexport will be inlined into the module that exports it, but if the reexport is targeted at a public type (like the prelude), then it is not inlined but rather hyperlinked.
2 parents a121f7b + 3425901 commit 9da4eac

File tree

6 files changed

+289
-149
lines changed

6 files changed

+289
-149
lines changed

src/librustc/driver/driver.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ pub fn phase_2_configure_and_expand(sess: Session,
209209
pub struct CrateAnalysis {
210210
exp_map2: middle::resolve::ExportMap2,
211211
exported_items: middle::privacy::ExportedItems,
212+
public_items: middle::privacy::PublicItems,
212213
ty_cx: ty::ctxt,
213214
maps: astencode::Maps,
214215
reachable: @RefCell<HashSet<ast::NodeId>>
@@ -268,9 +269,10 @@ pub fn phase_3_run_analysis_passes(sess: Session,
268269
method_map, ty_cx));
269270

270271
let maps = (external_exports, last_private_map);
271-
let exported_items = time(time_passes, "privacy checking", maps, |(a, b)|
272-
middle::privacy::check_crate(ty_cx, &method_map, &exp_map2,
273-
a, b, crate));
272+
let (exported_items, public_items) =
273+
time(time_passes, "privacy checking", maps, |(a, b)|
274+
middle::privacy::check_crate(ty_cx, &method_map, &exp_map2,
275+
a, b, crate));
274276

275277
time(time_passes, "effect checking", (), |_|
276278
middle::effect::check_crate(ty_cx, method_map, crate));
@@ -322,6 +324,7 @@ pub fn phase_3_run_analysis_passes(sess: Session,
322324
exp_map2: exp_map2,
323325
ty_cx: ty_cx,
324326
exported_items: exported_items,
327+
public_items: public_items,
325328
maps: astencode::Maps {
326329
root_map: root_map,
327330
method_map: method_map,

src/librustc/middle/privacy.rs

+26-5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ type Context<'a> = (&'a method_map, &'a resolve::ExportMap2);
3535
/// A set of AST nodes exported by the crate.
3636
pub type ExportedItems = HashSet<ast::NodeId>;
3737

38+
/// A set of AST nodes that are fully public in the crate. This map is used for
39+
/// documentation purposes (reexporting a private struct inlines the doc,
40+
/// reexporting a public struct doesn't inline the doc).
41+
pub type PublicItems = HashSet<ast::NodeId>;
42+
3843
////////////////////////////////////////////////////////////////////////////////
3944
/// The parent visitor, used to determine what's the parent of what (node-wise)
4045
////////////////////////////////////////////////////////////////////////////////
@@ -165,6 +170,12 @@ struct EmbargoVisitor<'a> {
165170
// means that the destination of the reexport is exported, and hence the
166171
// destination must also be exported.
167172
reexports: HashSet<ast::NodeId>,
173+
174+
// These two fields are closely related to one another in that they are only
175+
// used for generation of the 'PublicItems' set, not for privacy checking at
176+
// all
177+
public_items: PublicItems,
178+
prev_public: bool,
168179
}
169180

170181
impl<'a> EmbargoVisitor<'a> {
@@ -186,7 +197,13 @@ impl<'a> EmbargoVisitor<'a> {
186197

187198
impl<'a> Visitor<()> for EmbargoVisitor<'a> {
188199
fn visit_item(&mut self, item: &ast::item, _: ()) {
189-
let orig_all_pub = self.prev_exported;
200+
let orig_all_pub = self.prev_public;
201+
self.prev_public = orig_all_pub && item.vis == ast::public;
202+
if self.prev_public {
203+
self.public_items.insert(item.id);
204+
}
205+
206+
let orig_all_exported = self.prev_exported;
190207
match item.node {
191208
// impls/extern blocks do not break the "public chain" because they
192209
// cannot have visibility qualifiers on them anyway
@@ -202,7 +219,7 @@ impl<'a> Visitor<()> for EmbargoVisitor<'a> {
202219
// `pub` is explicitly listed.
203220
_ => {
204221
self.prev_exported =
205-
(orig_all_pub && item.vis == ast::public) ||
222+
(orig_all_exported && item.vis == ast::public) ||
206223
self.reexports.contains(&item.id);
207224
}
208225
}
@@ -304,7 +321,8 @@ impl<'a> Visitor<()> for EmbargoVisitor<'a> {
304321

305322
visit::walk_item(self, item, ());
306323

307-
self.prev_exported = orig_all_pub;
324+
self.prev_exported = orig_all_exported;
325+
self.prev_public = orig_all_pub;
308326
}
309327

310328
fn visit_foreign_item(&mut self, a: &ast::foreign_item, _: ()) {
@@ -1002,7 +1020,7 @@ pub fn check_crate(tcx: ty::ctxt,
10021020
exp_map2: &resolve::ExportMap2,
10031021
external_exports: resolve::ExternalExports,
10041022
last_private_map: resolve::LastPrivateMap,
1005-
crate: &ast::Crate) -> ExportedItems {
1023+
crate: &ast::Crate) -> (ExportedItems, PublicItems) {
10061024
// Figure out who everyone's parent is
10071025
let mut visitor = ParentVisitor {
10081026
parents: HashMap::new(),
@@ -1038,9 +1056,11 @@ pub fn check_crate(tcx: ty::ctxt,
10381056
let mut visitor = EmbargoVisitor {
10391057
tcx: tcx,
10401058
exported_items: HashSet::new(),
1059+
public_items: HashSet::new(),
10411060
reexports: HashSet::new(),
10421061
exp_map2: exp_map2,
10431062
prev_exported: true,
1063+
prev_public: true,
10441064
};
10451065
loop {
10461066
let before = visitor.exported_items.len();
@@ -1050,5 +1070,6 @@ pub fn check_crate(tcx: ty::ctxt,
10501070
}
10511071
}
10521072

1053-
return visitor.exported_items;
1073+
let EmbargoVisitor { exported_items, public_items, .. } = visitor;
1074+
return (exported_items, public_items);
10541075
}

src/librustdoc/clean.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub struct Crate {
7272
externs: HashMap<ast::CrateNum, ExternalCrate>,
7373
}
7474

75-
impl Clean<Crate> for visit_ast::RustdocVisitor {
75+
impl<'a> Clean<Crate> for visit_ast::RustdocVisitor<'a> {
7676
fn clean(&self) -> Crate {
7777
use syntax::attr::find_crateid;
7878
let cx = local_data::get(super::ctxtkey, |x| *x.unwrap());

src/librustdoc/core.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pub struct DocContext {
3434

3535
pub struct CrateAnalysis {
3636
exported_items: privacy::ExportedItems,
37+
public_items: privacy::PublicItems,
3738
}
3839

3940
/// Parses, resolves, and typechecks the given crate
@@ -75,21 +76,27 @@ fn get_ast_and_resolve(cpath: &Path,
7576
let crate = phase_1_parse_input(sess, cfg.clone(), &input);
7677
let (crate, ast_map) = phase_2_configure_and_expand(sess, cfg, crate);
7778
let driver::driver::CrateAnalysis {
78-
exported_items, ty_cx, ..
79+
exported_items, public_items, ty_cx, ..
7980
} = phase_3_run_analysis_passes(sess, &crate, ast_map);
8081

8182
debug!("crate: {:?}", crate);
8283
return (DocContext { crate: crate, tycx: Some(ty_cx), sess: sess },
83-
CrateAnalysis { exported_items: exported_items });
84+
CrateAnalysis {
85+
exported_items: exported_items,
86+
public_items: public_items,
87+
});
8488
}
8589

8690
pub fn run_core (libs: HashSet<Path>, cfgs: ~[~str], path: &Path) -> (clean::Crate, CrateAnalysis) {
8791
let (ctxt, analysis) = get_ast_and_resolve(path, libs, cfgs);
8892
let ctxt = @ctxt;
8993
local_data::set(super::ctxtkey, ctxt);
9094

91-
let mut v = RustdocVisitor::new();
92-
v.visit(&ctxt.crate);
95+
let crate = {
96+
let mut v = RustdocVisitor::new(ctxt, Some(&analysis));
97+
v.visit(&ctxt.crate);
98+
v.clean()
99+
};
93100

94-
(v.clean(), analysis)
101+
(crate, analysis)
95102
}

src/librustdoc/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ pub fn run(input: &str, matches: &getopts::Matches) -> int {
6767
};
6868
local_data::set(super::ctxtkey, ctx);
6969

70-
let mut v = RustdocVisitor::new();
70+
let mut v = RustdocVisitor::new(ctx, None);
7171
v.visit(&ctx.crate);
7272
let crate = v.clean();
7373
let (crate, _) = passes::unindent_comments(crate);

0 commit comments

Comments
 (0)