Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions compiler/rustc_builtin_macros/src/deriving/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,7 @@ fn show_substructure(
BlockOrExpr::new_expr(expr)
} else if fields.len() <= CUTOFF {
// Few enough fields that we can use a specific-length method.
let debug = if is_struct {
format!("debug_struct_field{}_finish", fields.len())
} else {
format!("debug_tuple_field{}_finish", fields.len())
};
let debug = format!("debug_struct_field{}_finish", fields.len());
let fn_path_debug = cx.std_path(span, &[sym::fmt, sym::Formatter, Symbol::intern(&debug)]);

let mut args = ThinVec::with_capacity(2 + fields.len());
Expand Down Expand Up @@ -196,13 +192,8 @@ fn show_substructure(
cx.expr_array_ref(span, value_exprs),
);

// `fmt::Formatter::debug_struct_fields_finish(fmt, names, values)` or
// `fmt::Formatter::debug_tuple_fields_finish(fmt, name, values)`
let sym_debug = if is_struct {
sym::debug_struct_fields_finish
} else {
sym::debug_tuple_fields_finish
};
// `fmt::Formatter::debug_struct_fields_finish(fmt, names, values)`
let sym_debug = sym::debug_struct_fields_finish;
let fn_path_debug_internal = cx.std_path(span, &[sym::fmt, sym::Formatter, sym_debug]);

let args = thin_vec![
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,6 @@ symbols! {
debug_assertions,
debug_c_like_enum_write_str,
debug_struct_fields_finish,
debug_tuple_fields_finish,
debugger_visualizer,
decl_macro,
declare_lint_pass,
Expand Down
180 changes: 36 additions & 144 deletions library/core/src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2497,10 +2497,7 @@ impl<'a> Formatter<'a> {
#[doc(hidden)]
#[unstable(feature = "fmt_helpers_for_derive", issue = "none")]
pub fn debug_struct_field1_finish<'b>(&'b mut self, names: &str, value1: &dyn Debug) -> Result {
let mut names = names.split(' ');
let mut builder = builders::debug_struct_new(self, names.next().unwrap());
builder.field(names.next().unwrap(), value1);
builder.finish()
self.debug_struct_fields_finish(names, &[value1])
}

/// Shrinks `derive(Debug)` code, for faster compilation and smaller
Expand All @@ -2514,11 +2511,7 @@ impl<'a> Formatter<'a> {
value1: &dyn Debug,
value2: &dyn Debug,
) -> Result {
let mut names = names.split(' ');
let mut builder = builders::debug_struct_new(self, names.next().unwrap());
builder.field(names.next().unwrap(), value1);
builder.field(names.next().unwrap(), value2);
builder.finish()
self.debug_struct_fields_finish(names, &[value1, value2])
}

/// Shrinks `derive(Debug)` code, for faster compilation and smaller
Expand All @@ -2533,12 +2526,7 @@ impl<'a> Formatter<'a> {
value2: &dyn Debug,
value3: &dyn Debug,
) -> Result {
let mut names = names.split(' ');
let mut builder = builders::debug_struct_new(self, names.next().unwrap());
builder.field(names.next().unwrap(), value1);
builder.field(names.next().unwrap(), value2);
builder.field(names.next().unwrap(), value3);
builder.finish()
self.debug_struct_fields_finish(names, &[value1, value2, value3])
}

/// Shrinks `derive(Debug)` code, for faster compilation and smaller
Expand All @@ -2554,13 +2542,7 @@ impl<'a> Formatter<'a> {
value3: &dyn Debug,
value4: &dyn Debug,
) -> Result {
let mut names = names.split(' ');
let mut builder = builders::debug_struct_new(self, names.next().unwrap());
builder.field(names.next().unwrap(), value1);
builder.field(names.next().unwrap(), value2);
builder.field(names.next().unwrap(), value3);
builder.field(names.next().unwrap(), value4);
builder.finish()
self.debug_struct_fields_finish(names, &[value1, value2, value3, value4])
}

/// Shrinks `derive(Debug)` code, for faster compilation and smaller
Expand All @@ -2577,14 +2559,7 @@ impl<'a> Formatter<'a> {
value4: &dyn Debug,
value5: &dyn Debug,
) -> Result {
let mut names = names.split(' ');
let mut builder = builders::debug_struct_new(self, names.next().unwrap());
builder.field(names.next().unwrap(), value1);
builder.field(names.next().unwrap(), value2);
builder.field(names.next().unwrap(), value3);
builder.field(names.next().unwrap(), value4);
builder.field(names.next().unwrap(), value5);
builder.finish()
self.debug_struct_fields_finish(names, &[value1, value2, value3, value4, value5])
}

/// Shrinks `derive(Debug)` code, for faster compilation and smaller binaries.
Expand All @@ -2597,13 +2572,26 @@ impl<'a> Formatter<'a> {
names: &str,
values: &[&dyn Debug],
) -> Result {
let mut names = names.split(' ');
let mut builder = builders::debug_struct_new(self, names.next().unwrap());
for value in values.into_iter() {
let name = names.next().unwrap();
builder.field(name, value);
// the macro generates either "Point x y" for a struct like `Point { x: u32, y: u32, }`
// or just "Point" for a tuple struct like `Point(u32, u32)`. If there are no field names
// (and therefore no space separator), we assume we format a tuple struct.
// `.as_bytes()` is to avoid bringing in UTF-8 code bloat
let is_tuple = !names.as_bytes().contains(&b' ');
if is_tuple {
let mut builder = builders::debug_tuple_new(self, names);
for value in values {
builder.field(value);
}
builder.finish()
} else {
let mut names = split_str_by_space(names);
let mut builder = builders::debug_struct_new(self, names.next().unwrap());
for value in values.into_iter() {
let name = names.next().unwrap();
builder.field(name, value);
}
builder.finish()
}
builder.finish()
}

/// Shrinks `derive(Debug)` code, for faster compilation and smaller binaries.
Expand All @@ -2616,7 +2604,9 @@ impl<'a> Formatter<'a> {
names: &str,
start_index: usize,
) -> Result {
self.write_str(names[start_index..].split(' ').next().unwrap())
// we use `get().unwrap()` instead of indexing to avoid binary size
// bloat from the error path (`slice_error_fail_rt`)
self.write_str(split_str_by_space(names.get(start_index..).unwrap()).next().unwrap())
}

/// Creates a `DebugTuple` builder designed to assist with creation of
Expand Down Expand Up @@ -2650,113 +2640,6 @@ impl<'a> Formatter<'a> {
builders::debug_tuple_new(self, name)
}

/// Shrinks `derive(Debug)` code, for faster compilation and smaller
/// binaries. `debug_tuple_fields_finish` is more general, but this is faster
/// for 1 field.
#[doc(hidden)]
#[unstable(feature = "fmt_helpers_for_derive", issue = "none")]
pub fn debug_tuple_field1_finish<'b>(&'b mut self, name: &str, value1: &dyn Debug) -> Result {
let mut builder = builders::debug_tuple_new(self, name);
builder.field(value1);
builder.finish()
}

/// Shrinks `derive(Debug)` code, for faster compilation and smaller
/// binaries. `debug_tuple_fields_finish` is more general, but this is faster
/// for 2 fields.
#[doc(hidden)]
#[unstable(feature = "fmt_helpers_for_derive", issue = "none")]
pub fn debug_tuple_field2_finish<'b>(
&'b mut self,
name: &str,
value1: &dyn Debug,
value2: &dyn Debug,
) -> Result {
let mut builder = builders::debug_tuple_new(self, name);
builder.field(value1);
builder.field(value2);
builder.finish()
}

/// Shrinks `derive(Debug)` code, for faster compilation and smaller
/// binaries. `debug_tuple_fields_finish` is more general, but this is faster
/// for 3 fields.
#[doc(hidden)]
#[unstable(feature = "fmt_helpers_for_derive", issue = "none")]
pub fn debug_tuple_field3_finish<'b>(
&'b mut self,
name: &str,
value1: &dyn Debug,
value2: &dyn Debug,
value3: &dyn Debug,
) -> Result {
let mut builder = builders::debug_tuple_new(self, name);
builder.field(value1);
builder.field(value2);
builder.field(value3);
builder.finish()
}

/// Shrinks `derive(Debug)` code, for faster compilation and smaller
/// binaries. `debug_tuple_fields_finish` is more general, but this is faster
/// for 4 fields.
#[doc(hidden)]
#[unstable(feature = "fmt_helpers_for_derive", issue = "none")]
pub fn debug_tuple_field4_finish<'b>(
&'b mut self,
name: &str,
value1: &dyn Debug,
value2: &dyn Debug,
value3: &dyn Debug,
value4: &dyn Debug,
) -> Result {
let mut builder = builders::debug_tuple_new(self, name);
builder.field(value1);
builder.field(value2);
builder.field(value3);
builder.field(value4);
builder.finish()
}

