From ca664b0fdbeeea77bc9b3e166d2448d24b9312b7 Mon Sep 17 00:00:00 2001 From: rslint Date: Tue, 22 Dec 2020 22:18:54 -0500 Subject: [PATCH 1/2] Fix: render tuple fields in structs correctly --- src/librustdoc/html/render/mod.rs | 19 ++++++++++--------- src/test/rustdoc/tuple_struct_fields.rs | 9 +++++++++ 2 files changed, 19 insertions(+), 9 deletions(-) create mode 100644 src/test/rustdoc/tuple_struct_fields.rs diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index c21570d9716ef..2e323c3aaa028 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -3079,21 +3079,22 @@ fn item_struct( _ => None, }) .peekable(); - if let doctree::Plain = s.struct_type { + if let doctree::Plain | doctree::Tuple = s.struct_type { if fields.peek().is_some() { + let field_string = + if let doctree::Plain = s.struct_type { "Fields" } else { "Tuple Fields" }; write!( w, "

- Fields{}

", + {}{}", + field_string, document_non_exhaustive_header(it) ); document_non_exhaustive(w, it); - for (field, ty) in fields { - let id = cx.derive_id(format!( - "{}.{}", - ItemType::StructField, - field.name.as_ref().unwrap() - )); + for (idx, (field, ty)) in fields.enumerate() { + let field_name = + field.name.map_or_else(|| idx.to_string(), |sym| (*sym.as_str()).to_string()); + let id = cx.derive_id(format!("{}.{}", ItemType::StructField, field_name)); write!( w, "\ @@ -3102,7 +3103,7 @@ fn item_struct( ", item_type = ItemType::StructField, id = id, - name = field.name.as_ref().unwrap(), + name = field_name, ty = ty.print() ); document(w, cx, field, Some(it)); diff --git a/src/test/rustdoc/tuple_struct_fields.rs b/src/test/rustdoc/tuple_struct_fields.rs new file mode 100644 index 0000000000000..b09885b3eea68 --- /dev/null +++ b/src/test/rustdoc/tuple_struct_fields.rs @@ -0,0 +1,9 @@ +// @has tuple_struct_fields/struct.Tooople.html +// @has - //span '0: usize' +// @has - 'Wow! i love tuple fields!' + +pub struct Tooople( + /// Wow! i love tuple fields! + pub usize, + u8, +); From bf0fd2f649f1917f24a17c85a67b4a21ad330dc0 Mon Sep 17 00:00:00 2001 From: rslint Date: Tue, 22 Dec 2020 22:25:17 -0500 Subject: [PATCH 2/2] Chore: test for no rendering if the field is not public --- src/test/rustdoc/tuple_struct_fields.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/test/rustdoc/tuple_struct_fields.rs b/src/test/rustdoc/tuple_struct_fields.rs index b09885b3eea68..4506642c57cb5 100644 --- a/src/test/rustdoc/tuple_struct_fields.rs +++ b/src/test/rustdoc/tuple_struct_fields.rs @@ -1,9 +1,11 @@ // @has tuple_struct_fields/struct.Tooople.html // @has - //span '0: usize' // @has - 'Wow! i love tuple fields!' +// @!has - 'I should be invisible' pub struct Tooople( /// Wow! i love tuple fields! pub usize, + /// I should be invisible u8, );