Skip to content

Commit 8bdcc62

Browse files
committed
Auto merge of rust-lang#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 rust-lang#99012 After this change, the `rt` module is private, and contains only three lang items used by format_args (`Placeholder`, `Alignment`, and `Count`): https://github.com/rust-lang/rust/blob/441682cca9a32314835a88fa981a3bf13ffb0aa8/library/core/src/fmt/rt.rs
2 parents 3d7a091 + bd917bb commit 8bdcc62

File tree

5 files changed

+49
-66
lines changed

5 files changed

+49
-66
lines changed

compiler/rustc_ast_lowering/src/format.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -220,19 +220,19 @@ fn make_argument<'hir>(
220220
/// Generates:
221221
///
222222
/// ```text
223-
/// <core::fmt::rt::v1::Count>::Is(…)
223+
/// <core::fmt::rt::Count>::Is(…)
224224
/// ```
225225
///
226226
/// or
227227
///
228228
/// ```text
229-
/// <core::fmt::rt::v1::Count>::Param(…)
229+
/// <core::fmt::rt::Count>::Param(…)
230230
/// ```
231231
///
232232
/// or
233233
///
234234
/// ```text
235-
/// <core::fmt::rt::v1::Count>::Implied
235+
/// <core::fmt::rt::Count>::Implied
236236
/// ```
237237
fn make_count<'hir>(
238238
ctx: &mut LoweringContext<'_, 'hir>,
@@ -278,13 +278,13 @@ fn make_count<'hir>(
278278
/// Generates
279279
///
280280
/// ```text
281-
/// <core::fmt::rt::v1::Argument::new(
281+
/// <core::fmt::rt::Placeholder::new(
282282
/// …usize, // position
283283
/// '…', // fill
284-
/// <core::fmt::rt::v1::Alignment>::…, // alignment
284+
/// <core::fmt::rt::Alignment>::…, // alignment
285285
/// …u32, // flags
286-
/// <core::fmt::rt::v1::Count::…>, // width
287-
/// <core::fmt::rt::v1::Count::…>, // precision
286+
/// <core::fmt::rt::Count::…>, // width
287+
/// <core::fmt::rt::Count::…>, // precision
288288
/// )
289289
/// ```
290290
fn make_format_spec<'hir>(

library/alloc/src/fmt.rs

-2
Original file line numberDiff line numberDiff line change
@@ -551,8 +551,6 @@
551551
552552
#![stable(feature = "rust1", since = "1.0.0")]
553553

554-
#[unstable(feature = "fmt_internals", issue = "none")]
555-
pub use core::fmt::rt;
556554
#[stable(feature = "fmt_flags_align", since = "1.28.0")]
557555
pub use core::fmt::Alignment;
558556
#[stable(feature = "rust1", since = "1.0.0")]

library/core/src/fmt/mod.rs

+35-42
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ mod float;
1818
#[cfg(no_fp_fmt_parse)]
1919
mod nofloat;
2020
mod num;
21+
mod rt;
2122

