@@ -2886,7 +2886,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
28862886
28872887 pub fn find_field_index ( self , ident : Ident , variant : & VariantDef ) -> Option < usize > {
28882888 variant. fields . iter ( ) . position ( |field| {
2889- self . adjust_ident ( ident, variant . def_id , hir :: DUMMY_HIR_ID ) . 0 == field . ident . modern ( )
2889+ self . hygienic_eq ( ident, field . ident , variant . def_id )
28902890 } )
28912891 }
28922892
@@ -3085,19 +3085,32 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
30853085 /// its supposed definition name (`def_name`). The method also needs `DefId` of the supposed
30863086 /// definition's parent/scope to perform comparison.
30873087 pub fn hygienic_eq ( self , use_name : Ident , def_name : Ident , def_parent_def_id : DefId ) -> bool {
3088- self . adjust_ident ( use_name, def_parent_def_id, hir:: DUMMY_HIR_ID ) . 0 == def_name. modern ( )
3088+ // We could use `Ident::eq` here, but we deliberately don't. The name
3089+ // comparison fails frequently, and we want to avoid the expensive
3090+ // `modern()` calls required for the span comparison whenever possible.
3091+ use_name. name == def_name. name &&
3092+ self . adjust_ident ( use_name, def_parent_def_id) . span . ctxt ( ) == def_name. modern ( ) . span . ctxt ( )
30893093 }
30903094
3091- pub fn adjust_ident ( self , mut ident : Ident , scope : DefId , block : hir:: HirId ) -> ( Ident , DefId ) {
3092- ident = ident. modern ( ) ;
3093- let target_expansion = match scope. krate {
3095+ fn expansion_that_defined ( self , scope : DefId ) -> Mark {
3096+ match scope. krate {
30943097 LOCAL_CRATE => self . hir ( ) . definitions ( ) . expansion_that_defined ( scope. index ) ,
30953098 _ => Mark :: root ( ) ,
3096- } ;
3097- let scope = match ident. span . adjust ( target_expansion) {
3099+ }
3100+ }
3101+
3102+ pub fn adjust_ident ( self , mut ident : Ident , scope : DefId ) -> Ident {
3103+ ident = ident. modern ( ) ;
3104+ ident. span . adjust ( self . expansion_that_defined ( scope) ) ;
3105+ ident
3106+ }
3107+
3108+ pub fn adjust_ident_and_get_scope ( self , mut ident : Ident , scope : DefId , block : hir:: HirId )
3109+ -> ( Ident , DefId ) {
3110+ ident = ident. modern ( ) ;
3111+ let scope = match ident. span . adjust ( self . expansion_that_defined ( scope) ) {
30983112 Some ( actual_expansion) =>
30993113 self . hir ( ) . definitions ( ) . parent_module_of_macro_def ( actual_expansion) ,
3100- None if block == hir:: DUMMY_HIR_ID => DefId :: local ( CRATE_DEF_INDEX ) , // Dummy DefId
31013114 None => self . hir ( ) . get_module_parent_by_hir_id ( block) ,
31023115 } ;
31033116 ( ident, scope)
0 commit comments