From 097126e2840425b7d11f269c39a22c86ef003d6b Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 4 Jan 2020 12:35:23 -0800 Subject: [PATCH 01/12] Omit underscore constants from rustdoc --- src/librustdoc/visit_ast.rs | 26 +++++++++++++++----------- src/test/rustdoc/const-underscore.rs | 7 +++++++ 2 files changed, 22 insertions(+), 11 deletions(-) create mode 100644 src/test/rustdoc/const-underscore.rs diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs index 959f61644d60d..53deca2180cea 100644 --- a/src/librustdoc/visit_ast.rs +++ b/src/librustdoc/visit_ast.rs @@ -9,7 +9,7 @@ use rustc::ty::TyCtxt; use rustc::util::nodemap::{FxHashMap, FxHashSet}; use rustc_span::hygiene::MacroKind; use rustc_span::source_map::Spanned; -use rustc_span::symbol::sym; +use rustc_span::symbol::{kw, sym}; use rustc_span::{self, Span}; use syntax::ast; @@ -513,16 +513,20 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { om.statics.push(s); } hir::ItemKind::Const(type_, expr) => { - let s = Constant { - type_, - expr, - id: item.hir_id, - name: ident.name, - attrs: &item.attrs, - whence: item.span, - vis: &item.vis, - }; - om.constants.push(s); + // Underscore constants do not correspond to a nameable item and + // so are never useful in documentation. + if ident.name != kw::Underscore { + let s = Constant { + type_, + expr, + id: item.hir_id, + name: ident.name, + attrs: &item.attrs, + whence: item.span, + vis: &item.vis, + }; + om.constants.push(s); + } } hir::ItemKind::Trait(is_auto, unsafety, ref generics, ref bounds, ref item_ids) => { let items = item_ids.iter().map(|ti| self.cx.tcx.hir().trait_item(ti.id)).collect(); diff --git a/src/test/rustdoc/const-underscore.rs b/src/test/rustdoc/const-underscore.rs new file mode 100644 index 0000000000000..0d4809409f3a2 --- /dev/null +++ b/src/test/rustdoc/const-underscore.rs @@ -0,0 +1,7 @@ +// compile-flags: --document-private-items + +// @!has const_underscore/constant._.html +const _: () = { + #[no_mangle] + extern "C" fn implementation_detail() {} +}; From e2305d0055805effb9d6200f995ad825aa0c56e1 Mon Sep 17 00:00:00 2001 From: Oliver Middleton Date: Sun, 5 Jan 2020 23:19:42 +0000 Subject: [PATCH 02/12] rustdoc: HTML escape const values --- src/librustdoc/html/format.rs | 17 ++++++++++++++--- src/librustdoc/html/render.rs | 4 ++-- src/test/rustdoc/const-generics/const-impl.rs | 7 +++++++ src/test/rustdoc/show-const-contents.rs | 3 +++ 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 96b841046ff22..9a34b60805b50 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -15,6 +15,7 @@ use rustc::util::nodemap::FxHashSet; use rustc_target::spec::abi::Abi; use crate::clean::{self, PrimitiveType}; +use crate::html::escape::Escape; use crate::html::item_type::ItemType; use crate::html::render::{self, cache, CURRENT_DEPTH}; @@ -314,8 +315,14 @@ impl clean::Lifetime { } impl clean::Constant { - crate fn print(&self) -> &str { - &self.expr + crate fn print(&self) -> impl fmt::Display + '_ { + display_fn(move |f| { + if f.alternate() { + f.write_str(&self.expr) + } else { + write!(f, "{}", Escape(&self.expr)) + } + }) } } @@ -689,7 +696,11 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter<'_>, use_absolute: bool) -> clean::Array(ref t, ref n) => { primitive_link(f, PrimitiveType::Array, "[")?; fmt::Display::fmt(&t.print(), f)?; - primitive_link(f, PrimitiveType::Array, &format!("; {}]", n)) + if f.alternate() { + primitive_link(f, PrimitiveType::Array, &format!("; {}]", n)) + } else { + primitive_link(f, PrimitiveType::Array, &format!("; {}]", Escape(n))) + } } clean::Never => primitive_link(f, PrimitiveType::Never, "!"), clean::RawPointer(m, ref t) => { diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index ad059463aa43a..24cb8a856b9ce 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -2279,7 +2279,7 @@ fn item_constant(w: &mut Buffer, cx: &Context, it: &clean::Item, c: &clean::Cons ); if c.value.is_some() || c.is_literal { - write!(w, " = {expr};", expr = c.expr); + write!(w, " = {expr};", expr = Escape(&c.expr)); } else { write!(w, ";"); } @@ -2292,7 +2292,7 @@ fn item_constant(w: &mut Buffer, cx: &Context, it: &clean::Item, c: &clean::Cons if value_lowercase != expr_lowercase && value_lowercase.trim_end_matches("i32") != expr_lowercase { - write!(w, " // {value}", value = value); + write!(w, " // {value}", value = Escape(value)); } } } diff --git a/src/test/rustdoc/const-generics/const-impl.rs b/src/test/rustdoc/const-generics/const-impl.rs index 819adfeb9c775..7361b22b74798 100644 --- a/src/test/rustdoc/const-generics/const-impl.rs +++ b/src/test/rustdoc/const-generics/const-impl.rs @@ -30,3 +30,10 @@ impl VSet { Self { inner: Vec::new() } } } + +pub struct Escape; + +// @has foo/struct.Escape.html '//h3[@id="impl"]/code' 'impl Escape<{ r#""# }>' +impl Escape<{ r#""# }> { + pub fn f() {} +} diff --git a/src/test/rustdoc/show-const-contents.rs b/src/test/rustdoc/show-const-contents.rs index 6d95f7827a1d7..e84f6e52c75aa 100644 --- a/src/test/rustdoc/show-const-contents.rs +++ b/src/test/rustdoc/show-const-contents.rs @@ -62,3 +62,6 @@ macro_rules! int_module { // @has show_const_contents/constant.MIN.html '= i16::min_value(); // -32_768i16' int_module!(i16); + +// @has show_const_contents/constant.ESCAPE.html //pre '= r#""#;' +pub const ESCAPE: &str = r#""#; From e6d95ce0b88bddd9cbb54bd272ba7c48601ba7a0 Mon Sep 17 00:00:00 2001 From: Grachev Mikhail Date: Mon, 6 Jan 2020 15:18:03 +0300 Subject: [PATCH 03/12] Formatting an example for method Vec.retain --- src/liballoc/vec.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs index 93a51ccb20737..e3e6d950953a7 100644 --- a/src/liballoc/vec.rs +++ b/src/liballoc/vec.rs @@ -1054,7 +1054,7 @@ impl Vec { /// /// ``` /// let mut vec = vec![1, 2, 3, 4]; - /// vec.retain(|&x| x%2 == 0); + /// vec.retain(|&x| x % 2 == 0); /// assert_eq!(vec, [2, 4]); /// ``` /// From 24c6cd80c3b44c5c9a33733e2524afb3e015ee92 Mon Sep 17 00:00:00 2001 From: dylan_DPC Date: Mon, 30 Dec 2019 17:29:03 +0530 Subject: [PATCH 04/12] stabilise remove_item --- src/liballoc/vec.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs index a27a13847d6a2..73c310bb2bfed 100644 --- a/src/liballoc/vec.rs +++ b/src/liballoc/vec.rs @@ -1696,7 +1696,6 @@ impl Vec { /// # Examples /// /// ``` - /// # #![feature(vec_remove_item)] /// let mut vec = vec![1, 2, 3, 1]; /// /// vec.remove_item(&1); From 0a739ce0a5200f2d1360fef126a93b5c1e8205ba Mon Sep 17 00:00:00 2001 From: dylan_DPC Date: Mon, 30 Dec 2019 17:57:00 +0530 Subject: [PATCH 05/12] remove usage of feature gate --- src/librustc/lib.rs | 1 - src/librustdoc/lib.rs | 1 - src/tools/compiletest/src/main.rs | 1 - 3 files changed, 3 deletions(-) diff --git a/src/librustc/lib.rs b/src/librustc/lib.rs index ce8263f81a72f..37761c17f5243 100644 --- a/src/librustc/lib.rs +++ b/src/librustc/lib.rs @@ -50,7 +50,6 @@ #![feature(thread_local)] #![feature(trace_macros)] #![feature(trusted_len)] -#![feature(vec_remove_item)] #![feature(stmt_expr_attributes)] #![feature(integer_atomics)] #![feature(test)] diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index c8a32306194df..2effbdaec6b5d 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -10,7 +10,6 @@ #![feature(nll)] #![feature(set_stdio)] #![feature(test)] -#![feature(vec_remove_item)] #![feature(ptr_offset_from)] #![feature(crate_visibility_modifier)] #![feature(const_fn)] diff --git a/src/tools/compiletest/src/main.rs b/src/tools/compiletest/src/main.rs index 487c1d5fb93a6..da1e3760e010d 100644 --- a/src/tools/compiletest/src/main.rs +++ b/src/tools/compiletest/src/main.rs @@ -1,6 +1,5 @@ #![crate_name = "compiletest"] #![feature(test)] -#![feature(vec_remove_item)] #![deny(warnings)] extern crate test; From 99fda5c1cec9ab67c3da197b5679408d2e51d093 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 6 Jan 2020 15:28:12 +0100 Subject: [PATCH 06/12] Clean up E0178 explanation --- src/librustc_error_codes/error_codes/E0178.md | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/librustc_error_codes/error_codes/E0178.md b/src/librustc_error_codes/error_codes/E0178.md index 07980ad83f1b5..0c6f918632f47 100644 --- a/src/librustc_error_codes/error_codes/E0178.md +++ b/src/librustc_error_codes/error_codes/E0178.md @@ -1,16 +1,27 @@ -In types, the `+` type operator has low precedence, so it is often necessary -to use parentheses. +The `+` type operator was used in an ambiguous context. -For example: +Erroneous code example: ```compile_fail,E0178 trait Foo {} struct Bar<'a> { - w: &'a Foo + Copy, // error, use &'a (Foo + Copy) - x: &'a Foo + 'a, // error, use &'a (Foo + 'a) - y: &'a mut Foo + 'a, // error, use &'a mut (Foo + 'a) - z: fn() -> Foo + 'a, // error, use fn() -> (Foo + 'a) + x: &'a Foo + 'a, // error! + y: &'a mut Foo + 'a, // error! + z: fn() -> Foo + 'a, // error! +} +``` + +In types, the `+` type operator has low precedence, so it is often necessary +to use parentheses: + +``` +trait Foo {} + +struct Bar<'a> { + x: &'a (Foo + 'a), // ok! + y: &'a mut (Foo + 'a), // ok! + z: fn() -> (Foo + 'a), // ok! } ``` From a7727c59ac0b80fbbd7b3ab045260727ab64ce21 Mon Sep 17 00:00:00 2001 From: Andy Russell Date: Mon, 6 Jan 2020 09:51:23 -0500 Subject: [PATCH 07/12] fire "non_camel_case_types" for associated types --- src/librustc_lint/nonstandard_style.rs | 6 ++++++ src/test/ui/issues/issue-17732.rs | 1 + src/test/ui/issues/issue-35600.rs | 2 ++ src/test/ui/lint/lint-non-camel-case-types.rs | 1 + src/test/ui/lint/lint-non-camel-case-types.stderr | 10 ++++++++-- 5 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/librustc_lint/nonstandard_style.rs b/src/librustc_lint/nonstandard_style.rs index a97061c50ae45..524de2cf40a83 100644 --- a/src/librustc_lint/nonstandard_style.rs +++ b/src/librustc_lint/nonstandard_style.rs @@ -142,6 +142,12 @@ impl EarlyLintPass for NonCamelCaseTypes { } } + fn check_trait_item(&mut self, cx: &EarlyContext<'_>, it: &ast::AssocItem) { + if let ast::AssocItemKind::TyAlias(..) = it.kind { + self.check_case(cx, "associated type", &it.ident); + } + } + fn check_variant(&mut self, cx: &EarlyContext<'_>, v: &ast::Variant) { self.check_case(cx, "variant", &v.ident); } diff --git a/src/test/ui/issues/issue-17732.rs b/src/test/ui/issues/issue-17732.rs index 5e11fc4fcfb58..8f63d5baef875 100644 --- a/src/test/ui/issues/issue-17732.rs +++ b/src/test/ui/issues/issue-17732.rs @@ -1,5 +1,6 @@ // check-pass #![allow(dead_code)] +#![allow(non_camel_case_types)] // pretty-expanded FIXME #23616 trait Person { diff --git a/src/test/ui/issues/issue-35600.rs b/src/test/ui/issues/issue-35600.rs index 9d74726d279e5..f0bab6010d724 100644 --- a/src/test/ui/issues/issue-35600.rs +++ b/src/test/ui/issues/issue-35600.rs @@ -1,5 +1,7 @@ // run-pass +#![allow(non_camel_case_types)] #![allow(unused_variables)] + trait Foo { type bar; fn bar(); diff --git a/src/test/ui/lint/lint-non-camel-case-types.rs b/src/test/ui/lint/lint-non-camel-case-types.rs index d3b119a944109..acd5c5df9e8f6 100644 --- a/src/test/ui/lint/lint-non-camel-case-types.rs +++ b/src/test/ui/lint/lint-non-camel-case-types.rs @@ -23,6 +23,7 @@ enum Foo5 { } trait foo6 { //~ ERROR trait `foo6` should have an upper camel case name + type foo7; //~ ERROR associated type `foo7` should have an upper camel case name fn dummy(&self) { } } diff --git a/src/test/ui/lint/lint-non-camel-case-types.stderr b/src/test/ui/lint/lint-non-camel-case-types.stderr index 177f8c8fe9b63..f82eefed4368a 100644 --- a/src/test/ui/lint/lint-non-camel-case-types.stderr +++ b/src/test/ui/lint/lint-non-camel-case-types.stderr @@ -46,11 +46,17 @@ error: trait `foo6` should have an upper camel case name LL | trait foo6 { | ^^^^ help: convert the identifier to upper camel case (notice the capitalization): `Foo6` +error: associated type `foo7` should have an upper camel case name + --> $DIR/lint-non-camel-case-types.rs:26:10 + | +LL | type foo7; + | ^^^^ help: convert the identifier to upper camel case (notice the capitalization): `Foo7` + error: type parameter `ty` should have an upper camel case name - --> $DIR/lint-non-camel-case-types.rs:29:6 + --> $DIR/lint-non-camel-case-types.rs:30:6 | LL | fn f(_: ty) {} | ^^ help: convert the identifier to upper camel case: `Ty` -error: aborting due to 8 previous errors +error: aborting due to 9 previous errors From 6bec8e997270bed18546b350336d88879b91ff78 Mon Sep 17 00:00:00 2001 From: dylan_DPC Date: Mon, 6 Jan 2020 20:37:49 +0530 Subject: [PATCH 08/12] stabilise it --- src/liballoc/vec.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs index 73c310bb2bfed..bc0cd6eff3708 100644 --- a/src/liballoc/vec.rs +++ b/src/liballoc/vec.rs @@ -1702,7 +1702,7 @@ impl Vec { /// /// assert_eq!(vec, vec![2, 3, 1]); /// ``` - #[unstable(feature = "vec_remove_item", reason = "recently added", issue = "40062")] + #[stable(feature = "vec_remove_item", since = "1.42.0")] pub fn remove_item(&mut self, item: &V) -> Option where T: PartialEq, From d9a7db901e33940cb2ccda6afe21b9916e66d9d2 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Mon, 23 Dec 2019 12:54:54 -0500 Subject: [PATCH 09/12] Add an unstable conversion from thread ID to u64 We see multiple cases inside rustc and ecosystem code where ThreadId is transmuted to u64, exploiting the underlying detail. This is suboptimal (can break unexpectedly if we change things in std). It is unlikely that ThreadId will ever need to be larger than u64 -- creating even 2^32 threads over the course of a program is quite hard, 2^64 is even harder. As such, we do not choose to return a larger sized type (e.g. u128). If we choose to shrink ThreadId in the future, or otherwise change its internals, it is likely that a mapping to u64 will still be applicable (though may become more complex). --- src/libstd/thread/mod.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/libstd/thread/mod.rs b/src/libstd/thread/mod.rs index b17418911380d..0dc43c7e6510a 100644 --- a/src/libstd/thread/mod.rs +++ b/src/libstd/thread/mod.rs @@ -1072,6 +1072,19 @@ impl ThreadId { ThreadId(NonZeroU64::new(id).unwrap()) } } + + /// This returns a numeric identifier for the thread identified by this + /// `ThreadId`. + /// + /// As noted in the documentation for the type itself, it is essentially an + /// opaque ID, but is guaranteed to be unique for each thread. The returned + /// value is entirely opaque -- only equality testing is stable. Note that + /// it is not guaranteed which values new threads will return, and this may + /// change across Rust versions. + #[unstable(feature = "thread_id_value", issue = "67939")] + pub fn as_u64(&self) -> u64 { + self.0.get() + } } //////////////////////////////////////////////////////////////////////////////// From 0113cacda2d08c135603bcb83da393252317e6a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Str=C3=B8mberg?= Date: Mon, 6 Jan 2020 17:25:17 +0100 Subject: [PATCH 10/12] Missing module std in example. --- src/libstd/net/tcp.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstd/net/tcp.rs b/src/libstd/net/tcp.rs index c33f98bdd8329..dd25d66658c3c 100644 --- a/src/libstd/net/tcp.rs +++ b/src/libstd/net/tcp.rs @@ -70,7 +70,7 @@ pub struct TcpStream(net_imp::TcpStream); /// // ... /// } /// -/// fn main() -> io::Result<()> { +/// fn main() -> std::io::Result<()> { /// let listener = TcpListener::bind("127.0.0.1:80")?; /// /// // accept connections and process them serially From a852941829dc835e9fb0f59b2c1e19aa1439211d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Str=C3=B8mberg?= Date: Mon, 6 Jan 2020 17:38:41 +0100 Subject: [PATCH 11/12] Removed module usage. --- src/libstd/net/tcp.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libstd/net/tcp.rs b/src/libstd/net/tcp.rs index dd25d66658c3c..5023d69240893 100644 --- a/src/libstd/net/tcp.rs +++ b/src/libstd/net/tcp.rs @@ -63,7 +63,6 @@ pub struct TcpStream(net_imp::TcpStream); /// # Examples /// /// ```no_run -/// # use std::io; /// use std::net::{TcpListener, TcpStream}; /// /// fn handle_client(stream: TcpStream) { From 503d06b90dfd8f6055c586c488dbd3a741e9f0c2 Mon Sep 17 00:00:00 2001 From: dylan_DPC Date: Mon, 6 Jan 2020 23:28:47 +0530 Subject: [PATCH 12/12] oh the one that was left behind --- src/liballoc/tests/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/liballoc/tests/lib.rs b/src/liballoc/tests/lib.rs index c1ae67a1a339f..3fdee8bbfdf10 100644 --- a/src/liballoc/tests/lib.rs +++ b/src/liballoc/tests/lib.rs @@ -11,7 +11,6 @@ #![feature(associated_type_bounds)] #![feature(binary_heap_into_iter_sorted)] #![feature(binary_heap_drain_sorted)] -#![feature(vec_remove_item)] use std::collections::hash_map::DefaultHasher; use std::hash::{Hash, Hasher};