diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index 779af7a3827..0389c88d22d 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -910,27 +910,34 @@ impl<'hir> Map<'hir> { } } + pub(super) fn opt_ident_span(self, id: HirId) -> Option { + 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 { - 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 diff --git a/compiler/rustc_middle/src/hir/mod.rs b/compiler/rustc_middle/src/hir/mod.rs index b50f121eff2..b5b21319afc 100644 --- a/compiler/rustc_middle/src/hir/mod.rs +++ b/compiler/rustc_middle/src/hir/mod.rs @@ -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()); diff --git a/compiler/rustc_ty_utils/src/ty.rs b/compiler/rustc_ty_utils/src/ty.rs index 8056198b20c..38ae6a25b18 100644 --- a/compiler/rustc_ty_utils/src/ty.rs +++ b/compiler/rustc_ty_utils/src/ty.rs @@ -5,7 +5,6 @@ use rustc_middle::ty::subst::Subst; 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 { - 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,