@@ -937,9 +937,19 @@ impl<'hir> Map<'hir> {
937937
938938 let span = match self . find ( hir_id) ? {
939939 // 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+ }
943953 // Constants and Statics.
944954 Node :: Item ( Item {
945955 kind :
@@ -981,7 +991,11 @@ impl<'hir> Map<'hir> {
981991 }
982992 // Other cases.
983993 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+ }
985999 _ => named_span ( item. span , item. ident , item. kind . generics ( ) ) ,
9861000 } ,
9871001 Node :: Variant ( variant) => named_span ( variant. span , variant. ident , None ) ,
@@ -991,11 +1005,17 @@ impl<'hir> Map<'hir> {
9911005 _ => named_span ( item. span , item. ident , None ) ,
9921006 } ,
9931007 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)
9961015 }
9971016 _ => self . span_with_body ( hir_id) ,
9981017 } ;
1018+ debug_assert_eq ! ( span. ctxt( ) , self . span_with_body( hir_id) . ctxt( ) ) ;
9991019 Some ( span)
10001020 }
10011021
0 commit comments