@@ -36,7 +36,7 @@ use crate::{
36
36
//
37
37
// impl Debug for S {
38
38
// $0fn fmt(&self, f: &mut Formatter) -> Result<()> {
39
- // f.debug_struct(S )
39
+ // f.debug_struct("S").finish( )
40
40
// }
41
41
// }
42
42
// ```
@@ -183,25 +183,37 @@ fn gen_debug_impl(adt: &ast::Adt, fn_: &ast::Fn, annotated_name: &ast::Name) {
183
183
match adt {
184
184
ast:: Adt :: Union ( _) => { } // `Debug` cannot be derived for unions, so no default impl can be provided.
185
185
ast:: Adt :: Enum ( _) => { } // TODO
186
- ast:: Adt :: Struct ( strukt) => {
187
- match strukt. field_list ( ) {
188
- Some ( ast:: FieldList :: RecordFieldList ( field_list) ) => {
189
- let name = format ! ( "\" {}\" " , annotated_name) ;
190
- let args = make:: arg_list ( Some ( make:: expr_literal ( & name) . into ( ) ) ) ;
191
- let target = make:: expr_path ( make:: ext:: ident_path ( "f" ) ) ;
192
- let mut expr = make:: expr_method_call ( target, "debug_struct" , args) ;
193
- for field in field_list. fields ( ) {
194
- let args = make:: arg_list ( Some ( make:: expr_path ( & name) . into ( ) ) ) ;
186
+ ast:: Adt :: Struct ( strukt) => match strukt. field_list ( ) {
187
+ Some ( ast:: FieldList :: RecordFieldList ( field_list) ) => {
188
+ let name = format ! ( "\" {}\" " , annotated_name) ;
189
+ let args = make:: arg_list ( Some ( make:: expr_literal ( & name) . into ( ) ) ) ;
190
+ let target = make:: expr_path ( make:: ext:: ident_path ( "f" ) ) ;
191
+ let mut expr = make:: expr_method_call ( target, "debug_struct" , args) ;
192
+ for field in field_list. fields ( ) {
193
+ if let Some ( name) = field. name ( ) {
194
+ let f_name = make:: expr_literal ( & ( format ! ( "\" {}\" " , name) ) ) . into ( ) ;
195
+ let f_path = make:: expr_path ( make:: ext:: ident_path ( "self" ) ) ;
196
+ let f_path = make:: expr_ref ( f_path, false ) ;
197
+ let f_path = make:: expr_field ( f_path, & format ! ( "{}" , name) ) . into ( ) ;
198
+ let args = make:: arg_list ( vec ! [ f_name, f_path] ) ;
195
199
expr = make:: expr_method_call ( expr, "field" , args) ;
196
200
}
197
- let expr = make:: expr_method_call ( expr, "finish" , make:: arg_list ( None ) ) ;
198
- let body = make:: block_expr ( None , Some ( expr) ) . indent ( ast:: edit:: IndentLevel ( 1 ) ) ;
199
- ted:: replace ( fn_. body ( ) . unwrap ( ) . syntax ( ) , body. clone_for_update ( ) . syntax ( ) ) ;
200
201
}
201
- Some ( ast:: FieldList :: TupleFieldList ( field_list) ) => { }
202
- None => { } // `Debug` cannot be implemented for an incomplete struct.
202
+ let expr = make:: expr_method_call ( expr, "finish" , make:: arg_list ( None ) ) ;
203
+ let body = make:: block_expr ( None , Some ( expr) ) . indent ( ast:: edit:: IndentLevel ( 1 ) ) ;
204
+ ted:: replace ( fn_. body ( ) . unwrap ( ) . syntax ( ) , body. clone_for_update ( ) . syntax ( ) ) ;
203
205
}
204
- }
206
+ Some ( ast:: FieldList :: TupleFieldList ( field_list) ) => { }
207
+ None => {
208
+ let name = format ! ( "\" {}\" " , annotated_name) ;
209
+ let args = make:: arg_list ( Some ( make:: expr_literal ( & name) . into ( ) ) ) ;
210
+ let target = make:: expr_path ( make:: ext:: ident_path ( "f" ) ) ;
211
+ let expr = make:: expr_method_call ( target, "debug_struct" , args) ;
212
+ let expr = make:: expr_method_call ( expr, "finish" , make:: arg_list ( None ) ) ;
213
+ let body = make:: block_expr ( None , Some ( expr) ) . indent ( ast:: edit:: IndentLevel ( 1 ) ) ;
214
+ ted:: replace ( fn_. body ( ) . unwrap ( ) . syntax ( ) , body. clone_for_update ( ) . syntax ( ) ) ;
215
+ }
216
+ } ,
205
217
}
206
218
}
207
219
0 commit comments