From f66946570276f854c5a12c1a5051e4a46c42a21f Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Thu, 26 May 2022 13:30:27 +0400 Subject: [PATCH 1/2] Remove `(fn(...) -> ...)` -> `usize` -> `*const ()` -> `usize` cast --- core/src/fmt/mod.rs | 54 +++++++++++++++++++++++++-------------------- core/src/ptr/mod.rs | 14 ++---------- 2 files changed, 32 insertions(+), 36 deletions(-) diff --git a/core/src/fmt/mod.rs b/core/src/fmt/mod.rs index dde9bc383..63655ae8a 100644 --- a/core/src/fmt/mod.rs +++ b/core/src/fmt/mod.rs @@ -2233,35 +2233,41 @@ impl Display for char { #[stable(feature = "rust1", since = "1.0.0")] impl Pointer for *const T { fn fmt(&self, f: &mut Formatter<'_>) -> Result { - /// Since the formatting will be identical for all pointer types, use a non-monomorphized - /// implementation for the actual formatting to reduce the amount of codegen work needed - fn inner(ptr: *const (), f: &mut Formatter<'_>) -> Result { - let old_width = f.width; - let old_flags = f.flags; - - // The alternate flag is already treated by LowerHex as being special- - // it denotes whether to prefix with 0x. We use it to work out whether - // or not to zero extend, and then unconditionally set it to get the - // prefix. - if f.alternate() { - f.flags |= 1 << (FlagV1::SignAwareZeroPad as u32); - - if f.width.is_none() { - f.width = Some((usize::BITS / 4) as usize + 2); - } - } - f.flags |= 1 << (FlagV1::Alternate as u32); + // Cast is needed here because `.addr()` requires `T: Sized`. + pointer_fmt_inner((*self as *const ()).addr(), f) + } +} - let ret = LowerHex::fmt(&(ptr.addr()), f); +/// Since the formatting will be identical for all pointer types, use a non-monomorphized +/// implementation for the actual formatting to reduce the amount of codegen work needed. +/// +/// This uses `ptr_addr: usize` and not `ptr: *const ()` to be able to use this for +/// `fn(...) -> ...` without using [problematic] "Oxford Casts". +/// +/// [problematic]: https://github.com/rust-lang/rust/issues/95489 +pub(crate) fn pointer_fmt_inner(ptr_addr: usize, f: &mut Formatter<'_>) -> Result { + let old_width = f.width; + let old_flags = f.flags; - f.width = old_width; - f.flags = old_flags; + // The alternate flag is already treated by LowerHex as being special- + // it denotes whether to prefix with 0x. We use it to work out whether + // or not to zero extend, and then unconditionally set it to get the + // prefix. + if f.alternate() { + f.flags |= 1 << (FlagV1::SignAwareZeroPad as u32); - ret + if f.width.is_none() { + f.width = Some((usize::BITS / 4) as usize + 2); } - - inner(*self as *const (), f) } + f.flags |= 1 << (FlagV1::Alternate as u32); + + let ret = LowerHex::fmt(&ptr_addr, f); + + f.width = old_width; + f.flags = old_flags; + + ret } #[stable(feature = "rust1", since = "1.0.0")] diff --git a/core/src/ptr/mod.rs b/core/src/ptr/mod.rs index dc229c9ff..8990bbc38 100644 --- a/core/src/ptr/mod.rs +++ b/core/src/ptr/mod.rs @@ -1831,24 +1831,14 @@ macro_rules! fnptr_impls_safety_abi { #[stable(feature = "fnptr_impls", since = "1.4.0")] impl fmt::Pointer for $FnTy { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - // HACK: The intermediate cast as usize is required for AVR - // so that the address space of the source function pointer - // is preserved in the final function pointer. - // - // https://github.com/avr-rust/rust/issues/143 - fmt::Pointer::fmt(&(*self as usize as *const ()), f) + fmt::pointer_fmt_inner(*self as usize, f) } } #[stable(feature = "fnptr_impls", since = "1.4.0")] impl fmt::Debug for $FnTy { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - // HACK: The intermediate cast as usize is required for AVR - // so that the address space of the source function pointer - // is preserved in the final function pointer. - // - // https://github.com/avr-rust/rust/issues/143 - fmt::Pointer::fmt(&(*self as usize as *const ()), f) + fmt::pointer_fmt_inner(*self as usize, f) } } } From 693b55df61602915c5eb876199335c51fe3c8916 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Thu, 2 Jun 2022 10:49:22 +1000 Subject: [PATCH 2/2] Revert #96682. The change was "Show invisible delimiters (within comments) when pretty printing". It's useful to show these delimiters, but is a breaking change for some proc macros. Fixes #97608. --- proc_macro/src/lib.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/proc_macro/src/lib.rs b/proc_macro/src/lib.rs index 6f7c6305a..f1c5eaad8 100644 --- a/proc_macro/src/lib.rs +++ b/proc_macro/src/lib.rs @@ -703,12 +703,11 @@ pub enum Delimiter { /// `[ ... ]` #[stable(feature = "proc_macro_lib2", since = "1.29.0")] Bracket, - /// `/*«*/ ... /*»*/` + /// `Ø ... Ø` /// An invisible delimiter, that may, for example, appear around tokens coming from a /// "macro variable" `$var`. It is important to preserve operator priorities in cases like /// `$var * 3` where `$var` is `1 + 2`. - /// Invisible delimiters are not directly writable in normal Rust code except as comments. - /// Therefore, they might not survive a roundtrip of a token stream through a string. + /// Invisible delimiters might not survive roundtrip of a token stream through a string. #[stable(feature = "proc_macro_lib2", since = "1.29.0")] None, }