@@ -2795,10 +2795,50 @@ impl<'tcx> TyCtxt<'tcx> {
2795
2795
. filter ( |item| item. kind == AssocKind :: Fn && item. defaultness . has_value ( ) )
2796
2796
}
2797
2797
2798
+ fn item_name_from_hir ( self , def_id : DefId ) -> Option < Ident > {
2799
+ self . hir ( ) . get_if_local ( def_id) . and_then ( |node| node. ident ( ) )
2800
+ }
2801
+
2802
+ fn item_name_from_def_id ( self , def_id : DefId ) -> Option < Symbol > {
2803
+ if def_id. index == CRATE_DEF_INDEX {
2804
+ Some ( self . original_crate_name ( def_id. krate ) )
2805
+ } else {
2806
+ let def_key = self . def_key ( def_id) ;
2807
+ match def_key. disambiguated_data . data {
2808
+ // The name of a constructor is that of its parent.
2809
+ rustc_hir:: definitions:: DefPathData :: Ctor => self . item_name_from_def_id ( DefId {
2810
+ krate : def_id. krate ,
2811
+ index : def_key. parent . unwrap ( ) ,
2812
+ } ) ,
2813
+ _ => def_key. disambiguated_data . data . get_opt_name ( ) ,
2814
+ }
2815
+ }
2816
+ }
2817
+
2818
+ /// Look up the name of an item across crates. This does not look at HIR.
2819
+ ///
2820
+ /// When possible, this function should be used for cross-crate lookups over
2821
+ /// [`opt_item_name`] to avoid invalidating the incremental cache. If you
2822
+ /// need to handle items without a name, or HIR items that will not be
2823
+ /// serialized cross-crate, or if you need the span of the item, use
2824
+ /// [`opt_item_name`] instead.
2825
+ ///
2826
+ /// [`opt_item_name`]: Self::opt_item_name
2827
+ pub fn item_name ( self , id : DefId ) -> Symbol {
2828
+ // Look at cross-crate items first to avoid invalidating the incremental cache
2829
+ // unless we have to.
2830
+ self . item_name_from_def_id ( id) . unwrap_or_else ( || {
2831
+ bug ! ( "item_name: no name for {:?}" , self . def_path( id) ) ;
2832
+ } )
2833
+ }
2834
+
2835
+ /// Look up the name and span of an item or [`Node`].
2836
+ ///
2837
+ /// See [`item_name`][Self::item_name] for more information.
2798
2838
pub fn opt_item_name ( self , def_id : DefId ) -> Option < Ident > {
2799
- def_id
2800
- . as_local ( )
2801
- . and_then ( |def_id | self . hir ( ) . get ( self . hir ( ) . local_def_id_to_hir_id ( def_id) ) . ident ( ) )
2839
+ // Look at the HIR first so the span will be correct if this is a local item.
2840
+ self . item_name_from_hir ( def_id )
2841
+ . or_else ( | | self . item_name_from_def_id ( def_id) . map ( Ident :: with_dummy_span ) )
2802
2842
}
2803
2843
2804
2844
pub fn opt_associated_item ( self , def_id : DefId ) -> Option < & ' tcx AssocItem > {
@@ -2921,23 +2961,6 @@ impl<'tcx> TyCtxt<'tcx> {
2921
2961
}
2922
2962
}
2923
2963
2924
- pub fn item_name ( self , id : DefId ) -> Symbol {
2925
- if id. index == CRATE_DEF_INDEX {
2926
- self . original_crate_name ( id. krate )
2927
- } else {
2928
- let def_key = self . def_key ( id) ;
2929
- match def_key. disambiguated_data . data {
2930
- // The name of a constructor is that of its parent.
2931
- rustc_hir:: definitions:: DefPathData :: Ctor => {
2932
- self . item_name ( DefId { krate : id. krate , index : def_key. parent . unwrap ( ) } )
2933
- }
2934
- _ => def_key. disambiguated_data . data . get_opt_name ( ) . unwrap_or_else ( || {
2935
- bug ! ( "item_name: no name for {:?}" , self . def_path( id) ) ;
2936
- } ) ,
2937
- }
2938
- }
2939
- }
2940
-
2941
2964
/// Returns the possibly-auto-generated MIR of a `(DefId, Subst)` pair.
2942
2965
pub fn instance_mir ( self , instance : ty:: InstanceDef < ' tcx > ) -> & ' tcx Body < ' tcx > {
2943
2966
match instance {
0 commit comments