@@ -32,6 +32,7 @@ use hir::print as pprust;
3232
3333use arena:: TypedArena ;
3434use std:: cell:: RefCell ;
35+ use std:: cmp;
3536use std:: io;
3637use std:: mem;
3738
@@ -127,7 +128,10 @@ impl<'ast> MapEntry<'ast> {
127128 EntryStructCtor ( id, _) => id,
128129 EntryLifetime ( id, _) => id,
129130 EntryTyParam ( id, _) => id,
130- _ => return None
131+
132+ NotPresent |
133+ RootCrate |
134+ RootInlinedParent ( _) => return None ,
131135 } )
132136 }
133137
@@ -196,6 +200,10 @@ pub struct Map<'ast> {
196200 map : RefCell < Vec < MapEntry < ' ast > > > ,
197201
198202 definitions : RefCell < Definitions > ,
203+
204+ /// All NodeIds that are numerically greater or equal to this value come
205+ /// from inlined items.
206+ local_node_id_watermark : NodeId ,
199207}
200208
201209impl < ' ast > Map < ' ast > {
@@ -550,6 +558,13 @@ impl<'ast> Map<'ast> {
550558 }
551559 }
552560
561+ pub fn expect_inlined_item ( & self , id : NodeId ) -> & ' ast InlinedItem {
562+ match self . find_entry ( id) {
563+ Some ( RootInlinedParent ( inlined_item) ) => inlined_item,
564+ _ => bug ! ( "expected inlined item, found {}" , self . node_to_string( id) ) ,
565+ }
566+ }
567+
553568 /// Returns the name associated with the given NodeId's AST.
554569 pub fn name ( & self , id : NodeId ) -> Name {
555570 match self . get ( id) {
@@ -649,6 +664,10 @@ impl<'ast> Map<'ast> {
649664 pub fn node_to_user_string ( & self , id : NodeId ) -> String {
650665 node_id_to_string ( self , id, false )
651666 }
667+
668+ pub fn is_inlined ( & self , id : NodeId ) -> bool {
669+ id >= self . local_node_id_watermark
670+ }
652671}
653672
654673pub struct NodesMatchingSuffix < ' a , ' ast : ' a > {
@@ -765,13 +784,37 @@ pub trait FoldOps {
765784}
766785
767786/// A Folder that updates IDs and Span's according to fold_ops.
768- struct IdAndSpanUpdater < F > {
769- fold_ops : F
787+ pub struct IdAndSpanUpdater < F > {
788+ fold_ops : F ,
789+ min_id_assigned : NodeId ,
790+ max_id_assigned : NodeId ,
791+ }
792+
793+ impl < F : FoldOps > IdAndSpanUpdater < F > {
794+ pub fn new ( fold_ops : F ) -> IdAndSpanUpdater < F > {
795+ IdAndSpanUpdater {
796+ fold_ops : fold_ops,
797+ min_id_assigned : :: std:: u32:: MAX ,
798+ max_id_assigned : :: std:: u32:: MIN ,
799+ }
800+ }
801+
802+ pub fn id_range ( & self ) -> intravisit:: IdRange {
803+ intravisit:: IdRange {
804+ min : self . min_id_assigned ,
805+ max : self . max_id_assigned + 1 ,
806+ }
807+ }
770808}
771809
772810impl < F : FoldOps > Folder for IdAndSpanUpdater < F > {
773811 fn new_id ( & mut self , id : NodeId ) -> NodeId {
774- self . fold_ops . new_id ( id)
812+ let id = self . fold_ops . new_id ( id) ;
813+
814+ self . min_id_assigned = cmp:: min ( self . min_id_assigned , id) ;
815+ self . max_id_assigned = cmp:: max ( self . max_id_assigned , id) ;
816+
817+ id
775818 }
776819
777820 fn new_span ( & mut self , span : Span ) -> Span {
@@ -802,11 +845,14 @@ pub fn map_crate<'ast>(forest: &'ast mut Forest,
802845 entries, vector_length, ( entries as f64 / vector_length as f64 ) * 100. ) ;
803846 }
804847
848+ let local_node_id_watermark = map. len ( ) as NodeId ;
849+
805850 Map {
806851 forest : forest,
807852 dep_graph : forest. dep_graph . clone ( ) ,
808853 map : RefCell :: new ( map) ,
809854 definitions : RefCell :: new ( definitions) ,
855+ local_node_id_watermark : local_node_id_watermark
810856 }
811857}
812858
@@ -818,7 +864,7 @@ pub fn map_decoded_item<'ast, F: FoldOps>(map: &Map<'ast>,
818864 ii : InlinedItem ,
819865 fold_ops : F )
820866 -> & ' ast InlinedItem {
821- let mut fld = IdAndSpanUpdater { fold_ops : fold_ops } ;
867+ let mut fld = IdAndSpanUpdater :: new ( fold_ops) ;
822868 let ii = match ii {
823869 II :: Item ( i) => II :: Item ( i. map ( |i| fld. fold_item ( i) ) ) ,
824870 II :: TraitItem ( d, ti) => {
@@ -835,6 +881,12 @@ pub fn map_decoded_item<'ast, F: FoldOps>(map: &Map<'ast>,
835881 let ii = map. forest . inlined_items . alloc ( ii) ;
836882 let ii_parent_id = fld. new_id ( DUMMY_NODE_ID ) ;
837883
884+ // Assert that the ii_parent_id is the last NodeId in our reserved range
885+ assert ! ( ii_parent_id == fld. max_id_assigned) ;
886+ // Assert that we did not violate the invariant that all inlined HIR items
887+ // have NodeIds greater than or equal to `local_node_id_watermark`
888+ assert ! ( fld. min_id_assigned >= map. local_node_id_watermark) ;
889+
838890 let defs = & mut * map. definitions . borrow_mut ( ) ;
839891 let mut def_collector = DefCollector :: extend ( ii_parent_id,
840892 parent_def_path. clone ( ) ,
0 commit comments