Skip to content

Commit f3ab5a6

Browse files
authored
Rollup merge of rust-lang#102538 - cjgillot:def-span-ctxt, r=fee1-dead
Give `def_span` the same SyntaxContext as `span_with_body`. rust-lang#102217 I'm not sure how to add a test, since the erroneous span was crafted using a proc macro. The debug assertion in `def_span` will ensure we have the correct behaviour.
2 parents ed97408 + c321933 commit f3ab5a6

File tree

1 file changed

+26
-6
lines changed
  • compiler/rustc_middle/src/hir/map

1 file changed

+26
-6
lines changed

compiler/rustc_middle/src/hir/map/mod.rs

+26-6
Original file line numberDiff line numberDiff line change
@@ -941,9 +941,19 @@ impl<'hir> Map<'hir> {
941941

942942
let span = match self.find(hir_id)? {
943943
// 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+
}
947957
// Constants and Statics.
948958
Node::Item(Item {
949959
kind:
@@ -985,7 +995,11 @@ impl<'hir> Map<'hir> {
985995
}
986996
// Other cases.
987997
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+
}
9891003
_ => named_span(item.span, item.ident, item.kind.generics()),
9901004
},
9911005
Node::Variant(variant) => named_span(variant.span, variant.ident, None),
@@ -995,11 +1009,17 @@ impl<'hir> Map<'hir> {
9951009
_ => named_span(item.span, item.ident, None),
9961010
},
9971011
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)
10001019
}
10011020
_ => self.span_with_body(hir_id),
10021021
};
1022+
debug_assert_eq!(span.ctxt(), self.span_with_body(hir_id).ctxt());
10031023
Some(span)
10041024
}
10051025

0 commit comments

Comments
 (0)