Skip to content

Commit e69a8df

Browse files
committed
Introduce a query for HIR spans.
1 parent 0b793a3 commit e69a8df

File tree

3 files changed

+29
-50
lines changed

3 files changed

+29
-50
lines changed

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

Lines changed: 19 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -853,61 +853,30 @@ impl<'hir> Map<'hir> {
853853
/// This is used by `tcx.get_span`
854854
pub fn span(&self, hir_id: HirId) -> Span {
855855
match self.find_entry(hir_id).map(|entry| entry.node) {
856-
Some(Node::Param(param)) => param.span,
857-
Some(Node::Item(item)) => match &item.kind {
858-
ItemKind::Fn(sig, _, _) => sig.span,
859-
_ => item.span,
860-
},
861-
Some(Node::ForeignItem(foreign_item)) => foreign_item.span,
862-
Some(Node::TraitItem(trait_item)) => match &trait_item.kind {
863-
TraitItemKind::Fn(sig, _) => sig.span,
864-
_ => trait_item.span,
865-
},
866-
Some(Node::ImplItem(impl_item)) => match &impl_item.kind {
867-
ImplItemKind::Fn(sig, _) => sig.span,
868-
_ => impl_item.span,
869-
},
870-
Some(Node::Variant(variant)) => variant.span,
871-
Some(Node::Field(field)) => field.span,
872-
Some(Node::AnonConst(constant)) => self.body(constant.body).value.span,
873-
Some(Node::Expr(expr)) => expr.span,
874-
Some(Node::Stmt(stmt)) => stmt.span,
875-
Some(Node::PathSegment(seg)) => seg.ident.span,
876-
Some(Node::Ty(ty)) => ty.span,
877-
Some(Node::TraitRef(tr)) => tr.path.span,
878-
Some(Node::Binding(pat)) => pat.span,
879-
Some(Node::Pat(pat)) => pat.span,
880-
Some(Node::Arm(arm)) => arm.span,
881-
Some(Node::Block(block)) => block.span,
882-
Some(Node::Ctor(..)) => match self.find(self.get_parent_node(hir_id)) {
883-
Some(Node::Item(item)) => item.span,
884-
Some(Node::Variant(variant)) => variant.span,
885-
_ => unreachable!(),
886-
},
887-
Some(Node::Lifetime(lifetime)) => lifetime.span,
888-
Some(Node::GenericParam(param)) => param.span,
889-
Some(Node::Visibility(&Spanned {
890-
node: VisibilityKind::Restricted { ref path, .. },
891-
..
892-
})) => path.span,
893-
Some(Node::Visibility(v)) => bug!("unexpected Visibility {:?}", v),
894-
Some(Node::Local(local)) => local.span,
895-
Some(Node::MacroDef(macro_def)) => macro_def.span,
896-
Some(Node::Crate(item)) => item.span,
897-
None => bug!("hir::map::Map::span: id not in map: {:?}", hir_id),
898-
}
856+
Some(Node::Item(item)) => {
857+
if let ItemKind::Fn(sig, _, _) = &item.kind {
858+
return sig.span;
859+
}
860+
}
861+
Some(Node::TraitItem(item)) => {
862+
if let TraitItemKind::Fn(sig, _) = &item.kind {
863+
return sig.span;
864+
}
865+
}
866+
Some(Node::ImplItem(item)) => {
867+
if let ImplItemKind::Fn(sig, _) = &item.kind {
868+
return sig.span;
869+
}
870+
}
871+
_ => {}
872+
};
873+
self.tcx.hir_owner_spans(hir_id.owner)[hir_id.local_id]
899874
}
900875

901876
/// Like `hir.span()`, but includes the body of function items
902877
/// (instead of just the function header)
903878
pub fn span_with_body(&self, hir_id: HirId) -> Span {
904-
match self.find_entry(hir_id).map(|entry| entry.node) {
905-
Some(Node::TraitItem(item)) => item.span,
906-
Some(Node::ImplItem(impl_item)) => impl_item.span,
907-
Some(Node::Item(item)) => item.span,
908-
Some(_) => self.span(hir_id),
909-
_ => bug!("hir::map::Map::span_with_body: id not in map: {:?}", hir_id),
910-
}
879+
self.tcx.hir_owner_spans(hir_id.owner)[hir_id.local_id]
911880
}
912881

913882
pub fn span_if_local(&self, id: DefId) -> Option<Span> {

compiler/rustc_middle/src/hir/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ pub fn provide(providers: &mut Providers) {
7777
};
7878
providers.hir_owner = |tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].signature;
7979
providers.hir_owner_nodes = |tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].with_bodies.as_deref();
80+
providers.hir_owner_spans = |tcx, id| tcx.hir_crate(LOCAL_CRATE).spans.get_owner(id);
8081
providers.fn_arg_names = |tcx, id| {
8182
let hir = tcx.hir();
8283
let hir_id = hir.local_def_id_to_hir_id(id.expect_local());

compiler/rustc_middle/src/query/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,15 @@ rustc_queries! {
8989
desc { |tcx| "HIR owner items in `{}`", tcx.def_path_str(key.to_def_id()) }
9090
}
9191

92+
// Gives access to the HIR spans inside the HIR owner `key`.
93+
//
94+
// This can be conveniently accessed by methods on `tcx.hir()`.
95+
// Avoid calling this query directly.
96+
query hir_owner_spans(key: LocalDefId) -> &'tcx IndexVec<ItemLocalId, Span> {
97+
eval_always
98+
desc { |tcx| "HIR owner spans in `{}`", tcx.def_path_str(key.to_def_id()) }
99+
}
100+
92101
/// Computes the `DefId` of the corresponding const parameter in case the `key` is a
93102
/// const argument and returns `None` otherwise.
94103
///

0 commit comments

Comments
 (0)