Skip to content

Commit f89d64d

Browse files
committed
Use def_key in tcx.item_name when possible.
1 parent b5dfa6a commit f89d64d

File tree

10 files changed

+41
-42
lines changed

10 files changed

+41
-42
lines changed

compiler/rustc_hir/src/definitions.rs

+5
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ impl DefKey {
147147
// DefPathHashes in this DefPathTable.
148148
DefPathHash::new(parent.stable_crate_id(), local_hash)
149149
}
150+
151+
#[inline]
152+
pub fn get_opt_name(&self) -> Option<Symbol> {
153+
self.disambiguated_data.data.get_opt_name()
154+
}
150155
}
151156

152157
/// A pair of `DefPathData` and an integer disambiguator. The integer is

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

+6-7
Original file line numberDiff line numberDiff line change
@@ -554,13 +554,12 @@ impl<'hir> Map<'hir> {
554554
}
555555
}
556556

557-
pub fn ty_param_name(self, id: HirId) -> Symbol {
558-
match self.get(id) {
559-
Node::Item(&Item { kind: ItemKind::Trait(..) | ItemKind::TraitAlias(..), .. }) => {
560-
kw::SelfUpper
561-
}
562-
Node::GenericParam(param) => param.name.ident().name,
563-
_ => bug!("ty_param_name: {} not a type parameter", self.node_to_string(id)),
557+
pub fn ty_param_name(self, def_id: LocalDefId) -> Symbol {
558+
let def_kind = self.tcx.def_kind(def_id);
559+
match def_kind {
560+
DefKind::Trait | DefKind::TraitAlias => kw::SelfUpper,
561+
DefKind::TyParam | DefKind::ConstParam => self.tcx.item_name(def_id.to_def_id()),
562+
_ => bug!("ty_param_name: {:?} is a {:?} not a type parameter", def_id, def_kind),
564563
}
565564
}
566565

compiler/rustc_middle/src/query/mod.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -517,10 +517,7 @@ rustc_queries! {
517517
/// To avoid cycles within the predicates of a single item we compute
518518
/// per-type-parameter predicates for resolving `T::AssocTy`.
519519
query type_param_predicates(key: (DefId, LocalDefId, rustc_span::symbol::Ident)) -> ty::GenericPredicates<'tcx> {
520-
desc { |tcx| "computing the bounds for type parameter `{}`", {
521-
let id = tcx.hir().local_def_id_to_hir_id(key.1);
522-
tcx.hir().ty_param_name(id)
523-
}}
520+
desc { |tcx| "computing the bounds for type parameter `{}`", tcx.hir().ty_param_name(key.1) }
524521
}
525522

526523
query trait_def(key: DefId) -> ty::TraitDef {

compiler/rustc_middle/src/ty/mod.rs

+18-17
Original file line numberDiff line numberDiff line change
@@ -1986,27 +1986,25 @@ impl<'tcx> TyCtxt<'tcx> {
19861986
.filter(|item| item.kind == AssocKind::Fn && item.defaultness.has_value())
19871987
}
19881988

1989-
fn item_name_from_hir(self, def_id: DefId) -> Option<Ident> {
1990-
self.hir().get_if_local(def_id).and_then(|node| node.ident())
1991-
}
1992-
1993-
fn item_name_from_def_id(self, def_id: DefId) -> Option<Symbol> {
1989+
fn opt_item_name(self, def_id: DefId) -> Option<Symbol> {
19941990
if def_id.index == CRATE_DEF_INDEX {
19951991
Some(self.crate_name(def_id.krate))
19961992
} else {
19971993
let def_key = self.def_key(def_id);
19981994
match def_key.disambiguated_data.data {
19991995
// The name of a constructor is that of its parent.
2000-
rustc_hir::definitions::DefPathData::Ctor => self.item_name_from_def_id(DefId {
2001-
krate: def_id.krate,
2002-
index: def_key.parent.unwrap(),
2003-
}),
2004-
_ => def_key.disambiguated_data.data.get_opt_name(),
1996+
rustc_hir::definitions::DefPathData::Ctor => self
1997+
.opt_item_name(DefId { krate: def_id.krate, index: def_key.parent.unwrap() }),
1998+
// The name of opaque types only exists in HIR.
1999+
rustc_hir::definitions::DefPathData::ImplTrait
2000+
if let Some(def_id) = def_id.as_local() =>
2001+
self.hir().opt_name(self.hir().local_def_id_to_hir_id(def_id)),
2002+
_ => def_key.get_opt_name(),
20052003
}
20062004
}
20072005
}
20082006

2009-
/// Look up the name of an item across crates. This does not look at HIR.
2007+
/// Look up the name of a definition across crates. This does not look at HIR.
20102008
///
20112009
/// When possible, this function should be used for cross-crate lookups over
20122010
/// [`opt_item_name`] to avoid invalidating the incremental cache. If you
@@ -2018,18 +2016,21 @@ impl<'tcx> TyCtxt<'tcx> {
20182016
pub fn item_name(self, id: DefId) -> Symbol {
20192017
// Look at cross-crate items first to avoid invalidating the incremental cache
20202018
// unless we have to.
2021-
self.item_name_from_def_id(id).unwrap_or_else(|| {
2019+
self.opt_item_name(id).unwrap_or_else(|| {
20222020
bug!("item_name: no name for {:?}", self.def_path(id));
20232021
})
20242022
}
20252023

2026-
/// Look up the name and span of an item or [`Node`].
2024+
/// Look up the name and span of a definition.
20272025
///
20282026
/// See [`item_name`][Self::item_name] for more information.
2029-
pub fn opt_item_name(self, def_id: DefId) -> Option<Ident> {
2030-
// Look at the HIR first so the span will be correct if this is a local item.
2031-
self.item_name_from_hir(def_id)
2032-
.or_else(|| self.item_name_from_def_id(def_id).map(Ident::with_dummy_span))
2027+
pub fn opt_item_ident(self, def_id: DefId) -> Option<Ident> {
2028+
let def = self.opt_item_name(def_id)?;
2029+
let span = def_id
2030+
.as_local()
2031+
.and_then(|id| self.def_ident_span(id))
2032+
.unwrap_or(rustc_span::DUMMY_SP);
2033+
Some(Ident::new(def, span))
20332034
}
20342035

20352036
pub fn opt_associated_item(self, def_id: DefId) -> Option<&'tcx AssocItem> {

compiler/rustc_monomorphize/src/polymorphize.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ fn emit_unused_generic_params_error<'tcx>(
201201
return;
202202
}
203203

204-
let fn_span = match tcx.opt_item_name(def_id) {
204+
let fn_span = match tcx.opt_item_ident(def_id) {
205205
Some(ident) => ident.span,
206206
_ => tcx.def_span(def_id),
207207
};

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -2064,7 +2064,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
20642064
ObligationCauseCode::BindingObligation(item_def_id, span) => {
20652065
let item_name = tcx.def_path_str(item_def_id);
20662066
let mut multispan = MultiSpan::from(span);
2067-
if let Some(ident) = tcx.opt_item_name(item_def_id) {
2067+
if let Some(ident) = tcx.opt_item_ident(item_def_id) {
20682068
let sm = tcx.sess.source_map();
20692069
let same_line =
20702070
match (sm.lookup_line(ident.span.hi()), sm.lookup_line(span.lo())) {
@@ -2267,7 +2267,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
22672267
if !is_upvar_tys_infer_tuple {
22682268
let msg = format!("required because it appears within the type `{}`", ty);
22692269
match ty.kind() {
2270-
ty::Adt(def, _) => match self.tcx.opt_item_name(def.did()) {
2270+
ty::Adt(def, _) => match self.tcx.opt_item_ident(def.did()) {
22712271
Some(ident) => err.span_note(ident.span, &msg),
22722272
None => err.note(&msg),
22732273
},
@@ -2475,7 +2475,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
24752475
);
24762476
let sp = self
24772477
.tcx
2478-
.opt_item_name(trait_item_def_id)
2478+
.opt_item_ident(trait_item_def_id)
24792479
.map(|i| i.span)
24802480
.unwrap_or_else(|| self.tcx.def_span(trait_item_def_id));
24812481
let mut assoc_span: MultiSpan = sp.into();
@@ -2486,7 +2486,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
24862486
if let Some(ident) = self
24872487
.tcx
24882488
.opt_associated_item(trait_item_def_id)
2489-
.and_then(|i| self.tcx.opt_item_name(i.container.id()))
2489+
.and_then(|i| self.tcx.opt_item_ident(i.container.id()))
24902490
{
24912491
assoc_span.push_span_label(ident.span, "in this trait");
24922492
}
@@ -2511,7 +2511,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
25112511
if let Some(ident) = self
25122512
.tcx
25132513
.opt_associated_item(trait_item_def_id)
2514-
.and_then(|i| self.tcx.opt_item_name(i.container.id()))
2514+
.and_then(|i| self.tcx.opt_item_ident(i.container.id()))
25152515
{
25162516
assoc_span.push_span_label(ident.span, "in this trait");
25172517
}

compiler/rustc_typeck/src/astconv/generics.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
8282
}
8383
Res::Def(DefKind::TyParam, src_def_id) => {
8484
if let Some(param_local_id) = param.def_id.as_local() {
85-
let param_hir_id = tcx.hir().local_def_id_to_hir_id(param_local_id);
86-
let param_name = tcx.hir().ty_param_name(param_hir_id);
85+
let param_name = tcx.hir().ty_param_name(param_local_id);
8786
let param_type = tcx.infer_ctxt().enter(|infcx| {
8887
infcx.resolve_numeric_literals_with_default(tcx.type_of(param.def_id))
8988
});

compiler/rustc_typeck/src/astconv/mod.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1620,8 +1620,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
16201620

16211621
debug!("find_bound_for_assoc_item: predicates={:#?}", predicates);
16221622

1623-
let param_hir_id = tcx.hir().local_def_id_to_hir_id(ty_param_def_id);
1624-
let param_name = tcx.hir().ty_param_name(param_hir_id);
1623+
let param_name = tcx.hir().ty_param_name(ty_param_def_id);
16251624
self.one_bound_for_assoc_type(
16261625
|| {
16271626
traits::transitive_bounds_that_define_assoc_type(
@@ -2266,11 +2265,10 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
22662265
self.prohibit_generics(path.segments);
22672266

22682267
let def_id = def_id.expect_local();
2269-
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
22702268
let item_def_id = tcx.hir().ty_param_owner(def_id);
22712269
let generics = tcx.generics_of(item_def_id);
22722270
let index = generics.param_def_id_to_index[&def_id.to_def_id()];
2273-
tcx.mk_ty_param(index, tcx.hir().name(hir_id))
2271+
tcx.mk_ty_param(index, tcx.hir().ty_param_name(def_id))
22742272
}
22752273
Res::SelfTy { trait_: Some(_), alias_to: None } => {
22762274
// `Self` in trait or type alias.

compiler/rustc_typeck/src/check/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2195,7 +2195,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21952195
None => return,
21962196
};
21972197
let param_span = self.tcx.hir().span(param_hir_id);
2198-
let param_name = self.tcx.hir().ty_param_name(param_hir_id);
2198+
let param_name = self.tcx.hir().ty_param_name(param_def_id.expect_local());
21992199

22002200
err.span_label(param_span, &format!("type parameter '{}' declared here", param_name));
22012201
}

compiler/rustc_typeck/src/collect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ fn type_param_predicates(
561561
let param_owner = tcx.hir().ty_param_owner(def_id);
562562
let generics = tcx.generics_of(param_owner);
563563
let index = generics.param_def_id_to_index[&def_id.to_def_id()];
564-
let ty = tcx.mk_ty_param(index, tcx.hir().ty_param_name(param_id));
564+
let ty = tcx.mk_ty_param(index, tcx.hir().ty_param_name(def_id));
565565

566566
// Don't look for bounds where the type parameter isn't in scope.
567567
let parent = if item_def_id == param_owner.to_def_id() {

0 commit comments

Comments
 (0)