/// Shrinks `derive(Debug)` code, for faster compilation and smaller
/// binaries. `debug_tuple_fields_finish` is more general, but this is faster
/// for 5 fields.
#[doc(hidden)]
#[unstable(feature = "fmt_helpers_for_derive", issue = "none")]
pub fn debug_tuple_field5_finish<'b>(
&'b mut self,
name: &str,
value1: &dyn Debug,
value2: &dyn Debug,
value3: &dyn Debug,
value4: &dyn Debug,
value5: &dyn Debug,
) -> Result {
let mut builder = builders::debug_tuple_new(self, name);
builder.field(value1);
builder.field(value2);
builder.field(value3);
builder.field(value4);
builder.field(value5);
builder.finish()
}

/// Shrinks `derive(Debug)` code, for faster compilation and smaller
/// binaries. For the cases not covered by `debug_tuple_field[12345]_finish`.
#[doc(hidden)]
#[unstable(feature = "fmt_helpers_for_derive", issue = "none")]
pub fn debug_tuple_fields_finish<'b>(
&'b mut self,
name: &str,
values: &[&dyn Debug],
) -> Result {
let mut builder = builders::debug_tuple_new(self, name);
for value in values {
builder.field(value);
}
builder.finish()
}

/// Creates a `DebugList` builder designed to assist with creation of
/// `fmt::Debug` implementations for list-like structures.
///
Expand Down Expand Up @@ -2877,6 +2760,15 @@ impl<'a> Formatter<'a> {
}
}

