@@ -941,9 +941,19 @@ impl<'hir> Map<'hir> {
941
941
942
942
let span = match self . find ( hir_id) ? {
943
943
// Function-like.
944
- Node :: Item ( Item { kind : ItemKind :: Fn ( sig, ..) , .. } )
945
- | Node :: TraitItem ( TraitItem { kind : TraitItemKind :: Fn ( sig, ..) , .. } )
946
- | Node :: ImplItem ( ImplItem { kind : ImplItemKind :: Fn ( sig, ..) , .. } ) => sig. span ,
944
+ Node :: Item ( Item { kind : ItemKind :: Fn ( sig, ..) , span : outer_span, .. } )
945
+ | Node :: TraitItem ( TraitItem {
946
+ kind : TraitItemKind :: Fn ( sig, ..) ,
947
+ span : outer_span,
948
+ ..
949
+ } )
950
+ | Node :: ImplItem ( ImplItem {
951
+ kind : ImplItemKind :: Fn ( sig, ..) , span : outer_span, ..
952
+ } ) => {
953
+ // Ensure that the returned span has the item's SyntaxContext, and not the
954
+ // SyntaxContext of the visibility.
955
+ sig. span . find_ancestor_in_same_ctxt ( * outer_span) . unwrap_or ( * outer_span)
956
+ }
947
957
// Constants and Statics.
948
958
Node :: Item ( Item {
949
959
kind :
@@ -985,7 +995,11 @@ impl<'hir> Map<'hir> {
985
995
}
986
996
// Other cases.
987
997
Node :: Item ( item) => match & item. kind {
988
- ItemKind :: Use ( path, _) => path. span ,
998
+ ItemKind :: Use ( path, _) => {
999
+ // Ensure that the returned span has the item's SyntaxContext, and not the
1000
+ // SyntaxContext of the path.
1001
+ path. span . find_ancestor_in_same_ctxt ( item. span ) . unwrap_or ( item. span )
1002
+ }
989
1003
_ => named_span ( item. span , item. ident , item. kind . generics ( ) ) ,
990
1004
} ,
991
1005
Node :: Variant ( variant) => named_span ( variant. span , variant. ident , None ) ,
@@ -995,11 +1009,17 @@ impl<'hir> Map<'hir> {
995
1009
_ => named_span ( item. span , item. ident , None ) ,
996
1010
} ,
997
1011
Node :: Ctor ( _) => return self . opt_span ( self . get_parent_node ( hir_id) ) ,
998
- Node :: Expr ( Expr { kind : ExprKind :: Closure ( Closure { fn_decl_span, .. } ) , .. } ) => {
999
- * fn_decl_span
1012
+ Node :: Expr ( Expr {
1013
+ kind : ExprKind :: Closure ( Closure { fn_decl_span, .. } ) ,
1014
+ span,
1015
+ ..
1016
+ } ) => {
1017
+ // Ensure that the returned span has the item's SyntaxContext.
1018
+ fn_decl_span. find_ancestor_in_same_ctxt ( * span) . unwrap_or ( * span)
1000
1019
}
1001
1020
_ => self . span_with_body ( hir_id) ,
1002
1021
} ;
1022
+ debug_assert_eq ! ( span. ctxt( ) , self . span_with_body( hir_id) . ctxt( ) ) ;
1003
1023
Some ( span)
1004
1024
}
1005
1025
0 commit comments