]> git.lizzy.rs Git - rust.git/blob - src/libsyntax_ext/deriving/debug.rs
Auto merge of #63655 - Centril:rollup-ty1ot40, r=Centril
[rust.git] / src / libsyntax_ext / deriving / debug.rs
1 use crate::deriving::path_std;
2 use crate::deriving::generic::*;
3 use crate::deriving::generic::ty::*;
4
5 use rustc_data_structures::thin_vec::ThinVec;
6
7 use syntax::ast::{self, Ident};
8 use syntax::ast::{Expr, MetaItem};
9 use syntax::ext::base::{Annotatable, ExtCtxt};
10 use syntax::ptr::P;
11 use syntax::symbol::sym;
12 use syntax_pos::{DUMMY_SP, Span};
13
14 pub fn expand_deriving_debug(cx: &mut ExtCtxt<'_>,
15                              span: Span,
16                              mitem: &MetaItem,
17                              item: &Annotatable,
18                              push: &mut dyn FnMut(Annotatable)) {
19     // &mut ::std::fmt::Formatter
20     let fmtr = Ptr(Box::new(Literal(path_std!(cx, fmt::Formatter))),
21                    Borrowed(None, ast::Mutability::Mutable));
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(..) |
58         StaticStruct(..) |
59         StaticEnum(..) => cx.span_bug(span, "nonsensical .fields in `#[derive(Debug)]`"),
60     };
61
62     // We want to make sure we have the ctxt set so that we can use unstable methods
63     let span = span.with_ctxt(cx.backtrace());
64     let name = cx.expr_lit(span, ast::LitKind::Str(ident.name, ast::StrStyle::Cooked));
65     let builder = Ident::from_str("debug_trait_builder").gensym();
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 =
75                 cx.expr_method_call(span, fmt, Ident::from_str("debug_tuple"), vec![name]);
76             stmts.push(cx.stmt_let(DUMMY_SP, true, builder, expr));
77
78             for field in fields {
79                 // Use double indirection to make sure this works for unsized types
80                 let field = cx.expr_addr_of(field.span, field.self_.clone());
81                 let field = cx.expr_addr_of(field.span, field);
82
83                 let expr = cx.expr_method_call(span,
84                                                 builder_expr.clone(),
85                                                 Ident::with_dummy_span(sym::field),
86                                                 vec![field]);
87
88                 // Use `let _ = expr;` to avoid triggering the
89                 // unused_results lint.
90                 stmts.push(stmt_let_undescore(cx, span, expr));
91             }
92         }
93         ast::VariantData::Struct(..) => {
94             // normal struct/struct variant
95             let expr =
96                 cx.expr_method_call(span, fmt, Ident::from_str("debug_struct"), vec![name]);
97             stmts.push(cx.stmt_let(DUMMY_SP, true, builder, expr));
98
99             for field in fields {
100                 let name = cx.expr_lit(field.span,
101                                         ast::LitKind::Str(field.name.unwrap().name,
102                                                             ast::StrStyle::Cooked));
103
104                 // Use double indirection to make sure this works for unsized types
105                 let field = cx.expr_addr_of(field.span, field.self_.clone());
106                 let field = cx.expr_addr_of(field.span, field);
107                 let expr = cx.expr_method_call(span,
108                                                 builder_expr.clone(),
109                                                 Ident::with_dummy_span(sym::field),
110                                                 vec![name, field]);
111                 stmts.push(stmt_let_undescore(cx, span, expr));
112             }
113         }
114     }
115
116     let expr = cx.expr_method_call(span, builder_expr, Ident::from_str("finish"), vec![]);
117
118     stmts.push(cx.stmt_expr(expr));
119     let block = cx.block(span, stmts);
120     cx.expr_block(block)
121 }
122
123 fn stmt_let_undescore(cx: &mut ExtCtxt<'_>, sp: Span, expr: P<ast::Expr>) -> ast::Stmt {
124     let local = P(ast::Local {
125         pat: cx.pat_wild(sp),
126         ty: None,
127         init: Some(expr),
128         id: ast::DUMMY_NODE_ID,
129         span: sp,
130         attrs: ThinVec::new(),
131     });
132     ast::Stmt {
133         id: ast::DUMMY_NODE_ID,
134         node: ast::StmtKind::Local(local),
135         span: sp,
136     }
137 }