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