2223
#[stable(feature = "fmt_flags_align", since = "1.28.0")]
2324
#[cfg_attr(not(test), rustc_diagnostic_item = "Alignment")]
@@ -38,12 +39,6 @@ pub enum Alignment {
3839
#[stable(feature = "debug_builders", since = "1.2.0")]
3940
pub use self::builders::{DebugList, DebugMap, DebugSet, DebugStruct, DebugTuple};
4041

41-
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
42-
#[doc(hidden)]
43-
pub mod rt {
44-
pub mod v1;
45-
}
46-
4742
/// The type returned by formatter methods.
4843
///
4944
/// # Examples
@@ -227,7 +222,7 @@ impl<W: Write + ?Sized> Write for &mut W {
227222
pub struct Formatter<'a> {
228223
flags: u32,
229224
fill: char,
230-
align: rt::v1::Alignment,
225+
align: rt::Alignment,
231226
width: Option<usize>,
232227
precision: Option<usize>,
233228

@@ -248,7 +243,7 @@ impl<'a> Formatter<'a> {
248243
Formatter {
249244
flags: 0,
250245
fill: ' ',
251-
align: rt::v1::Alignment::Unknown,
246+
align: rt::Alignment::Unknown,
252247
width: None,
253248
precision: None,
254249
buf,
@@ -433,17 +428,15 @@ impl<'a> Arguments<'a> {
433428
/// An `UnsafeArg` is required because the following invariants must be held
434429
/// in order for this function to be safe:
435430
/// 1. The `pieces` slice must be at least as long as `fmt`.
436-
/// 2. Every [`rt::v1::Argument::position`] value within `fmt` must be a
437-
/// valid index of `args`.
438-
/// 3. Every [`rt::v1::Count::Param`] within `fmt` must contain a valid index of
439-
/// `args`.
431+
/// 2. Every `rt::Placeholder::position` value within `fmt` must be a valid index of `args`.
432+
/// 3. Every `rt::Count::Param` within `fmt` must contain a valid index of `args`.
440433
#[doc(hidden)]
441434
#[inline]
442435
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
443436
pub fn new_v1_formatted(
444437
pieces: &'a [&'static str],
445438
args: &'a [ArgumentV1<'a>],
446-
fmt: &'a [rt::v1::Argument],
439+
fmt: &'a [rt::Placeholder],
447440
_unsafe_arg: UnsafeArg,
448441
) -> Arguments<'a> {
449442
Arguments { pieces, fmt: Some(fmt), args }
@@ -505,7 +498,7 @@ pub struct Arguments<'a> {
505498
pieces: &'a [&'static str],
506499

507500
// Placeholder specs, or `None` if all specs are default (as in "{}{}").
508-
fmt: Option<&'a [rt::v1::Argument]>,
501+
fmt: Option<&'a [rt::Placeholder]>,
509502

510503
// Dynamic arguments for interpolation, to be interleaved with string
511504
// pieces. (Every argument is preceded by a string piece.)
@@ -1281,15 +1274,15 @@ pub fn write(output: &mut dyn Write, args: Arguments<'_>) -> Result {
12811274
Ok(())
12821275
}
12831276

1284-
unsafe fn run(fmt: &mut Formatter<'_>, arg: &rt::v1::Argument, args: &[ArgumentV1<'_>]) -> Result {
1285-
fmt.fill = arg.format.fill;
1286-
fmt.align = arg.format.align;
1287-
fmt.flags = arg.format.flags;
1277+
unsafe fn run(fmt: &mut Formatter<'_>, arg: &rt::Placeholder, args: &[ArgumentV1<'_>]) -> Result {
1278+
fmt.fill = arg.fill;
1279+
fmt.align = arg.align;
1280+
fmt.flags = arg.flags;
12881281
// SAFETY: arg and args come from the same Arguments,
12891282
// which guarantees the indexes are always within bounds.
12901283
unsafe {
1291-
fmt.width = getcount(args, &arg.format.width);
1292-
fmt.precision = getcount(args, &arg.format.precision);
1284+
fmt.width = getcount(args, &arg.width);
1285+
fmt.precision = getcount(args, &arg.precision);
12931286
}
12941287

12951288
// Extract the correct argument
@@ -1302,11 +1295,11 @@ unsafe fn run(fmt: &mut Formatter<'_>, arg: &rt::v1::Argument, args: &[ArgumentV
13021295
(value.formatter)(value.value, fmt)
13031296
}
13041297

1305-
unsafe fn getcount(args: &[ArgumentV1<'_>], cnt: &rt::v1::Count) -> Option<usize> {
1298+
unsafe fn getcount(args: &[ArgumentV1<'_>], cnt: &rt::Count) -> Option<usize> {
13061299
match *cnt {
1307-
rt::v1::Count::Is(n) => Some(n),
1308-
rt::v1::Count::Implied => None,
1309-
rt::v1::Count::Param(i) => {
1300+
rt::Count::Is(n) => Some(n),
1301+
rt::Count::Implied => None,
1302+
rt::Count::Param(i) => {
13101303
debug_assert!(i < args.len());
13111304
// SAFETY: cnt and args come from the same Arguments,
13121305
// which guarantees this index is always within bounds.
@@ -1449,9 +1442,9 @@ impl<'a> Formatter<'a> {
14491442
// is zero
14501443
Some(min) if self.sign_aware_zero_pad() => {
14511444
let old_fill = crate::mem::replace(&mut self.fill, '0');
1452-
let old_align = crate::mem::replace(&mut self.align, rt::v1::Alignment::Right);
1445+
let old_align = crate::mem::replace(&mut self.align, rt::Alignment::Right);
14531446
write_prefix(self, sign, prefix)?;
1454-
let post_padding = self.padding(min - width, rt::v1::Alignment::Right)?;
1447+
let post_padding = self.padding(min - width, Alignment::Right)?;
14551448
self.buf.write_str(buf)?;
14561449
post_padding.write(self)?;
14571450
self.fill = old_fill;
@@ -1460,7 +1453,7 @@ impl<'a> Formatter<'a> {
14601453
}
14611454
// Otherwise, the sign and prefix goes after the padding
14621455
Some(min) => {
1463-
let post_padding = self.padding(min - width, rt::v1::Alignment::Right)?;
1456+
let post_padding = self.padding(min - width, Alignment::Right)?;
14641457
write_prefix(self, sign, prefix)?;
14651458
self.buf.write_str(buf)?;
14661459
post_padding.write(self)
@@ -1535,7 +1528,7 @@ impl<'a> Formatter<'a> {
15351528
// If we're under both the maximum and the minimum width, then fill
15361529
// up the minimum width with the specified string + some alignment.
15371530
else {
1538-
let align = rt::v1::Alignment::Left;
1531+
let align = Alignment::Left;
15391532
let post_padding = self.padding(width - chars_count, align)?;
15401533
self.buf.write_str(s)?;
15411534
post_padding.write(self)
@@ -1550,17 +1543,19 @@ impl<'a> Formatter<'a> {
15501543
pub(crate) fn padding(
15511544
&mut self,
15521545
padding: usize,
1553-
default: rt::v1::Alignment,
1546+
default: Alignment,
15541547
) -> result::Result<PostPadding, Error> {
15551548
let align = match self.align {
1556-
rt::v1::Alignment::Unknown => default,
1557-
_ => self.align,
1549+
rt::Alignment::Unknown => default,
1550+
rt::Alignment::Left => Alignment::Left,
1551+
rt::Alignment::Right => Alignment::Right,
1552+
rt::Alignment::Center => Alignment::Center,
15581553
};
15591554

15601555
let (pre_pad, post_pad) = match align {
1561-
rt::v1::Alignment::Left => (0, padding),
1562-
rt::v1::Alignment::Right | rt::v1::Alignment::Unknown => (padding, 0),
1563-
rt::v1::Alignment::Center => (padding / 2, (padding + 1) / 2),
1556+
Alignment::Left => (0, padding),
1557+
Alignment::Right => (padding, 0),
1558+
Alignment::Center => (padding / 2, (padding + 1) / 2),
15641559
};
15651560

15661561
for _ in 0..pre_pad {
@@ -1580,7 +1575,6 @@ impl<'a> Formatter<'a> {
15801575
let mut formatted = formatted.clone();
15811576
let old_fill = self.fill;
15821577
let old_align = self.align;
1583-
let mut align = old_align;
15841578
if self.sign_aware_zero_pad() {
15851579
// a sign always goes first
15861580
let sign = formatted.sign;
@@ -1589,9 +1583,8 @@ impl<'a> Formatter<'a> {
15891583
// remove the sign from the formatted parts
15901584
formatted.sign = "";
15911585
width = width.saturating_sub(sign.len());
1592-
align = rt::v1::Alignment::Right;
15931586
self.fill = '0';
1594-
self.align = rt::v1::Alignment::Right;
1587+
self.align = rt::Alignment::Right;
15951588
}
15961589

15971590
// remaining parts go through the ordinary padding process.
@@ -1600,7 +1593,7 @@ impl<'a> Formatter<'a> {
16001593
// no padding
16011594
self.write_formatted_parts(&formatted)
16021595
} else {
1603-
let post_padding = self.padding(width - len, align)?;
1596+
let post_padding = self.padding(width - len, Alignment::Right)?;
16041597
self.write_formatted_parts(&formatted)?;
16051598
post_padding.write(self)
16061599
};
@@ -1788,10 +1781,10 @@ impl<'a> Formatter<'a> {
17881781
#[stable(feature = "fmt_flags_align", since = "1.28.0")]
17891782
pub fn align(&self) -> Option<Alignment> {
17901783
match self.align {
1791-
rt::v1::Alignment::Left => Some(Alignment::Left),
1792-
rt::v1::Alignment::Right => Some(Alignment::Right),
1793-
rt::v1::Alignment::Center => Some(Alignment::Center),
1794-
rt::v1::Alignment::Unknown => None,
1784+
rt::Alignment::Left => Some(Alignment::Left),
1785+
rt::Alignment::Right => Some(Alignment::Right),
1786+
rt::Alignment::Center => Some(Alignment::Center),
1787+
rt::Alignment::Unknown => None,
17951788
}
17961789
}
17971790

library/core/src/fmt/rt/v1.rs library/core/src/fmt/rt.rs

+6-14
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,20 @@
1-
//! This is an internal module used by the ifmt! runtime. These structures are
2-
//! emitted to static arrays to precompile format strings ahead of time.
3-
//!
4-
//! These definitions are similar to their `ct` equivalents, but differ in that
5-
//! these can be statically allocated and are slightly optimized for the runtime
61
#![allow(missing_debug_implementations)]
2+
#![unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
3+
4+
//! These are the lang items used by format_args!().
75
86
#[lang = "format_placeholder"]
97
#[derive(Copy, Clone)]
10-
// FIXME: Rename this to Placeholder
11-
pub struct Argument {
8+
pub struct Placeholder {
129
pub position: usize,
13-
pub format: FormatSpec,
14-
}
15-
16-
#[derive(Copy, Clone)]
17-
pub struct FormatSpec {
1810
pub fill: char,
1911
pub align: Alignment,
2012
pub flags: u32,
2113
pub precision: Count,
2214
pub width: Count,
2315
}
2416

25-
impl Argument {
17+
impl Placeholder {
2618
#[inline(always)]
2719
pub const fn new(
2820
position: usize,
@@ -32,7 +24,7 @@ impl Argument {
3224
precision: Count,
3325
width: Count,
3426
) -> Self {
35-
Self { position, format: FormatSpec { fill, align, flags, precision, width } }
27+
Self { position, fill, align, flags, precision, width }
3628
}
3729
}
3830

library/core/src/time.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1172,7 +1172,7 @@ impl fmt::Debug for Duration {
11721172
emit_without_padding(f)
11731173
} else {
11741174
// We need to add padding. Use the `Formatter::padding` helper function.
1175-
let default_align = crate::fmt::rt::v1::Alignment::Left;
1175+
let default_align = fmt::Alignment::Left;
11761176
let post_padding = f.padding(requested_w - actual_w, default_align)?;
11771177
emit_without_padding(f)?;
11781178
post_padding.write(f)

0 commit comments

Comments
 (0)