Skip to content

Remove ItemLikeVisitor impls from rustc_typeck #96531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_metadata/src/foreign_modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rustc_session::cstore::ForeignModule;
crate fn collect(tcx: TyCtxt<'_>) -> Vec<ForeignModule> {
let mut modules = Vec::new();
for id in tcx.hir().items() {
if !matches!(tcx.hir().def_kind(id.def_id), DefKind::ForeignMod) {
if !matches!(tcx.def_kind(id.def_id), DefKind::ForeignMod) {
continue;
}
let item = tcx.hir().item(id);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_metadata/src/native_libs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct Collector<'tcx> {

impl<'tcx> Collector<'tcx> {
fn process_item(&mut self, id: rustc_hir::ItemId) {
if !matches!(self.tcx.hir().def_kind(id.def_id), DefKind::ForeignMod) {
if !matches!(self.tcx.def_kind(id.def_id), DefKind::ForeignMod) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1813,7 +1813,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
FxHashMap::default();

for id in tcx.hir().items() {
if matches!(tcx.hir().def_kind(id.def_id), DefKind::Impl) {
if matches!(tcx.def_kind(id.def_id), DefKind::Impl) {
if let Some(trait_ref) = tcx.impl_trait_ref(id.def_id.to_def_id()) {
let simplified_self_ty = fast_reject::simplify_type(
self.tcx,
Expand Down
5 changes: 0 additions & 5 deletions compiler/rustc_middle/src/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,6 @@ impl<'hir> Map<'hir> {
Some(def_kind)
}

pub fn def_kind(self, local_def_id: LocalDefId) -> DefKind {
self.opt_def_kind(local_def_id)
.unwrap_or_else(|| bug!("def_kind: unsupported node: {:?}", local_def_id))
}

pub fn find_parent_node(self, id: HirId) -> Option<HirId> {
if id.local_id == ItemLocalId::from_u32(0) {
Some(self.tcx.hir_owner_parent(id.owner))
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2676,7 +2676,7 @@ fn for_each_def(tcx: TyCtxt<'_>, mut collect_fn: impl for<'b> FnMut(&'b Ident, N
// Iterate all local crate items no matter where they are defined.
let hir = tcx.hir();
for id in hir.items() {
if matches!(hir.def_kind(id.def_id), DefKind::Use) {
if matches!(tcx.def_kind(id.def_id), DefKind::Use) {
continue;
}

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_monomorphize/src/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1167,7 +1167,7 @@ struct RootCollector<'a, 'tcx> {

impl<'v> RootCollector<'_, 'v> {
fn process_item(&mut self, id: hir::ItemId) {
match self.tcx.hir().def_kind(id.def_id) {
match self.tcx.def_kind(id.def_id) {
DefKind::Enum | DefKind::Struct | DefKind::Union => {
let item = self.tcx.hir().item(id);
match item.kind {
Expand Down Expand Up @@ -1228,7 +1228,7 @@ impl<'v> RootCollector<'_, 'v> {
}

fn process_impl_item(&mut self, id: hir::ImplItemId) {
if matches!(self.tcx.hir().def_kind(id.def_id), DefKind::AssocFn) {
if matches!(self.tcx.def_kind(id.def_id), DefKind::AssocFn) {
self.push_if_root(id.def_id);
}
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_passes/src/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,9 @@ fn get_lang_items(tcx: TyCtxt<'_>, (): ()) -> LanguageItems {
let crate_items = tcx.hir_crate_items(());

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

if matches!(tcx.hir().def_kind(id.def_id), DefKind::Enum) {
if matches!(tcx.def_kind(id.def_id), DefKind::Enum) {
let item = tcx.hir().item(id);
if let hir::ItemKind::Enum(def, ..) = &item.kind {
for variant in def.variants {
Expand Down
81 changes: 52 additions & 29 deletions compiler/rustc_typeck/src/check/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use rustc_trait_selection::traits;
use rustc_trait_selection::traits::error_reporting::InferCtxtExt as _;
use rustc_ty_utils::representability::{self, Representability};

use rustc_hir::def::DefKind;
use std::iter;
use std::ops::ControlFlow;

Expand Down Expand Up @@ -711,28 +712,35 @@ fn check_opaque_meets_bounds<'tcx>(
});
}

pub fn check_item_type<'tcx>(tcx: TyCtxt<'tcx>, it: &'tcx hir::Item<'tcx>) {
pub fn check_item_type<'tcx>(tcx: TyCtxt<'tcx>, id: hir::ItemId) {
debug!(
"check_item_type(it.def_id={:?}, it.name={})",
it.def_id,
tcx.def_path_str(it.def_id.to_def_id())
id.def_id,
tcx.def_path_str(id.def_id.to_def_id())
);
let _indenter = indenter();
match it.kind {
// Consts can play a role in type-checking, so they are included here.
hir::ItemKind::Static(..) => {
tcx.ensure().typeck(it.def_id);
maybe_check_static_with_link_section(tcx, it.def_id, it.span);
check_static_inhabited(tcx, it.def_id, it.span);
match tcx.def_kind(id.def_id) {
DefKind::Static(..) => {
tcx.ensure().typeck(id.def_id);
maybe_check_static_with_link_section(tcx, id.def_id, tcx.def_span(id.def_id));
check_static_inhabited(tcx, id.def_id, tcx.def_span(id.def_id));
}
hir::ItemKind::Const(..) => {
tcx.ensure().typeck(it.def_id);
DefKind::Const => {
tcx.ensure().typeck(id.def_id);
}
hir::ItemKind::Enum(ref enum_definition, _) => {
check_enum(tcx, it.span, &enum_definition.variants, it.def_id);
DefKind::Enum => {
let item = tcx.hir().item(id);
let hir::ItemKind::Enum(ref enum_definition, _) = item.kind else {
return;
};
check_enum(tcx, item.span, &enum_definition.variants, item.def_id);
}
hir::ItemKind::Fn(..) => {} // entirely within check_item_body
hir::ItemKind::Impl(ref impl_) => {
DefKind::Fn => {} // entirely within check_item_body
DefKind::Impl => {
let it = tcx.hir().item(id);
let hir::ItemKind::Impl(ref impl_) = it.kind else {
return;
};
debug!("ItemKind::Impl {} with id {:?}", it.ident, it.def_id);
if let Some(impl_trait_ref) = tcx.impl_trait_ref(it.def_id) {
check_impl_items_against_trait(
Expand All @@ -745,7 +753,11 @@ pub fn check_item_type<'tcx>(tcx: TyCtxt<'tcx>, it: &'tcx hir::Item<'tcx>) {
check_on_unimplemented(tcx, it);
}
}
hir::ItemKind::Trait(_, _, _, _, ref items) => {
DefKind::Trait => {
let it = tcx.hir().item(id);
let hir::ItemKind::Trait(_, _, _, _, ref items) = it.kind else {
return;
};
check_on_unimplemented(tcx, it);

for item in items.iter() {
Expand All @@ -771,28 +783,36 @@ pub fn check_item_type<'tcx>(tcx: TyCtxt<'tcx>, it: &'tcx hir::Item<'tcx>) {
}
}
}
hir::ItemKind::Struct(..) => {
check_struct(tcx, it.def_id, it.span);
DefKind::Struct => {
check_struct(tcx, id.def_id, tcx.def_span(id.def_id));
}
hir::ItemKind::Union(..) => {
check_union(tcx, it.def_id, it.span);
DefKind::Union => {
check_union(tcx, id.def_id, tcx.def_span(id.def_id));
}
hir::ItemKind::OpaqueTy(hir::OpaqueTy { origin, .. }) => {
DefKind::OpaqueTy => {
let item = tcx.hir().item(id);
let hir::ItemKind::OpaqueTy(hir::OpaqueTy { origin, .. }) = item.kind else {
return;
};
// HACK(jynelson): trying to infer the type of `impl trait` breaks documenting
// `async-std` (and `pub async fn` in general).
// Since rustdoc doesn't care about the concrete type behind `impl Trait`, just don't look at it!
// See https://github.com/rust-lang/rust/issues/75100
if !tcx.sess.opts.actually_rustdoc {
let substs = InternalSubsts::identity_for_item(tcx, it.def_id.to_def_id());
check_opaque(tcx, it.def_id, substs, it.span, &origin);
let substs = InternalSubsts::identity_for_item(tcx, item.def_id.to_def_id());
check_opaque(tcx, item.def_id, substs, item.span, &origin);
}
}
hir::ItemKind::TyAlias(..) => {
let pty_ty = tcx.type_of(it.def_id);
let generics = tcx.generics_of(it.def_id);
DefKind::TyAlias => {
let pty_ty = tcx.type_of(id.def_id);
let generics = tcx.generics_of(id.def_id);
check_type_params_are_used(tcx, &generics, pty_ty);
}
hir::ItemKind::ForeignMod { abi, items } => {
DefKind::ForeignMod => {
let it = tcx.hir().item(id);
let hir::ItemKind::ForeignMod { abi, items } = it.kind else {
return;
};
check_abi(tcx, it.hir_id(), it.span, abi);

if abi == Abi::RustIntrinsic {
Expand Down Expand Up @@ -851,7 +871,7 @@ pub fn check_item_type<'tcx>(tcx: TyCtxt<'tcx>, it: &'tcx hir::Item<'tcx>) {
}
}
}
_ => { /* nothing to do */ }
_ => {}
}
}

Expand Down Expand Up @@ -1451,7 +1471,10 @@ pub(super) fn check_type_params_are_used<'tcx>(
}

pub(super) fn check_mod_item_types(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
tcx.hir().visit_item_likes_in_module(module_def_id, &mut CheckItemTypesVisitor { tcx });
let module = tcx.hir_module_items(module_def_id);
for id in module.items() {
check_item_type(tcx, id);
}
}

pub(super) use wfcheck::check_item_well_formed;
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_typeck/src/check/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
Some(Node::Ctor(hir::VariantData::Tuple(fields, _))) => {
sugg_call = fields.iter().map(|_| "_").collect::<Vec<_>>().join(", ");
match def_id.as_local().map(|def_id| hir.def_kind(def_id)) {
match def_id.as_local().map(|def_id| self.tcx.def_kind(def_id)) {
Some(DefKind::Ctor(hir::def::CtorOf::Variant, _)) => {
msg = "instantiate this tuple variant";
}
Expand Down
14 changes: 0 additions & 14 deletions compiler/rustc_typeck/src/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ use rustc_hir as hir;
use rustc_hir::def::Res;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::intravisit::Visitor;
use rustc_hir::itemlikevisit::ItemLikeVisitor;
use rustc_hir::{HirIdMap, ImplicitSelfKind, Node};
use rustc_index::bit_set::BitSet;
use rustc_index::vec::Idx;
Expand Down Expand Up @@ -933,19 +932,6 @@ impl<'a, 'tcx> MaybeInProgressTables<'a, 'tcx> {
}
}

struct CheckItemTypesVisitor<'tcx> {
tcx: TyCtxt<'tcx>,
}

impl<'tcx> ItemLikeVisitor<'tcx> for CheckItemTypesVisitor<'tcx> {
fn visit_item(&mut self, i: &'tcx hir::Item<'tcx>) {
check_item_type(self.tcx, i);
}
fn visit_trait_item(&mut self, _: &'tcx hir::TraitItem<'tcx>) {}
fn visit_impl_item(&mut self, _: &'tcx hir::ImplItem<'tcx>) {}
fn visit_foreign_item(&mut self, _: &'tcx hir::ForeignItem<'tcx>) {}
}

fn typeck_item_bodies(tcx: TyCtxt<'_>, (): ()) {
tcx.hir().par_body_owners(|body_owner_def_id| tcx.ensure().typeck(body_owner_def_id));
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_typeck/src/check_unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
}

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

for id in tcx.hir().items() {
if matches!(tcx.hir().def_kind(id.def_id), DefKind::ExternCrate) {
if matches!(tcx.def_kind(id.def_id), DefKind::ExternCrate) {
let item = tcx.hir().item(id);
if let hir::ItemKind::ExternCrate(orig_name) = item.kind {
crates_to_lint.push(ExternCrateToLint {
Expand Down
Loading