2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-09-21 20:07:17 -05:00
|
|
|
//! Prune things that are private
|
|
|
|
|
2013-05-17 17:28:44 -05:00
|
|
|
use core::prelude::*;
|
|
|
|
|
2013-03-25 16:53:19 -05:00
|
|
|
use extract;
|
|
|
|
use syntax::ast;
|
|
|
|
use syntax::ast_map;
|
2012-12-23 16:41:37 -06:00
|
|
|
use astsrv;
|
|
|
|
use doc;
|
2012-12-05 17:06:54 -06:00
|
|
|
use fold::Fold;
|
2012-12-23 16:41:37 -06:00
|
|
|
use fold;
|
2013-01-08 21:37:25 -06:00
|
|
|
use pass::Pass;
|
2012-12-23 16:41:37 -06:00
|
|
|
|
|
|
|
use core::util;
|
2012-12-05 17:06:54 -06:00
|
|
|
|
2013-01-30 16:10:03 -06:00
|
|
|
pub fn mk_pass() -> Pass {
|
2013-01-08 16:00:45 -06:00
|
|
|
Pass {
|
2012-09-21 20:07:17 -05:00
|
|
|
name: ~"prune_private",
|
|
|
|
f: run
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-30 21:32:36 -06:00
|
|
|
pub fn run(srv: astsrv::Srv, doc: doc::Doc) -> doc::Doc {
|
2013-03-25 16:53:19 -05:00
|
|
|
// First strip private methods out of impls
|
|
|
|
let fold = Fold {
|
|
|
|
ctxt: srv.clone(),
|
|
|
|
fold_impl: fold_impl,
|
|
|
|
.. fold::default_any_fold(srv.clone())
|
|
|
|
};
|
|
|
|
let doc = (fold.fold_doc)(&fold, doc);
|
|
|
|
|
|
|
|
// Then strip private items and empty impls
|
2012-12-05 17:06:54 -06:00
|
|
|
let fold = Fold {
|
2013-01-31 22:24:03 -06:00
|
|
|
ctxt: srv.clone(),
|
2012-09-21 20:07:17 -05:00
|
|
|
fold_mod: fold_mod,
|
2012-12-05 17:06:54 -06:00
|
|
|
.. fold::default_any_fold(srv)
|
|
|
|
};
|
2013-03-25 16:53:19 -05:00
|
|
|
let doc = (fold.fold_doc)(&fold, doc);
|
|
|
|
|
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_impl(
|
|
|
|
fold: &fold::Fold<astsrv::Srv>,
|
|
|
|
doc: doc::ImplDoc
|
|
|
|
) -> doc::ImplDoc {
|
|
|
|
let doc = fold::default_seq_fold_impl(fold, doc);
|
|
|
|
|
|
|
|
do astsrv::exec(fold.ctxt.clone()) |ctxt| {
|
2013-05-12 15:50:57 -05:00
|
|
|
match ctxt.ast_map.get_copy(&doc.item.id) {
|
2013-03-25 16:53:19 -05:00
|
|
|
ast_map::node_item(item, _) => {
|
|
|
|
match item.node {
|
|
|
|
ast::item_impl(_, None, _, ref methods) => {
|
|
|
|
// Associated impls have complex rules for method visibility
|
|
|
|
strip_priv_methods(copy doc, *methods, item.vis)
|
|
|
|
}
|
|
|
|
ast::item_impl(_, Some(_), _ ,_) => {
|
|
|
|
// Trait impls don't
|
|
|
|
copy doc
|
|
|
|
}
|
|
|
|
_ => fail!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => fail!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn strip_priv_methods(
|
|
|
|
doc: doc::ImplDoc,
|
|
|
|
methods: &[@ast::method],
|
|
|
|
item_vis: ast::visibility
|
|
|
|
) -> doc::ImplDoc {
|
|
|
|
let methods = do (&doc.methods).filtered |method| {
|
|
|
|
let ast_method = do methods.find |m| {
|
|
|
|
extract::to_str(m.ident) == method.name
|
|
|
|
};
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(ast_method.is_some());
|
2013-03-25 16:53:19 -05:00
|
|
|
let ast_method = ast_method.unwrap();
|
|
|
|
match ast_method.vis {
|
|
|
|
ast::public => true,
|
|
|
|
ast::private => false,
|
|
|
|
ast::inherited => item_vis == ast::public
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
doc::ImplDoc {
|
|
|
|
methods: methods,
|
|
|
|
.. doc
|
|
|
|
}
|
2012-09-21 20:07:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_mod(
|
2012-11-19 20:48:46 -06:00
|
|
|
fold: &fold::Fold<astsrv::Srv>,
|
2013-01-30 21:32:36 -06:00
|
|
|
doc: doc::ModDoc
|
2012-09-21 20:07:17 -05:00
|
|
|
) -> doc::ModDoc {
|
|
|
|
let doc = fold::default_any_fold_mod(fold, doc);
|
|
|
|
|
2013-01-29 21:45:53 -06:00
|
|
|
doc::ModDoc {
|
2013-01-10 23:18:12 -06:00
|
|
|
items: doc.items.filtered(|ItemTag| {
|
2013-03-25 16:53:19 -05:00
|
|
|
match ItemTag {
|
|
|
|
&doc::ImplTag(ref doc) => {
|
|
|
|
if doc.trait_types.is_empty() {
|
|
|
|
// This is an associated impl. We have already pruned the
|
|
|
|
// non-visible methods. If there are any left then
|
|
|
|
// retain the impl, otherwise throw it away
|
|
|
|
!doc.methods.is_empty()
|
|
|
|
} else {
|
|
|
|
// This is a trait implementation, make it visible
|
2013-04-02 11:42:08 -05:00
|
|
|
// NB: This is not quite right since this could be an impl
|
2013-03-25 16:53:19 -05:00
|
|
|
// of a private trait. We can't know that without running
|
|
|
|
// resolve though.
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
is_visible(fold.ctxt.clone(), ItemTag.item())
|
|
|
|
}
|
|
|
|
}
|
2013-01-07 23:15:25 -06:00
|
|
|
}),
|
2013-01-29 21:45:53 -06:00
|
|
|
.. doc
|
|
|
|
}
|
2012-09-21 20:07:17 -05:00
|
|
|
}
|
|
|
|
|
2013-01-30 21:32:36 -06:00
|
|
|
fn is_visible(srv: astsrv::Srv, doc: doc::ItemDoc) -> bool {
|
2012-09-21 20:07:17 -05:00
|
|
|
let id = doc.id;
|
|
|
|
|
|
|
|
do astsrv::exec(srv) |ctxt| {
|
2013-05-12 15:50:57 -05:00
|
|
|
match ctxt.ast_map.get_copy(&id) {
|
2012-09-21 20:07:17 -05:00
|
|
|
ast_map::node_item(item, _) => {
|
2013-03-25 16:53:19 -05:00
|
|
|
match &item.node {
|
|
|
|
&ast::item_impl(*) => {
|
|
|
|
// Impls handled elsewhere
|
|
|
|
fail!()
|
2013-03-16 18:17:56 -05:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// Otherwise just look at the visibility
|
|
|
|
item.vis == ast::public
|
|
|
|
}
|
|
|
|
}
|
2012-09-21 20:07:17 -05:00
|
|
|
}
|
2012-12-23 16:41:37 -06:00
|
|
|
_ => util::unreachable()
|
2012-09-21 20:07:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-25 16:53:19 -05:00
|
|
|
|
2012-09-21 20:07:17 -05:00
|
|
|
#[cfg(test)]
|
2013-04-15 10:09:55 -05:00
|
|
|
mod test {
|
2012-12-29 19:38:20 -06:00
|
|
|
use astsrv;
|
|
|
|
use doc;
|
|
|
|
use extract;
|
2013-03-25 16:53:19 -05:00
|
|
|
use tystr_pass;
|
2013-01-08 21:37:25 -06:00
|
|
|
use prune_private_pass::run;
|
2013-04-15 10:09:55 -05:00
|
|
|
use core::vec;
|
2012-12-29 19:38:20 -06:00
|
|
|
|
2013-04-15 10:09:55 -05:00
|
|
|
fn mk_doc(source: ~str) -> doc::Doc {
|
2013-01-30 15:14:35 -06:00
|
|
|
do astsrv::from_str(copy source) |srv| {
|
2013-01-31 22:24:03 -06:00
|
|
|
let doc = extract::from_srv(srv.clone(), ~"");
|
2013-03-25 16:53:19 -05:00
|
|
|
let doc = tystr_pass::run(srv.clone(), doc);
|
2013-01-31 22:24:03 -06:00
|
|
|
run(srv.clone(), doc)
|
2012-09-21 20:07:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-15 10:09:55 -05:00
|
|
|
#[test]
|
|
|
|
fn should_prune_items_without_pub_modifier() {
|
|
|
|
let doc = mk_doc(~"mod a { }");
|
|
|
|
assert!(vec::is_empty(doc.cratemod().mods()));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_not_prune_trait_impls() {
|
|
|
|
// Impls are more complicated
|
|
|
|
let doc = mk_doc(
|
|
|
|
~" \
|
|
|
|
trait Foo { } \
|
|
|
|
impl Foo for int { } \
|
|
|
|
");
|
|
|
|
assert!(!doc.cratemod().impls().is_empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_prune_associated_methods_without_vis_modifier_on_impls_without_vis_modifier() {
|
|
|
|
let doc = mk_doc(
|
|
|
|
~"impl Foo {\
|
|
|
|
pub fn bar() { }\
|
|
|
|
fn baz() { }\
|
|
|
|
}");
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(doc.cratemod().impls()[0].methods.len(), 1);
|
2013-04-15 10:09:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_prune_priv_associated_methods_on_impls_without_vis_modifier() {
|
|
|
|
let doc = mk_doc(
|
|
|
|
~"impl Foo {\
|
|
|
|
pub fn bar() { }\
|
|
|
|
priv fn baz() { }\
|
|
|
|
}");
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(doc.cratemod().impls()[0].methods.len(), 1);
|
2013-04-15 10:09:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_prune_priv_associated_methods_on_pub_impls() {
|
|
|
|
let doc = mk_doc(
|
2013-05-31 17:17:22 -05:00
|
|
|
~"impl Foo {\
|
|
|
|
pub fn bar() { }\
|
2013-04-15 10:09:55 -05:00
|
|
|
priv fn baz() { }\
|
|
|
|
}");
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(doc.cratemod().impls()[0].methods.len(), 1);
|
2013-04-15 10:09:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_prune_associated_methods_without_vis_modifier_on_priv_impls() {
|
|
|
|
let doc = mk_doc(
|
2013-05-31 17:17:22 -05:00
|
|
|
~"impl Foo {\
|
2013-04-15 10:09:55 -05:00
|
|
|
pub fn bar() { }\
|
|
|
|
fn baz() { }\
|
|
|
|
}");
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(doc.cratemod().impls()[0].methods.len(), 1);
|
2013-04-15 10:09:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_prune_priv_associated_methods_on_priv_impls() {
|
|
|
|
let doc = mk_doc(
|
2013-05-31 17:17:22 -05:00
|
|
|
~"impl Foo {\
|
2013-04-15 10:09:55 -05:00
|
|
|
pub fn bar() { }\
|
|
|
|
priv fn baz() { }\
|
|
|
|
}");
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(doc.cratemod().impls()[0].methods.len(), 1);
|
2013-04-15 10:09:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_prune_associated_impls_with_no_pub_methods() {
|
|
|
|
let doc = mk_doc(
|
2013-05-31 17:17:22 -05:00
|
|
|
~"impl Foo {\
|
2013-04-15 10:09:55 -05:00
|
|
|
fn baz() { }\
|
|
|
|
}");
|
|
|
|
assert!(doc.cratemod().impls().is_empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_not_prune_associated_impls_with_pub_methods() {
|
|
|
|
let doc = mk_doc(
|
|
|
|
~" \
|
|
|
|
impl Foo { pub fn bar() { } } \
|
|
|
|
");
|
|
|
|
assert!(!doc.cratemod().impls().is_empty());
|
|
|
|
}
|
|
|
|
}
|