Skip to content

Commit 1264eb5

Browse files
committed
Retire hir::*ItemRef.
1 parent 4cc1973 commit 1264eb5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+219
-278
lines changed

compiler/rustc_ast_lowering/src/index.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -381,20 +381,12 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
381381
})
382382
}
383383

384-
fn visit_trait_item_ref(&mut self, ii: &'hir TraitItemRef) {
385-
// Do not visit the duplicate information in TraitItemRef. We want to
386-
// map the actual nodes, not the duplicate ones in the *Ref.
387-
let TraitItemRef { id, ident: _, span: _ } = *ii;
388-
389-
self.visit_nested_trait_item(id);
384+
fn visit_trait_item_ref(&mut self, id: &'hir TraitItemId) {
385+
self.visit_nested_trait_item(*id);
390386
}
391387

392-
fn visit_impl_item_ref(&mut self, ii: &'hir ImplItemRef) {
393-
// Do not visit the duplicate information in ImplItemRef. We want to
394-
// map the actual nodes, not the duplicate ones in the *Ref.
395-
let ImplItemRef { id, ident: _, span: _ } = *ii;
396-
397-
self.visit_nested_impl_item(id);
388+
fn visit_impl_item_ref(&mut self, id: &'hir ImplItemId) {
389+
self.visit_nested_impl_item(*id);
398390
}
399391

400392
fn visit_foreign_item_ref(&mut self, id: &'hir ForeignItemId) {

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -963,13 +963,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
963963
self.arena.alloc(item)
964964
}
965965

966-
fn lower_trait_item_ref(&mut self, i: &AssocItem) -> hir::TraitItemRef {
967-
let id = hir::TraitItemId { owner_id: self.owner_id(i.id) };
968-
hir::TraitItemRef {
969-
id,
970-
ident: self.lower_ident(i.kind.ident().unwrap()),
971-
span: self.lower_span(i.span),
972-
}
966+
fn lower_trait_item_ref(&mut self, i: &AssocItem) -> hir::TraitItemId {
967+
hir::TraitItemId { owner_id: self.owner_id(i.id) }
973968
}
974969

975970
/// Construct `ExprKind::Err` for the given `span`.
@@ -1109,14 +1104,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
11091104
self.arena.alloc(item)
11101105
}
11111106

1112-
fn lower_impl_item_ref(&mut self, i: &AssocItem) -> hir::ImplItemRef {
1113-
hir::ImplItemRef {
1114-
id: hir::ImplItemId { owner_id: self.owner_id(i.id) },
1115-
// `unwrap` is safe because `AssocItemKind::{MacCall,DelegationMac}` are the only
1116-
// assoc item kinds without an identifier and they cannot reach here.
1117-
ident: self.lower_ident(i.kind.ident().unwrap()),
1118-
span: self.lower_span(i.span),
1119-
}
1107+
fn lower_impl_item_ref(&mut self, i: &AssocItem) -> hir::ImplItemId {
1108+
hir::ImplItemId { owner_id: self.owner_id(i.id) }
11201109
}
11211110

11221111
fn lower_defaultness(

compiler/rustc_hir/src/hir.rs

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4161,7 +4161,7 @@ impl<'hir> Item<'hir> {
41614161
Ident,
41624162
&'hir Generics<'hir>,
41634163
GenericBounds<'hir>,
4164-
&'hir [TraitItemRef]
4164+
&'hir [TraitItemId]
41654165
),
41664166
ItemKind::Trait(is_auto, safety, ident, generics, bounds, items),
41674167
(*is_auto, *safety, *ident, generics, bounds, items);
@@ -4334,7 +4334,7 @@ pub enum ItemKind<'hir> {
43344334
/// A union definition, e.g., `union Foo<A, B> {x: A, y: B}`.
43354335
Union(Ident, &'hir Generics<'hir>, VariantData<'hir>),
43364336
/// A trait definition.
4337-
Trait(IsAuto, Safety, Ident, &'hir Generics<'hir>, GenericBounds<'hir>, &'hir [TraitItemRef]),
4337+
Trait(IsAuto, Safety, Ident, &'hir Generics<'hir>, GenericBounds<'hir>, &'hir [TraitItemId]),
43384338
/// A trait alias.
43394339
TraitAlias(Ident, &'hir Generics<'hir>, GenericBounds<'hir>),
43404340

@@ -4361,7 +4361,7 @@ pub struct Impl<'hir> {
43614361
pub of_trait: Option<TraitRef<'hir>>,
43624362

43634363
pub self_ty: &'hir Ty<'hir>,
4364-
pub items: &'hir [ImplItemRef],
4364+
pub items: &'hir [ImplItemId],
43654365
}
43664366

43674367
impl ItemKind<'_> {
@@ -4425,32 +4425,6 @@ impl ItemKind<'_> {
44254425
}
44264426
}
44274427

4428-
/// A reference from an trait to one of its associated items. This
4429-
/// contains the item's id, naturally, but also the item's name and
4430-
/// some other high-level details (like whether it is an associated
4431-
/// type or method, and whether it is public). This allows other
4432-
/// passes to find the impl they want without loading the ID (which
4433-
/// means fewer edges in the incremental compilation graph).
4434-
#[derive(Debug, Clone, Copy, HashStable_Generic)]
4435-
pub struct TraitItemRef {
4436-
pub id: TraitItemId,
4437-
pub ident: Ident,
4438-
pub span: Span,
4439-
}
4440-
4441-
/// A reference from an impl to one of its associated items. This
4442-
/// contains the item's ID, naturally, but also the item's name and
4443-
/// some other high-level details (like whether it is an associated
4444-
/// type or method, and whether it is public). This allows other
4445-
/// passes to find the impl they want without loading the ID (which
4446-
/// means fewer edges in the incremental compilation graph).
4447-
#[derive(Debug, Clone, Copy, HashStable_Generic)]
4448-
pub struct ImplItemRef {
4449-
pub id: ImplItemId,
4450-
pub ident: Ident,
4451-
pub span: Span,
4452-
}
4453-
44544428
// The bodies for items are stored "out of line", in a separate
44554429
// hashmap in the `Crate`. Here we just record the hir-id of the item
44564430
// so it can fetched later.

compiler/rustc_hir/src/intravisit.rs

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -435,17 +435,17 @@ pub trait Visitor<'v>: Sized {
435435
fn visit_trait_item(&mut self, ti: &'v TraitItem<'v>) -> Self::Result {
436436
walk_trait_item(self, ti)
437437
}
438-
fn visit_trait_item_ref(&mut self, ii: &'v TraitItemRef) -> Self::Result {
439-
walk_trait_item_ref(self, ii)
438+
fn visit_trait_item_ref(&mut self, ii: &'v TraitItemId) -> Self::Result {
439+
walk_trait_item_ref(self, *ii)
440440
}
441441
fn visit_impl_item(&mut self, ii: &'v ImplItem<'v>) -> Self::Result {
442442
walk_impl_item(self, ii)
443443
}
444444
fn visit_foreign_item_ref(&mut self, ii: &'v ForeignItemId) -> Self::Result {
445445
walk_foreign_item_ref(self, *ii)
446446
}
447-
fn visit_impl_item_ref(&mut self, ii: &'v ImplItemRef) -> Self::Result {
448-
walk_impl_item_ref(self, ii)
447+
fn visit_impl_item_ref(&mut self, ii: &'v ImplItemId) -> Self::Result {
448+
walk_impl_item_ref(self, *ii)
449449
}
450450
fn visit_trait_ref(&mut self, t: &'v TraitRef<'v>) -> Self::Result {
451451
walk_trait_ref(self, t)
@@ -1245,13 +1245,8 @@ pub fn walk_trait_item<'v, V: Visitor<'v>>(
12451245
V::Result::output()
12461246
}
12471247

1248-
pub fn walk_trait_item_ref<'v, V: Visitor<'v>>(
1249-
visitor: &mut V,
1250-
trait_item_ref: &'v TraitItemRef,
1251-
) -> V::Result {
1252-
let TraitItemRef { id, ident, span: _ } = *trait_item_ref;
1253-
try_visit!(visitor.visit_nested_trait_item(id));
1254-
visitor.visit_ident(ident)
1248+
pub fn walk_trait_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, id: TraitItemId) -> V::Result {
1249+
visitor.visit_nested_trait_item(id)
12551250
}
12561251

12571252
pub fn walk_impl_item<'v, V: Visitor<'v>>(
@@ -1294,13 +1289,8 @@ pub fn walk_foreign_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, id: ForeignIte
12941289
visitor.visit_nested_foreign_item(id)
12951290
}
12961291

1297-
pub fn walk_impl_item_ref<'v, V: Visitor<'v>>(
1298-
visitor: &mut V,
1299-
impl_item_ref: &'v ImplItemRef,
1300-
) -> V::Result {
1301-
let ImplItemRef { id, ident, span: _ } = *impl_item_ref;
1302-
try_visit!(visitor.visit_nested_impl_item(id));
1303-
visitor.visit_ident(ident)
1292+
pub fn walk_impl_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, id: ImplItemId) -> V::Result {
1293+
visitor.visit_nested_impl_item(id)
13041294
}
13051295

13061296
pub fn walk_trait_ref<'v, V: Visitor<'v>>(

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,11 +1125,9 @@ fn adt_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::AdtDef<'_> {
11251125
fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef {
11261126
let item = tcx.hir_expect_item(def_id);
11271127

1128-
let (is_alias, is_auto, safety, items) = match item.kind {
1129-
hir::ItemKind::Trait(is_auto, safety, .., items) => {
1130-
(false, is_auto == hir::IsAuto::Yes, safety, items)
1131-
}
1132-
hir::ItemKind::TraitAlias(..) => (true, false, hir::Safety::Safe, &[][..]),
1128+
let (is_alias, is_auto, safety) = match item.kind {
1129+
hir::ItemKind::Trait(is_auto, safety, ..) => (false, is_auto == hir::IsAuto::Yes, safety),
1130+
hir::ItemKind::TraitAlias(..) => (true, false, hir::Safety::Safe),
11331131
_ => span_bug!(item.span, "trait_def_of_item invoked on non-trait"),
11341132
};
11351133

@@ -1201,13 +1199,16 @@ fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef {
12011199
// functions in the trait with default implementations
12021200
.and_then(|(list, attr_span)| {
12031201
let errors = list.iter().filter_map(|ident| {
1204-
let item = items.iter().find(|item| item.ident == *ident);
1202+
let item = tcx
1203+
.associated_items(def_id)
1204+
.filter_by_name_unhygienic(ident.name)
1205+
.find(|item| item.ident(tcx) == *ident);
12051206

12061207
match item {
1207-
Some(item) if matches!(tcx.def_kind(item.id.owner_id), DefKind::AssocFn) => {
1208-
if !tcx.defaultness(item.id.owner_id).has_value() {
1208+
Some(item) if matches!(item.kind, ty::AssocKind::Fn { .. }) => {
1209+
if !item.defaultness(tcx).has_value() {
12091210
tcx.dcx().emit_err(errors::FunctionNotHaveDefaultImplementation {
1210-
span: item.span,
1211+
span: tcx.def_span(item.def_id),
12111212
note_span: attr_span,
12121213
});
12131214

@@ -1218,7 +1219,7 @@ fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef {
12181219
}
12191220
Some(item) => {
12201221
tcx.dcx().emit_err(errors::MustImplementNotFunction {
1221-
span: item.span,
1222+
span: tcx.def_span(item.def_id),
12221223
span_note: errors::MustImplementNotFunctionSpanNote { span: attr_span },
12231224
note: errors::MustImplementNotFunctionNote {},
12241225
});

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -731,8 +731,8 @@ impl<'a> State<'a> {
731731

732732
self.space();
733733
self.bopen(ib);
734-
for impl_item in items {
735-
self.ann.nested(self, Nested::ImplItem(impl_item.id));
734+
for &impl_item in items {
735+
self.ann.nested(self, Nested::ImplItem(impl_item));
736736
}
737737
self.bclose(item.span, cb);
738738
}
@@ -747,8 +747,8 @@ impl<'a> State<'a> {
747747
self.print_where_clause(generics);
748748
self.word(" ");
749749
self.bopen(ib);
750-
for trait_item in trait_items {
751-
self.ann.nested(self, Nested::TraitItem(trait_item.id));
750+
for &trait_item in trait_items {
751+
self.ann.nested(self, Nested::TraitItem(trait_item));
752752
}
753753
self.bclose(item.span, cb);
754754
}

compiler/rustc_lint/src/deref_into_dyn_supertrait.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_hir::{self as hir, LangItem};
22
use rustc_middle::ty;
33
use rustc_session::{declare_lint, declare_lint_pass};
4-
use rustc_span::sym;
4+
use rustc_span::{Ident, sym};
55
use rustc_trait_selection::traits::supertraits;
66

77
use crate::lints::{SupertraitAsDerefTarget, SupertraitAsDerefTargetLabel};
@@ -79,11 +79,15 @@ impl<'tcx> LateLintPass<'tcx> for DerefIntoDynSupertrait {
7979
// erase regions in self type for better diagnostic presentation
8080
let (self_ty, target_principal, supertrait_principal) =
8181
tcx.erase_regions((self_ty, target_principal, supertrait_principal));
82-
let label2 = impl_
83-
.items
84-
.iter()
85-
.find_map(|i| (i.ident.name == sym::Target).then_some(i.span))
86-
.map(|label| SupertraitAsDerefTargetLabel { label });
82+
let label2 = tcx
83+
.associated_items(item.owner_id)
84+
.find_by_ident_and_kind(
85+
tcx,
86+
Ident::with_dummy_span(sym::Target),
87+
ty::AssocTag::Type,
88+
item.owner_id.to_def_id(),
89+
)
90+
.map(|label| SupertraitAsDerefTargetLabel { label: tcx.def_span(label.def_id) });
8791
let span = tcx.def_span(item.owner_id.def_id);
8892
cx.emit_span_lint(
8993
DEREF_INTO_DYN_SUPERTRAIT,

compiler/rustc_passes/src/check_attr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1075,7 +1075,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
10751075
ItemKind::Trait(_, _, _, generics, _, items)
10761076
if generics.params.len() != 0
10771077
|| items.iter().any(|item| {
1078-
matches!(self.tcx.def_kind(item.id.owner_id), DefKind::AssocTy)
1078+
matches!(self.tcx.def_kind(item.owner_id), DefKind::AssocTy)
10791079
}) => {}
10801080
ItemKind::TyAlias(_, generics, _) if generics.params.len() != 0 => {}
10811081
_ => {

compiler/rustc_passes/src/dead.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,8 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
415415
hir::ItemKind::Trait(.., trait_item_refs) => {
416416
// mark assoc ty live if the trait is live
417417
for trait_item in trait_item_refs {
418-
if matches!(self.tcx.def_kind(trait_item.id.owner_id), DefKind::AssocTy) {
419-
self.check_def_id(trait_item.id.owner_id.to_def_id());
418+
if matches!(self.tcx.def_kind(trait_item.owner_id), DefKind::AssocTy) {
419+
self.check_def_id(trait_item.owner_id.to_def_id());
420420
}
421421
}
422422
intravisit::walk_item(self, item)

compiler/rustc_passes/src/input_stats.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -446,9 +446,9 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
446446
hir_visit::walk_trait_item(self, ti)
447447
}
448448

449-
fn visit_trait_item_ref(&mut self, ti: &'v hir::TraitItemRef) {
450-
self.record("TraitItemRef", Some(ti.id.hir_id()), ti);
451-
hir_visit::walk_trait_item_ref(self, ti)
449+
fn visit_trait_item_ref(&mut self, ti: &'v hir::TraitItemId) {
450+
self.record("TraitItemId", Some(ti.hir_id()), ti);
451+
hir_visit::walk_trait_item_ref(self, *ti)
452452
}
453453

454454
fn visit_impl_item(&mut self, ii: &'v hir::ImplItem<'v>) {
@@ -464,9 +464,9 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
464464
hir_visit::walk_foreign_item_ref(self, *fi)
465465
}
466466

467-
fn visit_impl_item_ref(&mut self, ii: &'v hir::ImplItemRef) {
468-
self.record("ImplItemRef", Some(ii.id.hir_id()), ii);
469-
hir_visit::walk_impl_item_ref(self, ii)
467+
fn visit_impl_item_ref(&mut self, ii: &'v hir::ImplItemId) {
468+
self.record("ImplItemId", Some(ii.hir_id()), ii);
469+
hir_visit::walk_impl_item_ref(self, *ii)
470470
}
471471

472472
fn visit_param_bound(&mut self, b: &'v hir::GenericBound<'v>) {

compiler/rustc_passes/src/stability.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -880,11 +880,16 @@ impl<'tcx> Visitor<'tcx> for Checker<'tcx> {
880880
}
881881

882882
for impl_item_ref in *items {
883-
let impl_item = self.tcx.associated_item(impl_item_ref.id.owner_id);
883+
let impl_item = self.tcx.associated_item(impl_item_ref.owner_id);
884884

885885
if let Some(def_id) = impl_item.trait_item_def_id {
886886
// Pass `None` to skip deprecation warnings.
887-
self.tcx.check_stability(def_id, None, impl_item_ref.span, None);
887+
self.tcx.check_stability(
888+
def_id,
889+
None,
890+
self.tcx.def_span(impl_item_ref.owner_id),
891+
None,
892+
);
888893
}
889894
}
890895
}

compiler/rustc_privacy/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -672,14 +672,14 @@ impl<'tcx> Visitor<'tcx> for EmbargoVisitor<'tcx> {
672672
self.reach(item.owner_id.def_id, item_ev).generics().predicates();
673673

674674
for trait_item_ref in trait_item_refs {
675-
self.update(trait_item_ref.id.owner_id.def_id, item_ev, Level::Reachable);
675+
self.update(trait_item_ref.owner_id.def_id, item_ev, Level::Reachable);
676676

677677
let tcx = self.tcx;
678-
let mut reach = self.reach(trait_item_ref.id.owner_id.def_id, item_ev);
678+
let mut reach = self.reach(trait_item_ref.owner_id.def_id, item_ev);
679679
reach.generics().predicates();
680680

681-
if let DefKind::AssocTy = tcx.def_kind(trait_item_ref.id.owner_id)
682-
&& !tcx.defaultness(trait_item_ref.id.owner_id).has_value()
681+
if let DefKind::AssocTy = tcx.def_kind(trait_item_ref.owner_id)
682+
&& !tcx.defaultness(trait_item_ref.owner_id).has_value()
683683
{
684684
// No type to visit.
685685
} else {
@@ -715,7 +715,7 @@ impl<'tcx> Visitor<'tcx> for EmbargoVisitor<'tcx> {
715715
self.reach(item.owner_id.def_id, item_ev).generics().predicates().ty().trait_ref();
716716

717717
for impl_item_ref in impl_.items {
718-
let def_id = impl_item_ref.id.owner_id.def_id;
718+
let def_id = impl_item_ref.owner_id.def_id;
719719
let max_vis =
720720
impl_.of_trait.is_none().then(|| self.tcx.local_visibility(def_id));
721721
self.update_eff_vis(def_id, item_ev, max_vis, Level::Direct);

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

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,8 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
355355
&& self.tcx.trait_of_item(*item_id) == Some(*trait_id)
356356
&& let None = self.tainted_by_errors()
357357
{
358-
let (verb, noun) = match self.tcx.associated_item(item_id).kind {
358+
let assoc_item = self.tcx.associated_item(item_id);
359+
let (verb, noun) = match assoc_item.kind {
359360
ty::AssocKind::Const { .. } => ("refer to the", "constant"),
360361
ty::AssocKind::Fn { .. } => ("call", "function"),
361362
// This is already covered by E0223, but this following single match
@@ -374,17 +375,10 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
374375
);
375376
err.code(E0790);
376377

377-
if let Some(local_def_id) = data.trait_ref.def_id.as_local()
378-
&& let hir::Node::Item(hir::Item {
379-
kind: hir::ItemKind::Trait(_, _, trait_ident, _, _, trait_item_refs),
380-
..
381-
}) = self.tcx.hir_node_by_def_id(local_def_id)
382-
&& let Some(method_ref) = trait_item_refs
383-
.iter()
384-
.find(|item_ref| item_ref.ident == *assoc_item_ident)
385-
{
378+
if item_id.is_local() {
379+
let trait_ident = self.tcx.item_name(*trait_id);
386380
err.span_label(
387-
method_ref.span,
381+
self.tcx.def_span(*item_id),
388382
format!("`{trait_ident}::{assoc_item_ident}` defined here"),
389383
);
390384
}

0 commit comments

Comments
 (0)