@@ -10,7 +10,6 @@ use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
1010use rustc_hir:: definitions:: { DefKey , DefPath , Definitions } ;
1111use rustc_hir:: intravisit;
1212use rustc_hir:: itemlikevisit:: ItemLikeVisitor ;
13- use rustc_hir:: print:: Nested ;
1413use rustc_hir:: * ;
1514use rustc_index:: vec:: IndexVec ;
1615use rustc_span:: hygiene:: MacroKind ;
@@ -890,20 +889,18 @@ impl<'hir> Map<'hir> {
890889 }
891890 }
892891
892+ /// Get a representation of this `id` for debugging purposes.
893+ /// NOTE: Do NOT use this in diagnostics!
893894 pub fn node_to_string ( & self , id : HirId ) -> String {
894- hir_id_to_string ( self , id, true )
895- }
896-
897- pub fn hir_to_user_string ( & self , id : HirId ) -> String {
898- hir_id_to_string ( self , id, false )
899- }
900-
901- pub fn hir_to_pretty_string ( & self , id : HirId ) -> String {
902- print:: to_string ( self , |s| s. print_node ( self . get ( id) ) )
895+ hir_id_to_string ( self , id)
903896 }
904897}
905898
906899impl < ' hir > intravisit:: Map < ' hir > for Map < ' hir > {
900+ fn find ( & self , hir_id : HirId ) -> Option < Node < ' hir > > {
901+ self . find ( hir_id)
902+ }
903+
907904 fn body ( & self , id : BodyId ) -> & ' hir Body < ' hir > {
908905 self . body ( id)
909906 }
@@ -982,23 +979,8 @@ pub(super) fn index_hir<'tcx>(tcx: TyCtxt<'tcx>, cnum: CrateNum) -> &'tcx Indexe
982979 tcx. arena . alloc ( IndexedHir { crate_hash, map } )
983980}
984981
985- /// Identical to the `PpAnn` implementation for `hir::Crate`,
986- /// except it avoids creating a dependency on the whole crate.
987- impl < ' hir > print:: PpAnn for Map < ' hir > {
988- fn nested ( & self , state : & mut print:: State < ' _ > , nested : print:: Nested ) {
989- match nested {
990- Nested :: Item ( id) => state. print_item ( self . expect_item ( id. id ) ) ,
991- Nested :: TraitItem ( id) => state. print_trait_item ( self . trait_item ( id) ) ,
992- Nested :: ImplItem ( id) => state. print_impl_item ( self . impl_item ( id) ) ,
993- Nested :: Body ( id) => state. print_expr ( & self . body ( id) . value ) ,
994- Nested :: BodyParamPat ( id, i) => state. print_pat ( & self . body ( id) . params [ i] . pat ) ,
995- }
996- }
997- }
998-
999- fn hir_id_to_string ( map : & Map < ' _ > , id : HirId , include_id : bool ) -> String {
982+ fn hir_id_to_string ( map : & Map < ' _ > , id : HirId ) -> String {
1000983 let id_str = format ! ( " (hir_id={})" , id) ;
1001- let id_str = if include_id { & id_str[ ..] } else { "" } ;
1002984
1003985 let path_str = || {
1004986 // This functionality is used for debugging, try to use `TyCtxt` to get
@@ -1019,6 +1001,9 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId, include_id: bool) -> String {
10191001 } )
10201002 } ;
10211003
1004+ let span_str = || map. tcx . sess . source_map ( ) . span_to_snippet ( map. span ( id) ) . unwrap_or_default ( ) ;
1005+ let node_str = |prefix| format ! ( "{} {}{}" , prefix, span_str( ) , id_str) ;
1006+
10221007 match map. find ( id) {
10231008 Some ( Node :: Item ( item) ) => {
10241009 let item_str = match item. kind {
@@ -1069,22 +1054,20 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId, include_id: bool) -> String {
10691054 Some ( Node :: Field ( ref field) ) => {
10701055 format ! ( "field {} in {}{}" , field. ident, path_str( ) , id_str)
10711056 }
1072- Some ( Node :: AnonConst ( _) ) => format ! ( "const {}{}" , map. hir_to_pretty_string( id) , id_str) ,
1073- Some ( Node :: Expr ( _) ) => format ! ( "expr {}{}" , map. hir_to_pretty_string( id) , id_str) ,
1074- Some ( Node :: Stmt ( _) ) => format ! ( "stmt {}{}" , map. hir_to_pretty_string( id) , id_str) ,
1075- Some ( Node :: PathSegment ( _) ) => {
1076- format ! ( "path segment {}{}" , map. hir_to_pretty_string( id) , id_str)
1077- }
1078- Some ( Node :: Ty ( _) ) => format ! ( "type {}{}" , map. hir_to_pretty_string( id) , id_str) ,
1079- Some ( Node :: TraitRef ( _) ) => format ! ( "trait_ref {}{}" , map. hir_to_pretty_string( id) , id_str) ,
1080- Some ( Node :: Binding ( _) ) => format ! ( "local {}{}" , map. hir_to_pretty_string( id) , id_str) ,
1081- Some ( Node :: Pat ( _) ) => format ! ( "pat {}{}" , map. hir_to_pretty_string( id) , id_str) ,
1082- Some ( Node :: Param ( _) ) => format ! ( "param {}{}" , map. hir_to_pretty_string( id) , id_str) ,
1083- Some ( Node :: Arm ( _) ) => format ! ( "arm {}{}" , map. hir_to_pretty_string( id) , id_str) ,
1084- Some ( Node :: Block ( _) ) => format ! ( "block {}{}" , map. hir_to_pretty_string( id) , id_str) ,
1085- Some ( Node :: Local ( _) ) => format ! ( "local {}{}" , map. hir_to_pretty_string( id) , id_str) ,
1057+ Some ( Node :: AnonConst ( _) ) => node_str ( "const" ) ,
1058+ Some ( Node :: Expr ( _) ) => node_str ( "expr" ) ,
1059+ Some ( Node :: Stmt ( _) ) => node_str ( "stmt" ) ,
1060+ Some ( Node :: PathSegment ( _) ) => node_str ( "path segment" ) ,
1061+ Some ( Node :: Ty ( _) ) => node_str ( "type" ) ,
1062+ Some ( Node :: TraitRef ( _) ) => node_str ( "trait ref" ) ,
1063+ Some ( Node :: Binding ( _) ) => node_str ( "local" ) ,
1064+ Some ( Node :: Pat ( _) ) => node_str ( "pat" ) ,
1065+ Some ( Node :: Param ( _) ) => node_str ( "param" ) ,
1066+ Some ( Node :: Arm ( _) ) => node_str ( "arm" ) ,
1067+ Some ( Node :: Block ( _) ) => node_str ( "block" ) ,
1068+ Some ( Node :: Local ( _) ) => node_str ( "local" ) ,
10861069 Some ( Node :: Ctor ( ..) ) => format ! ( "ctor {}{}" , path_str( ) , id_str) ,
1087- Some ( Node :: Lifetime ( _) ) => format ! ( "lifetime {}{}" , map . hir_to_pretty_string ( id ) , id_str ) ,
1070+ Some ( Node :: Lifetime ( _) ) => node_str ( "lifetime" ) ,
10881071 Some ( Node :: GenericParam ( ref param) ) => format ! ( "generic_param {:?}{}" , param, id_str) ,
10891072 Some ( Node :: Visibility ( ref vis) ) => format ! ( "visibility {:?}{}" , vis, id_str) ,
10901073 Some ( Node :: MacroDef ( _) ) => format ! ( "macro {}{}" , path_str( ) , id_str) ,
0 commit comments