Auto merge of #107599 - clubby789:debug-less-ref, r=nnethercote

Don't generate unecessary `&&self.field` in deriving Debug

Since unsized fields may only be the last one in a struct, we only need to generate a double reference (`&&self.field`) for the  final one.

cc `@nnethercote`
This commit is contained in:
bors 2023-02-03 14:22:42 +00:00
commit 9545094994
2 changed files with 27 additions and 12 deletions

View File

@ -76,6 +76,21 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
// The number of fields that can be handled without an array.
const CUTOFF: usize = 5;
fn expr_for_field(
cx: &ExtCtxt<'_>,
field: &FieldInfo,
index: usize,
len: usize,
) -> ast::ptr::P<ast::Expr> {
if index < len - 1 {
field.self_expr.clone()
} else {
// Unsized types need an extra indirection, but only the last field
// may be unsized.
cx.expr_addr_of(field.span, field.self_expr.clone())
}
}
if fields.is_empty() {
// Special case for no fields.
let fn_path_write_str = cx.std_path(&[sym::fmt, sym::Formatter, sym::write_str]);
@ -98,8 +113,8 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
let name = cx.expr_str(field.span, field.name.unwrap().name);
args.push(name);
}
// Use an extra indirection to make sure this works for unsized types.
let field = cx.expr_addr_of(field.span, field.self_expr.clone());
let field = expr_for_field(cx, field, i, fields.len());
args.push(field);
}
let expr = cx.expr_call_global(span, fn_path_debug, args);
@ -109,13 +124,13 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
let mut name_exprs = Vec::with_capacity(fields.len());
let mut value_exprs = Vec::with_capacity(fields.len());
for field in fields {
for i in 0..fields.len() {
let field = &fields[i];
if is_struct {
name_exprs.push(cx.expr_str(field.span, field.name.unwrap().name));
}
// Use an extra indirection to make sure this works for unsized types.
let field = cx.expr_addr_of(field.span, field.self_expr.clone());
let field = expr_for_field(cx, field, i, fields.len());
value_exprs.push(field);
}

View File

@ -98,7 +98,7 @@ impl ::core::marker::Copy for Point { }
impl ::core::fmt::Debug for Point {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f, "Point", "x",
&&self.x, "y", &&self.y)
&self.x, "y", &&self.y)
}
}
#[automatically_derived]
@ -183,7 +183,7 @@ impl ::core::marker::Copy for PackedPoint { }
impl ::core::fmt::Debug for PackedPoint {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f, "PackedPoint",
"x", &&{ self.x }, "y", &&{ self.y })
"x", &{ self.x }, "y", &&{ self.y })
}
}
#[automatically_derived]
@ -277,8 +277,8 @@ impl ::core::fmt::Debug for Big {
let names: &'static _ =
&["b1", "b2", "b3", "b4", "b5", "b6", "b7", "b8"];
let values: &[&dyn ::core::fmt::Debug] =
&[&&self.b1, &&self.b2, &&self.b3, &&self.b4, &&self.b5,
&&self.b6, &&self.b7, &&self.b8];
&[&self.b1, &self.b2, &self.b3, &self.b4, &self.b5, &self.b6,
&self.b7, &&self.b8];
::core::fmt::Formatter::debug_struct_fields_finish(f, "Big", names,
values)
}
@ -565,7 +565,7 @@ impl<T: ::core::fmt::Debug + Trait, U: ::core::fmt::Debug> ::core::fmt::Debug
for Generic<T, U> where T::A: ::core::fmt::Debug {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field3_finish(f, "Generic", "t",
&&self.t, "ta", &&self.ta, "u", &&self.u)
&self.t, "ta", &self.ta, "u", &&self.u)
}
}
#[automatically_derived]
@ -682,7 +682,7 @@ impl<T: ::core::fmt::Debug + ::core::marker::Copy + Trait,
{
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field3_finish(f, "PackedGeneric",
&&{ self.0 }, &&{ self.1 }, &&{ self.2 })
&{ self.0 }, &{ self.1 }, &&{ self.2 })
}
}
#[automatically_derived]
@ -1084,7 +1084,7 @@ impl ::core::fmt::Debug for Mixed {
&__self_0),
Mixed::S { d1: __self_0, d2: __self_1 } =>
::core::fmt::Formatter::debug_struct_field2_finish(f, "S",
"d1", &__self_0, "d2", &__self_1),
"d1", __self_0, "d2", &__self_1),
}
}
}