/// Like `.split(' ')`, but doesn't bloat binary size
fn split_str_by_space(s: &str) -> impl Iterator<Item = &str> {
s.as_bytes()
.split(|&b| b == b' ')
// SAFETY:
// splitting a valid UTF-8 string on an ASCII byte results in valid UTF-8 substrings
.map(|bytes| unsafe { str::from_utf8_unchecked(bytes) })
}

#[stable(since = "1.2.0", feature = "formatter_write")]
impl Write for Formatter<'_> {
fn write_str(&mut self, s: &str) -> Result {
Expand Down
2 changes: 1 addition & 1 deletion library/stdarch/crates/core_arch/src/simd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ pub(crate) fn debug_simd_finish<T: crate::fmt::Debug, const N: usize>(
type_name: &str,
array: &[T; N],
) -> crate::fmt::Result {
crate::fmt::Formatter::debug_tuple_fields_finish(
crate::fmt::Formatter::debug_struct_fields_finish(
formatter,
type_name,
&crate::array::from_fn::<&dyn crate::fmt::Debug, N, _>(|i| &array[i]),
Expand Down
18 changes: 9 additions & 9 deletions tests/ui/derives/deriving-all-codegen.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ impl ::core::marker::Copy for TupleSingleField { }
impl ::core::fmt::Debug for TupleSingleField {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field1_finish(f,
::core::fmt::Formatter::debug_struct_field1_finish(f,
"TupleSingleField", &&self.0)
}
}
Expand Down Expand Up @@ -670,7 +670,7 @@ struct Unsized([u32]);
impl ::core::fmt::Debug for Unsized {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Unsized",
::core::fmt::Formatter::debug_struct_field1_finish(f, "Unsized",
&&self.0)
}
}
Expand Down Expand Up @@ -869,7 +869,7 @@ impl<T: ::core::fmt::Debug + ::core::marker::Copy + Trait,
{
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field3_finish(f, "PackedGeneric",
::core::fmt::Formatter::debug_struct_field3_finish(f, "PackedGeneric",
&{ self.0 }, &{ self.1 }, &&{ self.2 })
}
}
Expand Down Expand Up @@ -1301,7 +1301,7 @@ impl ::core::fmt::Debug for Mixed {
Self::P => ::core::fmt::Formatter::write_str(f, "P"),
Self::Q => ::core::fmt::Formatter::write_str(f, "Q"),
Self::R(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "R",
::core::fmt::Formatter::debug_struct_field1_finish(f, "R",
&__self_0),
Self::S { d1: __self_0, d2: __self_1 } =>
::core::fmt::Formatter::debug_struct_field2_finish(f,
Expand Down Expand Up @@ -1479,13 +1479,13 @@ impl ::core::fmt::Debug for Fielded {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
Self::X(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "X",
::core::fmt::Formatter::debug_struct_field1_finish(f, "X",
&__self_0),
Self::Y(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Y",
::core::fmt::Formatter::debug_struct_field1_finish(f, "Y",
&__self_0),
Self::Z(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Z",
::core::fmt::Formatter::debug_struct_field1_finish(f, "Z",
&__self_0),
}
}
Expand Down Expand Up @@ -1585,10 +1585,10 @@ impl<T: ::core::fmt::Debug, U: ::core::fmt::Debug> ::core::fmt::Debug for
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
Self::One(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "One",
::core::fmt::Formatter::debug_struct_field1_finish(f, "One",
&__self_0),
Self::Two(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Two",
::core::fmt::Formatter::debug_struct_field1_finish(f, "Two",
&__self_0),
}
}
Expand Down
Loading