Implement def_ident_span in rustc_middle.

This commit is contained in:
Camille GILLOT 2022-04-08 18:53:16 +02:00
parent c3384ea35c
commit 16f9f7c7b1
3 changed files with 29 additions and 34 deletions

View File

@ -910,27 +910,34 @@ pub fn expect_expr(self, id: HirId) -> &'hir Expr<'hir> {
}
}
pub(super) fn opt_ident_span(self, id: HirId) -> Option<Span> {
let ident = match self.get(id) {
// A `Ctor` doesn't have an identifier itself, but its parent
// struct/variant does. Compare with `hir::Map::opt_span`.
Node::Ctor(..) => match self.find(self.get_parent_node(id))? {
Node::Item(item) => Some(item.ident),
Node::Variant(variant) => Some(variant.ident),
_ => unreachable!(),
},
node => node.ident(),
};
ident.map(|ident| ident.span)
}
pub fn opt_name(self, id: HirId) -> Option<Symbol> {
Some(match self.get(id) {
Node::Item(i) => i.ident.name,
Node::ForeignItem(fi) => fi.ident.name,
Node::ImplItem(ii) => ii.ident.name,
Node::TraitItem(ti) => ti.ident.name,
Node::Variant(v) => v.ident.name,
Node::Field(f) => f.ident.name,
Node::Lifetime(lt) => lt.name.ident().name,
Node::GenericParam(param) => param.name.ident().name,
Node::Binding(&Pat { kind: PatKind::Binding(_, _, l, _), .. }) => l.name,
Node::Ctor(..) => self.name(HirId::make_owner(self.get_parent_item(id))),
_ => return None,
})
match self.get(id) {
Node::Binding(&Pat { kind: PatKind::Binding(_, _, l, _), .. }) => Some(l.name),
Node::Ctor(..) => match self.find(self.get_parent_node(id))? {
Node::Item(item) => Some(item.ident.name),
Node::Variant(variant) => Some(variant.ident.name),
_ => unreachable!(),
},
node => node.ident().map(|i| i.name),
}
}
pub fn name(self, id: HirId) -> Symbol {
match self.opt_name(id) {
Some(name) => name,
None => bug!("no name for {}", self.node_to_string(id)),
}
self.opt_name(id).unwrap_or_else(|| bug!("no name for {}", self.node_to_string(id)))
}
/// Given a node ID, gets a list of attributes associated with the AST

View File

@ -122,6 +122,11 @@ pub fn provide(providers: &mut Providers) {
|tcx, id| tcx.hir_crate(()).owners[id].as_owner().map_or(AttributeMap::EMPTY, |o| &o.attrs);
providers.source_span = |tcx, def_id| tcx.resolutions(()).definitions.def_span(def_id);
providers.def_span = |tcx, def_id| tcx.hir().span_if_local(def_id).unwrap_or(DUMMY_SP);
providers.def_ident_span = |tcx, def_id| {
let def_id = def_id.expect_local();
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
tcx.hir().opt_ident_span(hir_id)
};
providers.fn_arg_names = |tcx, id| {
let hir = tcx.hir();
let hir_id = hir.local_def_id_to_hir_id(id.expect_local());

View File

@ -5,7 +5,6 @@
use rustc_middle::ty::{
self, Binder, EarlyBinder, Predicate, PredicateKind, ToPredicate, Ty, TyCtxt,
};
use rustc_span::Span;
use rustc_trait_selection::traits;
fn sized_constraint_for_ty<'tcx>(
@ -103,21 +102,6 @@ fn adt_sized_constraint(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AdtSizedConstrain
ty::AdtSizedConstraint(result)
}
fn def_ident_span(tcx: TyCtxt<'_>, def_id: DefId) -> Option<Span> {
tcx.hir()
.get_if_local(def_id)
.and_then(|node| match node {
// A `Ctor` doesn't have an identifier itself, but its parent
// struct/variant does. Compare with `hir::Map::opt_span`.
hir::Node::Ctor(ctor) => ctor
.ctor_hir_id()
.and_then(|ctor_id| tcx.hir().find(tcx.hir().get_parent_node(ctor_id)))
.and_then(|parent| parent.ident()),
_ => node.ident(),
})
.map(|ident| ident.span)
}
/// See `ParamEnv` struct definition for details.
#[instrument(level = "debug", skip(tcx))]
fn param_env(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ParamEnv<'_> {
@ -480,7 +464,6 @@ pub fn provide(providers: &mut ty::query::Providers) {
*providers = ty::query::Providers {
asyncness,
adt_sized_constraint,
def_ident_span,
param_env,
param_env_reveal_all_normalized,
instance_def_size_estimate,