From ffb925c0f04cd2ba475ae923f92542a136ca3fbc Mon Sep 17 00:00:00 2001 From: Miguel Guarniz Date: Sun, 7 Aug 2022 13:54:01 -0400 Subject: [PATCH 1/8] Remove opt_remap_env_constness from rustc_query_impl Signed-off-by: Miguel Guarniz --- compiler/rustc_middle/src/ty/query.rs | 1 + compiler/rustc_query_impl/src/plumbing.rs | 12 +----------- 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_middle/src/ty/query.rs b/compiler/rustc_middle/src/ty/query.rs index 2452bcf6a61b8..2911be819662d 100644 --- a/compiler/rustc_middle/src/ty/query.rs +++ b/compiler/rustc_middle/src/ty/query.rs @@ -162,6 +162,7 @@ macro_rules! separate_provide_extern_default { }; } +#[macro_export] macro_rules! opt_remap_env_constness { ([][$name:ident]) => {}; ([(remap_env_constness) $($rest:tt)*][$name:ident]) => { diff --git a/compiler/rustc_query_impl/src/plumbing.rs b/compiler/rustc_query_impl/src/plumbing.rs index eda4401c81d01..5ebde81534b85 100644 --- a/compiler/rustc_query_impl/src/plumbing.rs +++ b/compiler/rustc_query_impl/src/plumbing.rs @@ -233,21 +233,11 @@ macro_rules! get_provider { }; } -macro_rules! opt_remap_env_constness { - ([][$name:ident]) => {}; - ([(remap_env_constness) $($rest:tt)*][$name:ident]) => { - let $name = $name.without_const(); - }; - ([$other:tt $($modifiers:tt)*][$name:ident]) => { - opt_remap_env_constness!([$($modifiers)*][$name]) - }; -} - macro_rules! define_queries { (<$tcx:tt> $($(#[$attr:meta])* [$($modifiers:tt)*] fn $name:ident($($K:tt)*) -> $V:ty,)*) => { - + use rustc_middle::opt_remap_env_constness; define_queries_struct! { tcx: $tcx, input: ($(([$($modifiers)*] [$($attr)*] [$name]))*) From 3a37f0b7aed8b82bf797e555a5a1bf3c8f578514 Mon Sep 17 00:00:00 2001 From: Miguel Guarniz Date: Mon, 15 Aug 2022 14:36:03 -0400 Subject: [PATCH 2/8] Remove usages of opt_remap_env_constness Signed-off-by: Miguel Guarniz --- compiler/rustc_middle/src/ty/query.rs | 1 - compiler/rustc_query_impl/src/plumbing.rs | 3 --- 2 files changed, 4 deletions(-) diff --git a/compiler/rustc_middle/src/ty/query.rs b/compiler/rustc_middle/src/ty/query.rs index 2911be819662d..2452bcf6a61b8 100644 --- a/compiler/rustc_middle/src/ty/query.rs +++ b/compiler/rustc_middle/src/ty/query.rs @@ -162,7 +162,6 @@ macro_rules! separate_provide_extern_default { }; } -#[macro_export] macro_rules! opt_remap_env_constness { ([][$name:ident]) => {}; ([(remap_env_constness) $($rest:tt)*][$name:ident]) => { diff --git a/compiler/rustc_query_impl/src/plumbing.rs b/compiler/rustc_query_impl/src/plumbing.rs index 5ebde81534b85..339683cf68958 100644 --- a/compiler/rustc_query_impl/src/plumbing.rs +++ b/compiler/rustc_query_impl/src/plumbing.rs @@ -237,7 +237,6 @@ macro_rules! define_queries { (<$tcx:tt> $($(#[$attr:meta])* [$($modifiers:tt)*] fn $name:ident($($K:tt)*) -> $V:ty,)*) => { - use rustc_middle::opt_remap_env_constness; define_queries_struct! { tcx: $tcx, input: ($(([$($modifiers)*] [$($attr)*] [$name]))*) @@ -249,7 +248,6 @@ macro_rules! define_queries { // Create an eponymous constructor for each query. $(#[allow(nonstandard_style)] $(#[$attr])* pub fn $name<$tcx>(tcx: QueryCtxt<$tcx>, key: query_keys::$name<$tcx>) -> QueryStackFrame { - opt_remap_env_constness!([$($modifiers)*][key]); let kind = dep_graph::DepKind::$name; let name = stringify!($name); // Disable visible paths printing for performance reasons. @@ -539,7 +537,6 @@ macro_rules! define_queries_struct { key: query_keys::$name<$tcx>, mode: QueryMode, ) -> Option> { - opt_remap_env_constness!([$($modifiers)*][key]); let qcx = QueryCtxt { tcx, queries: self }; get_query::, _>(qcx, span, key, mode) })* From 1f7d1eae9677f16a94a15b58d2b2477ad0776bd7 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 16 Aug 2022 15:33:46 +0200 Subject: [PATCH 3/8] Use `merged_ty` method instead of rewriting it every time --- compiler/rustc_typeck/src/check/coercion.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_typeck/src/check/coercion.rs b/compiler/rustc_typeck/src/check/coercion.rs index da575cb1367b8..def592c46c2e5 100644 --- a/compiler/rustc_typeck/src/check/coercion.rs +++ b/compiler/rustc_typeck/src/check/coercion.rs @@ -1488,14 +1488,14 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> { // `break`, we want to call the `()` "expected" // since it is implied by the syntax. // (Note: not all force-units work this way.)" - (expression_ty, self.final_ty.unwrap_or(self.expected_ty)) + (expression_ty, self.merged_ty()) } else { // Otherwise, the "expected" type for error // reporting is the current unification type, // which is basically the LUB of the expressions // we've seen so far (combined with the expected // type) - (self.final_ty.unwrap_or(self.expected_ty), expression_ty) + (self.merged_ty(), expression_ty) }; let (expected, found) = fcx.resolve_vars_if_possible((expected, found)); From dbeb506eaefdda5d9d7927c4d10d6d13a336eaf3 Mon Sep 17 00:00:00 2001 From: Martin Nordholts Date: Tue, 16 Aug 2022 15:37:31 +0200 Subject: [PATCH 4/8] rustdoc JSON: Fix ICE with `pub extern crate self as ` --- src/librustdoc/json/mod.rs | 4 ++-- .../reexport/export_extern_crate_as_self.rs | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 src/test/rustdoc-json/reexport/export_extern_crate_as_self.rs diff --git a/src/librustdoc/json/mod.rs b/src/librustdoc/json/mod.rs index 6364d00d0624e..7b1b059e14dcb 100644 --- a/src/librustdoc/json/mod.rs +++ b/src/librustdoc/json/mod.rs @@ -209,11 +209,11 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> { } types::ItemEnum::Method(_) + | types::ItemEnum::Module(_) | types::ItemEnum::AssocConst { .. } | types::ItemEnum::AssocType { .. } | types::ItemEnum::PrimitiveType(_) => true, - types::ItemEnum::Module(_) - | types::ItemEnum::ExternCrate { .. } + types::ItemEnum::ExternCrate { .. } | types::ItemEnum::Import(_) | types::ItemEnum::StructField(_) | types::ItemEnum::Variant(_) diff --git a/src/test/rustdoc-json/reexport/export_extern_crate_as_self.rs b/src/test/rustdoc-json/reexport/export_extern_crate_as_self.rs new file mode 100644 index 0000000000000..fda38056a094a --- /dev/null +++ b/src/test/rustdoc-json/reexport/export_extern_crate_as_self.rs @@ -0,0 +1,11 @@ +//! Regression test for + +#![feature(no_core)] +#![no_core] + +#![crate_name = "export_extern_crate_as_self"] + +// ignore-tidy-linelength + +// @is export_extern_crate_as_self.json "$.index[*][?(@.kind=='module')].name" \"export_extern_crate_as_self\" +pub extern crate self as export_extern_crate_as_self; // Must be the same name as the crate already has From 5a11b814d4cd84d43f7b96a81f3e1595187c26a1 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Tue, 16 Aug 2022 14:42:00 +0200 Subject: [PATCH 5/8] Add `IpDisplayBuffer` helper struct. --- library/std/src/net/ip.rs | 79 ++++++++++++++++++++++++--------------- 1 file changed, 48 insertions(+), 31 deletions(-) diff --git a/library/std/src/net/ip.rs b/library/std/src/net/ip.rs index 41ca9ba842588..f4fba7f73869f 100644 --- a/library/std/src/net/ip.rs +++ b/library/std/src/net/ip.rs @@ -3,12 +3,44 @@ mod tests; use crate::cmp::Ordering; -use crate::fmt::{self, Write as FmtWrite}; -use crate::io::Write as IoWrite; +use crate::fmt::{self, Write}; use crate::mem::transmute; +use crate::str; use crate::sys::net::netc as c; use crate::sys_common::{FromInner, IntoInner}; +/// Used for slow path in `Display` implementations when alignment is required. +struct IpDisplayBuffer { + buf: [u8; SIZE], + len: usize, +} + +impl IpDisplayBuffer { + #[inline(always)] + pub const fn new(_ip: &[u8; SIZE]) -> Self { + Self { buf: [0; SIZE], len: 0 } + } + + #[inline(always)] + pub fn as_str(&self) -> &str { + // SAFETY: `buf` is only written to by the `fmt::Write::write_str` implementation + // which writes a valid UTF-8 string to `buf` and correctly sets `len`. + unsafe { str::from_utf8_unchecked(&self.buf[..self.len]) } + } +} + +impl fmt::Write for IpDisplayBuffer { + fn write_str(&mut self, s: &str) -> fmt::Result { + if let Some(buf) = self.buf.get_mut(self.len..(self.len + s.len())) { + buf.copy_from_slice(s.as_bytes()); + self.len += s.len(); + Ok(()) + } else { + Err(fmt::Error) + } + } +} + /// An IP address, either IPv4 or IPv6. /// /// This enum can contain either an [`Ipv4Addr`] or an [`Ipv6Addr`], see their @@ -991,21 +1023,17 @@ impl From for IpAddr { impl fmt::Display for Ipv4Addr { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { let octets = self.octets(); - // Fast Path: if there's no alignment stuff, write directly to the buffer + + // If there are no alignment requirements, write the IP address directly to `f`. + // Otherwise, write it to a local buffer and then use `f.pad`. if fmt.precision().is_none() && fmt.width().is_none() { write!(fmt, "{}.{}.{}.{}", octets[0], octets[1], octets[2], octets[3]) } else { - const IPV4_BUF_LEN: usize = 15; // Long enough for the longest possible IPv4 address - let mut buf = [0u8; IPV4_BUF_LEN]; - let mut buf_slice = &mut buf[..]; - - // Note: The call to write should never fail, hence the unwrap - write!(buf_slice, "{}.{}.{}.{}", octets[0], octets[1], octets[2], octets[3]).unwrap(); - let len = IPV4_BUF_LEN - buf_slice.len(); + let mut buf = IpDisplayBuffer::new(b"255.255.255.255"); + // Buffer is long enough for the longest possible IPv4 address, so this should never fail. + write!(buf, "{}.{}.{}.{}", octets[0], octets[1], octets[2], octets[3]).unwrap(); - // This unsafe is OK because we know what is being written to the buffer - let buf = unsafe { crate::str::from_utf8_unchecked(&buf[..len]) }; - fmt.pad(buf) + fmt.pad(buf.as_str()) } } } @@ -1708,8 +1736,8 @@ impl Ipv6Addr { #[stable(feature = "rust1", since = "1.0.0")] impl fmt::Display for Ipv6Addr { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - // If there are no alignment requirements, write out the IP address to - // f. Otherwise, write it to a local buffer, then use f.pad. + // If there are no alignment requirements, write the IP address directly to `f`. + // Otherwise, write it to a local buffer and then use `f.pad`. if f.precision().is_none() && f.width().is_none() { let segments = self.segments(); @@ -1780,22 +1808,11 @@ impl fmt::Display for Ipv6Addr { } } } else { - // Slow path: write the address to a local buffer, then use f.pad. - // Defined recursively by using the fast path to write to the - // buffer. - - // This is the largest possible size of an IPv6 address - const IPV6_BUF_LEN: usize = (4 * 8) + 7; - let mut buf = [0u8; IPV6_BUF_LEN]; - let mut buf_slice = &mut buf[..]; - - // Note: This call to write should never fail, so unwrap is okay. - write!(buf_slice, "{}", self).unwrap(); - let len = IPV6_BUF_LEN - buf_slice.len(); - - // This is safe because we know exactly what can be in this buffer - let buf = unsafe { crate::str::from_utf8_unchecked(&buf[..len]) }; - f.pad(buf) + let mut buf = IpDisplayBuffer::new(b"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); + // Buffer is long enough for the longest possible IPv6 address, so this should never fail. + write!(buf, "{}", self).unwrap(); + + f.pad(buf.as_str()) } } } From 033e9d66ffa918b2cd78649c5b8cba372f58f9e4 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Tue, 16 Aug 2022 17:48:55 +0200 Subject: [PATCH 6/8] Move `IpDisplayBuffer` into submodule. --- library/std/src/net/ip.rs | 34 ++---------------------- library/std/src/net/ip/display_buffer.rs | 34 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 32 deletions(-) create mode 100644 library/std/src/net/ip/display_buffer.rs diff --git a/library/std/src/net/ip.rs b/library/std/src/net/ip.rs index f4fba7f73869f..198dcff12b95c 100644 --- a/library/std/src/net/ip.rs +++ b/library/std/src/net/ip.rs @@ -5,41 +5,11 @@ mod tests; use crate::cmp::Ordering; use crate::fmt::{self, Write}; use crate::mem::transmute; -use crate::str; use crate::sys::net::netc as c; use crate::sys_common::{FromInner, IntoInner}; -/// Used for slow path in `Display` implementations when alignment is required. -struct IpDisplayBuffer { - buf: [u8; SIZE], - len: usize, -} - -impl IpDisplayBuffer { - #[inline(always)] - pub const fn new(_ip: &[u8; SIZE]) -> Self { - Self { buf: [0; SIZE], len: 0 } - } - - #[inline(always)] - pub fn as_str(&self) -> &str { - // SAFETY: `buf` is only written to by the `fmt::Write::write_str` implementation - // which writes a valid UTF-8 string to `buf` and correctly sets `len`. - unsafe { str::from_utf8_unchecked(&self.buf[..self.len]) } - } -} - -impl fmt::Write for IpDisplayBuffer { - fn write_str(&mut self, s: &str) -> fmt::Result { - if let Some(buf) = self.buf.get_mut(self.len..(self.len + s.len())) { - buf.copy_from_slice(s.as_bytes()); - self.len += s.len(); - Ok(()) - } else { - Err(fmt::Error) - } - } -} +mod display_buffer; +use display_buffer::IpDisplayBuffer; /// An IP address, either IPv4 or IPv6. /// diff --git a/library/std/src/net/ip/display_buffer.rs b/library/std/src/net/ip/display_buffer.rs new file mode 100644 index 0000000000000..06acfa5e21522 --- /dev/null +++ b/library/std/src/net/ip/display_buffer.rs @@ -0,0 +1,34 @@ +use crate::fmt; +use crate::str; + +/// Used for slow path in `Display` implementations when alignment is required. +pub struct IpDisplayBuffer { + buf: [u8; SIZE], + len: usize, +} + +impl IpDisplayBuffer { + #[inline(always)] + pub const fn new(_ip: &[u8; SIZE]) -> Self { + Self { buf: [0; SIZE], len: 0 } + } + + #[inline(always)] + pub fn as_str(&self) -> &str { + // SAFETY: `buf` is only written to by the `fmt::Write::write_str` implementation + // which writes a valid UTF-8 string to `buf` and correctly sets `len`. + unsafe { str::from_utf8_unchecked(&self.buf[..self.len]) } + } +} + +impl fmt::Write for IpDisplayBuffer { + fn write_str(&mut self, s: &str) -> fmt::Result { + if let Some(buf) = self.buf.get_mut(self.len..(self.len + s.len())) { + buf.copy_from_slice(s.as_bytes()); + self.len += s.len(); + Ok(()) + } else { + Err(fmt::Error) + } + } +} From 31540f5e15d5785a61d96869b373f7ed4d4a3654 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Tue, 16 Aug 2022 18:12:06 +0200 Subject: [PATCH 7/8] Use `MaybeUninit` for `IpDisplayBuffer`. --- library/std/src/lib.rs | 2 ++ library/std/src/net/ip/display_buffer.rs | 18 ++++++++++++------ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/library/std/src/lib.rs b/library/std/src/lib.rs index 475a1d9fd9920..a8d6645794ae5 100644 --- a/library/std/src/lib.rs +++ b/library/std/src/lib.rs @@ -294,6 +294,8 @@ #![feature(std_internals)] #![feature(str_internals)] #![feature(strict_provenance)] +#![feature(maybe_uninit_uninit_array)] +#![feature(const_maybe_uninit_uninit_array)] // // Library features (alloc): #![feature(alloc_layout_extra)] diff --git a/library/std/src/net/ip/display_buffer.rs b/library/std/src/net/ip/display_buffer.rs index 06acfa5e21522..65f11b8ae93f5 100644 --- a/library/std/src/net/ip/display_buffer.rs +++ b/library/std/src/net/ip/display_buffer.rs @@ -1,31 +1,37 @@ use crate::fmt; +use crate::mem::MaybeUninit; use crate::str; /// Used for slow path in `Display` implementations when alignment is required. pub struct IpDisplayBuffer { - buf: [u8; SIZE], + buf: [MaybeUninit; SIZE], len: usize, } impl IpDisplayBuffer { #[inline(always)] pub const fn new(_ip: &[u8; SIZE]) -> Self { - Self { buf: [0; SIZE], len: 0 } + Self { buf: MaybeUninit::uninit_array::(), len: 0 } } #[inline(always)] pub fn as_str(&self) -> &str { // SAFETY: `buf` is only written to by the `fmt::Write::write_str` implementation // which writes a valid UTF-8 string to `buf` and correctly sets `len`. - unsafe { str::from_utf8_unchecked(&self.buf[..self.len]) } + unsafe { + let s = MaybeUninit::slice_assume_init_ref(&self.buf[..self.len]); + str::from_utf8_unchecked(s) + } } } impl fmt::Write for IpDisplayBuffer { fn write_str(&mut self, s: &str) -> fmt::Result { - if let Some(buf) = self.buf.get_mut(self.len..(self.len + s.len())) { - buf.copy_from_slice(s.as_bytes()); - self.len += s.len(); + let bytes = s.as_bytes(); + + if let Some(buf) = self.buf.get_mut(self.len..(self.len + bytes.len())) { + MaybeUninit::write_slice(buf, bytes); + self.len += bytes.len(); Ok(()) } else { Err(fmt::Error) From 44d62425b9e08c1eebd329766b5e4dd7f8bc3216 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Tue, 16 Aug 2022 19:32:00 +0200 Subject: [PATCH 8/8] Simplify `IpDisplayBuffer` API. --- library/std/src/net/ip.rs | 8 ++++++-- library/std/src/net/ip/display_buffer.rs | 8 ++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/library/std/src/net/ip.rs b/library/std/src/net/ip.rs index 198dcff12b95c..189754a161e71 100644 --- a/library/std/src/net/ip.rs +++ b/library/std/src/net/ip.rs @@ -999,7 +999,9 @@ impl fmt::Display for Ipv4Addr { if fmt.precision().is_none() && fmt.width().is_none() { write!(fmt, "{}.{}.{}.{}", octets[0], octets[1], octets[2], octets[3]) } else { - let mut buf = IpDisplayBuffer::new(b"255.255.255.255"); + const LONGEST_IPV4_ADDR: &str = "255.255.255.255"; + + let mut buf = IpDisplayBuffer::<{ LONGEST_IPV4_ADDR.len() }>::new(); // Buffer is long enough for the longest possible IPv4 address, so this should never fail. write!(buf, "{}.{}.{}.{}", octets[0], octets[1], octets[2], octets[3]).unwrap(); @@ -1778,7 +1780,9 @@ impl fmt::Display for Ipv6Addr { } } } else { - let mut buf = IpDisplayBuffer::new(b"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); + const LONGEST_IPV6_ADDR: &str = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"; + + let mut buf = IpDisplayBuffer::<{ LONGEST_IPV6_ADDR.len() }>::new(); // Buffer is long enough for the longest possible IPv6 address, so this should never fail. write!(buf, "{}", self).unwrap(); diff --git a/library/std/src/net/ip/display_buffer.rs b/library/std/src/net/ip/display_buffer.rs index 65f11b8ae93f5..bd852d5da8ec5 100644 --- a/library/std/src/net/ip/display_buffer.rs +++ b/library/std/src/net/ip/display_buffer.rs @@ -9,12 +9,12 @@ pub struct IpDisplayBuffer { } impl IpDisplayBuffer { - #[inline(always)] - pub const fn new(_ip: &[u8; SIZE]) -> Self { - Self { buf: MaybeUninit::uninit_array::(), len: 0 } + #[inline] + pub const fn new() -> Self { + Self { buf: MaybeUninit::uninit_array(), len: 0 } } - #[inline(always)] + #[inline] pub fn as_str(&self) -> &str { // SAFETY: `buf` is only written to by the `fmt::Write::write_str` implementation // which writes a valid UTF-8 string to `buf` and correctly sets `len`.