@@ -275,7 +275,7 @@ impl<'tcx> TyCtxt<'tcx> {
275
275
span_bug ! (
276
276
self . hir( ) . span( hir_id) ,
277
277
"body_owned_by: {} has no associated body" ,
278
- self . hir ( ) . node_to_string ( hir_id)
278
+ self . hir_id_to_string ( hir_id)
279
279
) ;
280
280
} )
281
281
}
@@ -674,6 +674,106 @@ impl<'tcx> TyCtxt<'tcx> {
674
674
}
675
675
}
676
676
}
677
+
678
+ /// Get a representation of this `id` for debugging purposes.
679
+ /// NOTE: Do NOT use this in diagnostics!
680
+ pub fn hir_id_to_string ( self , id : HirId ) -> String {
681
+ let path_str = |def_id : LocalDefId | self . def_path_str ( def_id) ;
682
+
683
+ let span_str = || {
684
+ self . sess . source_map ( ) . span_to_snippet ( Map { tcx : self } . span ( id) ) . unwrap_or_default ( )
685
+ } ;
686
+ let node_str = |prefix| format ! ( "{id} ({prefix} `{}`)" , span_str( ) ) ;
687
+
688
+ match self . hir_node ( id) {
689
+ Node :: Item ( item) => {
690
+ let item_str = match item. kind {
691
+ ItemKind :: ExternCrate ( ..) => "extern crate" ,
692
+ ItemKind :: Use ( ..) => "use" ,
693
+ ItemKind :: Static ( ..) => "static" ,
694
+ ItemKind :: Const ( ..) => "const" ,
695
+ ItemKind :: Fn { .. } => "fn" ,
696
+ ItemKind :: Macro ( ..) => "macro" ,
697
+ ItemKind :: Mod ( ..) => "mod" ,
698
+ ItemKind :: ForeignMod { .. } => "foreign mod" ,
699
+ ItemKind :: GlobalAsm { .. } => "global asm" ,
700
+ ItemKind :: TyAlias ( ..) => "ty" ,
701
+ ItemKind :: Enum ( ..) => "enum" ,
702
+ ItemKind :: Struct ( ..) => "struct" ,
703
+ ItemKind :: Union ( ..) => "union" ,
704
+ ItemKind :: Trait ( ..) => "trait" ,
705
+ ItemKind :: TraitAlias ( ..) => "trait alias" ,
706
+ ItemKind :: Impl { .. } => "impl" ,
707
+ } ;
708
+ format ! ( "{id} ({item_str} {})" , path_str( item. owner_id. def_id) )
709
+ }
710
+ Node :: ForeignItem ( item) => {
711
+ format ! ( "{id} (foreign item {})" , path_str( item. owner_id. def_id) )
712
+ }
713
+ Node :: ImplItem ( ii) => {
714
+ let kind = match ii. kind {
715
+ ImplItemKind :: Const ( ..) => "associated constant" ,
716
+ ImplItemKind :: Fn ( fn_sig, _) => match fn_sig. decl . implicit_self {
717
+ ImplicitSelfKind :: None => "associated function" ,
718
+ _ => "method" ,
719
+ } ,
720
+ ImplItemKind :: Type ( _) => "associated type" ,
721
+ } ;
722
+ format ! ( "{id} ({kind} `{}` in {})" , ii. ident, path_str( ii. owner_id. def_id) )
723
+ }
724
+ Node :: TraitItem ( ti) => {
725
+ let kind = match ti. kind {
726
+ TraitItemKind :: Const ( ..) => "associated constant" ,
727
+ TraitItemKind :: Fn ( fn_sig, _) => match fn_sig. decl . implicit_self {
728
+ ImplicitSelfKind :: None => "associated function" ,
729
+ _ => "trait method" ,
730
+ } ,
731
+ TraitItemKind :: Type ( ..) => "associated type" ,
732
+ } ;
733
+
734
+ format ! ( "{id} ({kind} `{}` in {})" , ti. ident, path_str( ti. owner_id. def_id) )
735
+ }
736
+ Node :: Variant ( variant) => {
737
+ format ! ( "{id} (variant `{}` in {})" , variant. ident, path_str( variant. def_id) )
738
+ }
739
+ Node :: Field ( field) => {
740
+ format ! ( "{id} (field `{}` in {})" , field. ident, path_str( field. def_id) )
741
+ }
742
+ Node :: AnonConst ( _) => node_str ( "const" ) ,
743
+ Node :: ConstBlock ( _) => node_str ( "const" ) ,
744
+ Node :: ConstArg ( _) => node_str ( "const" ) ,
745
+ Node :: Expr ( _) => node_str ( "expr" ) ,
746
+ Node :: ExprField ( _) => node_str ( "expr field" ) ,
747
+ Node :: Stmt ( _) => node_str ( "stmt" ) ,
748
+ Node :: PathSegment ( _) => node_str ( "path segment" ) ,
749
+ Node :: Ty ( _) => node_str ( "type" ) ,
750
+ Node :: AssocItemConstraint ( _) => node_str ( "assoc item constraint" ) ,
751
+ Node :: TraitRef ( _) => node_str ( "trait ref" ) ,
752
+ Node :: OpaqueTy ( _) => node_str ( "opaque type" ) ,
753
+ Node :: Pat ( _) => node_str ( "pat" ) ,
754
+ Node :: TyPat ( _) => node_str ( "pat ty" ) ,
755
+ Node :: PatField ( _) => node_str ( "pattern field" ) ,
756
+ Node :: PatExpr ( _) => node_str ( "pattern literal" ) ,
757
+ Node :: Param ( _) => node_str ( "param" ) ,
758
+ Node :: Arm ( _) => node_str ( "arm" ) ,
759
+ Node :: Block ( _) => node_str ( "block" ) ,
760
+ Node :: Infer ( _) => node_str ( "infer" ) ,
761
+ Node :: LetStmt ( _) => node_str ( "local" ) ,
762
+ Node :: Ctor ( ctor) => format ! (
763
+ "{id} (ctor {})" ,
764
+ ctor. ctor_def_id( ) . map_or( "<missing path>" . into( ) , |def_id| path_str( def_id) ) ,
765
+ ) ,
766
+ Node :: Lifetime ( _) => node_str ( "lifetime" ) ,
767
+ Node :: GenericParam ( param) => {
768
+ format ! ( "{id} (generic_param {})" , path_str( param. def_id) )
769
+ }
770
+ Node :: Crate ( ..) => String :: from ( "(root_crate)" ) ,
771
+ Node :: WherePredicate ( _) => node_str ( "where predicate" ) ,
772
+ Node :: Synthetic => unreachable ! ( ) ,
773
+ Node :: Err ( _) => node_str ( "error" ) ,
774
+ Node :: PreciseCapturingNonLifetimeArg ( _param) => node_str ( "parameter" ) ,
775
+ }
776
+ }
677
777
}
678
778
679
779
impl < ' hir > Map < ' hir > {
@@ -686,28 +786,34 @@ impl<'hir> Map<'hir> {
686
786
}
687
787
bug ! (
688
788
"expected foreign mod or inlined parent, found {}" ,
689
- self . node_to_string ( HirId :: make_owner( parent. def_id) )
789
+ self . tcx . hir_id_to_string ( HirId :: make_owner( parent. def_id) )
690
790
)
691
791
}
692
792
693
793
pub fn expect_item ( self , id : LocalDefId ) -> & ' hir Item < ' hir > {
694
794
match self . tcx . expect_hir_owner_node ( id) {
695
795
OwnerNode :: Item ( item) => item,
696
- _ => bug ! ( "expected item, found {}" , self . node_to_string ( HirId :: make_owner( id) ) ) ,
796
+ _ => bug ! ( "expected item, found {}" , self . tcx . hir_id_to_string ( HirId :: make_owner( id) ) ) ,
697
797
}
698
798
}
699
799
700
800
pub fn expect_impl_item ( self , id : LocalDefId ) -> & ' hir ImplItem < ' hir > {
701
801
match self . tcx . expect_hir_owner_node ( id) {
702
802
OwnerNode :: ImplItem ( item) => item,
703
- _ => bug ! ( "expected impl item, found {}" , self . node_to_string( HirId :: make_owner( id) ) ) ,
803
+ _ => bug ! (
804
+ "expected impl item, found {}" ,
805
+ self . tcx. hir_id_to_string( HirId :: make_owner( id) )
806
+ ) ,
704
807
}
705
808
}
706
809
707
810
pub fn expect_trait_item ( self , id : LocalDefId ) -> & ' hir TraitItem < ' hir > {
708
811
match self . tcx . expect_hir_owner_node ( id) {
709
812
OwnerNode :: TraitItem ( item) => item,
710
- _ => bug ! ( "expected trait item, found {}" , self . node_to_string( HirId :: make_owner( id) ) ) ,
813
+ _ => bug ! (
814
+ "expected trait item, found {}" ,
815
+ self . tcx. hir_id_to_string( HirId :: make_owner( id) )
816
+ ) ,
711
817
}
712
818
}
713
819
@@ -718,14 +824,14 @@ impl<'hir> Map<'hir> {
718
824
pub fn expect_variant ( self , id : HirId ) -> & ' hir Variant < ' hir > {
719
825
match self . tcx . hir_node ( id) {
720
826
Node :: Variant ( variant) => variant,
721
- _ => bug ! ( "expected variant, found {}" , self . node_to_string ( id) ) ,
827
+ _ => bug ! ( "expected variant, found {}" , self . tcx . hir_id_to_string ( id) ) ,
722
828
}
723
829
}
724
830
725
831
pub fn expect_field ( self , id : HirId ) -> & ' hir FieldDef < ' hir > {
726
832
match self . tcx . hir_node ( id) {
727
833
Node :: Field ( field) => field,
728
- _ => bug ! ( "expected field, found {}" , self . node_to_string ( id) ) ,
834
+ _ => bug ! ( "expected field, found {}" , self . tcx . hir_id_to_string ( id) ) ,
729
835
}
730
836
}
731
837
@@ -735,7 +841,7 @@ impl<'hir> Map<'hir> {
735
841
_ => {
736
842
bug ! (
737
843
"expected foreign item, found {}" ,
738
- self . node_to_string ( HirId :: make_owner( id. def_id) )
844
+ self . tcx . hir_id_to_string ( HirId :: make_owner( id. def_id) )
739
845
)
740
846
}
741
847
}
@@ -748,7 +854,7 @@ impl<'hir> Map<'hir> {
748
854
_ => {
749
855
bug ! (
750
856
"expected opaque type definition, found {}" ,
751
- self . node_to_string ( self . tcx. local_def_id_to_hir_id( id) )
857
+ self . tcx . hir_id_to_string ( self . tcx. local_def_id_to_hir_id( id) )
752
858
)
753
859
}
754
860
}
@@ -757,7 +863,7 @@ impl<'hir> Map<'hir> {
757
863
pub fn expect_expr ( self , id : HirId ) -> & ' hir Expr < ' hir > {
758
864
match self . tcx . hir_node ( id) {
759
865
Node :: Expr ( expr) => expr,
760
- _ => bug ! ( "expected expr, found {}" , self . node_to_string ( id) ) ,
866
+ _ => bug ! ( "expected expr, found {}" , self . tcx . hir_id_to_string ( id) ) ,
761
867
}
762
868
}
763
869
@@ -796,7 +902,7 @@ impl<'hir> Map<'hir> {
796
902
}
797
903
798
904
pub fn name ( self , id : HirId ) -> Symbol {
799
- self . opt_name ( id) . unwrap_or_else ( || bug ! ( "no name for {}" , self . node_to_string ( id) ) )
905
+ self . opt_name ( id) . unwrap_or_else ( || bug ! ( "no name for {}" , self . tcx . hir_id_to_string ( id) ) )
800
906
}
801
907
802
908
/// Given a node ID, gets a list of attributes associated with the AST
@@ -977,12 +1083,6 @@ impl<'hir> Map<'hir> {
977
1083
}
978
1084
}
979
1085
980
- /// Get a representation of this `id` for debugging purposes.
981
- /// NOTE: Do NOT use this in diagnostics!
982
- pub fn node_to_string ( self , id : HirId ) -> String {
983
- hir_id_to_string ( self , id)
984
- }
985
-
986
1086
/// Returns the HirId of `N` in `struct Foo<const N: usize = { ... }>` when
987
1087
/// called with the HirId for the `{ ... }` anon const
988
1088
pub fn opt_const_param_default_param_def_id ( self , anon_const : HirId ) -> Option < LocalDefId > {
@@ -1147,102 +1247,6 @@ fn upstream_crates(tcx: TyCtxt<'_>) -> Vec<(StableCrateId, Svh)> {
1147
1247
upstream_crates
1148
1248
}
1149
1249
1150
- fn hir_id_to_string ( map : Map < ' _ > , id : HirId ) -> String {
1151
- let path_str = |def_id : LocalDefId | map. tcx . def_path_str ( def_id) ;
1152
-
1153
- let span_str = || map. tcx . sess . source_map ( ) . span_to_snippet ( map. span ( id) ) . unwrap_or_default ( ) ;
1154
- let node_str = |prefix| format ! ( "{id} ({prefix} `{}`)" , span_str( ) ) ;
1155
-
1156
- match map. tcx . hir_node ( id) {
1157
- Node :: Item ( item) => {
1158
- let item_str = match item. kind {
1159
- ItemKind :: ExternCrate ( ..) => "extern crate" ,
1160
- ItemKind :: Use ( ..) => "use" ,
1161
- ItemKind :: Static ( ..) => "static" ,
1162
- ItemKind :: Const ( ..) => "const" ,
1163
- ItemKind :: Fn { .. } => "fn" ,
1164
- ItemKind :: Macro ( ..) => "macro" ,
1165
- ItemKind :: Mod ( ..) => "mod" ,
1166
- ItemKind :: ForeignMod { .. } => "foreign mod" ,
1167
- ItemKind :: GlobalAsm { .. } => "global asm" ,
1168
- ItemKind :: TyAlias ( ..) => "ty" ,
1169
- ItemKind :: Enum ( ..) => "enum" ,
1170
- ItemKind :: Struct ( ..) => "struct" ,
1171
- ItemKind :: Union ( ..) => "union" ,
1172
- ItemKind :: Trait ( ..) => "trait" ,
1173
- ItemKind :: TraitAlias ( ..) => "trait alias" ,
1174
- ItemKind :: Impl { .. } => "impl" ,
1175
- } ;
1176
- format ! ( "{id} ({item_str} {})" , path_str( item. owner_id. def_id) )
1177
- }
1178
- Node :: ForeignItem ( item) => {
1179
- format ! ( "{id} (foreign item {})" , path_str( item. owner_id. def_id) )
1180
- }
1181
- Node :: ImplItem ( ii) => {
1182
- let kind = match ii. kind {
1183
- ImplItemKind :: Const ( ..) => "associated constant" ,
1184
- ImplItemKind :: Fn ( fn_sig, _) => match fn_sig. decl . implicit_self {
1185
- ImplicitSelfKind :: None => "associated function" ,
1186
- _ => "method" ,
1187
- } ,
1188
- ImplItemKind :: Type ( _) => "associated type" ,
1189
- } ;
1190
- format ! ( "{id} ({kind} `{}` in {})" , ii. ident, path_str( ii. owner_id. def_id) )
1191
- }
1192
- Node :: TraitItem ( ti) => {
1193
- let kind = match ti. kind {
1194
- TraitItemKind :: Const ( ..) => "associated constant" ,
1195
- TraitItemKind :: Fn ( fn_sig, _) => match fn_sig. decl . implicit_self {
1196
- ImplicitSelfKind :: None => "associated function" ,
1197
- _ => "trait method" ,
1198
- } ,
1199
- TraitItemKind :: Type ( ..) => "associated type" ,
1200
- } ;
1201
-
1202
- format ! ( "{id} ({kind} `{}` in {})" , ti. ident, path_str( ti. owner_id. def_id) )
1203
- }
1204
- Node :: Variant ( variant) => {
1205
- format ! ( "{id} (variant `{}` in {})" , variant. ident, path_str( variant. def_id) )
1206
- }
1207
- Node :: Field ( field) => {
1208
- format ! ( "{id} (field `{}` in {})" , field. ident, path_str( field. def_id) )
1209
- }
1210
- Node :: AnonConst ( _) => node_str ( "const" ) ,
1211
- Node :: ConstBlock ( _) => node_str ( "const" ) ,
1212
- Node :: ConstArg ( _) => node_str ( "const" ) ,
1213
- Node :: Expr ( _) => node_str ( "expr" ) ,
1214
- Node :: ExprField ( _) => node_str ( "expr field" ) ,
1215
- Node :: Stmt ( _) => node_str ( "stmt" ) ,
1216
- Node :: PathSegment ( _) => node_str ( "path segment" ) ,
1217
- Node :: Ty ( _) => node_str ( "type" ) ,
1218
- Node :: AssocItemConstraint ( _) => node_str ( "assoc item constraint" ) ,
1219
- Node :: TraitRef ( _) => node_str ( "trait ref" ) ,
1220
- Node :: OpaqueTy ( _) => node_str ( "opaque type" ) ,
1221
- Node :: Pat ( _) => node_str ( "pat" ) ,
1222
- Node :: TyPat ( _) => node_str ( "pat ty" ) ,
1223
- Node :: PatField ( _) => node_str ( "pattern field" ) ,
1224
- Node :: PatExpr ( _) => node_str ( "pattern literal" ) ,
1225
- Node :: Param ( _) => node_str ( "param" ) ,
1226
- Node :: Arm ( _) => node_str ( "arm" ) ,
1227
- Node :: Block ( _) => node_str ( "block" ) ,
1228
- Node :: Infer ( _) => node_str ( "infer" ) ,
1229
- Node :: LetStmt ( _) => node_str ( "local" ) ,
1230
- Node :: Ctor ( ctor) => format ! (
1231
- "{id} (ctor {})" ,
1232
- ctor. ctor_def_id( ) . map_or( "<missing path>" . into( ) , |def_id| path_str( def_id) ) ,
1233
- ) ,
1234
- Node :: Lifetime ( _) => node_str ( "lifetime" ) ,
1235
- Node :: GenericParam ( param) => {
1236
- format ! ( "{id} (generic_param {})" , path_str( param. def_id) )
1237
- }
1238
- Node :: Crate ( ..) => String :: from ( "(root_crate)" ) ,
1239
- Node :: WherePredicate ( _) => node_str ( "where predicate" ) ,
1240
- Node :: Synthetic => unreachable ! ( ) ,
1241
- Node :: Err ( _) => node_str ( "error" ) ,
1242
- Node :: PreciseCapturingNonLifetimeArg ( _param) => node_str ( "parameter" ) ,
1243
- }
1244
- }
1245
-
1246
1250
pub ( super ) fn hir_module_items ( tcx : TyCtxt < ' _ > , module_id : LocalModDefId ) -> ModuleItems {
1247
1251
let mut collector = ItemCollector :: new ( tcx, false ) ;
1248
1252
0 commit comments