@@ -6,7 +6,7 @@ use rustc_ast::ptr::P;
6
6
use rustc_ast:: { self as ast, Expr , LocalKind , MetaItem } ;
7
7
use rustc_expand:: base:: { Annotatable , ExtCtxt } ;
8
8
use rustc_span:: symbol:: { sym, Ident } ;
9
- use rustc_span:: { Span , DUMMY_SP } ;
9
+ use rustc_span:: Span ;
10
10
11
11
fn make_mut_borrow ( cx : & mut ExtCtxt < ' _ > , sp : Span , expr : P < Expr > ) -> P < Expr > {
12
12
cx. expr ( sp, ast:: ExprKind :: AddrOf ( ast:: BorrowKind :: Ref , ast:: Mutability :: Mut , expr) )
@@ -78,6 +78,65 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
78
78
return cx. expr_block ( block) ;
79
79
}
80
80
81
+ if let ast:: VariantData :: Struct ( ..) = vdata {
82
+ let mut name_exprs = Vec :: with_capacity ( fields. len ( ) + 1 ) ;
83
+ let mut value_exprs = Vec :: with_capacity ( fields. len ( ) ) ;
84
+
85
+ name_exprs. push ( name) ;
86
+ for field in fields {
87
+ name_exprs. push ( cx. expr_lit (
88
+ field. span ,
89
+ ast:: LitKind :: Str ( field. name . unwrap ( ) . name , ast:: StrStyle :: Cooked ) ,
90
+ ) ) ;
91
+
92
+ // Use double indirection to make sure this works for unsized types
93
+ let value_ref = cx. expr_addr_of ( field. span , field. self_ . clone ( ) ) ;
94
+ value_exprs. push ( cx. expr_addr_of ( field. span , value_ref) ) ;
95
+ }
96
+
97
+ // `let names: &'static _ = &["TheType", "field1", "field2"];`
98
+ let lt_static = Some ( cx. lifetime_static ( span) ) ;
99
+ let ty_static_ref = cx. ty_rptr ( span, cx. ty_infer ( span) , lt_static, ast:: Mutability :: Not ) ;
100
+ let names_let = cx. stmt_let_ty (
101
+ span,
102
+ false ,
103
+ Ident :: new ( sym:: names, span) ,
104
+ Some ( ty_static_ref) ,
105
+ cx. expr_array_ref ( span, name_exprs) ,
106
+ ) ;
107
+
108
+ // `let values: &[&dyn Debug] = &[&&self.field1, &&self.field2];`
109
+ let path_debug = cx. path_global ( span, cx. std_path ( & [ sym:: fmt, sym:: Debug ] ) ) ;
110
+ let ty_dyn_debug = cx. ty (
111
+ span,
112
+ ast:: TyKind :: TraitObject ( vec ! [ cx. trait_bound( path_debug) ] , ast:: TraitObjectSyntax :: Dyn ) ,
113
+ ) ;
114
+ let ty_slice = cx. ty (
115
+ span,
116
+ ast:: TyKind :: Slice ( cx. ty_rptr ( span, ty_dyn_debug, None , ast:: Mutability :: Not ) ) ,
117
+ ) ;
118
+ let values_let = cx. stmt_let_ty (
119
+ span,
120
+ false ,
121
+ Ident :: new ( sym:: values, span) ,
122
+ Some ( cx. ty_rptr ( span, ty_slice, None , ast:: Mutability :: Not ) ) ,
123
+ cx. expr_array_ref ( span, value_exprs) ,
124
+ ) ;
125
+
126
+ // `fmt::DebugStruct::internal_debug_from_slices(fmt, names, values)`
127
+ let fn_path_debug_internal =
128
+ cx. std_path ( & [ sym:: fmt, sym:: DebugStruct , sym:: internal_debug_from_slices] ) ;
129
+ let args = vec ! [
130
+ fmt,
131
+ cx. expr_ident( span, Ident :: new( sym:: names, span) ) ,
132
+ cx. expr_ident( span, Ident :: new( sym:: values, span) ) ,
133
+ ] ;
134
+ let expr = cx. expr_call_global ( span, fn_path_debug_internal, args) ;
135
+
136
+ let block = cx. block ( span, vec ! [ names_let, values_let, cx. stmt_expr( expr) ] ) ;
137
+ return cx. expr_block ( block) ;
138
+ }
139
+
81
140
let builder = Ident :: new ( sym:: debug_trait_builder, span) ;
82
141
let builder_expr = cx. expr_ident ( span, builder) ;
83
142
@@ -111,6 +170,9 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
111
170
fn_path_finish = cx. std_path ( & [ sym:: fmt, sym:: DebugTuple , sym:: finish] ) ;
112
171
}
113
172
ast:: VariantData :: Struct ( ..) => {
173
+ cx. span_bug ( span, "structs should have been handled above" ) ;
174
+
175
+ /*
114
176
// normal struct/struct variant
115
177
let fn_path_debug_struct = cx.std_path(&[sym::fmt, sym::Formatter, sym::debug_struct]);
116
178
let expr = cx.expr_call_global(span, fn_path_debug_struct, vec![fmt, name]);
@@ -135,6 +197,7 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
135
197
stmts.push(stmt_let_underscore(cx, span, expr));
136
198
}
137
199
fn_path_finish = cx.std_path(&[sym::fmt, sym::DebugStruct, sym::finish]);
200
+ */
138
201
}
139
202
}
140
203
0 commit comments