@@ -138,10 +138,10 @@ struct LoweringContext<'a, 'hir> {
138138 impl_trait_defs : Vec < hir:: GenericParam < ' hir > > ,
139139 impl_trait_bounds : Vec < hir:: WherePredicate < ' hir > > ,
140140
141- /// NodeIds of labelled nodes that are lowered inside the current HIR owner.
142- labelled_node_id_to_local_id : NodeMap < hir:: ItemLocalId > ,
143- /// NodeIds of identifier that are lowered inside the current HIR owner.
144- ident_to_local_id : NodeMap < hir:: ItemLocalId > ,
141+ /// NodeIds of pattern identifiers and labelled nodes that are lowered inside the current HIR owner.
142+ ident_and_label_to_local_id : NodeMap < hir:: ItemLocalId > ,
143+ /// NodeIds that are lowered inside the current HIR owner. Only used for duplicate lowering check .
144+ node_id_to_local_id : NodeMap < hir:: ItemLocalId > ,
145145
146146 allow_try_trait : Lrc < [ Symbol ] > ,
147147 allow_gen_future : Lrc < [ Symbol ] > ,
@@ -178,8 +178,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
178178 current_hir_id_owner : hir:: CRATE_OWNER_ID ,
179179 current_def_id_parent : CRATE_DEF_ID ,
180180 item_local_id_counter : hir:: ItemLocalId :: ZERO ,
181- labelled_node_id_to_local_id : Default :: default ( ) ,
182- ident_to_local_id : Default :: default ( ) ,
181+ ident_and_label_to_local_id : Default :: default ( ) ,
182+ node_id_to_local_id : Default :: default ( ) ,
183183 trait_map : Default :: default ( ) ,
184184
185185 // Lowering state.
@@ -589,9 +589,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
589589
590590 let current_attrs = std:: mem:: take ( & mut self . attrs ) ;
591591 let current_bodies = std:: mem:: take ( & mut self . bodies ) ;
592- let current_labelled_node_id_to_local_id =
593- std:: mem:: take ( & mut self . labelled_node_id_to_local_id ) ;
594- let current_ident_to_local_id = std:: mem:: take ( & mut self . ident_to_local_id ) ;
592+ let current_ident_and_label_to_local_id =
593+ std:: mem:: take ( & mut self . ident_and_label_to_local_id ) ;
594+ let current_node_id_to_local_id = std:: mem:: take ( & mut self . node_id_to_local_id ) ;
595595 let current_trait_map = std:: mem:: take ( & mut self . trait_map ) ;
596596 let current_owner =
597597 std:: mem:: replace ( & mut self . current_hir_id_owner , hir:: OwnerId { def_id } ) ;
@@ -604,6 +604,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
604604 // we want `f` to be able to refer to the `LocalDefId`s that the caller created.
605605 // and the caller to refer to some of the subdefinitions' nodes' `LocalDefId`s.
606606
607+ // Always allocate the first `HirId` for the owner itself.
608+ let _old = self . node_id_to_local_id . insert ( owner, hir:: ItemLocalId :: ZERO ) ;
609+ debug_assert_eq ! ( _old, None ) ;
610+
607611 let item = self . with_def_id_parent ( def_id, f) ;
608612 debug_assert_eq ! ( def_id, item. def_id( ) . def_id) ;
609613 // `f` should have consumed all the elements in these vectors when constructing `item`.
@@ -613,8 +617,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
613617
614618 self . attrs = current_attrs;
615619 self . bodies = current_bodies;
616- self . labelled_node_id_to_local_id = current_labelled_node_id_to_local_id ;
617- self . ident_to_local_id = current_ident_to_local_id ;
620+ self . ident_and_label_to_local_id = current_ident_and_label_to_local_id ;
621+ self . node_id_to_local_id = current_node_id_to_local_id ;
618622 self . trait_map = current_trait_map;
619623 self . current_hir_id_owner = current_owner;
620624 self . item_local_id_counter = current_local_counter;
@@ -703,6 +707,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
703707 self . trait_map . insert ( hir_id. local_id , traits. into_boxed_slice ( ) ) ;
704708 }
705709
710+ // Check whether the same `NodeId` is lowered more than once.
711+ #[ cfg( debug_assertions) ]
712+ {
713+ let old = self . node_id_to_local_id . insert ( ast_node_id, local_id) ;
714+ assert_eq ! ( old, None ) ;
715+ }
716+
706717 hir_id
707718 }
708719
@@ -720,7 +731,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
720731 fn lower_res ( & mut self , res : Res < NodeId > ) -> Res {
721732 let res: Result < Res , ( ) > = res. apply_id ( |id| {
722733 let owner = self . current_hir_id_owner ;
723- let local_id = self . ident_to_local_id . get ( & id) . copied ( ) . ok_or ( ( ) ) ?;
734+ let local_id = self . ident_and_label_to_local_id . get ( & id) . copied ( ) . ok_or ( ( ) ) ?;
724735 Ok ( HirId { owner, local_id } )
725736 } ) ;
726737 trace ! ( ?res) ;
0 commit comments