From 7a25123845778fdaa715815cc9ec917a67c35dbd Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Sun, 18 Oct 2020 15:38:32 +0200 Subject: [PATCH 01/12] Add #[inline] to the Utf8Error accessors. --- library/core/src/str/error.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/library/core/src/str/error.rs b/library/core/src/str/error.rs index 427f720d68cdb..ccf7b20285cb3 100644 --- a/library/core/src/str/error.rs +++ b/library/core/src/str/error.rs @@ -72,6 +72,7 @@ impl Utf8Error { /// assert_eq!(1, error.valid_up_to()); /// ``` #[stable(feature = "utf8_error", since = "1.5.0")] + #[inline] pub fn valid_up_to(&self) -> usize { self.valid_up_to } @@ -92,6 +93,7 @@ impl Utf8Error { /// /// [U+FFFD]: ../../std/char/constant.REPLACEMENT_CHARACTER.html #[stable(feature = "utf8_error_error_len", since = "1.20.0")] + #[inline] pub fn error_len(&self) -> Option { self.error_len.map(|len| len as usize) } From 76daca2791f3deeee925e449d7db51795f55121d Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Sun, 18 Oct 2020 15:39:09 +0200 Subject: [PATCH 02/12] Add #[inline] to some core::str functions. Almost all these functions already had #[inline]. These were missing. --- library/core/src/str/iter.rs | 1 + library/core/src/str/mod.rs | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/library/core/src/str/iter.rs b/library/core/src/str/iter.rs index bee86df520c80..28cd350019ebf 100644 --- a/library/core/src/str/iter.rs +++ b/library/core/src/str/iter.rs @@ -326,6 +326,7 @@ unsafe impl TrustedLen for Bytes<'_> {} #[doc(hidden)] #[unstable(feature = "trusted_random_access", issue = "none")] unsafe impl TrustedRandomAccess for Bytes<'_> { + #[inline] fn may_have_side_effect() -> bool { false } diff --git a/library/core/src/str/mod.rs b/library/core/src/str/mod.rs index 3e18a4e70627d..5d80d2be9a4d3 100644 --- a/library/core/src/str/mod.rs +++ b/library/core/src/str/mod.rs @@ -1711,6 +1711,7 @@ impl str { /// /// assert_eq!("Hello\tworld", s.trim()); /// ``` + #[inline] #[must_use = "this returns the trimmed string as a slice, \ without modifying the original"] #[stable(feature = "rust1", since = "1.0.0")] @@ -1748,6 +1749,7 @@ impl str { /// let s = " עברית "; /// assert!(Some('ע') == s.trim_start().chars().next()); /// ``` + #[inline] #[must_use = "this returns the trimmed string as a new slice, \ without modifying the original"] #[stable(feature = "trim_direction", since = "1.30.0")] @@ -1785,6 +1787,7 @@ impl str { /// let s = " עברית "; /// assert!(Some('ת') == s.trim_end().chars().rev().next()); /// ``` + #[inline] #[must_use = "this returns the trimmed string as a new slice, \ without modifying the original"] #[stable(feature = "trim_direction", since = "1.30.0")] @@ -1823,6 +1826,7 @@ impl str { /// let s = " עברית"; /// assert!(Some('ע') == s.trim_left().chars().next()); /// ``` + #[inline] #[stable(feature = "rust1", since = "1.0.0")] #[rustc_deprecated( since = "1.33.0", @@ -1864,6 +1868,7 @@ impl str { /// let s = "עברית "; /// assert!(Some('ת') == s.trim_right().chars().rev().next()); /// ``` + #[inline] #[stable(feature = "rust1", since = "1.0.0")] #[rustc_deprecated( since = "1.33.0", @@ -2261,6 +2266,7 @@ impl str { /// assert_eq!("GRüßE, JüRGEN ❤", s); /// ``` #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")] + #[inline] pub fn make_ascii_uppercase(&mut self) { // SAFETY: safe because we transmute two types with the same layout. let me = unsafe { self.as_bytes_mut() }; @@ -2287,6 +2293,7 @@ impl str { /// assert_eq!("grÜße, jÜrgen ❤", s); /// ``` #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")] + #[inline] pub fn make_ascii_lowercase(&mut self) { // SAFETY: safe because we transmute two types with the same layout. let me = unsafe { self.as_bytes_mut() }; From cc850ecba0641f0ec56507ee656ad9a301705c83 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Sun, 18 Oct 2020 15:39:42 +0200 Subject: [PATCH 03/12] Add #[inline] to {&str, &mut str}::default. --- library/core/src/str/mod.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/library/core/src/str/mod.rs b/library/core/src/str/mod.rs index 5d80d2be9a4d3..345447a4cbb2a 100644 --- a/library/core/src/str/mod.rs +++ b/library/core/src/str/mod.rs @@ -2431,6 +2431,7 @@ impl AsRef<[u8]> for str { #[stable(feature = "rust1", since = "1.0.0")] impl Default for &str { /// Creates an empty str + #[inline] fn default() -> Self { "" } @@ -2439,6 +2440,7 @@ impl Default for &str { #[stable(feature = "default_mut_str", since = "1.28.0")] impl Default for &mut str { /// Creates an empty mutable str + #[inline] fn default() -> Self { // SAFETY: The empty string is valid UTF-8. unsafe { from_utf8_unchecked_mut(&mut []) } From 3baf6a4a749bd6ac4a8b9f1054d3f2ad2fc91e45 Mon Sep 17 00:00:00 2001 From: Ivan Pavluk Date: Sat, 31 Oct 2020 17:50:08 +0700 Subject: [PATCH 04/12] Fix doc links to std::fmt std::format and core::write macros' docs linked to core::fmt for format string reference, even though only std::fmt has format string documentation and the link titles were std::fmt. --- library/alloc/src/macros.rs | 4 ++-- library/core/src/macros/mod.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/library/alloc/src/macros.rs b/library/alloc/src/macros.rs index 2f744618d6936..a992d768d6314 100644 --- a/library/alloc/src/macros.rs +++ b/library/alloc/src/macros.rs @@ -71,7 +71,7 @@ macro_rules! vec { /// /// Additional parameters passed to `format!` replace the `{}`s within the /// formatting string in the order given unless named or positional parameters -/// are used; see [`std::fmt`][fmt] for more information. +/// are used; see [`std::fmt`] for more information. /// /// A common use for `format!` is concatenation and interpolation of strings. /// The same convention is used with [`print!`] and [`write!`] macros, @@ -80,7 +80,7 @@ macro_rules! vec { /// To convert a single value to a string, use the [`to_string`] method. This /// will use the [`Display`] formatting trait. /// -/// [fmt]: core::fmt +/// [`std::fmt`]: ../std/fmt/index.html /// [`print!`]: ../std/macro.print.html /// [`write!`]: core::write /// [`to_string`]: crate::string::ToString diff --git a/library/core/src/macros/mod.rs b/library/core/src/macros/mod.rs index bce8a70e92f31..079d9f6006a32 100644 --- a/library/core/src/macros/mod.rs +++ b/library/core/src/macros/mod.rs @@ -327,7 +327,7 @@ macro_rules! r#try { /// /// See [`std::fmt`] for more information on the format string syntax. /// -/// [`std::fmt`]: crate::fmt +/// [`std::fmt`]: ../std/fmt/index.html /// [`fmt::Write`]: crate::fmt::Write /// [`io::Write`]: ../std/io/trait.Write.html /// [`fmt::Result`]: crate::fmt::Result From d417bbef952bfd213c99f73a706ec785720124a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maty=C3=A1=C5=A1=20Racek?= Date: Sat, 31 Oct 2020 14:40:36 +0100 Subject: [PATCH 05/12] Add note to process::arg[s] that args shouldn't be escaped or quoted --- library/std/src/process.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/library/std/src/process.rs b/library/std/src/process.rs index a149946774404..ae4a077587565 100644 --- a/library/std/src/process.rs +++ b/library/std/src/process.rs @@ -557,6 +557,9 @@ impl Command { /// /// [`args`]: Command::args /// + /// Note that the argument is passed to the program directly as is, so you shouldn't wrap it in quotes + /// or escape special characters the same way you would do that when running the program from terminal. + /// /// # Examples /// /// Basic usage: @@ -582,6 +585,9 @@ impl Command { /// /// [`arg`]: Command::arg /// + /// Note that each argument is passed to the program directly as is, so you shouldn't wrap it in quotes + /// or escape special characters the same way you would do that when running the program from terminal directly. + /// /// # Examples /// /// Basic usage: From 7652bc3f683d2d0a70ef4da47dc730bf41f2f87d Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sat, 31 Oct 2020 18:09:21 +0300 Subject: [PATCH 06/12] expand: Tweak a comment in implementation of `macro_rules` --- compiler/rustc_expand/src/mbe/transcribe.rs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_expand/src/mbe/transcribe.rs b/compiler/rustc_expand/src/mbe/transcribe.rs index 0e5c5fe4d4473..3d8a4bbff1833 100644 --- a/compiler/rustc_expand/src/mbe/transcribe.rs +++ b/compiler/rustc_expand/src/mbe/transcribe.rs @@ -232,17 +232,19 @@ pub(super) fn transcribe<'a>( // the meta-var. let ident = MacroRulesNormalizedIdent::new(orignal_ident); if let Some(cur_matched) = lookup_cur_matched(ident, interp, &repeats) { - if let MatchedNonterminal(ref nt) = cur_matched { - // FIXME #2887: why do we apply a mark when matching a token tree meta-var - // (e.g. `$x:tt`), but not when we are matching any other type of token - // tree? - if let NtTT(ref tt) = **nt { - result.push(tt.clone().into()); + if let MatchedNonterminal(nt) = cur_matched { + let token = if let NtTT(tt) = &**nt { + // `tt`s are emitted into the output stream directly as "raw tokens", + // without wrapping them into groups. + tt.clone() } else { + // Other variables are emitted into the output stream as groups with + // `Delimiter::None` to maintain parsing priorities. + // `Interpolated` is currenty used for such groups in rustc parser. marker.visit_span(&mut sp); - let token = TokenTree::token(token::Interpolated(nt.clone()), sp); - result.push(token.into()); - } + TokenTree::token(token::Interpolated(nt.clone()), sp) + }; + result.push(token.into()); } else { // We were unable to descend far enough. This is an error. return Err(cx.struct_span_err( From ed96321e7e32d3c047f204445b849c75f3c63e28 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 31 Oct 2020 16:13:33 +0100 Subject: [PATCH 07/12] fix aliasing issues in u128 formatting code --- library/core/src/fmt/num.rs | 12 +++++++++--- library/core/src/mem/maybe_uninit.rs | 4 ++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/library/core/src/fmt/num.rs b/library/core/src/fmt/num.rs index 795cbf5fbbd44..7a98210995ec7 100644 --- a/library/core/src/fmt/num.rs +++ b/library/core/src/fmt/num.rs @@ -595,7 +595,6 @@ fn fmt_u128(n: u128, is_nonnegative: bool, f: &mut fmt::Formatter<'_>) -> fmt::R // 2^128 is about 3*10^38, so 39 gives an extra byte of space let mut buf = [MaybeUninit::::uninit(); 39]; let mut curr = buf.len() as isize; - let buf_ptr = MaybeUninit::slice_as_mut_ptr(&mut buf); let (n, rem) = udiv_1e19(n); parse_u64_into(rem, &mut buf, &mut curr); @@ -606,7 +605,11 @@ fn fmt_u128(n: u128, is_nonnegative: bool, f: &mut fmt::Formatter<'_>) -> fmt::R // SAFETY: Guaranteed that we wrote at most 19 bytes, and there must be space // remaining since it has length 39 unsafe { - ptr::write_bytes(buf_ptr.offset(target), b'0', (curr - target) as usize); + ptr::write_bytes( + MaybeUninit::slice_as_mut_ptr(&mut buf).offset(target), + b'0', + (curr - target) as usize, + ); } curr = target; @@ -615,6 +618,9 @@ fn fmt_u128(n: u128, is_nonnegative: bool, f: &mut fmt::Formatter<'_>) -> fmt::R // Should this following branch be annotated with unlikely? if n != 0 { let target = (buf.len() - 38) as isize; + // The raw `buf_ptr` pointer is only valid until `buf` is used the next time, + // buf `buf` is not used in this scope so we are good. + let buf_ptr = MaybeUninit::slice_as_mut_ptr(&mut buf); // SAFETY: At this point we wrote at most 38 bytes, pad up to that point, // There can only be at most 1 digit remaining. unsafe { @@ -629,7 +635,7 @@ fn fmt_u128(n: u128, is_nonnegative: bool, f: &mut fmt::Formatter<'_>) -> fmt::R // UTF-8 since `DEC_DIGITS_LUT` is let buf_slice = unsafe { str::from_utf8_unchecked(slice::from_raw_parts( - buf_ptr.offset(curr), + MaybeUninit::slice_as_mut_ptr(&mut buf).offset(curr), buf.len() - curr as usize, )) }; diff --git a/library/core/src/mem/maybe_uninit.rs b/library/core/src/mem/maybe_uninit.rs index 862c452a43480..660b7db70be92 100644 --- a/library/core/src/mem/maybe_uninit.rs +++ b/library/core/src/mem/maybe_uninit.rs @@ -842,13 +842,13 @@ impl MaybeUninit { #[unstable(feature = "maybe_uninit_slice", issue = "63569")] #[inline(always)] pub fn slice_as_ptr(this: &[MaybeUninit]) -> *const T { - this as *const [MaybeUninit] as *const T + this.as_ptr() as *const T } /// Gets a mutable pointer to the first element of the array. #[unstable(feature = "maybe_uninit_slice", issue = "63569")] #[inline(always)] pub fn slice_as_mut_ptr(this: &mut [MaybeUninit]) -> *mut T { - this as *mut [MaybeUninit] as *mut T + this.as_mut_ptr() as *mut T } } From 9f630af93077b276f78701dcd2307d19dccf0e14 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 31 Oct 2020 16:16:54 +0100 Subject: [PATCH 08/12] fix aliasing issue in unix sleep function --- library/std/src/sys/unix/thread.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/library/std/src/sys/unix/thread.rs b/library/std/src/sys/unix/thread.rs index fdf114d6df6fe..f1ab302d30e1e 100644 --- a/library/std/src/sys/unix/thread.rs +++ b/library/std/src/sys/unix/thread.rs @@ -178,7 +178,8 @@ impl Thread { tv_nsec: nsecs, }; secs -= ts.tv_sec as u64; - if libc::nanosleep(&ts, &mut ts) == -1 { + let ts_ptr = &mut ts as *mut _; + if libc::nanosleep(ts_ptr, ts_ptr) == -1 { assert_eq!(os::errno(), libc::EINTR); secs += ts.tv_sec as u64; nsecs = ts.tv_nsec; From 607076e20903bb5267d5d8f2bc7520ba1686cec8 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 31 Oct 2020 16:22:24 +0100 Subject: [PATCH 09/12] fix aliasing issue in binary_heap --- library/alloc/src/collections/binary_heap.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/library/alloc/src/collections/binary_heap.rs b/library/alloc/src/collections/binary_heap.rs index f2852b1cc2b80..b67c72d7136a6 100644 --- a/library/alloc/src/collections/binary_heap.rs +++ b/library/alloc/src/collections/binary_heap.rs @@ -1036,8 +1036,9 @@ impl<'a, T> Hole<'a, T> { debug_assert!(index != self.pos); debug_assert!(index < self.data.len()); unsafe { - let index_ptr: *const _ = self.data.get_unchecked(index); - let hole_ptr = self.data.get_unchecked_mut(self.pos); + let ptr = self.data.as_mut_ptr(); + let index_ptr: *const _ = ptr.add(index); + let hole_ptr = ptr.add(self.pos); ptr::copy_nonoverlapping(index_ptr, hole_ptr, 1); } self.pos = index; From 9749eb72af8f817fd72c0a7cdad324a9baa8b635 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 28 Oct 2020 14:01:04 +0100 Subject: [PATCH 10/12] fix aliasing issues in SipHasher --- library/core/src/hash/sip.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/hash/sip.rs b/library/core/src/hash/sip.rs index a9882d54de4f1..6178b0af137e8 100644 --- a/library/core/src/hash/sip.rs +++ b/library/core/src/hash/sip.rs @@ -111,7 +111,7 @@ macro_rules! load_int_le { debug_assert!($i + mem::size_of::<$int_ty>() <= $buf.len()); let mut data = 0 as $int_ty; ptr::copy_nonoverlapping( - $buf.get_unchecked($i), + $buf.as_ptr().add($i), &mut data as *mut _ as *mut u8, mem::size_of::<$int_ty>(), ); From db416b232c1b7ebeb250e19cbca7b52ab81bc3f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maty=C3=A1=C5=A1=20Racek?= Date: Sat, 31 Oct 2020 17:28:44 +0100 Subject: [PATCH 11/12] Apply suggestions from code review Co-authored-by: Mara Bos --- library/std/src/process.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/library/std/src/process.rs b/library/std/src/process.rs index ae4a077587565..2c7ed4614bce9 100644 --- a/library/std/src/process.rs +++ b/library/std/src/process.rs @@ -557,8 +557,10 @@ impl Command { /// /// [`args`]: Command::args /// - /// Note that the argument is passed to the program directly as is, so you shouldn't wrap it in quotes - /// or escape special characters the same way you would do that when running the program from terminal. + /// Note that the argument is not passed through a shell, but given + /// literally to the program. This means that shell syntax like quotes, + /// escaped characters, word splitting, glob patterns, substitution, etc. + /// have no effect. /// /// # Examples /// @@ -585,8 +587,10 @@ impl Command { /// /// [`arg`]: Command::arg /// - /// Note that each argument is passed to the program directly as is, so you shouldn't wrap it in quotes - /// or escape special characters the same way you would do that when running the program from terminal directly. + /// Note that the arguments are not passed through a shell, but given + /// literally to the program. This means that shell syntax like quotes, + /// escaped characters, word splitting, glob patterns, substitution, etc. + /// have no effect. /// /// # Examples /// From e5b1f69d631d6637e51f3715e8276f2c57676361 Mon Sep 17 00:00:00 2001 From: Scott Olson Date: Sun, 1 Nov 2020 04:38:41 +0000 Subject: [PATCH 12/12] Inline Default::default() for atomics --- library/core/src/sync/atomic.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/library/core/src/sync/atomic.rs b/library/core/src/sync/atomic.rs index 5c9cfe27101f0..a3d93d7074b69 100644 --- a/library/core/src/sync/atomic.rs +++ b/library/core/src/sync/atomic.rs @@ -155,6 +155,7 @@ pub struct AtomicBool { #[stable(feature = "rust1", since = "1.0.0")] impl Default for AtomicBool { /// Creates an `AtomicBool` initialized to `false`. + #[inline] fn default() -> Self { Self::new(false) } @@ -1212,6 +1213,7 @@ macro_rules! atomic_int { #[$stable] impl Default for $atomic_type { + #[inline] fn default() -> Self { Self::new(Default::default()) }