Skip to content

Commit 3daded0

Browse files
committed
rustdoc: Add support for pub(restricted)
1 parent fe63e47 commit 3daded0

File tree

3 files changed

+71
-5
lines changed

3 files changed

+71
-5
lines changed

src/librustdoc/clean/mod.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub use self::ItemEnum::*;
1717
pub use self::TyParamBound::*;
1818
pub use self::SelfTy::*;
1919
pub use self::FunctionRetTy::*;
20-
pub use self::Visibility::*;
20+
pub use self::Visibility::{Public, Inherited};
2121

2222
use syntax;
2323
use rustc_target::spec::abi::Abi;
@@ -2976,11 +2976,22 @@ impl<'tcx> Clean<Item> for ty::FieldDef {
29762976
pub enum Visibility {
29772977
Public,
29782978
Inherited,
2979+
Crate,
2980+
Restricted(DefId, Path),
29792981
}
29802982

29812983
impl Clean<Option<Visibility>> for hir::Visibility {
2982-
fn clean(&self, _: &DocContext) -> Option<Visibility> {
2983-
Some(if *self == hir::Visibility::Public { Public } else { Inherited })
2984+
fn clean(&self, cx: &DocContext) -> Option<Visibility> {
2985+
Some(match *self {
2986+
hir::Visibility::Public => Visibility::Public,
2987+
hir::Visibility::Inherited => Visibility::Inherited,
2988+
hir::Visibility::Crate => Visibility::Crate,
2989+
hir::Visibility::Restricted { ref path, .. } => {
2990+
let path = path.clean(cx);
2991+
let did = register_def(cx, path.def);
2992+
Visibility::Restricted(did, path)
2993+
}
2994+
})
29842995
}
29852996
}
29862997

src/librustdoc/html/format.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -927,8 +927,19 @@ impl<'a> fmt::Display for Method<'a> {
927927
impl<'a> fmt::Display for VisSpace<'a> {
928928
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
929929
match *self.get() {
930-
Some(clean::Public) => write!(f, "pub "),
931-
Some(clean::Inherited) | None => Ok(())
930+
Some(clean::Public) => f.write_str("pub "),
931+
Some(clean::Inherited) | None => Ok(()),
932+
Some(clean::Visibility::Crate) => write!(f, "pub(crate) "),
933+
Some(clean::Visibility::Restricted(did, ref path)) => {
934+
f.write_str("pub(")?;
935+
if path.segments.len() != 1
936+
|| (path.segments[0].name != "self" && path.segments[0].name != "super")
937+
{
938+
f.write_str("in ")?;
939+
}
940+
resolved_path(f, did, path, true, false)?;
941+
f.write_str(") ")
942+
}
932943
}
933944
}
934945
}

src/test/rustdoc/pub-restricted.rs

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// ignore-tidy-linelength
12+
13+
// compile-flags: --document-private-items
14+
15+
#![feature(crate_visibility_modifier)]
16+
17+
#![crate_name = "foo"]
18+
19+
// @has 'foo/struct.FooPublic.html' '//pre' 'pub struct FooPublic'
20+
pub struct FooPublic;
21+
// @has 'foo/struct.FooJustCrate.html' '//pre' 'pub(crate) struct FooJustCrate'
22+
crate struct FooJustCrate;
23+
// @has 'foo/struct.FooPubCrate.html' '//pre' 'pub(crate) struct FooPubCrate'
24+
pub(crate) struct FooPubCrate;
25+
// @has 'foo/struct.FooSelf.html' '//pre' 'pub(self) struct FooSelf'
26+
pub(self) struct FooSelf;
27+
// @has 'foo/struct.FooInSelf.html' '//pre' 'pub(self) struct FooInSelf'
28+
pub(in self) struct FooInSelf;
29+
mod a {
30+
// @has 'foo/a/struct.FooSuper.html' '//pre' 'pub(super) struct FooSuper'
31+
pub(super) struct FooSuper;
32+
// @has 'foo/a/struct.FooInSuper.html' '//pre' 'pub(super) struct FooInSuper'
33+
pub(in super) struct FooInSuper;
34+
// @has 'foo/a/struct.FooInA.html' '//pre' 'pub(in a) struct FooInA'
35+
pub(in a) struct FooInA;
36+
mod b {
37+
// @has 'foo/a/b/struct.FooInSelfSuperB.html' '//pre' 'pub(in self::super::b) struct FooInSelfSuperB'
38+
pub(in self::super::b) struct FooInSelfSuperB;
39+
// @has 'foo/a/b/struct.FooInSuperSuper.html' '//pre' 'pub(in super::super) struct FooInSuperSuper'
40+
pub(in super::super) struct FooInSuperSuper;
41+
// @has 'foo/a/b/struct.FooInAB.html' '//pre' 'pub(in a::b) struct FooInAB'
42+
pub(in a::b) struct FooInAB;
43+
}
44+
}

0 commit comments

Comments
 (0)