Skip to content

Commit 16f9f7c

Browse files
committed
Implement def_ident_span in rustc_middle.
1 parent c3384ea commit 16f9f7c

File tree

3 files changed

+29
-34
lines changed

3 files changed

+29
-34
lines changed

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

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -910,27 +910,34 @@ impl<'hir> Map<'hir> {
910910
}
911911
}
912912

913+
pub(super) fn opt_ident_span(self, id: HirId) -> Option<Span> {
914+
let ident = match self.get(id) {
915+
// A `Ctor` doesn't have an identifier itself, but its parent
916+
// struct/variant does. Compare with `hir::Map::opt_span`.
917+
Node::Ctor(..) => match self.find(self.get_parent_node(id))? {
918+
Node::Item(item) => Some(item.ident),
919+
Node::Variant(variant) => Some(variant.ident),
920+
_ => unreachable!(),
921+
},
922+
node => node.ident(),
923+
};
924+
ident.map(|ident| ident.span)
925+
}
926+
913927
pub fn opt_name(self, id: HirId) -> Option<Symbol> {
914-
Some(match self.get(id) {
915-
Node::Item(i) => i.ident.name,
916-
Node::ForeignItem(fi) => fi.ident.name,
917-
Node::ImplItem(ii) => ii.ident.name,
918-
Node::TraitItem(ti) => ti.ident.name,
919-
Node::Variant(v) => v.ident.name,
920-
Node::Field(f) => f.ident.name,
921-
Node::Lifetime(lt) => lt.name.ident().name,
922-
Node::GenericParam(param) => param.name.ident().name,
923-
Node::Binding(&Pat { kind: PatKind::Binding(_, _, l, _), .. }) => l.name,
924-
Node::Ctor(..) => self.name(HirId::make_owner(self.get_parent_item(id))),
925-
_ => return None,
926-
})
928+
match self.get(id) {
929+
Node::Binding(&Pat { kind: PatKind::Binding(_, _, l, _), .. }) => Some(l.name),
930+
Node::Ctor(..) => match self.find(self.get_parent_node(id))? {
931+
Node::Item(item) => Some(item.ident.name),
932+
Node::Variant(variant) => Some(variant.ident.name),
933+
_ => unreachable!(),
934+
},
935+
node => node.ident().map(|i| i.name),
936+
}
927937
}
928938

929939
pub fn name(self, id: HirId) -> Symbol {
930-
match self.opt_name(id) {
931-
Some(name) => name,
932-
None => bug!("no name for {}", self.node_to_string(id)),
933-
}
940+
self.opt_name(id).unwrap_or_else(|| bug!("no name for {}", self.node_to_string(id)))
934941
}
935942

936943
/// Given a node ID, gets a list of attributes associated with the AST

compiler/rustc_middle/src/hir/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ pub fn provide(providers: &mut Providers) {
122122
|tcx, id| tcx.hir_crate(()).owners[id].as_owner().map_or(AttributeMap::EMPTY, |o| &o.attrs);
123123
providers.source_span = |tcx, def_id| tcx.resolutions(()).definitions.def_span(def_id);
124124
providers.def_span = |tcx, def_id| tcx.hir().span_if_local(def_id).unwrap_or(DUMMY_SP);
125+
providers.def_ident_span = |tcx, def_id| {
126+
let def_id = def_id.expect_local();
127+
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
128+
tcx.hir().opt_ident_span(hir_id)
129+
};
125130
providers.fn_arg_names = |tcx, id| {
126131
let hir = tcx.hir();
127132
let hir_id = hir.local_def_id_to_hir_id(id.expect_local());

compiler/rustc_ty_utils/src/ty.rs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use rustc_middle::ty::subst::Subst;
55
use rustc_middle::ty::{
66
self, Binder, EarlyBinder, Predicate, PredicateKind, ToPredicate, Ty, TyCtxt,
77
};
8-
use rustc_span::Span;
98
use rustc_trait_selection::traits;
109

1110
fn sized_constraint_for_ty<'tcx>(
@@ -103,21 +102,6 @@ fn adt_sized_constraint(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AdtSizedConstrain
103102
ty::AdtSizedConstraint(result)
104103
}
105104

106-
fn def_ident_span(tcx: TyCtxt<'_>, def_id: DefId) -> Option<Span> {
107-
tcx.hir()
108-
.get_if_local(def_id)
109-
.and_then(|node| match node {
110-
// A `Ctor` doesn't have an identifier itself, but its parent
111-
// struct/variant does. Compare with `hir::Map::opt_span`.
112-
hir::Node::Ctor(ctor) => ctor
113-
.ctor_hir_id()
114-
.and_then(|ctor_id| tcx.hir().find(tcx.hir().get_parent_node(ctor_id)))
115-
.and_then(|parent| parent.ident()),
116-
_ => node.ident(),
117-
})
118-
.map(|ident| ident.span)
119-
}
120-
121105
/// See `ParamEnv` struct definition for details.
122106
#[instrument(level = "debug", skip(tcx))]
123107
fn param_env(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ParamEnv<'_> {
@@ -480,7 +464,6 @@ pub fn provide(providers: &mut ty::query::Providers) {
480464
*providers = ty::query::Providers {
481465
asyncness,
482466
adt_sized_constraint,
483-
def_ident_span,
484467
param_env,
485468
param_env_reveal_all_normalized,
486469
instance_def_size_estimate,

0 commit comments

Comments
 (0)