Skip to content

Commit 4e5d3e2

Browse files
committed
Retire hir::*ItemRef.
1 parent a37db46 commit 4e5d3e2

File tree

52 files changed

+246
-312
lines changed

Some content is hidden

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

52 files changed

+246
-312
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<'_> {
@@ -4404,32 +4404,6 @@ impl ItemKind<'_> {
44044404
}
44054405
}
44064406

4407-
/// A reference from an trait to one of its associated items. This
4408-
/// contains the item's id, naturally, but also the item's name and
4409-
/// some other high-level details (like whether it is an associated
4410-
/// type or method, and whether it is public). This allows other
4411-
/// passes to find the impl they want without loading the ID (which
4412-
/// means fewer edges in the incremental compilation graph).
4413-
#[derive(Debug, Clone, Copy, HashStable_Generic)]
4414-
pub struct TraitItemRef {
4415-
pub id: TraitItemId,
4416-
pub ident: Ident,
4417-
pub span: Span,
4418-
}
4419-
4420-
/// A reference from an impl to one of its associated items. This
4421-
/// contains the item's ID, naturally, but also the item's name and
4422-
/// some other high-level details (like whether it is an associated
4423-
/// type or method, and whether it is public). This allows other
4424-
/// passes to find the impl they want without loading the ID (which
4425-
/// means fewer edges in the incremental compilation graph).
4426-
#[derive(Debug, Clone, Copy, HashStable_Generic)]
4427-
pub struct ImplItemRef {
4428-
pub id: ImplItemId,
4429-
pub ident: Ident,
4430-
pub span: Span,
4431-
}
4432-
44334407
// The bodies for items are stored "out of line", in a separate
44344408
// hashmap in the `Crate`. Here we just record the hir-id of the item
44354409
// 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
@@ -847,11 +847,9 @@ fn adt_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::AdtDef<'_> {
847847
fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef {
848848
let item = tcx.hir_expect_item(def_id);
849849

850-
let (is_alias, is_auto, safety, items) = match item.kind {
851-
hir::ItemKind::Trait(is_auto, safety, .., items) => {
852-
(false, is_auto == hir::IsAuto::Yes, safety, items)
853-
}
854-
hir::ItemKind::TraitAlias(..) => (true, false, hir::Safety::Safe, &[][..]),
850+
let (is_alias, is_auto, safety) = match item.kind {
851+
hir::ItemKind::Trait(is_auto, safety, ..) => (false, is_auto == hir::IsAuto::Yes, safety),
852+
hir::ItemKind::TraitAlias(..) => (true, false, hir::Safety::Safe),
855853
_ => span_bug!(item.span, "trait_def_of_item invoked on non-trait"),
856854
};
857855

@@ -912,13 +910,16 @@ fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef {
912910
// functions in the trait with default implementations
913911
.and_then(|(list, attr_span)| {
914912
let errors = list.iter().filter_map(|ident| {
915-
let item = items.iter().find(|item| item.ident == *ident);
913+
let item = tcx
914+
.associated_items(def_id)
915+
.filter_by_name_unhygienic(ident.name)
916+
.find(|item| item.ident(tcx) == *ident);
916917

917918
match item {
918-
Some(item) if matches!(tcx.def_kind(item.id.owner_id), DefKind::AssocFn) => {
919-
if !tcx.defaultness(item.id.owner_id).has_value() {
919+
Some(item) if matches!(item.kind, ty::AssocKind::Fn { .. }) => {
920+
if !item.defaultness(tcx).has_value() {
920921
tcx.dcx().emit_err(errors::FunctionNotHaveDefaultImplementation {
921-
span: item.span,
922+
span: tcx.def_span(item.def_id),
922923
note_span: attr_span,
923924
});
924925

@@ -929,7 +930,7 @@ fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef {
929930
}
930931
Some(item) => {
931932
tcx.dcx().emit_err(errors::MustImplementNotFunction {
932-
span: item.span,
933+
span: tcx.def_span(item.def_id),
933934
span_note: errors::MustImplementNotFunctionSpanNote { span: attr_span },
934935
note: errors::MustImplementNotFunctionNote {},
935936
});

compiler/rustc_hir_pretty/src/lib.rs

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

731731
self.space();
732732
self.bopen(ib);
733-
for impl_item in items {
734-
self.ann.nested(self, Nested::ImplItem(impl_item.id));
733+
for &impl_item in items {
734+
self.ann.nested(self, Nested::ImplItem(impl_item));
735735
}
736736
self.bclose(item.span, cb);
737737
}
@@ -746,8 +746,8 @@ impl<'a> State<'a> {
746746
self.print_where_clause(generics);
747747
self.word(" ");
748748
self.bopen(ib);
749-
for trait_item in trait_items {
750-
self.ann.nested(self, Nested::TraitItem(trait_item.id));
749+
for &trait_item in trait_items {
750+
self.ann.nested(self, Nested::TraitItem(trait_item));
751751
}
752752
self.bclose(item.span, cb);
753753
}

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
@@ -1096,7 +1096,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
10961096
ItemKind::Trait(_, _, _, generics, _, items)
10971097
if generics.params.len() != 0
10981098
|| items.iter().any(|item| {
1099-
matches!(self.tcx.def_kind(item.id.owner_id), DefKind::AssocTy)
1099+
matches!(self.tcx.def_kind(item.owner_id), DefKind::AssocTy)
11001100
}) => {}
11011101
ItemKind::TyAlias(_, generics, _) if generics.params.len() != 0 => {}
11021102
_ => {

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
@@ -467,9 +467,9 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
467467
hir_visit::walk_trait_item(self, ti)
468468
}
469469

470-
fn visit_trait_item_ref(&mut self, ti: &'v hir::TraitItemRef) {
471-
self.record("TraitItemRef", Some(ti.id.hir_id()), ti);
472-
hir_visit::walk_trait_item_ref(self, ti)
470+
fn visit_trait_item_ref(&mut self, ti: &'v hir::TraitItemId) {
471+
self.record("TraitItemId", Some(ti.hir_id()), ti);
472+
hir_visit::walk_trait_item_ref(self, *ti)
473473
}
474474

475475
fn visit_impl_item(&mut self, ii: &'v hir::ImplItem<'v>) {
@@ -485,9 +485,9 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
485485
hir_visit::walk_foreign_item_ref(self, *fi)
486486
}
487487

488-
fn visit_impl_item_ref(&mut self, ii: &'v hir::ImplItemRef) {
489-
self.record("ImplItemRef", Some(ii.id.hir_id()), ii);
490-
hir_visit::walk_impl_item_ref(self, ii)
488+
fn visit_impl_item_ref(&mut self, ii: &'v hir::ImplItemId) {
489+
self.record("ImplItemId", Some(ii.hir_id()), ii);
490+
hir_visit::walk_impl_item_ref(self, *ii)
491491
}
492492

493493
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)