Infallible version of def_span.
This commit is contained in:
parent
a4cbb44ae2
commit
064a351953
3 changed files with 39 additions and 37 deletions
|
@ -848,50 +848,55 @@ impl<'hir> Map<'hir> {
|
||||||
/// Gets the span of the definition of the specified HIR node.
|
/// Gets the span of the definition of the specified HIR node.
|
||||||
/// This is used by `tcx.get_span`
|
/// This is used by `tcx.get_span`
|
||||||
pub fn span(&self, hir_id: HirId) -> Span {
|
pub fn span(&self, hir_id: HirId) -> Span {
|
||||||
match self.find_entry(hir_id).map(|entry| entry.node) {
|
self.opt_span(hir_id)
|
||||||
Some(Node::Param(param)) => param.span,
|
.unwrap_or_else(|| bug!("hir::map::Map::span: id not in map: {:?}", hir_id))
|
||||||
Some(Node::Item(item)) => match &item.kind {
|
}
|
||||||
|
|
||||||
|
pub fn opt_span(&self, hir_id: HirId) -> Option<Span> {
|
||||||
|
let span = match self.find_entry(hir_id)?.node {
|
||||||
|
Node::Param(param) => param.span,
|
||||||
|
Node::Item(item) => match &item.kind {
|
||||||
ItemKind::Fn(sig, _, _) => sig.span,
|
ItemKind::Fn(sig, _, _) => sig.span,
|
||||||
_ => item.span,
|
_ => item.span,
|
||||||
},
|
},
|
||||||
Some(Node::ForeignItem(foreign_item)) => foreign_item.span,
|
Node::ForeignItem(foreign_item) => foreign_item.span,
|
||||||
Some(Node::TraitItem(trait_item)) => match &trait_item.kind {
|
Node::TraitItem(trait_item) => match &trait_item.kind {
|
||||||
TraitItemKind::Fn(sig, _) => sig.span,
|
TraitItemKind::Fn(sig, _) => sig.span,
|
||||||
_ => trait_item.span,
|
_ => trait_item.span,
|
||||||
},
|
},
|
||||||
Some(Node::ImplItem(impl_item)) => match &impl_item.kind {
|
Node::ImplItem(impl_item) => match &impl_item.kind {
|
||||||
ImplItemKind::Fn(sig, _) => sig.span,
|
ImplItemKind::Fn(sig, _) => sig.span,
|
||||||
_ => impl_item.span,
|
_ => impl_item.span,
|
||||||
},
|
},
|
||||||
Some(Node::Variant(variant)) => variant.span,
|
Node::Variant(variant) => variant.span,
|
||||||
Some(Node::Field(field)) => field.span,
|
Node::Field(field) => field.span,
|
||||||
Some(Node::AnonConst(constant)) => self.body(constant.body).value.span,
|
Node::AnonConst(constant) => self.body(constant.body).value.span,
|
||||||
Some(Node::Expr(expr)) => expr.span,
|
Node::Expr(expr) => expr.span,
|
||||||
Some(Node::Stmt(stmt)) => stmt.span,
|
Node::Stmt(stmt) => stmt.span,
|
||||||
Some(Node::PathSegment(seg)) => seg.ident.span,
|
Node::PathSegment(seg) => seg.ident.span,
|
||||||
Some(Node::Ty(ty)) => ty.span,
|
Node::Ty(ty) => ty.span,
|
||||||
Some(Node::TraitRef(tr)) => tr.path.span,
|
Node::TraitRef(tr) => tr.path.span,
|
||||||
Some(Node::Binding(pat)) => pat.span,
|
Node::Binding(pat) => pat.span,
|
||||||
Some(Node::Pat(pat)) => pat.span,
|
Node::Pat(pat) => pat.span,
|
||||||
Some(Node::Arm(arm)) => arm.span,
|
Node::Arm(arm) => arm.span,
|
||||||
Some(Node::Block(block)) => block.span,
|
Node::Block(block) => block.span,
|
||||||
Some(Node::Ctor(..)) => match self.find(self.get_parent_node(hir_id)) {
|
Node::Ctor(..) => match self.find(self.get_parent_node(hir_id))? {
|
||||||
Some(Node::Item(item)) => item.span,
|
Node::Item(item) => item.span,
|
||||||
Some(Node::Variant(variant)) => variant.span,
|
Node::Variant(variant) => variant.span,
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
},
|
},
|
||||||
Some(Node::Lifetime(lifetime)) => lifetime.span,
|
Node::Lifetime(lifetime) => lifetime.span,
|
||||||
Some(Node::GenericParam(param)) => param.span,
|
Node::GenericParam(param) => param.span,
|
||||||
Some(Node::Visibility(&Spanned {
|
Node::Visibility(&Spanned {
|
||||||
node: VisibilityKind::Restricted { ref path, .. },
|
node: VisibilityKind::Restricted { ref path, .. },
|
||||||
..
|
..
|
||||||
})) => path.span,
|
}) => path.span,
|
||||||
Some(Node::Visibility(v)) => bug!("unexpected Visibility {:?}", v),
|
Node::Visibility(v) => bug!("unexpected Visibility {:?}", v),
|
||||||
Some(Node::Local(local)) => local.span,
|
Node::Local(local) => local.span,
|
||||||
Some(Node::MacroDef(macro_def)) => macro_def.span,
|
Node::MacroDef(macro_def) => macro_def.span,
|
||||||
Some(Node::Crate(item)) => item.span,
|
Node::Crate(item) => item.span,
|
||||||
None => bug!("hir::map::Map::span: id not in map: {:?}", hir_id),
|
};
|
||||||
}
|
Some(span)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Like `hir.span()`, but includes the body of function items
|
/// Like `hir.span()`, but includes the body of function items
|
||||||
|
@ -907,7 +912,7 @@ impl<'hir> Map<'hir> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn span_if_local(&self, id: DefId) -> Option<Span> {
|
pub fn span_if_local(&self, id: DefId) -> Option<Span> {
|
||||||
id.as_local().map(|id| self.span(self.local_def_id_to_hir_id(id)))
|
id.as_local().and_then(|id| self.opt_span(self.local_def_id_to_hir_id(id)))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn res_span(&self, res: Res) -> Option<Span> {
|
pub fn res_span(&self, res: Res) -> Option<Span> {
|
||||||
|
|
|
@ -15,6 +15,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
||||||
use rustc_hir::def_id::{LocalDefId, LOCAL_CRATE};
|
use rustc_hir::def_id::{LocalDefId, LOCAL_CRATE};
|
||||||
use rustc_hir::*;
|
use rustc_hir::*;
|
||||||
use rustc_index::vec::IndexVec;
|
use rustc_index::vec::IndexVec;
|
||||||
|
use rustc_span::DUMMY_SP;
|
||||||
|
|
||||||
pub struct Owner<'tcx> {
|
pub struct Owner<'tcx> {
|
||||||
parent: HirId,
|
parent: HirId,
|
||||||
|
@ -77,6 +78,7 @@ pub fn provide(providers: &mut Providers) {
|
||||||
};
|
};
|
||||||
providers.hir_owner = |tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].signature;
|
providers.hir_owner = |tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].signature;
|
||||||
providers.hir_owner_nodes = |tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].with_bodies.as_deref();
|
providers.hir_owner_nodes = |tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].with_bodies.as_deref();
|
||||||
|
providers.def_span = |tcx, def_id| tcx.hir().span_if_local(def_id).unwrap_or(DUMMY_SP);
|
||||||
providers.fn_arg_names = |tcx, id| {
|
providers.fn_arg_names = |tcx, id| {
|
||||||
let hir = tcx.hir();
|
let hir = tcx.hir();
|
||||||
let hir_id = hir.local_def_id_to_hir_id(id.expect_local());
|
let hir_id = hir.local_def_id_to_hir_id(id.expect_local());
|
||||||
|
|
|
@ -218,10 +218,6 @@ fn associated_items(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AssociatedItems<'_> {
|
||||||
ty::AssociatedItems::new(items)
|
ty::AssociatedItems::new(items)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn def_span(tcx: TyCtxt<'_>, def_id: DefId) -> Span {
|
|
||||||
tcx.hir().span_if_local(def_id).unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn def_ident_span(tcx: TyCtxt<'_>, def_id: DefId) -> Option<Span> {
|
fn def_ident_span(tcx: TyCtxt<'_>, def_id: DefId) -> Option<Span> {
|
||||||
tcx.hir().get_if_local(def_id).and_then(|node| node.ident()).map(|ident| ident.span)
|
tcx.hir().get_if_local(def_id).and_then(|node| node.ident()).map(|ident| ident.span)
|
||||||
}
|
}
|
||||||
|
@ -495,7 +491,6 @@ pub fn provide(providers: &mut ty::query::Providers) {
|
||||||
associated_item_def_ids,
|
associated_item_def_ids,
|
||||||
associated_items,
|
associated_items,
|
||||||
adt_sized_constraint,
|
adt_sized_constraint,
|
||||||
def_span,
|
|
||||||
def_ident_span,
|
def_ident_span,
|
||||||
param_env,
|
param_env,
|
||||||
param_env_reveal_all_normalized,
|
param_env_reveal_all_normalized,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue