@@ -265,21 +265,24 @@ impl IrMaps<'tcx> {
265
265
self . capture_info_map . insert ( hir_id, Rc :: new ( cs) ) ;
266
266
}
267
267
268
- fn add_from_pat ( & mut self , pat : & hir:: Pat < ' tcx > ) {
268
+ fn collect_shorthand_field_ids ( & self , pat : & hir:: Pat < ' tcx > ) -> HirIdSet {
269
269
// For struct patterns, take note of which fields used shorthand
270
270
// (`x` rather than `x: x`).
271
271
let mut shorthand_field_ids = HirIdSet :: default ( ) ;
272
272
let mut pats = VecDeque :: new ( ) ;
273
273
pats. push_back ( pat) ;
274
+
274
275
while let Some ( pat) = pats. pop_front ( ) {
275
276
use rustc_hir:: PatKind :: * ;
276
277
match & pat. kind {
277
278
Binding ( .., inner_pat) => {
278
279
pats. extend ( inner_pat. iter ( ) ) ;
279
280
}
280
281
Struct ( _, fields, _) => {
281
- let ids = fields. iter ( ) . filter ( |f| f. is_shorthand ) . map ( |f| f. pat . hir_id ) ;
282
- shorthand_field_ids. extend ( ids) ;
282
+ let ( short, not_short) : ( Vec < & _ > , Vec < & _ > ) =
283
+ fields. iter ( ) . partition ( |f| f. is_shorthand ) ;
284
+ shorthand_field_ids. extend ( short. iter ( ) . map ( |f| f. pat . hir_id ) ) ;
285
+ pats. extend ( not_short. iter ( ) . map ( |f| f. pat ) ) ;
283
286
}
284
287
Ref ( inner_pat, _) | Box ( inner_pat) => {
285
288
pats. push_back ( inner_pat) ;
@@ -296,6 +299,12 @@ impl IrMaps<'tcx> {
296
299
}
297
300
}
298
301
302
+ return shorthand_field_ids;
303
+ }
304
+
305
+ fn add_from_pat ( & mut self , pat : & hir:: Pat < ' tcx > ) {
306
+ let shorthand_field_ids = self . collect_shorthand_field_ids ( pat) ;
307
+
299
308
pat. each_binding ( |_, hir_id, _, ident| {
300
309
self . add_live_node_for_node ( hir_id, VarDefNode ( ident. span , hir_id) ) ;
301
310
self . add_variable ( Local ( LocalInfo {
@@ -373,15 +382,13 @@ impl<'tcx> Visitor<'tcx> for IrMaps<'tcx> {
373
382
}
374
383
375
384
fn visit_param ( & mut self , param : & ' tcx hir:: Param < ' tcx > ) {
385
+ let shorthand_field_ids = self . collect_shorthand_field_ids ( param. pat ) ;
376
386
param. pat . each_binding ( |_bm, hir_id, _x, ident| {
377
387
let var = match param. pat . kind {
378
- rustc_hir:: PatKind :: Struct ( _ , fields , _ ) => Local ( LocalInfo {
388
+ rustc_hir:: PatKind :: Struct ( .. ) => Local ( LocalInfo {
379
389
id : hir_id,
380
390
name : ident. name ,
381
- is_shorthand : fields
382
- . iter ( )
383
- . find ( |f| f. ident == ident)
384
- . map_or ( false , |f| f. is_shorthand ) ,
391
+ is_shorthand : shorthand_field_ids. contains ( & hir_id) ,
385
392
} ) ,
386
393
_ => Param ( hir_id, ident. name ) ,
387
394
} ;
0 commit comments