Skip to content

Commit

Permalink
auto merge of #13639 : alexcrichton/rust/rustdoc-tuple-fields, r=sfac…
Browse files Browse the repository at this point in the history
…kler

The fields of tuple structs recently gained the ability to have privacy
associated with them, but rustdoc was not updated accodingly. This moves the
struct field filtering to the rendering phase in order to preserve the ordering
of struct fields to allow tuple structs to have their private fields printed as
underscores.

Closes #13594
  • Loading branch information
bors committed Apr 20, 2014
2 parents 02081e7 + 9d546d6 commit 97dd726
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 23 deletions.
19 changes: 9 additions & 10 deletions src/librustdoc/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ pub enum Type {
Proc(~ClosureDecl),
/// extern "ABI" fn
BareFunction(~BareFunctionDecl),
Tuple(Vec<Type> ),
Tuple(Vec<Type>),
Vector(~Type),
FixedVector(~Type, ~str),
String,
Expand Down Expand Up @@ -713,25 +713,24 @@ impl Clean<Type> for ast::Ty {
}

#[deriving(Clone, Encodable, Decodable)]
pub struct StructField {
pub type_: Type,
pub enum StructField {
HiddenStructField,
TypedStructField(Type),
}

impl Clean<Item> for ast::StructField {
fn clean(&self) -> Item {
let (name, vis) = match self.node.kind {
ast::NamedField(id, vis) => (Some(id), Some(vis)),
_ => (None, None)
ast::NamedField(id, vis) => (Some(id), vis),
ast::UnnamedField(vis) => (None, vis)
};
Item {
name: name.clean(),
attrs: self.node.attrs.clean().move_iter().collect(),
source: self.span.clean(),
visibility: vis,
visibility: Some(vis),
id: self.node.id,
inner: StructFieldItem(StructField {
type_: self.node.ty.clean(),
}),
inner: StructFieldItem(TypedStructField(self.node.ty.clean())),
}
}
}
Expand Down Expand Up @@ -837,7 +836,7 @@ impl Clean<Item> for doctree::Variant {
#[deriving(Clone, Encodable, Decodable)]
pub enum VariantKind {
CLikeVariant,
TupleVariant(Vec<Type> ),
TupleVariant(Vec<Type>),
StructVariant(VariantStruct),
}

Expand Down
44 changes: 32 additions & 12 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1345,16 +1345,22 @@ fn item_struct(w: &mut Writer, it: &clean::Item,
Some(&s.generics),
s.struct_type,
s.fields.as_slice(),
s.fields_stripped,
"",
true));
try!(write!(w, "</pre>"));

try!(document(w, it));
let mut fields = s.fields.iter().filter(|f| {
match f.inner {
clean::StructFieldItem(clean::HiddenStructField) => false,
clean::StructFieldItem(clean::TypedStructField(..)) => true,
_ => false,
}
}).peekable();
match s.struct_type {
doctree::Plain if s.fields.len() > 0 => {
doctree::Plain if fields.peek().is_some() => {
try!(write!(w, "<h2 class='fields'>Fields</h2>\n<table>"));
for field in s.fields.iter() {
for field in fields {
try!(write!(w, "<tr><td id='structfield.{name}'>\
<code>{name}</code></td><td>",
name = field.name.get_ref().as_slice()));
Expand Down Expand Up @@ -1400,7 +1406,6 @@ fn item_enum(w: &mut Writer, it: &clean::Item, e: &clean::Enum) -> fmt::Result {
None,
s.struct_type,
s.fields.as_slice(),
s.fields_stripped,
" ",
false));
}
Expand Down Expand Up @@ -1429,9 +1434,18 @@ fn item_enum(w: &mut Writer, it: &clean::Item, e: &clean::Enum) -> fmt::Result {
clean::VariantItem(ref var) => {
match var.kind {
clean::StructVariant(ref s) => {
let mut fields = s.fields.iter().filter(|f| {
match f.inner {
clean::StructFieldItem(ref t) => match *t {
clean::HiddenStructField => false,
clean::TypedStructField(..) => true,
},
_ => false,
}
});
try!(write!(w, "<h3 class='fields'>Fields</h3>\n
<table>"));
for field in s.fields.iter() {
for field in fields {
try!(write!(w, "<tr><td \
id='variant.{v}.field.{f}'>\
<code>{f}</code></td><td>",
Expand Down Expand Up @@ -1460,7 +1474,6 @@ fn render_struct(w: &mut Writer, it: &clean::Item,
g: Option<&clean::Generics>,
ty: doctree::StructType,
fields: &[clean::Item],
fields_stripped: bool,
tab: &str,
structhead: bool) -> fmt::Result {
try!(write!(w, "{}{}{}",
Expand All @@ -1474,17 +1487,21 @@ fn render_struct(w: &mut Writer, it: &clean::Item,
match ty {
doctree::Plain => {
try!(write!(w, " \\{\n{}", tab));
let mut fields_stripped = false;
for field in fields.iter() {
match field.inner {
clean::StructFieldItem(ref ty) => {
clean::StructFieldItem(clean::HiddenStructField) => {
fields_stripped = true;
}
clean::StructFieldItem(clean::TypedStructField(ref ty)) => {
try!(write!(w, " {}{}: {},\n{}",
VisSpace(field.visibility),
field.name.get_ref().as_slice(),
ty.type_,
*ty,
tab));
}
_ => unreachable!()
}
_ => unreachable!(),
};
}

if fields_stripped {
Expand All @@ -1499,8 +1516,11 @@ fn render_struct(w: &mut Writer, it: &clean::Item,
try!(write!(w, ", "));
}
match field.inner {
clean::StructFieldItem(ref field) => {
try!(write!(w, "{}", field.type_));
clean::StructFieldItem(clean::HiddenStructField) => {
try!(write!(w, "_"))
}
clean::StructFieldItem(clean::TypedStructField(ref ty)) => {
try!(write!(w, "{}{}", VisSpace(field.visibility), *ty))
}
_ => unreachable!()
}
Expand Down
11 changes: 10 additions & 1 deletion src/librustdoc/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,21 @@ impl<'a> fold::DocFolder for Stripper<'a> {
}
}

clean::ViewItemItem(..) | clean::StructFieldItem(..) => {
clean::ViewItemItem(..) => {
if i.visibility != Some(ast::Public) {
return None
}
}

clean::StructFieldItem(..) => {
if i.visibility != Some(ast::Public) {
return Some(clean::Item {
inner: clean::StructFieldItem(clean::HiddenStructField),
..i
})
}
}

// handled below
clean::ModuleItem(..) => {}

Expand Down

0 comments on commit 97dd726

Please sign in to comment.