1 use crate::deriving::generic::ty::*;
2 use crate::deriving::generic::*;
3 use crate::deriving::path_std;
6 use rustc_ast::{self as ast, MetaItem};
7 use rustc_expand::base::{Annotatable, ExtCtxt};
8 use rustc_span::symbol::{sym, Ident, Symbol};
11 pub fn expand_deriving_debug(
16 push: &mut dyn FnMut(Annotatable),
19 // &mut ::std::fmt::Formatter
20 let fmtr = Ref(Box::new(Path(path_std!(fmt::Formatter))), ast::Mutability::Mut);
22 let trait_def = TraitDef {
24 path: path_std!(fmt::Debug),
25 skip_path_as_bound: false,
26 needs_copy_as_bound_if_packed: true,
27 additional_bounds: Vec::new(),
28 supports_unions: false,
29 methods: vec![MethodDef {
31 generics: Bounds::empty(),
33 nonself_args: vec![(fmtr, sym::f)],
34 ret_ty: Path(path_std!(fmt::Result)),
35 attributes: ast::AttrVec::new(),
36 fieldless_variants_strategy:
37 FieldlessVariantsStrategy::SpecializeIfAllVariantsFieldless,
38 combine_substructure: combine_substructure(Box::new(|a, b, c| {
39 show_substructure(a, b, c)
42 associated_types: Vec::new(),
45 trait_def.expand(cx, mitem, item, push)
48 fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> BlockOrExpr {
49 // We want to make sure we have the ctxt set so that we can use unstable methods
50 let span = cx.with_def_site_ctxt(span);
52 let (ident, vdata, fields) = match substr.fields {
53 Struct(vdata, fields) => (substr.type_ident, *vdata, fields),
54 EnumMatching(_, _, v, fields) => (v.ident, &v.data, fields),
55 AllFieldlessEnum(enum_def) => return show_fieldless_enum(cx, span, enum_def, substr),
56 EnumTag(..) | StaticStruct(..) | StaticEnum(..) => {
57 cx.span_bug(span, "nonsensical .fields in `#[derive(Debug)]`")
61 let name = cx.expr_str(span, ident.name);
62 let fmt = substr.nonselflike_args[0].clone();
64 // Struct and tuples are similar enough that we use the same code for both,
65 // with some extra pieces for structs due to the field names.
66 let (is_struct, args_per_field) = match vdata {
67 ast::VariantData::Unit(..) => {
68 // Special fast path for unit variants.
69 assert!(fields.is_empty());
72 ast::VariantData::Tuple(..) => (false, 1),
73 ast::VariantData::Struct(..) => (true, 2),
76 // The number of fields that can be handled without an array.
77 const CUTOFF: usize = 5;
79 if fields.is_empty() {
80 // Special case for no fields.
81 let fn_path_write_str = cx.std_path(&[sym::fmt, sym::Formatter, sym::write_str]);
82 let expr = cx.expr_call_global(span, fn_path_write_str, vec![fmt, name]);
83 BlockOrExpr::new_expr(expr)
84 } else if fields.len() <= CUTOFF {
85 // Few enough fields that we can use a specific-length method.
86 let debug = if is_struct {
87 format!("debug_struct_field{}_finish", fields.len())
89 format!("debug_tuple_field{}_finish", fields.len())
91 let fn_path_debug = cx.std_path(&[sym::fmt, sym::Formatter, Symbol::intern(&debug)]);
93 let mut args = Vec::with_capacity(2 + fields.len() * args_per_field);
94 args.extend([fmt, name]);
95 for i in 0..fields.len() {
96 let field = &fields[i];
98 let name = cx.expr_str(field.span, field.name.unwrap().name);
101 // Use an extra indirection to make sure this works for unsized types.
102 let field = cx.expr_addr_of(field.span, field.self_expr.clone());
105 let expr = cx.expr_call_global(span, fn_path_debug, args);
106 BlockOrExpr::new_expr(expr)
108 // Enough fields that we must use the any-length method.
109 let mut name_exprs = Vec::with_capacity(fields.len());
110 let mut value_exprs = Vec::with_capacity(fields.len());
112 for field in fields {
114 name_exprs.push(cx.expr_str(field.span, field.name.unwrap().name));
117 // Use an extra indirection to make sure this works for unsized types.
118 let field = cx.expr_addr_of(field.span, field.self_expr.clone());
119 value_exprs.push(field);
122 // `let names: &'static _ = &["field1", "field2"];`
123 let names_let = if is_struct {
124 let lt_static = Some(cx.lifetime_static(span));
125 let ty_static_ref = cx.ty_ref(span, cx.ty_infer(span), lt_static, ast::Mutability::Not);
129 Ident::new(sym::names, span),
131 cx.expr_array_ref(span, name_exprs),
137 // `let values: &[&dyn Debug] = &[&&self.field1, &&self.field2];`
138 let path_debug = cx.path_global(span, cx.std_path(&[sym::fmt, sym::Debug]));
139 let ty_dyn_debug = cx.ty(
141 ast::TyKind::TraitObject(vec![cx.trait_bound(path_debug)], ast::TraitObjectSyntax::Dyn),
143 let ty_slice = cx.ty(
145 ast::TyKind::Slice(cx.ty_ref(span, ty_dyn_debug, None, ast::Mutability::Not)),
147 let values_let = cx.stmt_let_ty(
150 Ident::new(sym::values, span),
151 Some(cx.ty_ref(span, ty_slice, None, ast::Mutability::Not)),
152 cx.expr_array_ref(span, value_exprs),
155 // `fmt::Formatter::debug_struct_fields_finish(fmt, name, names, values)` or
156 // `fmt::Formatter::debug_tuple_fields_finish(fmt, name, values)`
157 let sym_debug = if is_struct {
158 sym::debug_struct_fields_finish
160 sym::debug_tuple_fields_finish
162 let fn_path_debug_internal = cx.std_path(&[sym::fmt, sym::Formatter, sym_debug]);
164 let mut args = Vec::with_capacity(4);
168 args.push(cx.expr_ident(span, Ident::new(sym::names, span)));
170 args.push(cx.expr_ident(span, Ident::new(sym::values, span)));
171 let expr = cx.expr_call_global(span, fn_path_debug_internal, args);
173 let mut stmts = Vec::with_capacity(3);
175 stmts.push(names_let.unwrap());
177 stmts.push(values_let);
178 BlockOrExpr::new_mixed(stmts, Some(expr))
182 /// Special case for enums with no fields. Builds:
184 /// impl ::core::fmt::Debug for A {
185 /// fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
186 /// ::core::fmt::Formatter::write_str(f,
195 fn show_fieldless_enum(
196 cx: &mut ExtCtxt<'_>,
199 substr: &Substructure<'_>,
201 let fmt = substr.nonselflike_args[0].clone();
206 let variant_path = cx.path(span, vec![substr.type_ident, v.ident]);
207 let pat = match &v.data {
208 ast::VariantData::Tuple(fields, _) => {
209 debug_assert!(fields.is_empty());
210 cx.pat_tuple_struct(span, variant_path, vec![])
212 ast::VariantData::Struct(fields, _) => {
213 debug_assert!(fields.is_empty());
214 cx.pat_struct(span, variant_path, vec![])
216 ast::VariantData::Unit(_) => cx.pat_path(span, variant_path),
218 cx.arm(span, pat, cx.expr_str(span, v.ident.name))
220 .collect::<Vec<_>>();
221 let name = cx.expr_match(span, cx.expr_self(span), arms);
222 let fn_path_write_str = cx.std_path(&[sym::fmt, sym::Formatter, sym::write_str]);
223 BlockOrExpr::new_expr(cx.expr_call_global(span, fn_path_write_str, vec![fmt, name]))