@@ -16,11 +16,11 @@ pub(crate) fn expand_deriving_debug(
1616 is_const : bool ,
1717) {
1818 // &mut ::std::fmt::Formatter
19- let fmtr = Ref ( Box :: new ( Path ( path_std ! ( fmt:: Formatter ) ) ) , ast:: Mutability :: Mut ) ;
19+ let fmtr = Ref ( Box :: new ( Path ( path_std ! ( cx , span , fmt:: Formatter ) ) ) , ast:: Mutability :: Mut ) ;
2020
2121 let trait_def = TraitDef {
2222 span,
23- path : path_std ! ( fmt:: Debug ) ,
23+ path : path_std ! ( cx , span , fmt:: Debug ) ,
2424 skip_path_as_bound : false ,
2525 needs_copy_as_bound_if_packed : true ,
2626 additional_bounds : SmallVec :: new ( ) ,
@@ -30,11 +30,16 @@ pub(crate) fn expand_deriving_debug(
3030 generics: cx. empty_generics( span) ,
3131 explicit_self: true ,
3232 nonself_args: smallvec![ ( fmtr, sym:: character( 'f' ) ) ] ,
33- ret_ty: Path ( path_std!( fmt:: Result ) ) ,
33+ ret_ty: Path ( path_std!( cx , span , fmt:: Result ) ) ,
3434 attributes: thin_vec![ cx. attr_word( sym:: inline, span) ] ,
3535 fieldless_variants_strategy:
3636 FieldlessVariantsStrategy :: SpecializeIfAllVariantsFieldless ,
37- combine_substructure: combine_substructure( show_substructure) ,
37+ combine_substructure: combine_substructure( |cx, span, substr| show_substructure(
38+ cx,
39+ span,
40+ substr,
41+ item. kind. ident( ) . unwrap( )
42+ ) ) ,
3843 } ] ,
3944 associated_types : SmallVec :: new ( ) ,
4045 is_const,
@@ -44,24 +49,30 @@ pub(crate) fn expand_deriving_debug(
4449 trait_def. expand ( cx, item, push)
4550}
4651
47- fn show_substructure ( cx : & ExtCtxt < ' _ > , span : Span , substr : Substructure < ' _ > ) -> BlockOrExpr {
48- // We want to make sure we have the ctxt set so that we can use unstable methods
49- let span = cx . with_def_site_ctxt ( span ) ;
52+ fn formatter_ident ( cx : & ExtCtxt < ' _ > , span : Span ) -> Box < ast :: Expr > {
53+ cx . expr_ident ( span , Ident :: new ( sym :: character ( 'f' ) , span ) )
54+ }
5055
56+ fn show_substructure (
57+ cx : & ExtCtxt < ' _ > ,
58+ span : Span ,
59+ substr : Substructure < ' _ > ,
60+ type_ident : Ident ,
61+ ) -> BlockOrExpr {
5162 let fmt_detail = cx. sess . opts . unstable_opts . fmt_debug ;
5263 if fmt_detail == FmtDebug :: None {
5364 return BlockOrExpr :: new_expr ( cx. expr_ok ( span, cx. expr_tuple ( span, ThinVec :: new ( ) ) ) ) ;
5465 }
5566
56- let ( ident, vdata, fields) = match substr. fields {
57- Struct ( vdata, fields) => ( substr . type_ident , vdata, fields) ,
67+ let ( ident, vdata, fields) = match substr {
68+ Struct ( vdata, fields) => ( type_ident, vdata, fields) ,
5869 EnumMatching ( v, fields) => ( v. ident , & v. data , fields) ,
59- AllFieldlessEnum ( enum_def) => return show_fieldless_enum ( cx, span, enum_def, substr ) ,
70+ AllFieldlessEnum ( enum_def) => return show_fieldless_enum ( cx, span, enum_def, type_ident ) ,
6071 _ => cx. dcx ( ) . span_bug ( span, "unexpected substructure in `derive(Debug)`" ) ,
6172 } ;
6273
6374 let name = cx. expr_str ( span, ident. name ) ;
64- let fmt = substr . nonselflike_args [ 0 ] . clone ( ) ;
75+ let fmt = formatter_ident ( cx , span ) ;
6576
6677 // Fieldless enums have been special-cased earlier
6778 if fmt_detail == FmtDebug :: Shallow {
@@ -85,13 +96,14 @@ fn show_substructure(cx: &ExtCtxt<'_>, span: Span, substr: Substructure<'_>) ->
8596 // The number of fields that can be handled without an array.
8697 const CUTOFF : usize = 5 ;
8798
88- let expr_for_field = |field : & FieldInfo , index : usize | -> Box < ast:: Expr > {
89- if index < fields. len ( ) - 1 {
90- field. self_expr . clone ( )
99+ let len = fields. len ( ) ;
100+ let expr_for_field = |field : FieldInfo , index : usize | -> Box < ast:: Expr > {
101+ if index < len - 1 {
102+ field. self_expr
91103 } else {
92104 // Unsized types need an extra indirection, but only the last field
93105 // may be unsized.
94- cx. expr_addr_of ( field. span , field. self_expr . clone ( ) )
106+ cx. expr_addr_of ( field. span , field. self_expr )
95107 }
96108 } ;
97109
@@ -111,8 +123,7 @@ fn show_substructure(cx: &ExtCtxt<'_>, span: Span, substr: Substructure<'_>) ->
111123
112124 let mut args = ThinVec :: with_capacity ( 2 + fields. len ( ) * args_per_field) ;
113125 args. extend ( [ fmt, name] ) ;
114- for i in 0 ..fields. len ( ) {
115- let field = & fields[ i] ;
126+ for ( i, field) in fields. into_iter ( ) . enumerate ( ) {
116127 if is_struct {
117128 let name = cx. expr_str ( field. span , field. name . unwrap ( ) . name ) ;
118129 args. push ( name) ;
@@ -128,8 +139,7 @@ fn show_substructure(cx: &ExtCtxt<'_>, span: Span, substr: Substructure<'_>) ->
128139 let mut name_exprs = ThinVec :: with_capacity ( fields. len ( ) ) ;
129140 let mut value_exprs = ThinVec :: with_capacity ( fields. len ( ) ) ;
130141
131- for i in 0 ..fields. len ( ) {
132- let field = & fields[ i] ;
142+ for ( i, field) in fields. into_iter ( ) . enumerate ( ) {
133143 if is_struct {
134144 name_exprs. push ( cx. expr_str ( field. span , field. name . unwrap ( ) . name ) ) ;
135145 }
@@ -216,14 +226,14 @@ fn show_fieldless_enum(
216226 cx : & ExtCtxt < ' _ > ,
217227 span : Span ,
218228 def : & EnumDef ,
219- substr : Substructure < ' _ > ,
229+ type_ident : Ident ,
220230) -> BlockOrExpr {
221- let fmt = substr . nonselflike_args [ 0 ] . clone ( ) ;
231+ let fmt = formatter_ident ( cx , span ) ;
222232 let arms = def
223233 . variants
224234 . iter ( )
225235 . map ( |v| {
226- let variant_path = cx. path ( span, vec ! [ substr . type_ident, v. ident] ) ;
236+ let variant_path = cx. path ( span, vec ! [ type_ident, v. ident] ) ;
227237 let pat = match & v. data {
228238 ast:: VariantData :: Tuple ( fields, _) => {
229239 debug_assert ! ( fields. is_empty( ) ) ;
0 commit comments