From 9d546d60c190ffe4c5ac69c128b59d0a2bed021c Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sat, 19 Apr 2014 22:24:52 -0700 Subject: [PATCH] rustdoc: Display tuple structs correctly 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 --- src/librustdoc/clean.rs | 19 +++++++-------- src/librustdoc/html/render.rs | 44 +++++++++++++++++++++++++---------- src/librustdoc/passes.rs | 11 ++++++++- 3 files changed, 51 insertions(+), 23 deletions(-) diff --git a/src/librustdoc/clean.rs b/src/librustdoc/clean.rs index fcd6629096c99..3f1c99cffd640 100644 --- a/src/librustdoc/clean.rs +++ b/src/librustdoc/clean.rs @@ -652,7 +652,7 @@ pub enum Type { Proc(~ClosureDecl), /// extern "ABI" fn BareFunction(~BareFunctionDecl), - Tuple(Vec ), + Tuple(Vec), Vector(~Type), FixedVector(~Type, ~str), String, @@ -713,25 +713,24 @@ impl Clean for ast::Ty { } #[deriving(Clone, Encodable, Decodable)] -pub struct StructField { - pub type_: Type, +pub enum StructField { + HiddenStructField, + TypedStructField(Type), } impl Clean 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())), } } } @@ -837,7 +836,7 @@ impl Clean for doctree::Variant { #[deriving(Clone, Encodable, Decodable)] pub enum VariantKind { CLikeVariant, - TupleVariant(Vec ), + TupleVariant(Vec), StructVariant(VariantStruct), } diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index 4ccba7a1f4427..aff9a29c838ab 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -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, "")); 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, "

Fields

\n")); - for field in s.fields.iter() { + for field in fields { try!(write!(w, "
\ {name}", name = field.name.get_ref().as_slice())); @@ -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)); } @@ -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, "

Fields

\n ")); - for field in s.fields.iter() { + for field in fields { try!(write!(w, "
\ {f}", @@ -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, "{}{}{}", @@ -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 { @@ -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!() } diff --git a/src/librustdoc/passes.rs b/src/librustdoc/passes.rs index ed71dc27d9d30..9b6d04f2ca442 100644 --- a/src/librustdoc/passes.rs +++ b/src/librustdoc/passes.rs @@ -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(..) => {}