Skip to content

Commit

Permalink
Auto merge of rust-lang#118031 - saethlin:drop-inline-for-complicated…
Browse files Browse the repository at this point in the history
…-debug, r=<try>

Try not applying #[inline] to big structs

Per rust-lang#117727 (comment)

r? `@ghost`
  • Loading branch information
bors committed Nov 18, 2023
2 parents 1a740c3 + 2a67ddf commit 6973d65
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion compiler/rustc_builtin_macros/src/deriving/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use crate::deriving::generic::*;
use crate::deriving::path_std;

use ast::EnumDef;
use rustc_ast::ItemKind;
use rustc_ast::VariantData;
use rustc_ast::{self as ast, MetaItem};
use rustc_expand::base::{Annotatable, ExtCtxt};
use rustc_span::symbol::{sym, Ident, Symbol};
Expand All @@ -20,6 +22,23 @@ pub fn expand_deriving_debug(
// &mut ::std::fmt::Formatter
let fmtr = Ref(Box::new(Path(path_std!(fmt::Formatter))), ast::Mutability::Mut);

// We default to applying #[inline]
let mut attributes = thin_vec![cx.attr_word(sym::inline, span)];
if let Annotatable::Item(item) = item {
match &item.kind {
ItemKind::Struct(VariantData::Struct(fields, _) | VariantData::Tuple(fields, _), _) => {
// Except that we drop it for structs with more than 5 fields, because with less
// than 5 fields we implement Debug with a single function call, which makes
// Debug::fmt a trivial wrapper that always should be optimized away. But with more
// than 5 fields, Debug::fmt has a complicated body.
if fields.len() > 5 {
attributes = ThinVec::new();
}
}
_ => {}
}
}

let trait_def = TraitDef {
span,
path: path_std!(fmt::Debug),
Expand All @@ -33,7 +52,7 @@ pub fn expand_deriving_debug(
explicit_self: true,
nonself_args: vec![(fmtr, sym::f)],
ret_ty: Path(path_std!(fmt::Result)),
attributes: thin_vec![cx.attr_word(sym::inline, span)],
attributes,
fieldless_variants_strategy:
FieldlessVariantsStrategy::SpecializeIfAllVariantsFieldless,
combine_substructure: combine_substructure(Box::new(|a, b, c| {
Expand Down

0 comments on commit 6973d65

Please sign in to comment.