Auto merge of #110616 - m-ou-se:fmt-lang-items, r=jyn514

Remove public doc(hidden) core::fmt::rt::v1

All the types used by format_arg!() are now lang items, so they are no longer required as publicly exported items.

Part of #99012

After this change, the `rt` module is private, and contains only three lang items used by format_args (`Placeholder`, `Alignment`, and `Count`): 441682cca9/library/core/src/fmt/rt.rs
This commit is contained in:
bors 2023-04-20 21:11:54 +00:00
commit 8bdcc62cb0
5 changed files with 49 additions and 66 deletions

View File

@ -220,19 +220,19 @@ fn make_argument<'hir>(
/// Generates: /// Generates:
/// ///
/// ```text /// ```text
/// <core::fmt::rt::v1::Count>::Is(…) /// <core::fmt::rt::Count>::Is(…)
/// ``` /// ```
/// ///
/// or /// or
/// ///
/// ```text /// ```text
/// <core::fmt::rt::v1::Count>::Param(…) /// <core::fmt::rt::Count>::Param(…)
/// ``` /// ```
/// ///
/// or /// or
/// ///
/// ```text /// ```text
/// <core::fmt::rt::v1::Count>::Implied /// <core::fmt::rt::Count>::Implied
/// ``` /// ```
fn make_count<'hir>( fn make_count<'hir>(
ctx: &mut LoweringContext<'_, 'hir>, ctx: &mut LoweringContext<'_, 'hir>,
@ -278,13 +278,13 @@ fn make_count<'hir>(
/// Generates /// Generates
/// ///
/// ```text /// ```text
/// <core::fmt::rt::v1::Argument::new( /// <core::fmt::rt::Placeholder::new(
/// …usize, // position /// …usize, // position
/// '…', // fill /// '…', // fill
/// <core::fmt::rt::v1::Alignment>::…, // alignment /// <core::fmt::rt::Alignment>::…, // alignment
/// …u32, // flags /// …u32, // flags
/// <core::fmt::rt::v1::Count::…>, // width /// <core::fmt::rt::Count::…>, // width
/// <core::fmt::rt::v1::Count::…>, // precision /// <core::fmt::rt::Count::…>, // precision
/// ) /// )
/// ``` /// ```
fn make_format_spec<'hir>( fn make_format_spec<'hir>(

View File

@ -551,8 +551,6 @@
#![stable(feature = "rust1", since = "1.0.0")] #![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")] #[stable(feature = "fmt_flags_align", since = "1.28.0")]
pub use core::fmt::Alignment; pub use core::fmt::Alignment;
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]

View File

@ -18,6 +18,7 @@
#[cfg(no_fp_fmt_parse)] #[cfg(no_fp_fmt_parse)]
mod nofloat; mod nofloat;
mod num; mod num;
mod rt;
#[stable(feature = "fmt_flags_align", since = "1.28.0")] #[stable(feature = "fmt_flags_align", since = "1.28.0")]
#[cfg_attr(not(test), rustc_diagnostic_item = "Alignment")] #[cfg_attr(not(test), rustc_diagnostic_item = "Alignment")]
@ -38,12 +39,6 @@ pub enum Alignment {
#[stable(feature = "debug_builders", since = "1.2.0")] #[stable(feature = "debug_builders", since = "1.2.0")]
pub use self::builders::{DebugList, DebugMap, DebugSet, DebugStruct, DebugTuple}; 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. /// The type returned by formatter methods.
/// ///
/// # Examples /// # Examples
@ -227,7 +222,7 @@ fn write_fmt(&mut self, args: Arguments<'_>) -> Result {
pub struct Formatter<'a> { pub struct Formatter<'a> {
flags: u32, flags: u32,
fill: char, fill: char,
align: rt::v1::Alignment, align: rt::Alignment,
width: Option<usize>, width: Option<usize>,
precision: Option<usize>, precision: Option<usize>,
@ -248,7 +243,7 @@ pub fn new(buf: &'a mut (dyn Write + 'a)) -> Formatter<'a> {
Formatter { Formatter {
flags: 0, flags: 0,
fill: ' ', fill: ' ',
align: rt::v1::Alignment::Unknown, align: rt::Alignment::Unknown,
width: None, width: None,
precision: None, precision: None,
buf, buf,
@ -433,17 +428,15 @@ pub const fn new_v1(pieces: &'a [&'static str], args: &'a [ArgumentV1<'a>]) -> A
/// An `UnsafeArg` is required because the following invariants must be held /// An `UnsafeArg` is required because the following invariants must be held
/// in order for this function to be safe: /// in order for this function to be safe:
/// 1. The `pieces` slice must be at least as long as `fmt`. /// 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::Placeholder::position` value within `fmt` must be a valid index of `args`.
/// valid index of `args`. /// 3. Every `rt::Count::Param` within `fmt` must contain a valid index of `args`.
/// 3. Every [`rt::v1::Count::Param`] within `fmt` must contain a valid index of
/// `args`.
#[doc(hidden)] #[doc(hidden)]
#[inline] #[inline]
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")] #[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
pub fn new_v1_formatted( pub fn new_v1_formatted(
pieces: &'a [&'static str], pieces: &'a [&'static str],
args: &'a [ArgumentV1<'a>], args: &'a [ArgumentV1<'a>],
fmt: &'a [rt::v1::Argument], fmt: &'a [rt::Placeholder],
_unsafe_arg: UnsafeArg, _unsafe_arg: UnsafeArg,
) -> Arguments<'a> { ) -> Arguments<'a> {
Arguments { pieces, fmt: Some(fmt), args } Arguments { pieces, fmt: Some(fmt), args }
@ -505,7 +498,7 @@ pub struct Arguments<'a> {
pieces: &'a [&'static str], pieces: &'a [&'static str],
// Placeholder specs, or `None` if all specs are default (as in "{}{}"). // Placeholder specs, or `None` if all specs are default (as in "{}{}").
fmt: Option<&'a [rt::v1::Argument]>, fmt: Option<&'a [rt::Placeholder]>,
// Dynamic arguments for interpolation, to be interleaved with string // Dynamic arguments for interpolation, to be interleaved with string
// pieces. (Every argument is preceded by a string piece.) // pieces. (Every argument is preceded by a string piece.)
@ -1281,15 +1274,15 @@ pub fn write(output: &mut dyn Write, args: Arguments<'_>) -> Result {
Ok(()) Ok(())
} }
unsafe fn run(fmt: &mut Formatter<'_>, arg: &rt::v1::Argument, args: &[ArgumentV1<'_>]) -> Result { unsafe fn run(fmt: &mut Formatter<'_>, arg: &rt::Placeholder, args: &[ArgumentV1<'_>]) -> Result {
fmt.fill = arg.format.fill; fmt.fill = arg.fill;
fmt.align = arg.format.align; fmt.align = arg.align;
fmt.flags = arg.format.flags; fmt.flags = arg.flags;
// SAFETY: arg and args come from the same Arguments, // SAFETY: arg and args come from the same Arguments,
// which guarantees the indexes are always within bounds. // which guarantees the indexes are always within bounds.
unsafe { unsafe {
fmt.width = getcount(args, &arg.format.width); fmt.width = getcount(args, &arg.width);
fmt.precision = getcount(args, &arg.format.precision); fmt.precision = getcount(args, &arg.precision);
} }
// Extract the correct argument // Extract the correct argument
@ -1302,11 +1295,11 @@ unsafe fn run(fmt: &mut Formatter<'_>, arg: &rt::v1::Argument, args: &[ArgumentV
(value.formatter)(value.value, fmt) (value.formatter)(value.value, fmt)
} }
unsafe fn getcount(args: &[ArgumentV1<'_>], cnt: &rt::v1::Count) -> Option<usize> { unsafe fn getcount(args: &[ArgumentV1<'_>], cnt: &rt::Count) -> Option<usize> {
match *cnt { match *cnt {
rt::v1::Count::Is(n) => Some(n), rt::Count::Is(n) => Some(n),
rt::v1::Count::Implied => None, rt::Count::Implied => None,
rt::v1::Count::Param(i) => { rt::Count::Param(i) => {
debug_assert!(i < args.len()); debug_assert!(i < args.len());
// SAFETY: cnt and args come from the same Arguments, // SAFETY: cnt and args come from the same Arguments,
// which guarantees this index is always within bounds. // which guarantees this index is always within bounds.
@ -1449,9 +1442,9 @@ fn write_prefix(f: &mut Formatter<'_>, sign: Option<char>, prefix: Option<&str>)
// is zero // is zero
Some(min) if self.sign_aware_zero_pad() => { Some(min) if self.sign_aware_zero_pad() => {
let old_fill = crate::mem::replace(&mut self.fill, '0'); 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)?; 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)?; self.buf.write_str(buf)?;
post_padding.write(self)?; post_padding.write(self)?;
self.fill = old_fill; self.fill = old_fill;
@ -1460,7 +1453,7 @@ fn write_prefix(f: &mut Formatter<'_>, sign: Option<char>, prefix: Option<&str>)
} }
// Otherwise, the sign and prefix goes after the padding // Otherwise, the sign and prefix goes after the padding
Some(min) => { 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)?; write_prefix(self, sign, prefix)?;
self.buf.write_str(buf)?; self.buf.write_str(buf)?;
post_padding.write(self) post_padding.write(self)
@ -1535,7 +1528,7 @@ pub fn pad(&mut self, s: &str) -> Result {
// If we're under both the maximum and the minimum width, then fill // If we're under both the maximum and the minimum width, then fill
// up the minimum width with the specified string + some alignment. // up the minimum width with the specified string + some alignment.
else { else {
let align = rt::v1::Alignment::Left; let align = Alignment::Left;
let post_padding = self.padding(width - chars_count, align)?; let post_padding = self.padding(width - chars_count, align)?;
self.buf.write_str(s)?; self.buf.write_str(s)?;
post_padding.write(self) post_padding.write(self)
@ -1550,17 +1543,19 @@ pub fn pad(&mut self, s: &str) -> Result {
pub(crate) fn padding( pub(crate) fn padding(
&mut self, &mut self,
padding: usize, padding: usize,
default: rt::v1::Alignment, default: Alignment,
) -> result::Result<PostPadding, Error> { ) -> result::Result<PostPadding, Error> {
let align = match self.align { let align = match self.align {
rt::v1::Alignment::Unknown => default, rt::Alignment::Unknown => default,
_ => self.align, rt::Alignment::Left => Alignment::Left,
rt::Alignment::Right => Alignment::Right,
rt::Alignment::Center => Alignment::Center,
}; };
let (pre_pad, post_pad) = match align { let (pre_pad, post_pad) = match align {
rt::v1::Alignment::Left => (0, padding), Alignment::Left => (0, padding),
rt::v1::Alignment::Right | rt::v1::Alignment::Unknown => (padding, 0), Alignment::Right => (padding, 0),
rt::v1::Alignment::Center => (padding / 2, (padding + 1) / 2), Alignment::Center => (padding / 2, (padding + 1) / 2),
}; };
for _ in 0..pre_pad { for _ in 0..pre_pad {
@ -1580,7 +1575,6 @@ fn pad_formatted_parts(&mut self, formatted: &numfmt::Formatted<'_>) -> Result {
let mut formatted = formatted.clone(); let mut formatted = formatted.clone();
let old_fill = self.fill; let old_fill = self.fill;
let old_align = self.align; let old_align = self.align;
let mut align = old_align;
if self.sign_aware_zero_pad() { if self.sign_aware_zero_pad() {
// a sign always goes first // a sign always goes first
let sign = formatted.sign; let sign = formatted.sign;
@ -1589,9 +1583,8 @@ fn pad_formatted_parts(&mut self, formatted: &numfmt::Formatted<'_>) -> Result {
// remove the sign from the formatted parts // remove the sign from the formatted parts
formatted.sign = ""; formatted.sign = "";
width = width.saturating_sub(sign.len()); width = width.saturating_sub(sign.len());
align = rt::v1::Alignment::Right;
self.fill = '0'; self.fill = '0';
self.align = rt::v1::Alignment::Right; self.align = rt::Alignment::Right;
} }
// remaining parts go through the ordinary padding process. // remaining parts go through the ordinary padding process.
@ -1600,7 +1593,7 @@ fn pad_formatted_parts(&mut self, formatted: &numfmt::Formatted<'_>) -> Result {
// no padding // no padding
self.write_formatted_parts(&formatted) self.write_formatted_parts(&formatted)
} else { } else {
let post_padding = self.padding(width - len, align)?; let post_padding = self.padding(width - len, Alignment::Right)?;
self.write_formatted_parts(&formatted)?; self.write_formatted_parts(&formatted)?;
post_padding.write(self) post_padding.write(self)
}; };
@ -1788,10 +1781,10 @@ pub fn fill(&self) -> char {
#[stable(feature = "fmt_flags_align", since = "1.28.0")] #[stable(feature = "fmt_flags_align", since = "1.28.0")]
pub fn align(&self) -> Option<Alignment> { pub fn align(&self) -> Option<Alignment> {
match self.align { match self.align {
rt::v1::Alignment::Left => Some(Alignment::Left), rt::Alignment::Left => Some(Alignment::Left),
rt::v1::Alignment::Right => Some(Alignment::Right), rt::Alignment::Right => Some(Alignment::Right),
rt::v1::Alignment::Center => Some(Alignment::Center), rt::Alignment::Center => Some(Alignment::Center),
rt::v1::Alignment::Unknown => None, rt::Alignment::Unknown => None,
} }
} }

View File

@ -1,20 +1,12 @@
//! 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)] #![allow(missing_debug_implementations)]
#![unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
//! These are the lang items used by format_args!().
#[lang = "format_placeholder"] #[lang = "format_placeholder"]
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
// FIXME: Rename this to Placeholder pub struct Placeholder {
pub struct Argument {
pub position: usize, pub position: usize,
pub format: FormatSpec,
}
#[derive(Copy, Clone)]
pub struct FormatSpec {
pub fill: char, pub fill: char,
pub align: Alignment, pub align: Alignment,
pub flags: u32, pub flags: u32,
@ -22,7 +14,7 @@ pub struct FormatSpec {
pub width: Count, pub width: Count,
} }
impl Argument { impl Placeholder {
#[inline(always)] #[inline(always)]
pub const fn new( pub const fn new(
position: usize, position: usize,
@ -32,7 +24,7 @@ pub const fn new(
precision: Count, precision: Count,
width: Count, width: Count,
) -> Self { ) -> Self {
Self { position, format: FormatSpec { fill, align, flags, precision, width } } Self { position, fill, align, flags, precision, width }
} }
} }

View File

@ -1172,7 +1172,7 @@ fn fmt_decimal(
emit_without_padding(f) emit_without_padding(f)
} else { } else {
// We need to add padding. Use the `Formatter::padding` helper function. // 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)?; let post_padding = f.padding(requested_w - actual_w, default_align)?;
emit_without_padding(f)?; emit_without_padding(f)?;
post_padding.write(f) post_padding.write(f)