Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 064a351

Browse files
committed
Infallible version of def_span.
1 parent a4cbb44 commit 064a351

File tree

3 files changed

+39
-37
lines changed

3 files changed

+39
-37
lines changed

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

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -848,50 +848,55 @@ impl<'hir> Map<'hir> {
848848
/// Gets the span of the definition of the specified HIR node.
849849
/// This is used by `tcx.get_span`
850850
pub fn span(&self, hir_id: HirId) -> Span {
851-
match self.find_entry(hir_id).map(|entry| entry.node) {
852-
Some(Node::Param(param)) => param.span,
853-
Some(Node::Item(item)) => match &item.kind {
851+
self.opt_span(hir_id)
852+
.unwrap_or_else(|| bug!("hir::map::Map::span: id not in map: {:?}", hir_id))
853+
}
854+
855+
pub fn opt_span(&self, hir_id: HirId) -> Option<Span> {
856+
let span = match self.find_entry(hir_id)?.node {
857+
Node::Param(param) => param.span,
858+
Node::Item(item) => match &item.kind {
854859
ItemKind::Fn(sig, _, _) => sig.span,
855860
_ => item.span,
856861
},
857-
Some(Node::ForeignItem(foreign_item)) => foreign_item.span,
858-
Some(Node::TraitItem(trait_item)) => match &trait_item.kind {
862+
Node::ForeignItem(foreign_item) => foreign_item.span,
863+
Node::TraitItem(trait_item) => match &trait_item.kind {
859864
TraitItemKind::Fn(sig, _) => sig.span,
860865
_ => trait_item.span,
861866
},
862-
Some(Node::ImplItem(impl_item)) => match &impl_item.kind {
867+
Node::ImplItem(impl_item) => match &impl_item.kind {
863868
ImplItemKind::Fn(sig, _) => sig.span,
864869
_ => impl_item.span,
865870
},
866-
Some(Node::Variant(variant)) => variant.span,
867-
Some(Node::Field(field)) => field.span,
868-
Some(Node::AnonConst(constant)) => self.body(constant.body).value.span,
869-
Some(Node::Expr(expr)) => expr.span,
870-
Some(Node::Stmt(stmt)) => stmt.span,
871-
Some(Node::PathSegment(seg)) => seg.ident.span,
872-
Some(Node::Ty(ty)) => ty.span,
873-
Some(Node::TraitRef(tr)) => tr.path.span,
874-
Some(Node::Binding(pat)) => pat.span,
875-
Some(Node::Pat(pat)) => pat.span,
876-
Some(Node::Arm(arm)) => arm.span,
877-
Some(Node::Block(block)) => block.span,
878-
Some(Node::Ctor(..)) => match self.find(self.get_parent_node(hir_id)) {
879-
Some(Node::Item(item)) => item.span,
880-
Some(Node::Variant(variant)) => variant.span,
871+
Node::Variant(variant) => variant.span,
872+
Node::Field(field) => field.span,
873+
Node::AnonConst(constant) => self.body(constant.body).value.span,
874+
Node::Expr(expr) => expr.span,
875+
Node::Stmt(stmt) => stmt.span,
876+
Node::PathSegment(seg) => seg.ident.span,
877+
Node::Ty(ty) => ty.span,
878+
Node::TraitRef(tr) => tr.path.span,
879+
Node::Binding(pat) => pat.span,
880+
Node::Pat(pat) => pat.span,
881+
Node::Arm(arm) => arm.span,
882+
Node::Block(block) => block.span,
883+
Node::Ctor(..) => match self.find(self.get_parent_node(hir_id))? {
884+
Node::Item(item) => item.span,
885+
Node::Variant(variant) => variant.span,
881886
_ => unreachable!(),
882887
},
883-
Some(Node::Lifetime(lifetime)) => lifetime.span,
884-
Some(Node::GenericParam(param)) => param.span,
885-
Some(Node::Visibility(&Spanned {
888+
Node::Lifetime(lifetime) => lifetime.span,
889+
Node::GenericParam(param) => param.span,
890+
Node::Visibility(&Spanned {
886891
node: VisibilityKind::Restricted { ref path, .. },
887892
..
888-
})) => path.span,
889-
Some(Node::Visibility(v)) => bug!("unexpected Visibility {:?}", v),
890-
Some(Node::Local(local)) => local.span,
891-
Some(Node::MacroDef(macro_def)) => macro_def.span,
892-
Some(Node::Crate(item)) => item.span,
893-
None => bug!("hir::map::Map::span: id not in map: {:?}", hir_id),
894-
}
893+
}) => path.span,
894+
Node::Visibility(v) => bug!("unexpected Visibility {:?}", v),
895+
Node::Local(local) => local.span,
896+
Node::MacroDef(macro_def) => macro_def.span,
897+
Node::Crate(item) => item.span,
898+
};
899+
Some(span)
895900
}
896901

897902
/// Like `hir.span()`, but includes the body of function items
@@ -907,7 +912,7 @@ impl<'hir> Map<'hir> {
907912
}
908913

909914
pub fn span_if_local(&self, id: DefId) -> Option<Span> {
910-
id.as_local().map(|id| self.span(self.local_def_id_to_hir_id(id)))
915+
id.as_local().and_then(|id| self.opt_span(self.local_def_id_to_hir_id(id)))
911916
}
912917

913918
pub fn res_span(&self, res: Res) -> Option<Span> {

compiler/rustc_middle/src/hir/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
1515
use rustc_hir::def_id::{LocalDefId, LOCAL_CRATE};
1616
use rustc_hir::*;
1717
use rustc_index::vec::IndexVec;
18+
use rustc_span::DUMMY_SP;
1819

1920
pub struct Owner<'tcx> {
2021
parent: HirId,
@@ -77,6 +78,7 @@ pub fn provide(providers: &mut Providers) {
7778
};
7879
providers.hir_owner = |tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].signature;
7980
providers.hir_owner_nodes = |tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].with_bodies.as_deref();
81+
providers.def_span = |tcx, def_id| tcx.hir().span_if_local(def_id).unwrap_or(DUMMY_SP);
8082
providers.fn_arg_names = |tcx, id| {
8183
let hir = tcx.hir();
8284
let hir_id = hir.local_def_id_to_hir_id(id.expect_local());

compiler/rustc_ty_utils/src/ty.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,6 @@ fn associated_items(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AssociatedItems<'_> {
218218
ty::AssociatedItems::new(items)
219219
}
220220

221-
fn def_span(tcx: TyCtxt<'_>, def_id: DefId) -> Span {
222-
tcx.hir().span_if_local(def_id).unwrap()
223-
}
224-
225221
fn def_ident_span(tcx: TyCtxt<'_>, def_id: DefId) -> Option<Span> {
226222
tcx.hir().get_if_local(def_id).and_then(|node| node.ident()).map(|ident| ident.span)
227223
}
@@ -495,7 +491,6 @@ pub fn provide(providers: &mut ty::query::Providers) {
495491
associated_item_def_ids,
496492
associated_items,
497493
adt_sized_constraint,
498-
def_span,
499494
def_ident_span,
500495
param_env,
501496
param_env_reveal_all_normalized,

0 commit comments

Comments
 (0)