From 147d173d02ea030af23fad07c7e99b2900e710f2 Mon Sep 17 00:00:00 2001 From: Oliver Middleton Date: Wed, 8 Feb 2017 21:15:20 +0000 Subject: [PATCH 01/11] rustdoc: Show attributes on all item types Currently attributes are only shown for structs, unions and enums but they should be shown for all items. For example it is useful to know if a function is `#[no_mangle]`. --- src/librustdoc/html/render.rs | 20 +++++++++++++++----- src/test/rustdoc/attributes.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 src/test/rustdoc/attributes.rs diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index 40eb7e5ab78c3..fe397e980bdcc 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -1936,7 +1936,9 @@ impl<'a> fmt::Display for Initializer<'a> { fn item_constant(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item, c: &clean::Constant) -> fmt::Result { - write!(w, "
{vis}const \
+    write!(w, "
")?;
+    render_attributes(w, it)?;
+    write!(w, "{vis}const \
                {name}: {typ}{init}
", vis = VisSpace(&it.visibility), name = it.name.as_ref().unwrap(), @@ -1947,7 +1949,9 @@ fn item_constant(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item, fn item_static(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item, s: &clean::Static) -> fmt::Result { - write!(w, "
{vis}static {mutability}\
+    write!(w, "
")?;
+    render_attributes(w, it)?;
+    write!(w, "{vis}static {mutability}\
                {name}: {typ}{init}
", vis = VisSpace(&it.visibility), mutability = MutableSpace(s.mutability), @@ -1971,7 +1975,9 @@ fn item_function(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item, AbiSpace(f.abi), it.name.as_ref().unwrap(), f.generics).len(); - write!(w, "
{vis}{constness}{unsafety}{abi}fn \
+    write!(w, "
")?;
+    render_attributes(w, it)?;
+    write!(w, "{vis}{constness}{unsafety}{abi}fn \
                {name}{generics}{decl}{where_clause}
", vis = VisSpace(&it.visibility), constness = ConstnessSpace(vis_constness), @@ -2006,7 +2012,9 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item, } // Output the trait definition - write!(w, "
{}{}trait {}{}{}{} ",
+    write!(w, "
")?;
+    render_attributes(w, it)?;
+    write!(w, "{}{}trait {}{}{}{} ",
            VisSpace(&it.visibility),
            UnsafetySpace(t.unsafety),
            it.name.as_ref().unwrap(),
@@ -2987,7 +2995,9 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
 fn item_typedef(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
                 t: &clean::Typedef) -> fmt::Result {
     let indent = format!("type {}{:#} ", it.name.as_ref().unwrap(), t.generics).len();
-    write!(w, "
type {}{}{where_clause} = {type_};
", + write!(w, "
")?;
+    render_attributes(w, it)?;
+    write!(w, "type {}{}{where_clause} = {type_};
", it.name.as_ref().unwrap(), t.generics, where_clause = WhereClause(&t.generics, indent), diff --git a/src/test/rustdoc/attributes.rs b/src/test/rustdoc/attributes.rs new file mode 100644 index 0000000000000..22e509001137e --- /dev/null +++ b/src/test/rustdoc/attributes.rs @@ -0,0 +1,27 @@ +// Copyright 2017 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. + +#![crate_name = "foo"] + +// @has foo/fn.f.html '//*[@class="docblock attributes"]' '#[no_mangle]' +#[no_mangle] +pub extern "C" fn f() {} + +// @has foo/fn.g.html '//*[@class="docblock attributes"]' '#[export_name = "bar"]' +#[export_name = "bar"] +pub extern "C" fn g() {} + +// @has foo/enum.Foo.html '//*[@class="docblock attributes"]' '#[repr(i64)]' +// @has foo/enum.Foo.html '//*[@class="docblock attributes"]' '#[must_use]' +#[repr(i64)] +#[must_use] +pub enum Foo { + Bar, +} From 51ef003deaf2ac82edeeb03a98630330b069817a Mon Sep 17 00:00:00 2001 From: Henning Kowalk Date: Thu, 9 Feb 2017 00:44:36 +0100 Subject: [PATCH 02/11] Fixed #39661 Clarified potential ambiguity. --- src/libstd/collections/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstd/collections/mod.rs b/src/libstd/collections/mod.rs index b9e92a01b2f8e..4fae9fcb51c4e 100644 --- a/src/libstd/collections/mod.rs +++ b/src/libstd/collections/mod.rs @@ -68,7 +68,7 @@ //! * You want to find the largest or smallest key that is smaller or larger //! than something. //! * You want to be able to get all of the entries in order on-demand. -//! * You want a sorted map. +//! * You want map sorted by its keys. //! //! ### Use the `Set` variant of any of these `Map`s when: //! * You just want to remember which keys you've seen. From 8251a2d73f077d3358ea88dbb6b7971a57f72d40 Mon Sep 17 00:00:00 2001 From: Henning Kowalk Date: Thu, 9 Feb 2017 01:18:50 +0100 Subject: [PATCH 03/11] Fix missing article. --- src/libstd/collections/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstd/collections/mod.rs b/src/libstd/collections/mod.rs index 4fae9fcb51c4e..8884d0688b8b7 100644 --- a/src/libstd/collections/mod.rs +++ b/src/libstd/collections/mod.rs @@ -68,7 +68,7 @@ //! * You want to find the largest or smallest key that is smaller or larger //! than something. //! * You want to be able to get all of the entries in order on-demand. -//! * You want map sorted by its keys. +//! * You want a map sorted by its keys. //! //! ### Use the `Set` variant of any of these `Map`s when: //! * You just want to remember which keys you've seen. From fce944d4e79b3a87ddf511206724edf33acfd704 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Thu, 9 Feb 2017 10:38:29 -0700 Subject: [PATCH 04/11] Add the item type to the tooltip See: https://users.rust-lang.org/t/seeking-opinions-from-colorblind-rustaceans-coloring-in-rustdoc-code-blocks --- src/librustdoc/html/render.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index 6234d89024441..29b0c00d11b42 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -1818,7 +1818,7 @@ fn item_module(w: &mut fmt::Formatter, cx: &Context, write!(w, " {name}{unsafety_flag} + title='{title_type} {title}'>{name}{unsafety_flag} {stab_docs} {docs} @@ -1830,6 +1830,7 @@ fn item_module(w: &mut fmt::Formatter, cx: &Context, stab = myitem.stability_class(), unsafety_flag = unsafety_flag, href = item_path(myitem.type_(), myitem.name.as_ref().unwrap()), + title_type = myitem.type_(), title = full_path(cx, myitem))?; } } From bc4ad1a2c9fb4a9f9f4793e0b62404f3e6150132 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Thu, 9 Feb 2017 11:02:01 -0700 Subject: [PATCH 05/11] Add the short type to inline links, too --- src/librustdoc/html/format.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index c591c09bf20e2..6f8c6aa7094dd 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -550,8 +550,8 @@ impl<'a> fmt::Display for HRef<'a> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match href(self.did) { Some((url, shortty, fqp)) => if !f.alternate() { - write!(f, "{}", - shortty, url, fqp.join("::"), self.text) + write!(f, "{}", + shortty, url, shortty, fqp.join("::"), self.text) } else { write!(f, "{}", self.text) }, From c603839d5f8050cf050b18d7e2c14f48e78d2382 Mon Sep 17 00:00:00 2001 From: Jimmy Cuadra Date: Sat, 11 Feb 2017 04:16:13 -0800 Subject: [PATCH 06/11] rustdoc: Only include a stability span if needed. --- src/librustdoc/clean/mod.rs | 30 ++++++++++++++++++++---------- src/librustdoc/html/render.rs | 17 ++++++++++++----- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 291fc8dfa9680..4a6e862246010 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -327,17 +327,27 @@ impl Item { } } - pub fn stability_class(&self) -> String { - self.stability.as_ref().map(|ref s| { - let mut base = match s.level { - stability::Unstable => "unstable".to_string(), - stability::Stable => String::new(), - }; - if !s.deprecated_since.is_empty() { - base.push_str(" deprecated"); + pub fn stability_class(&self) -> Option { + match self.stability { + Some(ref s) => { + let mut classes = Vec::with_capacity(2); + + if s.level == stability::Unstable { + classes.push("unstable"); + } + + if !s.deprecated_since.is_empty() { + classes.push("deprecated"); + } + + if classes.len() != 0 { + Some(classes.join(" ")) + } else { + None + } } - base - }).unwrap_or(String::new()) + None => None, + } } pub fn stable_since(&self) -> Option<&str> { diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index 6234d89024441..fc1597469b1cd 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -1827,7 +1827,7 @@ fn item_module(w: &mut fmt::Formatter, cx: &Context, stab_docs = stab_docs, docs = shorter(Some(&Markdown(doc_value).to_string())), class = myitem.type_(), - stab = myitem.stability_class(), + stab = myitem.stability_class().unwrap_or("".to_string()), unsafety_flag = unsafety_flag, href = item_path(myitem.type_(), myitem.name.as_ref().unwrap()), title = full_path(cx, myitem))?; @@ -2369,13 +2369,16 @@ fn item_struct(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item, write!(w, " ", + ", item_type = ItemType::StructField, id = id, ns_id = ns_id, - stab = field.stability_class(), name = field.name.as_ref().unwrap(), ty = ty)?; + if let Some(stability_class) = field.stability_class() { + write!(w, "", + stab = stability_class)?; + } document(w, cx, field)?; } } @@ -2406,11 +2409,15 @@ fn item_union(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item, write!(w, "

Fields

")?; for (field, ty) in fields { write!(w, "{name}: {ty} - ", + ", shortty = ItemType::StructField, - stab = field.stability_class(), name = field.name.as_ref().unwrap(), ty = ty)?; + if let Some(stability_class) = field.stability_class() { + write!(w, "", + stab = stability_class)?; + } + write!(w, "")?; document(w, cx, field)?; } } From bd14c7f333d55e7b84ab56e4fe6fef2e5325b1be Mon Sep 17 00:00:00 2001 From: Jimmy Cuadra Date: Sat, 11 Feb 2017 07:10:03 -0800 Subject: [PATCH 07/11] Remove extra closing span element. --- src/librustdoc/html/render.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index fc1597469b1cd..fd83711f1f446 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -2417,7 +2417,6 @@ fn item_union(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item, write!(w, "", stab = stability_class)?; } - write!(w, "")?; document(w, cx, field)?; } } From c951ed7fb6daa7dff6dfb3e648576488f74ccaee Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 11 Feb 2017 16:36:31 +0100 Subject: [PATCH 08/11] Add tested item in the rustdoc --test output --- src/librustdoc/test.rs | 8 ++++++-- src/test/run-make/issue-22131/Makefile | 2 +- src/tools/compiletest/src/runtest.rs | 16 ++++++++++------ 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/librustdoc/test.rs b/src/librustdoc/test.rs index 930cf401e7450..349bddc87405c 100644 --- a/src/librustdoc/test.rs +++ b/src/librustdoc/test.rs @@ -418,8 +418,12 @@ impl Collector { should_panic: bool, no_run: bool, should_ignore: bool, as_test_harness: bool, compile_fail: bool, error_codes: Vec, line: usize, filename: String) { - let name = format!("{} - line {}", filename, line); - self.cnt += 1; + let name = if self.use_headers { + let s = self.current_header.as_ref().map(|s| &**s).unwrap_or(""); + format!("{} - {} (line {})", filename, s, line) + } else { + format!("{} - {} (line {})", filename, self.names.join("::"), line) + }; let cfgs = self.cfgs.clone(); let libs = self.libs.clone(); let externs = self.externs.clone(); diff --git a/src/test/run-make/issue-22131/Makefile b/src/test/run-make/issue-22131/Makefile index f65cc9e06a329..64af91b487b32 100644 --- a/src/test/run-make/issue-22131/Makefile +++ b/src/test/run-make/issue-22131/Makefile @@ -4,4 +4,4 @@ all: foo.rs $(RUSTC) --cfg 'feature="bar"' --crate-type lib foo.rs $(HOST_RPATH_ENV) '$(RUSTDOC)' --test --cfg 'feature="bar"' \ -L $(TMPDIR) foo.rs |\ - grep -q 'foo.rs - line 11 ... ok' + grep -q 'foo.rs - foo (line 11) ... ok' diff --git a/src/tools/compiletest/src/runtest.rs b/src/tools/compiletest/src/runtest.rs index 1a3d7a190be36..d0aba8a0b0a7e 100644 --- a/src/tools/compiletest/src/runtest.rs +++ b/src/tools/compiletest/src/runtest.rs @@ -1998,16 +1998,20 @@ actual:\n\ for _ in res.stdout.split("\n") .filter(|s| s.starts_with("test ")) .inspect(|s| { - let tmp: Vec<&str> = s.split(" - line ").collect(); + let tmp: Vec<&str> = s.split(" - ").collect(); if tmp.len() == 2 { let path = tmp[0].rsplit("test ").next().unwrap(); if let Some(ref mut v) = files.get_mut(path) { tested += 1; - let line = tmp[1].split(" ...") - .next() - .unwrap_or("0") - .parse() - .unwrap_or(0); + let mut iter = tmp[1].split("(line "); + iter.next(); + let line = iter.next() + .unwrap_or(")") + .split(")") + .next() + .unwrap_or("0") + .parse() + .unwrap_or(0); if let Ok(pos) = v.binary_search(&line) { v.remove(pos); } else { From 1fa9dbc00e8d5eff8f698097afb112c142bb8da0 Mon Sep 17 00:00:00 2001 From: Jimmy Cuadra Date: Sat, 11 Feb 2017 10:00:56 -0800 Subject: [PATCH 09/11] Use functional transformations on the option instead of matching. --- src/librustdoc/clean/mod.rs | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 4a6e862246010..751ed7d443d29 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -328,26 +328,23 @@ impl Item { } pub fn stability_class(&self) -> Option { - match self.stability { - Some(ref s) => { - let mut classes = Vec::with_capacity(2); + self.stability.as_ref().and_then(|ref s| { + let mut classes = Vec::with_capacity(2); - if s.level == stability::Unstable { - classes.push("unstable"); - } + if s.level == stability::Unstable { + classes.push("unstable"); + } - if !s.deprecated_since.is_empty() { - classes.push("deprecated"); - } + if !s.deprecated_since.is_empty() { + classes.push("deprecated"); + } - if classes.len() != 0 { - Some(classes.join(" ")) - } else { - None - } + if classes.len() != 0 { + Some(classes.join(" ")) + } else { + None } - None => None, - } + }) } pub fn stable_since(&self) -> Option<&str> { From 6b5c5f29ce18fbb6fe7f004c47ae0f1c5f84d3fc Mon Sep 17 00:00:00 2001 From: Jordi Polo Date: Sun, 12 Feb 2017 11:02:55 +0900 Subject: [PATCH 10/11] Sustitutes try! for ? in the Result documentation --- src/libcore/result.rs | 38 +++++++++++++++----------------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/src/libcore/result.rs b/src/libcore/result.rs index 0a2e363965347..a05db9b489ca1 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -139,7 +139,7 @@ //! assert!(file.write_all(b"important message").is_ok()); //! ``` //! -//! Or propagate the error up the call stack with [`try!`]: +//! Or propagate the error up the call stack with [`?`]: //! //! ``` //! # use std::fs::File; @@ -147,17 +147,17 @@ //! # use std::io; //! # #[allow(dead_code)] //! fn write_message() -> io::Result<()> { -//! let mut file = try!(File::create("valuable_data.txt")); -//! try!(file.write_all(b"important message")); +//! let mut file = File::create("valuable_data.txt")?; +//! file.write_all(b"important message")?; //! Ok(()) //! } //! ``` //! -//! # The `try!` macro +//! # The `?` syntax //! //! When writing code that calls many functions that return the -//! [`Result`] type, the error handling can be tedious. The [`try!`] -//! macro hides some of the boilerplate of propagating errors up the +//! [`Result`] type, the error handling can be tedious. The [`?`] +//! syntax hides some of the boilerplate of propagating errors up the //! call stack. //! //! It replaces this: @@ -208,37 +208,29 @@ //! } //! //! fn write_info(info: &Info) -> io::Result<()> { -//! let mut file = try!(File::create("my_best_friends.txt")); +//! let mut file = File::create("my_best_friends.txt")?; //! // Early return on error -//! try!(file.write_all(format!("name: {}\n", info.name).as_bytes())); -//! try!(file.write_all(format!("age: {}\n", info.age).as_bytes())); -//! try!(file.write_all(format!("rating: {}\n", info.rating).as_bytes())); +//! file.write_all(format!("name: {}\n", info.name).as_bytes())?; +//! file.write_all(format!("age: {}\n", info.age).as_bytes())?; +//! file.write_all(format!("rating: {}\n", info.rating).as_bytes())?; //! Ok(()) //! } //! ``` //! //! *It's much nicer!* //! -//! Wrapping an expression in [`try!`] will result in the unwrapped +//! Ending the expression with [`?`] will result in the unwrapped //! success ([`Ok`]) value, unless the result is [`Err`], in which case -//! [`Err`] is returned early from the enclosing function. Its simple definition -//! makes it clear: +//! [`Err`] is returned early from the enclosing function. //! -//! ``` -//! macro_rules! try { -//! ($e:expr) => (match $e { Ok(e) => e, Err(e) => return Err(e) }) -//! } -//! ``` -//! -//! [`try!`] is imported by the prelude and is available everywhere, but it can only -//! be used in functions that return [`Result`] because of the early return of -//! [`Err`] that it provides. +//! [`?`] can only be used in functions that return [`Result`] because of the +//! early return of [`Err`] that it provides. //! //! [`expect`]: enum.Result.html#method.expect //! [`Write`]: ../../std/io/trait.Write.html //! [`write_all`]: ../../std/io/trait.Write.html#method.write_all //! [`io::Result`]: ../../std/io/type.Result.html -//! [`try!`]: ../../std/macro.try.html +//! [`?`]: ../../std/macro.try.html //! [`Result`]: enum.Result.html //! [`Ok(T)`]: enum.Result.html#variant.Ok //! [`Err(E)`]: enum.Result.html#variant.Err From 037ef0bbc88742187e6059a846f027bb99884267 Mon Sep 17 00:00:00 2001 From: Jake Goulding Date: Sat, 11 Feb 2017 23:42:39 -0500 Subject: [PATCH 11/11] Improve grammar on field init docs --- src/doc/book/structs.md | 4 ++-- src/doc/reference.md | 8 ++------ 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/doc/book/structs.md b/src/doc/book/structs.md index dcf74cbb0a7c4..f10fb6274c61c 100644 --- a/src/doc/book/structs.md +++ b/src/doc/book/structs.md @@ -117,8 +117,8 @@ fn main() { } ``` -Initialization of a data structure (struct, enum, union) can be simplified if -fields of the data structure are initialized with variables which has same +Initialization of a data structure (struct, enum, union) can be simplified when +fields of the data structure are initialized with variables of the same names as the fields. ``` diff --git a/src/doc/reference.md b/src/doc/reference.md index 8aefabe61fdf6..ab4da862033d3 100644 --- a/src/doc/reference.md +++ b/src/doc/reference.md @@ -2819,12 +2819,8 @@ Point3d {y: 0, z: 10, .. base}; #### Struct field init shorthand When initializing a data structure (struct, enum, union) with named fields, -allow writing `fieldname` as a shorthand for `fieldname: fieldname`. This -allows a compact syntax for initialization, with less duplication. - -In the initializer for a `struct` with named fields, a `union` with named -fields, or an enum variant with named fields, accept an identifier `field` as a -shorthand for `field: field`. +it is allowed to write `fieldname` as a shorthand for `fieldname: fieldname`. +This allows a compact syntax with less duplication. Example: