Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f6e5570

Browse files
committedMay 7, 2022
Auto merge of rust-lang#96531 - kckeiks:remove-item-like-visitor-from-rustc-typeck, r=cjgillot
Remove ItemLikeVisitor impls from rustc_typeck Issue rust-lang#95004 cc `@cjgillot`
2 parents 4799baa + 91ef3ba commit f6e5570

File tree

23 files changed

+504
-545
lines changed

23 files changed

+504
-545
lines changed
 

‎compiler/rustc_metadata/src/foreign_modules.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_session::cstore::ForeignModule;
66
crate fn collect(tcx: TyCtxt<'_>) -> Vec<ForeignModule> {
77
let mut modules = Vec::new();
88
for id in tcx.hir().items() {
9-
if !matches!(tcx.hir().def_kind(id.def_id), DefKind::ForeignMod) {
9+
if !matches!(tcx.def_kind(id.def_id), DefKind::ForeignMod) {
1010
continue;
1111
}
1212
let item = tcx.hir().item(id);

‎compiler/rustc_metadata/src/native_libs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ struct Collector<'tcx> {
3636

3737
impl<'tcx> Collector<'tcx> {
3838
fn process_item(&mut self, id: rustc_hir::ItemId) {
39-
if !matches!(self.tcx.hir().def_kind(id.def_id), DefKind::ForeignMod) {
39+
if !matches!(self.tcx.def_kind(id.def_id), DefKind::ForeignMod) {
4040
return;
4141
}
4242

‎compiler/rustc_metadata/src/rmeta/encoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1813,7 +1813,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
18131813
FxHashMap::default();
18141814

18151815
for id in tcx.hir().items() {
1816-
if matches!(tcx.hir().def_kind(id.def_id), DefKind::Impl) {
1816+
if matches!(tcx.def_kind(id.def_id), DefKind::Impl) {
18171817
if let Some(trait_ref) = tcx.impl_trait_ref(id.def_id.to_def_id()) {
18181818
let simplified_self_ty = fast_reject::simplify_type(
18191819
self.tcx,

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

-5
Original file line numberDiff line numberDiff line change
@@ -308,11 +308,6 @@ impl<'hir> Map<'hir> {
308308
Some(def_kind)
309309
}
310310

311-
pub fn def_kind(self, local_def_id: LocalDefId) -> DefKind {
312-
self.opt_def_kind(local_def_id)
313-
.unwrap_or_else(|| bug!("def_kind: unsupported node: {:?}", local_def_id))
314-
}
315-
316311
pub fn find_parent_node(self, id: HirId) -> Option<HirId> {
317312
if id.local_id == ItemLocalId::from_u32(0) {
318313
Some(self.tcx.hir_owner_parent(id.owner))

‎compiler/rustc_middle/src/ty/print/pretty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2676,7 +2676,7 @@ fn for_each_def(tcx: TyCtxt<'_>, mut collect_fn: impl for<'b> FnMut(&'b Ident, N
26762676
// Iterate all local crate items no matter where they are defined.
26772677
let hir = tcx.hir();
26782678
for id in hir.items() {
2679-
if matches!(hir.def_kind(id.def_id), DefKind::Use) {
2679+
if matches!(tcx.def_kind(id.def_id), DefKind::Use) {
26802680
continue;
26812681
}
26822682

‎compiler/rustc_monomorphize/src/collector.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1164,7 +1164,7 @@ struct RootCollector<'a, 'tcx> {
11641164

11651165
impl<'v> RootCollector<'_, 'v> {
11661166
fn process_item(&mut self, id: hir::ItemId) {
1167-
match self.tcx.hir().def_kind(id.def_id) {
1167+
match self.tcx.def_kind(id.def_id) {
11681168
DefKind::Enum | DefKind::Struct | DefKind::Union => {
11691169
let item = self.tcx.hir().item(id);
11701170
match item.kind {
@@ -1225,7 +1225,7 @@ impl<'v> RootCollector<'_, 'v> {
12251225
}
12261226

12271227
fn process_impl_item(&mut self, id: hir::ImplItemId) {
1228-
if matches!(self.tcx.hir().def_kind(id.def_id), DefKind::AssocFn) {
1228+
if matches!(self.tcx.def_kind(id.def_id), DefKind::AssocFn) {
12291229
self.push_if_root(id.def_id);
12301230
}
12311231
}

‎compiler/rustc_passes/src/lang_items.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,9 @@ fn get_lang_items(tcx: TyCtxt<'_>, (): ()) -> LanguageItems {
240240
let crate_items = tcx.hir_crate_items(());
241241

242242
for id in crate_items.items() {
243-
collector.check_for_lang(Target::from_def_kind(tcx.hir().def_kind(id.def_id)), id.hir_id());
243+
collector.check_for_lang(Target::from_def_kind(tcx.def_kind(id.def_id)), id.hir_id());
244244

245-
if matches!(tcx.hir().def_kind(id.def_id), DefKind::Enum) {
245+
if matches!(tcx.def_kind(id.def_id), DefKind::Enum) {
246246
let item = tcx.hir().item(id);
247247
if let hir::ItemKind::Enum(def, ..) = &item.kind {
248248
for variant in def.variants {

‎compiler/rustc_typeck/src/check/check.rs

+52-29
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use rustc_trait_selection::traits;
2727
use rustc_trait_selection::traits::error_reporting::InferCtxtExt as _;
2828
use rustc_ty_utils::representability::{self, Representability};
2929

30+
use rustc_hir::def::DefKind;
3031
use std::iter;
3132
use std::ops::ControlFlow;
3233

@@ -711,28 +712,35 @@ fn check_opaque_meets_bounds<'tcx>(
711712
});
712713
}
713714

714-
pub fn check_item_type<'tcx>(tcx: TyCtxt<'tcx>, it: &'tcx hir::Item<'tcx>) {
715+
pub fn check_item_type<'tcx>(tcx: TyCtxt<'tcx>, id: hir::ItemId) {
715716
debug!(
716717
"check_item_type(it.def_id={:?}, it.name={})",
717-
it.def_id,
718-
tcx.def_path_str(it.def_id.to_def_id())
718+
id.def_id,
719+
tcx.def_path_str(id.def_id.to_def_id())
719720
);
720721
let _indenter = indenter();
721-
match it.kind {
722-
// Consts can play a role in type-checking, so they are included here.
723-
hir::ItemKind::Static(..) => {
724-
tcx.ensure().typeck(it.def_id);
725-
maybe_check_static_with_link_section(tcx, it.def_id, it.span);
726-
check_static_inhabited(tcx, it.def_id, it.span);
722+
match tcx.def_kind(id.def_id) {
723+
DefKind::Static(..) => {
724+
tcx.ensure().typeck(id.def_id);
725+
maybe_check_static_with_link_section(tcx, id.def_id, tcx.def_span(id.def_id));
726+
check_static_inhabited(tcx, id.def_id, tcx.def_span(id.def_id));
727727
}
728-
hir::ItemKind::Const(..) => {
729-
tcx.ensure().typeck(it.def_id);
728+
DefKind::Const => {
729+
tcx.ensure().typeck(id.def_id);
730730
}
731-
hir::ItemKind::Enum(ref enum_definition, _) => {
732-
check_enum(tcx, it.span, &enum_definition.variants, it.def_id);
731+
DefKind::Enum => {
732+
let item = tcx.hir().item(id);
733+
let hir::ItemKind::Enum(ref enum_definition, _) = item.kind else {
734+
return;
735+
};
736+
check_enum(tcx, item.span, &enum_definition.variants, item.def_id);
733737
}
734-
hir::ItemKind::Fn(..) => {} // entirely within check_item_body
735-
hir::ItemKind::Impl(ref impl_) => {
738+
DefKind::Fn => {} // entirely within check_item_body
739+
DefKind::Impl => {
740+
let it = tcx.hir().item(id);
741+
let hir::ItemKind::Impl(ref impl_) = it.kind else {
742+
return;
743+
};
736744
debug!("ItemKind::Impl {} with id {:?}", it.ident, it.def_id);
737745
if let Some(impl_trait_ref) = tcx.impl_trait_ref(it.def_id) {
738746
check_impl_items_against_trait(
@@ -745,7 +753,11 @@ pub fn check_item_type<'tcx>(tcx: TyCtxt<'tcx>, it: &'tcx hir::Item<'tcx>) {
745753
check_on_unimplemented(tcx, it);
746754
}
747755
}
748-
hir::ItemKind::Trait(_, _, _, _, ref items) => {
756+
DefKind::Trait => {
757+
let it = tcx.hir().item(id);
758+
let hir::ItemKind::Trait(_, _, _, _, ref items) = it.kind else {
759+
return;
760+
};
749761
check_on_unimplemented(tcx, it);
750762

751763
for item in items.iter() {
@@ -771,28 +783,36 @@ pub fn check_item_type<'tcx>(tcx: TyCtxt<'tcx>, it: &'tcx hir::Item<'tcx>) {
771783
}
772784
}
773785
}
774-
hir::ItemKind::Struct(..) => {
775-
check_struct(tcx, it.def_id, it.span);
786+
DefKind::Struct => {
787+
check_struct(tcx, id.def_id, tcx.def_span(id.def_id));
776788
}
777-
hir::ItemKind::Union(..) => {
778-
check_union(tcx, it.def_id, it.span);
789+
DefKind::Union => {
790+
check_union(tcx, id.def_id, tcx.def_span(id.def_id));
779791
}
780-
hir::ItemKind::OpaqueTy(hir::OpaqueTy { origin, .. }) => {
792+
DefKind::OpaqueTy => {
793+
let item = tcx.hir().item(id);
794+
let hir::ItemKind::OpaqueTy(hir::OpaqueTy { origin, .. }) = item.kind else {
795+
return;
796+
};
781797
// HACK(jynelson): trying to infer the type of `impl trait` breaks documenting
782798
// `async-std` (and `pub async fn` in general).
783799
// Since rustdoc doesn't care about the concrete type behind `impl Trait`, just don't look at it!
784800
// See https://github.com/rust-lang/rust/issues/75100
785801
if !tcx.sess.opts.actually_rustdoc {
786-
let substs = InternalSubsts::identity_for_item(tcx, it.def_id.to_def_id());
787-
check_opaque(tcx, it.def_id, substs, it.span, &origin);
802+
let substs = InternalSubsts::identity_for_item(tcx, item.def_id.to_def_id());
803+
check_opaque(tcx, item.def_id, substs, item.span, &origin);
788804
}
789805
}
790-
hir::ItemKind::TyAlias(..) => {
791-
let pty_ty = tcx.type_of(it.def_id);
792-
let generics = tcx.generics_of(it.def_id);
806+
DefKind::TyAlias => {
807+
let pty_ty = tcx.type_of(id.def_id);
808+
let generics = tcx.generics_of(id.def_id);
793809
check_type_params_are_used(tcx, &generics, pty_ty);
794810
}
795-
hir::ItemKind::ForeignMod { abi, items } => {
811+
DefKind::ForeignMod => {
812+
let it = tcx.hir().item(id);
813+
let hir::ItemKind::ForeignMod { abi, items } = it.kind else {
814+
return;
815+
};
796816
check_abi(tcx, it.hir_id(), it.span, abi);
797817

798818
if abi == Abi::RustIntrinsic {
@@ -851,7 +871,7 @@ pub fn check_item_type<'tcx>(tcx: TyCtxt<'tcx>, it: &'tcx hir::Item<'tcx>) {
851871
}
852872
}
853873
}
854-
_ => { /* nothing to do */ }
874+
_ => {}
855875
}
856876
}
857877

@@ -1451,7 +1471,10 @@ pub(super) fn check_type_params_are_used<'tcx>(
14511471
}
14521472

14531473
pub(super) fn check_mod_item_types(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
1454-
tcx.hir().visit_item_likes_in_module(module_def_id, &mut CheckItemTypesVisitor { tcx });
1474+
let module = tcx.hir_module_items(module_def_id);
1475+
for id in module.items() {
1476+
check_item_type(tcx, id);
1477+
}
14551478
}
14561479

14571480
pub(super) use wfcheck::check_item_well_formed;

‎compiler/rustc_typeck/src/check/fn_ctxt/suggestions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
151151
}
152152
Some(Node::Ctor(hir::VariantData::Tuple(fields, _))) => {
153153
sugg_call = fields.iter().map(|_| "_").collect::<Vec<_>>().join(", ");
154-
match def_id.as_local().map(|def_id| hir.def_kind(def_id)) {
154+
match def_id.as_local().map(|def_id| self.tcx.def_kind(def_id)) {
155155
Some(DefKind::Ctor(hir::def::CtorOf::Variant, _)) => {
156156
msg = "instantiate this tuple variant";
157157
}

‎compiler/rustc_typeck/src/check/mod.rs

-14
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ use rustc_hir as hir;
111111
use rustc_hir::def::Res;
112112
use rustc_hir::def_id::{DefId, LocalDefId};
113113
use rustc_hir::intravisit::Visitor;
114-
use rustc_hir::itemlikevisit::ItemLikeVisitor;
115114
use rustc_hir::{HirIdMap, ImplicitSelfKind, Node};
116115
use rustc_index::bit_set::BitSet;
117116
use rustc_index::vec::Idx;
@@ -933,19 +932,6 @@ impl<'a, 'tcx> MaybeInProgressTables<'a, 'tcx> {
933932
}
934933
}
935934

936-
struct CheckItemTypesVisitor<'tcx> {
937-
tcx: TyCtxt<'tcx>,
938-
}
939-
940-
impl<'tcx> ItemLikeVisitor<'tcx> for CheckItemTypesVisitor<'tcx> {
941-
fn visit_item(&mut self, i: &'tcx hir::Item<'tcx>) {
942-
check_item_type(self.tcx, i);
943-
}
944-
fn visit_trait_item(&mut self, _: &'tcx hir::TraitItem<'tcx>) {}
945-
fn visit_impl_item(&mut self, _: &'tcx hir::ImplItem<'tcx>) {}
946-
fn visit_foreign_item(&mut self, _: &'tcx hir::ForeignItem<'tcx>) {}
947-
}
948-
949935
fn typeck_item_bodies(tcx: TyCtxt<'_>, (): ()) {
950936
tcx.hir().par_body_owners(|body_owner_def_id| tcx.ensure().typeck(body_owner_def_id));
951937
}

‎compiler/rustc_typeck/src/check_unused.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
1717
}
1818

1919
for id in tcx.hir().items() {
20-
if matches!(tcx.hir().def_kind(id.def_id), DefKind::Use) {
20+
if matches!(tcx.def_kind(id.def_id), DefKind::Use) {
2121
if tcx.visibility(id.def_id).is_public() {
2222
continue;
2323
}
@@ -101,7 +101,7 @@ fn unused_crates_lint(tcx: TyCtxt<'_>) {
101101
let mut crates_to_lint = vec![];
102102

103103
for id in tcx.hir().items() {
104-
if matches!(tcx.hir().def_kind(id.def_id), DefKind::ExternCrate) {
104+
if matches!(tcx.def_kind(id.def_id), DefKind::ExternCrate) {
105105
let item = tcx.hir().item(id);
106106
if let hir::ItemKind::ExternCrate(orig_name) = item.kind {
107107
crates_to_lint.push(ExternCrateToLint {
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Failed to load comments.