From debf305d9e0a21244339820923bbbc1f8589a39d Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Thu, 20 Apr 2023 18:01:59 +0200 Subject: [PATCH 1/8] Don't reexport core::fmt::rt from alloc::fmt. --- library/alloc/src/fmt.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/library/alloc/src/fmt.rs b/library/alloc/src/fmt.rs index 1da86e1a46a57..e03b501ae4d23 100644 --- a/library/alloc/src/fmt.rs +++ b/library/alloc/src/fmt.rs @@ -551,8 +551,6 @@ #![stable(feature = "rust1", since = "1.0.0")] -#[unstable(feature = "fmt_internals", issue = "none")] -pub use core::fmt::rt; #[stable(feature = "fmt_flags_align", since = "1.28.0")] pub use core::fmt::Alignment; #[stable(feature = "rust1", since = "1.0.0")] From a77159341e12e02d7f738c709b9798e68bc4af04 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Thu, 20 Apr 2023 18:03:47 +0200 Subject: [PATCH 2/8] Use fmt::Alignment instead of fmt::rt::v1::Alignment. --- library/core/src/fmt/mod.rs | 24 ++++++++++++------------ library/core/src/time.rs | 2 +- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/library/core/src/fmt/mod.rs b/library/core/src/fmt/mod.rs index fcda097f01fce..af40dcdc5f0de 100644 --- a/library/core/src/fmt/mod.rs +++ b/library/core/src/fmt/mod.rs @@ -1451,7 +1451,7 @@ impl<'a> Formatter<'a> { let old_fill = crate::mem::replace(&mut self.fill, '0'); let old_align = crate::mem::replace(&mut self.align, rt::v1::Alignment::Right); write_prefix(self, sign, prefix)?; - let post_padding = self.padding(min - width, rt::v1::Alignment::Right)?; + let post_padding = self.padding(min - width, Alignment::Right)?; self.buf.write_str(buf)?; post_padding.write(self)?; self.fill = old_fill; @@ -1460,7 +1460,7 @@ impl<'a> Formatter<'a> { } // Otherwise, the sign and prefix goes after the padding Some(min) => { - let post_padding = self.padding(min - width, rt::v1::Alignment::Right)?; + let post_padding = self.padding(min - width, Alignment::Right)?; write_prefix(self, sign, prefix)?; self.buf.write_str(buf)?; post_padding.write(self) @@ -1535,7 +1535,7 @@ impl<'a> Formatter<'a> { // If we're under both the maximum and the minimum width, then fill // up the minimum width with the specified string + some alignment. else { - let align = rt::v1::Alignment::Left; + let align = Alignment::Left; let post_padding = self.padding(width - chars_count, align)?; self.buf.write_str(s)?; post_padding.write(self) @@ -1550,17 +1550,19 @@ impl<'a> Formatter<'a> { pub(crate) fn padding( &mut self, padding: usize, - default: rt::v1::Alignment, + default: Alignment, ) -> result::Result { let align = match self.align { rt::v1::Alignment::Unknown => default, - _ => self.align, + rt::v1::Alignment::Left => Alignment::Left, + rt::v1::Alignment::Right => Alignment::Right, + rt::v1::Alignment::Center => Alignment::Center, }; let (pre_pad, post_pad) = match align { - rt::v1::Alignment::Left => (0, padding), - rt::v1::Alignment::Right | rt::v1::Alignment::Unknown => (padding, 0), - rt::v1::Alignment::Center => (padding / 2, (padding + 1) / 2), + Alignment::Left => (0, padding), + Alignment::Right => (padding, 0), + Alignment::Center => (padding / 2, (padding + 1) / 2), }; for _ in 0..pre_pad { @@ -1580,7 +1582,6 @@ impl<'a> Formatter<'a> { let mut formatted = formatted.clone(); let old_fill = self.fill; let old_align = self.align; - let mut align = old_align; if self.sign_aware_zero_pad() { // a sign always goes first let sign = formatted.sign; @@ -1589,9 +1590,8 @@ impl<'a> Formatter<'a> { // remove the sign from the formatted parts formatted.sign = ""; width = width.saturating_sub(sign.len()); - align = rt::v1::Alignment::Right; self.fill = '0'; - self.align = rt::v1::Alignment::Right; + self.align = rt::Alignment::Right; } // remaining parts go through the ordinary padding process. @@ -1600,7 +1600,7 @@ impl<'a> Formatter<'a> { // no padding self.write_formatted_parts(&formatted) } else { - let post_padding = self.padding(width - len, align)?; + let post_padding = self.padding(width - len, Alignment::Right)?; self.write_formatted_parts(&formatted)?; post_padding.write(self) }; diff --git a/library/core/src/time.rs b/library/core/src/time.rs index b74fe01366594..b08d5782ab6f4 100644 --- a/library/core/src/time.rs +++ b/library/core/src/time.rs @@ -1172,7 +1172,7 @@ impl fmt::Debug for Duration { emit_without_padding(f) } else { // We need to add padding. Use the `Formatter::padding` helper function. - let default_align = crate::fmt::rt::v1::Alignment::Left; + let default_align = fmt::Alignment::Left; let post_padding = f.padding(requested_w - actual_w, default_align)?; emit_without_padding(f)?; post_padding.write(f) From bc11b459af451e4c35bf611a0baacba2b7919629 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Thu, 20 Apr 2023 18:04:28 +0200 Subject: [PATCH 3/8] Turn core::fmt::rt::v1 into a private module. --- library/core/src/fmt/mod.rs | 47 +++++++++++------------- library/core/src/fmt/{rt/v1.rs => rt.rs} | 8 ++-- 2 files changed, 24 insertions(+), 31 deletions(-) rename library/core/src/fmt/{rt/v1.rs => rt.rs} (82%) diff --git a/library/core/src/fmt/mod.rs b/library/core/src/fmt/mod.rs index af40dcdc5f0de..6988411095f0d 100644 --- a/library/core/src/fmt/mod.rs +++ b/library/core/src/fmt/mod.rs @@ -18,6 +18,7 @@ mod float; #[cfg(no_fp_fmt_parse)] mod nofloat; mod num; +mod rt; #[stable(feature = "fmt_flags_align", since = "1.28.0")] #[cfg_attr(not(test), rustc_diagnostic_item = "Alignment")] @@ -38,12 +39,6 @@ pub enum Alignment { #[stable(feature = "debug_builders", since = "1.2.0")] pub use self::builders::{DebugList, DebugMap, DebugSet, DebugStruct, DebugTuple}; -#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")] -#[doc(hidden)] -pub mod rt { - pub mod v1; -} - /// The type returned by formatter methods. /// /// # Examples @@ -227,7 +222,7 @@ impl Write for &mut W { pub struct Formatter<'a> { flags: u32, fill: char, - align: rt::v1::Alignment, + align: rt::Alignment, width: Option, precision: Option, @@ -248,7 +243,7 @@ impl<'a> Formatter<'a> { Formatter { flags: 0, fill: ' ', - align: rt::v1::Alignment::Unknown, + align: rt::Alignment::Unknown, width: None, precision: None, buf, @@ -433,9 +428,9 @@ impl<'a> Arguments<'a> { /// An `UnsafeArg` is required because the following invariants must be held /// in order for this function to be safe: /// 1. The `pieces` slice must be at least as long as `fmt`. - /// 2. Every [`rt::v1::Argument::position`] value within `fmt` must be a + /// 2. Every [`rt::Argument::position`] value within `fmt` must be a /// valid index of `args`. - /// 3. Every [`rt::v1::Count::Param`] within `fmt` must contain a valid index of + /// 3. Every [`rt::Count::Param`] within `fmt` must contain a valid index of /// `args`. #[doc(hidden)] #[inline] @@ -443,7 +438,7 @@ impl<'a> Arguments<'a> { pub fn new_v1_formatted( pieces: &'a [&'static str], args: &'a [ArgumentV1<'a>], - fmt: &'a [rt::v1::Argument], + fmt: &'a [rt::Argument], _unsafe_arg: UnsafeArg, ) -> Arguments<'a> { Arguments { pieces, fmt: Some(fmt), args } @@ -505,7 +500,7 @@ pub struct Arguments<'a> { pieces: &'a [&'static str], // Placeholder specs, or `None` if all specs are default (as in "{}{}"). - fmt: Option<&'a [rt::v1::Argument]>, + fmt: Option<&'a [rt::Argument]>, // Dynamic arguments for interpolation, to be interleaved with string // pieces. (Every argument is preceded by a string piece.) @@ -1281,7 +1276,7 @@ pub fn write(output: &mut dyn Write, args: Arguments<'_>) -> Result { Ok(()) } -unsafe fn run(fmt: &mut Formatter<'_>, arg: &rt::v1::Argument, args: &[ArgumentV1<'_>]) -> Result { +unsafe fn run(fmt: &mut Formatter<'_>, arg: &rt::Argument, args: &[ArgumentV1<'_>]) -> Result { fmt.fill = arg.format.fill; fmt.align = arg.format.align; fmt.flags = arg.format.flags; @@ -1302,11 +1297,11 @@ unsafe fn run(fmt: &mut Formatter<'_>, arg: &rt::v1::Argument, args: &[ArgumentV (value.formatter)(value.value, fmt) } -unsafe fn getcount(args: &[ArgumentV1<'_>], cnt: &rt::v1::Count) -> Option { +unsafe fn getcount(args: &[ArgumentV1<'_>], cnt: &rt::Count) -> Option { match *cnt { - rt::v1::Count::Is(n) => Some(n), - rt::v1::Count::Implied => None, - rt::v1::Count::Param(i) => { + rt::Count::Is(n) => Some(n), + rt::Count::Implied => None, + rt::Count::Param(i) => { debug_assert!(i < args.len()); // SAFETY: cnt and args come from the same Arguments, // which guarantees this index is always within bounds. @@ -1449,7 +1444,7 @@ impl<'a> Formatter<'a> { // is zero Some(min) if self.sign_aware_zero_pad() => { let old_fill = crate::mem::replace(&mut self.fill, '0'); - let old_align = crate::mem::replace(&mut self.align, rt::v1::Alignment::Right); + let old_align = crate::mem::replace(&mut self.align, rt::Alignment::Right); write_prefix(self, sign, prefix)?; let post_padding = self.padding(min - width, Alignment::Right)?; self.buf.write_str(buf)?; @@ -1553,10 +1548,10 @@ impl<'a> Formatter<'a> { default: Alignment, ) -> result::Result { let align = match self.align { - rt::v1::Alignment::Unknown => default, - rt::v1::Alignment::Left => Alignment::Left, - rt::v1::Alignment::Right => Alignment::Right, - rt::v1::Alignment::Center => Alignment::Center, + rt::Alignment::Unknown => default, + rt::Alignment::Left => Alignment::Left, + rt::Alignment::Right => Alignment::Right, + rt::Alignment::Center => Alignment::Center, }; let (pre_pad, post_pad) = match align { @@ -1788,10 +1783,10 @@ impl<'a> Formatter<'a> { #[stable(feature = "fmt_flags_align", since = "1.28.0")] pub fn align(&self) -> Option { match self.align { - rt::v1::Alignment::Left => Some(Alignment::Left), - rt::v1::Alignment::Right => Some(Alignment::Right), - rt::v1::Alignment::Center => Some(Alignment::Center), - rt::v1::Alignment::Unknown => None, + rt::Alignment::Left => Some(Alignment::Left), + rt::Alignment::Right => Some(Alignment::Right), + rt::Alignment::Center => Some(Alignment::Center), + rt::Alignment::Unknown => None, } } diff --git a/library/core/src/fmt/rt/v1.rs b/library/core/src/fmt/rt.rs similarity index 82% rename from library/core/src/fmt/rt/v1.rs rename to library/core/src/fmt/rt.rs index 6d70796f70718..58dbe314d08be 100644 --- a/library/core/src/fmt/rt/v1.rs +++ b/library/core/src/fmt/rt.rs @@ -1,9 +1,7 @@ -//! This is an internal module used by the ifmt! runtime. These structures are -//! emitted to static arrays to precompile format strings ahead of time. -//! -//! These definitions are similar to their `ct` equivalents, but differ in that -//! these can be statically allocated and are slightly optimized for the runtime #![allow(missing_debug_implementations)] +#![unstable(feature = "fmt_internals", issue = "none")] + +//! These are the lang items used by format_args!(). #[lang = "format_placeholder"] #[derive(Copy, Clone)] From 938efe6f498b24734e68647aefb1e17459ef49b2 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Thu, 20 Apr 2023 18:05:35 +0200 Subject: [PATCH 4/8] Rename fmt::rt::Argument to Placeholder. --- library/core/src/fmt/mod.rs | 8 ++++---- library/core/src/fmt/rt.rs | 5 ++--- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/library/core/src/fmt/mod.rs b/library/core/src/fmt/mod.rs index 6988411095f0d..bbbfe06656cd7 100644 --- a/library/core/src/fmt/mod.rs +++ b/library/core/src/fmt/mod.rs @@ -428,7 +428,7 @@ impl<'a> Arguments<'a> { /// An `UnsafeArg` is required because the following invariants must be held /// in order for this function to be safe: /// 1. The `pieces` slice must be at least as long as `fmt`. - /// 2. Every [`rt::Argument::position`] value within `fmt` must be a + /// 2. Every [`rt::Placeholder::position`] value within `fmt` must be a /// valid index of `args`. /// 3. Every [`rt::Count::Param`] within `fmt` must contain a valid index of /// `args`. @@ -438,7 +438,7 @@ impl<'a> Arguments<'a> { pub fn new_v1_formatted( pieces: &'a [&'static str], args: &'a [ArgumentV1<'a>], - fmt: &'a [rt::Argument], + fmt: &'a [rt::Placeholder], _unsafe_arg: UnsafeArg, ) -> Arguments<'a> { Arguments { pieces, fmt: Some(fmt), args } @@ -500,7 +500,7 @@ pub struct Arguments<'a> { pieces: &'a [&'static str], // Placeholder specs, or `None` if all specs are default (as in "{}{}"). - fmt: Option<&'a [rt::Argument]>, + fmt: Option<&'a [rt::Placeholder]>, // Dynamic arguments for interpolation, to be interleaved with string // pieces. (Every argument is preceded by a string piece.) @@ -1276,7 +1276,7 @@ pub fn write(output: &mut dyn Write, args: Arguments<'_>) -> Result { Ok(()) } -unsafe fn run(fmt: &mut Formatter<'_>, arg: &rt::Argument, args: &[ArgumentV1<'_>]) -> Result { +unsafe fn run(fmt: &mut Formatter<'_>, arg: &rt::Placeholder, args: &[ArgumentV1<'_>]) -> Result { fmt.fill = arg.format.fill; fmt.align = arg.format.align; fmt.flags = arg.format.flags; diff --git a/library/core/src/fmt/rt.rs b/library/core/src/fmt/rt.rs index 58dbe314d08be..497b2e27739f9 100644 --- a/library/core/src/fmt/rt.rs +++ b/library/core/src/fmt/rt.rs @@ -5,8 +5,7 @@ #[lang = "format_placeholder"] #[derive(Copy, Clone)] -// FIXME: Rename this to Placeholder -pub struct Argument { +pub struct Placeholder { pub position: usize, pub format: FormatSpec, } @@ -20,7 +19,7 @@ pub struct FormatSpec { pub width: Count, } -impl Argument { +impl Placeholder { #[inline(always)] pub const fn new( position: usize, From bca80d811c44998e99014c4f85c1130c26cd9dd3 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Thu, 20 Apr 2023 18:07:34 +0200 Subject: [PATCH 5/8] Get rid of core::fmt::FormatSpec. --- library/core/src/fmt/mod.rs | 10 +++++----- library/core/src/fmt/rt.rs | 7 +------ 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/library/core/src/fmt/mod.rs b/library/core/src/fmt/mod.rs index bbbfe06656cd7..de958a82d9640 100644 --- a/library/core/src/fmt/mod.rs +++ b/library/core/src/fmt/mod.rs @@ -1277,14 +1277,14 @@ pub fn write(output: &mut dyn Write, args: Arguments<'_>) -> Result { } unsafe fn run(fmt: &mut Formatter<'_>, arg: &rt::Placeholder, args: &[ArgumentV1<'_>]) -> Result { - fmt.fill = arg.format.fill; - fmt.align = arg.format.align; - fmt.flags = arg.format.flags; + fmt.fill = arg.fill; + fmt.align = arg.align; + fmt.flags = arg.flags; // SAFETY: arg and args come from the same Arguments, // which guarantees the indexes are always within bounds. unsafe { - fmt.width = getcount(args, &arg.format.width); - fmt.precision = getcount(args, &arg.format.precision); + fmt.width = getcount(args, &arg.width); + fmt.precision = getcount(args, &arg.precision); } // Extract the correct argument diff --git a/library/core/src/fmt/rt.rs b/library/core/src/fmt/rt.rs index 497b2e27739f9..a431ee54d9508 100644 --- a/library/core/src/fmt/rt.rs +++ b/library/core/src/fmt/rt.rs @@ -7,11 +7,6 @@ #[derive(Copy, Clone)] pub struct Placeholder { pub position: usize, - pub format: FormatSpec, -} - -#[derive(Copy, Clone)] -pub struct FormatSpec { pub fill: char, pub align: Alignment, pub flags: u32, @@ -29,7 +24,7 @@ impl Placeholder { precision: Count, width: Count, ) -> Self { - Self { position, format: FormatSpec { fill, align, flags, precision, width } } + Self { position, fill, align, flags, precision, width } } } From 441682cca9a32314835a88fa981a3bf13ffb0aa8 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Thu, 20 Apr 2023 18:09:32 +0200 Subject: [PATCH 6/8] Update comments in format args lowering. These lang items have been moved/renamed. --- compiler/rustc_ast_lowering/src/format.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/format.rs b/compiler/rustc_ast_lowering/src/format.rs index c41bdc440935c..ccf481cb9b39d 100644 --- a/compiler/rustc_ast_lowering/src/format.rs +++ b/compiler/rustc_ast_lowering/src/format.rs @@ -220,19 +220,19 @@ fn make_argument<'hir>( /// Generates: /// /// ```text -/// ::Is(…) +/// ::Is(…) /// ``` /// /// or /// /// ```text -/// ::Param(…) +/// ::Param(…) /// ``` /// /// or /// /// ```text -/// ::Implied +/// ::Implied /// ``` fn make_count<'hir>( ctx: &mut LoweringContext<'_, 'hir>, @@ -278,13 +278,13 @@ fn make_count<'hir>( /// Generates /// /// ```text -/// ::…, // alignment +/// ::…, // alignment /// …u32, // flags -/// , // width -/// , // precision +/// , // width +/// , // precision /// ) /// ``` fn make_format_spec<'hir>( From 687b3fa4396ed45eee967e223750e9aa3b4b5ee9 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Thu, 20 Apr 2023 18:47:47 +0200 Subject: [PATCH 7/8] Remove doc link to private item. --- library/core/src/fmt/mod.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/library/core/src/fmt/mod.rs b/library/core/src/fmt/mod.rs index de958a82d9640..a901ae726698a 100644 --- a/library/core/src/fmt/mod.rs +++ b/library/core/src/fmt/mod.rs @@ -428,10 +428,8 @@ impl<'a> Arguments<'a> { /// An `UnsafeArg` is required because the following invariants must be held /// in order for this function to be safe: /// 1. The `pieces` slice must be at least as long as `fmt`. - /// 2. Every [`rt::Placeholder::position`] value within `fmt` must be a - /// valid index of `args`. - /// 3. Every [`rt::Count::Param`] within `fmt` must contain a valid index of - /// `args`. + /// 2. Every `rt::Placeholder::position` value within `fmt` must be a valid index of `args`. + /// 3. Every `rt::Count::Param` within `fmt` must contain a valid index of `args`. #[doc(hidden)] #[inline] #[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")] From bd917bbcd272c3f9b02bb07a6231860448c8d530 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Thu, 20 Apr 2023 19:38:33 +0200 Subject: [PATCH 8/8] Add reason to #![unstable] tag. Co-authored-by: jyn --- library/core/src/fmt/rt.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/fmt/rt.rs b/library/core/src/fmt/rt.rs index a431ee54d9508..2c1a767691b8b 100644 --- a/library/core/src/fmt/rt.rs +++ b/library/core/src/fmt/rt.rs @@ -1,5 +1,5 @@ #![allow(missing_debug_implementations)] -#![unstable(feature = "fmt_internals", issue = "none")] +#![unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")] //! These are the lang items used by format_args!().