Skip to content

Commit 559c019

Browse files
committed
Auto merge of #95435 - cjgillot:one-name, r=oli-obk
Make def names and HIR names consistent. The name in the `DefKey` is interned to create the `DefId`, so it does not require any query to access. This can be leveraged to avoid a few useless HIR accesses for names. ~In order to achieve that, generic parameters created from universal impl-trait are given the pretty-printed ast as a name, instead of `{{opaque}}`.~ ~Drive-by: the `TyCtxt::opt_item_name` used a dummy span for non-local definitions. We have access to `def_ident_span`, so we use it.~
2 parents 8bf93e9 + 672ce15 commit 559c019

File tree

12 files changed

+56
-56
lines changed

12 files changed

+56
-56
lines changed

Diff for: 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

Diff for: compiler/rustc_middle/src/hir/map/mod.rs

+13-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::hir::{ModuleItems, Owner};
2-
use crate::ty::TyCtxt;
2+
use crate::ty::{DefIdTree, TyCtxt};
33
use rustc_ast as ast;
44
use rustc_data_structures::fingerprint::Fingerprint;
55
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
@@ -545,23 +545,21 @@ impl<'hir> Map<'hir> {
545545
});
546546
}
547547

548-
pub fn ty_param_owner(self, id: HirId) -> LocalDefId {
549-
match self.get(id) {
550-
Node::Item(&Item { kind: ItemKind::Trait(..) | ItemKind::TraitAlias(..), .. }) => {
551-
id.expect_owner()
552-
}
553-
Node::GenericParam(_) => self.get_parent_item(id),
554-
_ => bug!("ty_param_owner: {} not a type parameter", self.node_to_string(id)),
548+
pub fn ty_param_owner(self, def_id: LocalDefId) -> LocalDefId {
549+
let def_kind = self.tcx.def_kind(def_id);
550+
match def_kind {
551+
DefKind::Trait | DefKind::TraitAlias => def_id,
552+
DefKind::TyParam | DefKind::ConstParam => self.tcx.local_parent(def_id).unwrap(),
553+
_ => bug!("ty_param_owner: {:?} is a {:?} not a type parameter", def_id, def_kind),
555554
}
556555
}
557556

558-
pub fn ty_param_name(self, id: HirId) -> Symbol {
559-
match self.get(id) {
560-
Node::Item(&Item { kind: ItemKind::Trait(..) | ItemKind::TraitAlias(..), .. }) => {
561-
kw::SelfUpper
562-
}
563-
Node::GenericParam(param) => param.name.ident().name,
564-
_ => 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),
565563
}
566564
}
567565

Diff for: 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 {

Diff for: compiler/rustc_middle/src/ty/mod.rs

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

1994-
fn item_name_from_hir(self, def_id: DefId) -> Option<Ident> {
1995-
self.hir().get_if_local(def_id).and_then(|node| node.ident())
1996-
}
1997-
1998-
fn item_name_from_def_id(self, def_id: DefId) -> Option<Symbol> {
1994+
fn opt_item_name(self, def_id: DefId) -> Option<Symbol> {
19991995
if def_id.index == CRATE_DEF_INDEX {
20001996
Some(self.crate_name(def_id.krate))
20011997
} else {
20021998
let def_key = self.def_key(def_id);
20031999
match def_key.disambiguated_data.data {
20042000
// The name of a constructor is that of its parent.
2005-
rustc_hir::definitions::DefPathData::Ctor => self.item_name_from_def_id(DefId {
2006-
krate: def_id.krate,
2007-
index: def_key.parent.unwrap(),
2008-
}),
2009-
_ => def_key.disambiguated_data.data.get_opt_name(),
2001+
rustc_hir::definitions::DefPathData::Ctor => self
2002+
.opt_item_name(DefId { krate: def_id.krate, index: def_key.parent.unwrap() }),
2003+
// The name of opaque types only exists in HIR.
2004+
rustc_hir::definitions::DefPathData::ImplTrait
2005+
if let Some(def_id) = def_id.as_local() =>
2006+
self.hir().opt_name(self.hir().local_def_id_to_hir_id(def_id)),
2007+
_ => def_key.get_opt_name(),
20102008
}
20112009
}
20122010
}
20132011

2014-
/// Look up the name of an item across crates. This does not look at HIR.
2012+
/// Look up the name of a definition across crates. This does not look at HIR.
20152013
///
20162014
/// When possible, this function should be used for cross-crate lookups over
20172015
/// [`opt_item_name`] to avoid invalidating the incremental cache. If you
@@ -2023,18 +2021,21 @@ impl<'tcx> TyCtxt<'tcx> {
20232021
pub fn item_name(self, id: DefId) -> Symbol {
20242022
// Look at cross-crate items first to avoid invalidating the incremental cache
20252023
// unless we have to.
2026-
self.item_name_from_def_id(id).unwrap_or_else(|| {
2024+
self.opt_item_name(id).unwrap_or_else(|| {
20272025
bug!("item_name: no name for {:?}", self.def_path(id));
20282026
})
20292027
}
20302028

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

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

Diff for: 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
};

Diff for: 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
}

Diff for: 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
});

Diff for: compiler/rustc_typeck/src/astconv/mod.rs

+5-7
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(
@@ -2265,12 +2264,11 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
22652264
assert_eq!(opt_self_ty, None);
22662265
self.prohibit_generics(path.segments);
22672266

2268-
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
2269-
let item_id = tcx.hir().get_parent_node(hir_id);
2270-
let item_def_id = tcx.hir().local_def_id(item_id);
2267+
let def_id = def_id.expect_local();
2268+
let item_def_id = tcx.hir().ty_param_owner(def_id);
22712269
let generics = tcx.generics_of(item_def_id);
2272-
let index = generics.param_def_id_to_index[&def_id];
2273-
tcx.mk_ty_param(index, tcx.hir().name(hir_id))
2270+
let index = generics.param_def_id_to_index[&def_id.to_def_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.

Diff for: 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
}

Diff for: compiler/rustc_typeck/src/check/fn_ctxt/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,7 @@ impl<'a, 'tcx> AstConv<'tcx> for FnCtxt<'a, 'tcx> {
184184
_: Ident,
185185
) -> ty::GenericPredicates<'tcx> {
186186
let tcx = self.tcx;
187-
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
188-
let item_def_id = tcx.hir().ty_param_owner(hir_id);
187+
let item_def_id = tcx.hir().ty_param_owner(def_id.expect_local());
189188
let generics = tcx.generics_of(item_def_id);
190189
let index = generics.param_def_id_to_index[&def_id];
191190
ty::GenericPredicates {

Diff for: compiler/rustc_typeck/src/collect.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -558,10 +558,10 @@ fn type_param_predicates(
558558
// `where T: Foo`.
559559

560560
let param_id = tcx.hir().local_def_id_to_hir_id(def_id);
561-
let param_owner = tcx.hir().ty_param_owner(param_id);
561+
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() {

Diff for: src/test/ui/const-generics/occurs-check/unused-substs-1.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ LL | let _ = A;
88
note: required by a bound in `A`
99
--> $DIR/unused-substs-1.rs:9:11
1010
|
11+
LL | struct A<const N: usize>
12+
| - required by a bound in this
13+
LL | where
1114
LL | A<N>: Bar<N>;
1215
| ^^^^^^ required by this bound in `A`
1316

0 commit comments

Comments
 (0)