Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline reexports in rustdoc #11391

Merged
merged 1 commit into from
Jan 8, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/librustc/driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ pub fn phase_2_configure_and_expand(sess: Session,
pub struct CrateAnalysis {
exp_map2: middle::resolve::ExportMap2,
exported_items: middle::privacy::ExportedItems,
public_items: middle::privacy::PublicItems,
ty_cx: ty::ctxt,
maps: astencode::Maps,
reachable: @RefCell<HashSet<ast::NodeId>>
Expand Down Expand Up @@ -268,9 +269,10 @@ pub fn phase_3_run_analysis_passes(sess: Session,
method_map, ty_cx));

let maps = (external_exports, last_private_map);
let exported_items = time(time_passes, "privacy checking", maps, |(a, b)|
middle::privacy::check_crate(ty_cx, &method_map, &exp_map2,
a, b, crate));
let (exported_items, public_items) =
time(time_passes, "privacy checking", maps, |(a, b)|
middle::privacy::check_crate(ty_cx, &method_map, &exp_map2,
a, b, crate));

time(time_passes, "effect checking", (), |_|
middle::effect::check_crate(ty_cx, method_map, crate));
Expand Down Expand Up @@ -322,6 +324,7 @@ pub fn phase_3_run_analysis_passes(sess: Session,
exp_map2: exp_map2,
ty_cx: ty_cx,
exported_items: exported_items,
public_items: public_items,
maps: astencode::Maps {
root_map: root_map,
method_map: method_map,
Expand Down
31 changes: 26 additions & 5 deletions src/librustc/middle/privacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ type Context<'a> = (&'a method_map, &'a resolve::ExportMap2);
/// A set of AST nodes exported by the crate.
pub type ExportedItems = HashSet<ast::NodeId>;

/// A set of AST nodes that are fully public in the crate. This map is used for
/// documentation purposes (reexporting a private struct inlines the doc,
/// reexporting a public struct doesn't inline the doc).
pub type PublicItems = HashSet<ast::NodeId>;

////////////////////////////////////////////////////////////////////////////////
/// The parent visitor, used to determine what's the parent of what (node-wise)
////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -165,6 +170,12 @@ struct EmbargoVisitor<'a> {
// means that the destination of the reexport is exported, and hence the
// destination must also be exported.
reexports: HashSet<ast::NodeId>,

// These two fields are closely related to one another in that they are only
// used for generation of the 'PublicItems' set, not for privacy checking at
// all
public_items: PublicItems,
prev_public: bool,
}

impl<'a> EmbargoVisitor<'a> {
Expand All @@ -186,7 +197,13 @@ impl<'a> EmbargoVisitor<'a> {

impl<'a> Visitor<()> for EmbargoVisitor<'a> {
fn visit_item(&mut self, item: &ast::item, _: ()) {
let orig_all_pub = self.prev_exported;
let orig_all_pub = self.prev_public;
self.prev_public = orig_all_pub && item.vis == ast::public;
if self.prev_public {
self.public_items.insert(item.id);
}

let orig_all_exported = self.prev_exported;
match item.node {
// impls/extern blocks do not break the "public chain" because they
// cannot have visibility qualifiers on them anyway
Expand All @@ -202,7 +219,7 @@ impl<'a> Visitor<()> for EmbargoVisitor<'a> {
// `pub` is explicitly listed.
_ => {
self.prev_exported =
(orig_all_pub && item.vis == ast::public) ||
(orig_all_exported && item.vis == ast::public) ||
self.reexports.contains(&item.id);
}
}
Expand Down Expand Up @@ -304,7 +321,8 @@ impl<'a> Visitor<()> for EmbargoVisitor<'a> {

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

self.prev_exported = orig_all_pub;
self.prev_exported = orig_all_exported;
self.prev_public = orig_all_pub;
}

fn visit_foreign_item(&mut self, a: &ast::foreign_item, _: ()) {
Expand Down Expand Up @@ -1002,7 +1020,7 @@ pub fn check_crate(tcx: ty::ctxt,
exp_map2: &resolve::ExportMap2,
external_exports: resolve::ExternalExports,
last_private_map: resolve::LastPrivateMap,
crate: &ast::Crate) -> ExportedItems {
crate: &ast::Crate) -> (ExportedItems, PublicItems) {
// Figure out who everyone's parent is
let mut visitor = ParentVisitor {
parents: HashMap::new(),
Expand Down Expand Up @@ -1038,9 +1056,11 @@ pub fn check_crate(tcx: ty::ctxt,
let mut visitor = EmbargoVisitor {
tcx: tcx,
exported_items: HashSet::new(),
public_items: HashSet::new(),
reexports: HashSet::new(),
exp_map2: exp_map2,
prev_exported: true,
prev_public: true,
};
loop {
let before = visitor.exported_items.len();
Expand All @@ -1050,5 +1070,6 @@ pub fn check_crate(tcx: ty::ctxt,
}
}

return visitor.exported_items;
let EmbargoVisitor { exported_items, public_items, .. } = visitor;
return (exported_items, public_items);
}
2 changes: 1 addition & 1 deletion src/librustdoc/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub struct Crate {
externs: HashMap<ast::CrateNum, ExternalCrate>,
}

impl Clean<Crate> for visit_ast::RustdocVisitor {
impl<'a> Clean<Crate> for visit_ast::RustdocVisitor<'a> {
fn clean(&self) -> Crate {
use syntax::attr::find_crateid;
let cx = local_data::get(super::ctxtkey, |x| *x.unwrap());
Expand Down
17 changes: 12 additions & 5 deletions src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub struct DocContext {

pub struct CrateAnalysis {
exported_items: privacy::ExportedItems,
public_items: privacy::PublicItems,
}

/// Parses, resolves, and typechecks the given crate
Expand Down Expand Up @@ -75,21 +76,27 @@ fn get_ast_and_resolve(cpath: &Path,
let crate = phase_1_parse_input(sess, cfg.clone(), &input);
let (crate, ast_map) = phase_2_configure_and_expand(sess, cfg, crate);
let driver::driver::CrateAnalysis {
exported_items, ty_cx, ..
exported_items, public_items, ty_cx, ..
} = phase_3_run_analysis_passes(sess, &crate, ast_map);

debug!("crate: {:?}", crate);
return (DocContext { crate: crate, tycx: Some(ty_cx), sess: sess },
CrateAnalysis { exported_items: exported_items });
CrateAnalysis {
exported_items: exported_items,
public_items: public_items,
});
}

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

let mut v = RustdocVisitor::new();
v.visit(&ctxt.crate);
let crate = {
let mut v = RustdocVisitor::new(ctxt, Some(&analysis));
v.visit(&ctxt.crate);
v.clean()
};

(v.clean(), analysis)
(crate, analysis)
}
2 changes: 1 addition & 1 deletion src/librustdoc/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub fn run(input: &str, matches: &getopts::Matches) -> int {
};
local_data::set(super::ctxtkey, ctx);

let mut v = RustdocVisitor::new();
let mut v = RustdocVisitor::new(ctx, None);
v.visit(&ctx.crate);
let crate = v.clean();
let (crate, _) = passes::unindent_comments(crate);
Expand Down
Loading