From 688e52201edb497453f33e098f23de136a0d64bf Mon Sep 17 00:00:00 2001 From: mitaa Date: Sat, 5 Mar 2016 11:20:34 +0100 Subject: [PATCH] Split out rustdoc pass to strip private imports --- src/librustdoc/lib.rs | 2 ++ src/librustdoc/passes.rs | 26 +++++++++++++++++++------- src/test/auxiliary/empty.rs | 9 +++++++++ src/test/rustdoc/issue-15347.rs | 2 +- src/test/rustdoc/issue-27104.rs | 20 ++++++++++++++++++++ 5 files changed, 51 insertions(+), 8 deletions(-) create mode 100644 src/test/auxiliary/empty.rs create mode 100644 src/test/rustdoc/issue-27104.rs diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index ffb15d157b066..5ddab8a286288 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -107,6 +107,8 @@ const PASSES: &'static [Pass] = &[ "concatenates all document attributes into one document attribute"), ("strip-private", passes::strip_private, "strips all private items from a crate which cannot be seen externally"), + ("strip-priv-imports", passes::strip_priv_imports, + "strips all private import statements (`use`, `extern crate`) from a crate"), ]; const DEFAULT_PASSES: &'static [&'static str] = &[ diff --git a/src/librustdoc/passes.rs b/src/librustdoc/passes.rs index 957957eaec6e5..1ce703940a62e 100644 --- a/src/librustdoc/passes.rs +++ b/src/librustdoc/passes.rs @@ -106,7 +106,7 @@ pub fn strip_private(mut krate: clean::Crate) -> plugins::PluginResult { retained: &mut retained, access_levels: &access_levels, }; - krate = stripper.fold_crate(krate); + krate = ImportStripper.fold_crate(stripper.fold_crate(krate)); } // strip all private implementations of traits @@ -144,12 +144,6 @@ impl<'a> fold::DocFolder for Stripper<'a> { } } - clean::ExternCrateItem(..) | clean::ImportItem(_) => { - if i.visibility != Some(hir::Public) { - return None - } - } - clean::StructFieldItem(..) => { if i.visibility != Some(hir::Public) { return Some(clean::Item { @@ -170,6 +164,9 @@ impl<'a> fold::DocFolder for Stripper<'a> { return None; } } + // handled in the `strip-priv-imports` pass + clean::ExternCrateItem(..) | clean::ImportItem(_) => {} + clean::DefaultImplItem(..) | clean::ImplItem(..) => {} // tymethods/macros have no control over privacy @@ -242,6 +239,21 @@ impl<'a> fold::DocFolder for ImplStripper<'a> { } } +// This stripper discards all private import statements (`use`, `extern crate`) +struct ImportStripper; +impl fold::DocFolder for ImportStripper { + fn fold_item(&mut self, i: Item) -> Option { + match i.inner { + clean::ExternCrateItem(..) | + clean::ImportItem(..) if i.visibility != Some(hir::Public) => None, + _ => self.fold_item_recur(i) + } + } +} + +pub fn strip_priv_imports(krate: clean::Crate) -> plugins::PluginResult { + (ImportStripper.fold_crate(krate), None) +} pub fn unindent_comments(krate: clean::Crate) -> plugins::PluginResult { struct CommentCleaner; diff --git a/src/test/auxiliary/empty.rs b/src/test/auxiliary/empty.rs new file mode 100644 index 0000000000000..3066947052294 --- /dev/null +++ b/src/test/auxiliary/empty.rs @@ -0,0 +1,9 @@ +// Copyright 2016 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. diff --git a/src/test/rustdoc/issue-15347.rs b/src/test/rustdoc/issue-15347.rs index 97c37bbc1eda0..266a30891941d 100644 --- a/src/test/rustdoc/issue-15347.rs +++ b/src/test/rustdoc/issue-15347.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -// compile-flags:--no-defaults --passes "collapse-docs" --passes "unindent-comments" +// compile-flags:--no-defaults --passes collapse-docs --passes unindent-comments // @has issue_15347/fn.foo.html #[doc(hidden)] diff --git a/src/test/rustdoc/issue-27104.rs b/src/test/rustdoc/issue-27104.rs new file mode 100644 index 0000000000000..5fa093d8f299b --- /dev/null +++ b/src/test/rustdoc/issue-27104.rs @@ -0,0 +1,20 @@ +// Copyright 2016 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// compile-flags:--no-defaults --passes strip-priv-imports +// aux-build:empty.rs +// ignore-cross-compile + +// @has issue_27104/index.html +// @!has - 'extern crate std' +// @!has - 'use std::prelude::' + +// @has - 'pub extern crate empty' +pub extern crate empty;