]> git.lizzy.rs Git - rust.git/blob - src/librustc_builtin_macros/deriving/debug.rs
Rollup merge of #69577 - GuillaumeGomez:cleanup-e0375, r=Dylan-DPC
[rust.git] / src / librustc_builtin_macros / deriving / debug.rs
1 use crate::deriving::generic::ty::*;
2 use crate::deriving::generic::*;
3 use crate::deriving::path_std;
4
5 use rustc_ast::ast::{self, Ident};
6 use rustc_ast::ast::{Expr, MetaItem};
7 use rustc_ast::ptr::P;
8 use rustc_expand::base::{Annotatable, ExtCtxt};
9 use rustc_span::symbol::sym;
10 use rustc_span::{Span, DUMMY_SP};
11
12 pub fn expand_deriving_debug(
13     cx: &mut ExtCtxt<'_>,
14     span: Span,
15     mitem: &MetaItem,
16     item: &Annotatable,
17     push: &mut dyn FnMut(Annotatable),
18 ) {
19     // &mut ::std::fmt::Formatter
20     let fmtr =
21         Ptr(Box::new(Literal(path_std!(cx, fmt::Formatter))), Borrowed(None, ast::Mutability::Mut));
22
23     let trait_def = TraitDef {
24         span,
25         attributes: Vec::new(),
26         path: path_std!(cx, fmt::Debug),
27         additional_bounds: Vec::new(),
28         generics: LifetimeBounds::empty(),
29         is_unsafe: false,
30         supports_unions: false,
31         methods: vec![MethodDef {
32             name: "fmt",
33             generics: LifetimeBounds::empty(),
34             explicit_self: borrowed_explicit_self(),
35             args: vec![(fmtr, "f")],
36             ret_ty: Literal(path_std!(cx, fmt::Result)),
37             attributes: Vec::new(),
38             is_unsafe: false,
39             unify_fieldless_variants: false,
40             combine_substructure: combine_substructure(Box::new(|a, b, c| {
41                 show_substructure(a, b, c)
42             })),
43         }],
44         associated_types: Vec::new(),
45     };
46     trait_def.expand(cx, mitem, item, push)
47 }
48
49 /// We use the debug builders to do the heavy lifting here
50 fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> P<Expr> {
51     // build fmt.debug_struct(<name>).field(<fieldname>, &<fieldval>)....build()
52     // or fmt.debug_tuple(<name>).field(&<fieldval>)....build()
53     // based on the "shape".
54     let (ident, vdata, fields) = match substr.fields {
55         Struct(vdata, fields) => (substr.type_ident, *vdata, fields),
56         EnumMatching(_, _, v, fields) => (v.ident, &v.data, fields),
57         EnumNonMatchingCollapsed(..) | StaticStruct(..) | StaticEnum(..) => {
58             cx.span_bug(span, "nonsensical .fields in `#[derive(Debug)]`")
59         }
60     };
61
62     // We want to make sure we have the ctxt set so that we can use unstable methods
63     let span = cx.with_def_site_ctxt(span);
64     let name = cx.expr_lit(span, ast::LitKind::Str(ident.name, ast::StrStyle::Cooked));
65     let builder = cx.ident_of("debug_trait_builder", span);
66     let builder_expr = cx.expr_ident(span, builder.clone());
67
68     let fmt = substr.nonself_args[0].clone();
69
70     let mut stmts = vec![];
71     match vdata {
72         ast::VariantData::Tuple(..) | ast::VariantData::Unit(..) => {
73             // tuple struct/"normal" variant
74             let expr = cx.expr_method_call(span, fmt, cx.ident_of("debug_tuple", span), vec![name]);
75             stmts.push(cx.stmt_let(span, true, builder, expr));
76
77             for field in fields {
78                 // Use double indirection to make sure this works for unsized types
79                 let field = cx.expr_addr_of(field.span, field.self_.clone());
80                 let field = cx.expr_addr_of(field.span, field);
81
82                 let expr = cx.expr_method_call(
83                     span,
84                     builder_expr.clone(),
85                     Ident::new(sym::field, span),
86                     vec![field],
87                 );
88
89                 // Use `let _ = expr;` to avoid triggering the
90                 // unused_results lint.
91                 stmts.push(stmt_let_undescore(cx, span, expr));
92             }
93         }
94         ast::VariantData::Struct(..) => {
95             // normal struct/struct variant
96             let expr =
97                 cx.expr_method_call(span, fmt, cx.ident_of("debug_struct", span), vec![name]);
98             stmts.push(cx.stmt_let(DUMMY_SP, true, builder, expr));
99
100             for field in fields {
101                 let name = cx.expr_lit(
102                     field.span,
103                     ast::LitKind::Str(field.name.unwrap().name, ast::StrStyle::Cooked),
104                 );
105
106                 // Use double indirection to make sure this works for unsized types
107                 let field = cx.expr_addr_of(field.span, field.self_.clone());
108                 let field = cx.expr_addr_of(field.span, field);
109                 let expr = cx.expr_method_call(
110                     span,
111                     builder_expr.clone(),
112                     Ident::new(sym::field, span),
113                     vec![name, field],
114                 );
115                 stmts.push(stmt_let_undescore(cx, span, expr));
116             }
117         }
118     }
119
120     let expr = cx.expr_method_call(span, builder_expr, cx.ident_of("finish", span), vec![]);
121
122     stmts.push(cx.stmt_expr(expr));
123     let block = cx.block(span, stmts);
124     cx.expr_block(block)
125 }
126
127 fn stmt_let_undescore(cx: &mut ExtCtxt<'_>, sp: Span, expr: P<ast::Expr>) -> ast::Stmt {
128     let local = P(ast::Local {
129         pat: cx.pat_wild(sp),
130         ty: None,
131         init: Some(expr),
132         id: ast::DUMMY_NODE_ID,
133         span: sp,
134         attrs: ast::AttrVec::new(),
135     });
136     ast::Stmt { id: ast::DUMMY_NODE_ID, kind: ast::StmtKind::Local(local), span: sp }
137 }