diff --git a/compiler/rustc_ast_lowering/src/format.rs b/compiler/rustc_ast_lowering/src/format.rs index 602635af1324e..7c1c0acc8f043 100644 --- a/compiler/rustc_ast_lowering/src/format.rs +++ b/compiler/rustc_ast_lowering/src/format.rs @@ -218,7 +218,7 @@ fn flatten_format_args(mut fmt: Cow<'_, FormatArgs>) -> Cow<'_, FormatArgs> { #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)] enum ArgumentType { - Format(FormatTrait), + Format { how: FormatTrait, simple: bool }, Usize, } @@ -241,18 +241,32 @@ fn make_argument<'hir>( sp, hir::LangItem::FormatArgument, match ty { - Format(Display) => sym::new_display, - Format(Debug) => match ctx.tcx.sess.opts.unstable_opts.fmt_debug { - FmtDebug::Full | FmtDebug::Shallow => sym::new_debug, + Format { how: Display, simple: false } => sym::new_display, + Format { how: Display, simple: true } => sym::new_display_simple, + Format { how: Debug, simple } => match ctx.tcx.sess.opts.unstable_opts.fmt_debug { + FmtDebug::Full | FmtDebug::Shallow => { + if simple { + sym::new_debug_simple + } else { + sym::new_debug + } + } FmtDebug::None => sym::new_debug_noop, }, - Format(LowerExp) => sym::new_lower_exp, - Format(UpperExp) => sym::new_upper_exp, - Format(Octal) => sym::new_octal, - Format(Pointer) => sym::new_pointer, - Format(Binary) => sym::new_binary, - Format(LowerHex) => sym::new_lower_hex, - Format(UpperHex) => sym::new_upper_hex, + Format { how: LowerExp, simple: false } => sym::new_lower_exp, + Format { how: LowerExp, simple: true } => sym::new_lower_exp_simple, + Format { how: UpperExp, simple: false } => sym::new_upper_exp, + Format { how: UpperExp, simple: true } => sym::new_upper_exp_simple, + Format { how: Octal, simple: false } => sym::new_octal, + Format { how: Octal, simple: true } => sym::new_octal_simple, + Format { how: Pointer, simple: false } => sym::new_pointer, + Format { how: Pointer, simple: true } => sym::new_pointer_simple, + Format { how: Binary, simple: false } => sym::new_binary, + Format { how: Binary, simple: true } => sym::new_binary_simple, + Format { how: LowerHex, simple: false } => sym::new_lower_hex, + Format { how: LowerHex, simple: true } => sym::new_lower_hex_simple, + Format { how: UpperHex, simple: false } => sym::new_upper_hex, + Format { how: UpperHex, simple: true } => sym::new_upper_hex_simple, Usize => sym::from_usize, }, )); @@ -360,16 +374,6 @@ fn expand_format_args<'hir>( let i = bytecode.len(); bytecode.push(0xC0); - let position = argmap - .insert_full( - ( - p.argument.index.unwrap_or(usize::MAX), - ArgumentType::Format(p.format_trait), - ), - p.span, - ) - .0 as u64; - // This needs to match the constants in library/core/src/fmt/mod.rs. let o = &p.format_options; let align = match o.alignment { @@ -389,6 +393,18 @@ fn expand_format_args<'hir>( | (o.width.is_some() as u32) << 27 | (o.precision.is_some() as u32) << 28 | align << 29; + + let is_simple = flags == default_flags; + let position = argmap + .insert_full( + ( + p.argument.index.unwrap_or(usize::MAX), + ArgumentType::Format { how: p.format_trait, simple: is_simple }, + ), + p.span, + ) + .0 as u64; + if flags != default_flags { bytecode[i] |= 1; bytecode.extend_from_slice(&flags.to_le_bytes()); diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index b80d624bc4966..985f508eeb914 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1557,18 +1557,27 @@ symbols! { never_type_fallback, new, new_binary, + new_binary_simple, new_const, new_debug, new_debug_noop, + new_debug_simple, new_display, + new_display_simple, new_lower_exp, + new_lower_exp_simple, new_lower_hex, + new_lower_hex_simple, new_octal, + new_octal_simple, new_pointer, + new_pointer_simple, new_range, new_unchecked, new_upper_exp, + new_upper_exp_simple, new_upper_hex, + new_upper_hex_simple, new_v1, new_v1_formatted, next, diff --git a/library/core/src/fmt/rt.rs b/library/core/src/fmt/rt.rs index 5221783e72901..13a01fd560093 100644 --- a/library/core/src/fmt/rt.rs +++ b/library/core/src/fmt/rt.rs @@ -73,10 +73,10 @@ macro_rules! argument_new { }, #[cfg(any(sanitize = "cfi", sanitize = "kcfi"))] formatter: |ptr: NonNull<()>, fmt: &mut Formatter<'_>| { - let func = $f; + let f: fn(&$t, &mut Formatter<'_>) -> Result = $f; // SAFETY: This is the same type as the `value` field. let r = unsafe { ptr.cast::<$t>().as_ref() }; - (func)(r, fmt) + f(r, fmt) }, _lifetime: PhantomData, }, @@ -84,47 +84,39 @@ macro_rules! argument_new { }; } +macro_rules! argument_constructor { + ($name:ident => $trait:path) => { + #[inline] + pub const fn $name(x: &T) -> Argument<'_> { + argument_new!(T, x, ::fmt) + } + + #[inline] + pub const fn ${concat($name, _simple)}(x: &T) -> Argument<'_> { + argument_new!(T, x, |x, f| { + let mut f = f.with_options(FormattingOptions::new()); + ::fmt(x, &mut f) + }) + } + }; +} + impl Argument<'_> { - #[inline] - pub const fn new_display(x: &T) -> Argument<'_> { - argument_new!(T, x, ::fmt) - } - #[inline] - pub const fn new_debug(x: &T) -> Argument<'_> { - argument_new!(T, x, ::fmt) - } + argument_constructor!(new_display => Display); + argument_constructor!(new_debug => Debug); + argument_constructor!(new_octal => Octal); + argument_constructor!(new_lower_hex => LowerHex); + argument_constructor!(new_upper_hex => UpperHex); + argument_constructor!(new_pointer => Pointer); + argument_constructor!(new_binary => Binary); + argument_constructor!(new_lower_exp => LowerExp); + argument_constructor!(new_upper_exp => UpperExp); + #[inline] pub const fn new_debug_noop(x: &T) -> Argument<'_> { argument_new!(T, x, |_: &T, _| Ok(())) } - #[inline] - pub const fn new_octal(x: &T) -> Argument<'_> { - argument_new!(T, x, ::fmt) - } - #[inline] - pub const fn new_lower_hex(x: &T) -> Argument<'_> { - argument_new!(T, x, ::fmt) - } - #[inline] - pub const fn new_upper_hex(x: &T) -> Argument<'_> { - argument_new!(T, x, ::fmt) - } - #[inline] - pub const fn new_pointer(x: &T) -> Argument<'_> { - argument_new!(T, x, ::fmt) - } - #[inline] - pub const fn new_binary(x: &T) -> Argument<'_> { - argument_new!(T, x, ::fmt) - } - #[inline] - pub const fn new_lower_exp(x: &T) -> Argument<'_> { - argument_new!(T, x, ::fmt) - } - #[inline] - pub const fn new_upper_exp(x: &T) -> Argument<'_> { - argument_new!(T, x, ::fmt) - } + #[inline] #[track_caller] pub const fn from_usize(x: &usize) -> Argument<'_> {