Skip to content

Commit 12c5f78

Browse files
authored
Rollup merge of rust-lang#78345 - jyn514:proper-names, r=varkor
Fix handling of item names for HIR - Handle variants, fields, macros in `Node::ident()` - Handle the crate root in `opt_item_name` - Rewrite `item_name` in terms of `opt_item_name` I need this for both rust-lang#77820 and rust-lang#78082, so splitting it out into a separate PR so it can land early.
2 parents 50086af + f60fd49 commit 12c5f78

File tree

3 files changed

+47
-21
lines changed

3 files changed

+47
-21
lines changed

compiler/rustc_hir/src/hir.rs

+3
Original file line numberDiff line numberDiff line change
@@ -2677,6 +2677,9 @@ impl<'hir> Node<'hir> {
26772677
Node::TraitItem(TraitItem { ident, .. })
26782678
| Node::ImplItem(ImplItem { ident, .. })
26792679
| Node::ForeignItem(ForeignItem { ident, .. })
2680+
| Node::Field(StructField { ident, .. })
2681+
| Node::Variant(Variant { ident, .. })
2682+
| Node::MacroDef(MacroDef { ident, .. })
26802683
| Node::Item(Item { ident, .. }) => Some(*ident),
26812684
_ => None,
26822685
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ impl<'hir> Map<'hir> {
478478
}
479479

480480
pub fn get_if_local(&self, id: DefId) -> Option<Node<'hir>> {
481-
id.as_local().map(|id| self.get(self.local_def_id_to_hir_id(id)))
481+
id.as_local().and_then(|id| self.find(self.local_def_id_to_hir_id(id)))
482482
}
483483

484484
pub fn get_generics(&self, id: DefId) -> Option<&'hir Generics<'hir>> {

compiler/rustc_middle/src/ty/mod.rs

+43-20
Original file line numberDiff line numberDiff line change
@@ -2795,10 +2795,50 @@ impl<'tcx> TyCtxt<'tcx> {
27952795
.filter(|item| item.kind == AssocKind::Fn && item.defaultness.has_value())
27962796
}
27972797

2798+
fn item_name_from_hir(self, def_id: DefId) -> Option<Ident> {
2799+
self.hir().get_if_local(def_id).and_then(|node| node.ident())
2800+
}
2801+
2802+
fn item_name_from_def_id(self, def_id: DefId) -> Option<Symbol> {
2803+
if def_id.index == CRATE_DEF_INDEX {
2804+
Some(self.original_crate_name(def_id.krate))
2805+
} else {
2806+
let def_key = self.def_key(def_id);
2807+
match def_key.disambiguated_data.data {
2808+
// The name of a constructor is that of its parent.
2809+
rustc_hir::definitions::DefPathData::Ctor => self.item_name_from_def_id(DefId {
2810+
krate: def_id.krate,
2811+
index: def_key.parent.unwrap(),
2812+
}),
2813+
_ => def_key.disambiguated_data.data.get_opt_name(),
2814+
}
2815+
}
2816+
}
2817+
2818+
/// Look up the name of an item across crates. This does not look at HIR.
2819+
///
2820+
/// When possible, this function should be used for cross-crate lookups over
2821+
/// [`opt_item_name`] to avoid invalidating the incremental cache. If you
2822+
/// need to handle items without a name, or HIR items that will not be
2823+
/// serialized cross-crate, or if you need the span of the item, use
2824+
/// [`opt_item_name`] instead.
2825+
///
2826+
/// [`opt_item_name`]: Self::opt_item_name
2827+
pub fn item_name(self, id: DefId) -> Symbol {
2828+
// Look at cross-crate items first to avoid invalidating the incremental cache
2829+
// unless we have to.
2830+
self.item_name_from_def_id(id).unwrap_or_else(|| {
2831+
bug!("item_name: no name for {:?}", self.def_path(id));
2832+
})
2833+
}
2834+
2835+
/// Look up the name and span of an item or [`Node`].
2836+
///
2837+
/// See [`item_name`][Self::item_name] for more information.
27982838
pub fn opt_item_name(self, def_id: DefId) -> Option<Ident> {
2799-
def_id
2800-
.as_local()
2801-
.and_then(|def_id| self.hir().get(self.hir().local_def_id_to_hir_id(def_id)).ident())
2839+
// Look at the HIR first so the span will be correct if this is a local item.
2840+
self.item_name_from_hir(def_id)
2841+
.or_else(|| self.item_name_from_def_id(def_id).map(Ident::with_dummy_span))
28022842
}
28032843

28042844
pub fn opt_associated_item(self, def_id: DefId) -> Option<&'tcx AssocItem> {
@@ -2921,23 +2961,6 @@ impl<'tcx> TyCtxt<'tcx> {
29212961
}
29222962
}
29232963

2924-
pub fn item_name(self, id: DefId) -> Symbol {
2925-
if id.index == CRATE_DEF_INDEX {
2926-
self.original_crate_name(id.krate)
2927-
} else {
2928-
let def_key = self.def_key(id);
2929-
match def_key.disambiguated_data.data {
2930-
// The name of a constructor is that of its parent.
2931-
rustc_hir::definitions::DefPathData::Ctor => {
2932-
self.item_name(DefId { krate: id.krate, index: def_key.parent.unwrap() })
2933-
}
2934-
_ => def_key.disambiguated_data.data.get_opt_name().unwrap_or_else(|| {
2935-
bug!("item_name: no name for {:?}", self.def_path(id));
2936-
}),
2937-
}
2938-
}
2939-
}
2940-
29412964
/// Returns the possibly-auto-generated MIR of a `(DefId, Subst)` pair.
29422965
pub fn instance_mir(self, instance: ty::InstanceDef<'tcx>) -> &'tcx Body<'tcx> {
29432966
match instance {

0 commit comments

Comments
 (0)