Skip to content

Commit

Permalink
Auto merge of rust-lang#98222 - cjgillot:single-wf, r=michaelwoerister
Browse files Browse the repository at this point in the history
Only keep a single query for well-formed checking

There are currently 3 queries to perform wf checks on different item-likes.  This complexity is not required.

This PR replaces the query by:
- one query per item;
- one query to invoke it for a whole module.

This allows to remove HIR `ParItemLikeVisitor`.
  • Loading branch information
bors committed Jun 28, 2022
2 parents 7f08d04 + f446bbc commit 5ffa8f6
Show file tree
Hide file tree
Showing 30 changed files with 241 additions and 442 deletions.
24 changes: 0 additions & 24 deletions compiler/rustc_hir/src/intravisit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
//! example generator inference, and possibly also HIR borrowck.

use crate::hir::*;
use crate::itemlikevisit::ParItemLikeVisitor;
use rustc_ast::walk_list;
use rustc_ast::{Attribute, Label};
use rustc_span::symbol::{Ident, Symbol};
Expand All @@ -76,29 +75,6 @@ pub trait IntoVisitor<'hir> {
fn into_visitor(&self) -> Self::Visitor;
}

pub struct ParDeepVisitor<V>(pub V);

impl<'hir, V> ParItemLikeVisitor<'hir> for ParDeepVisitor<V>
where
V: IntoVisitor<'hir>,
{
fn visit_item(&self, item: &'hir Item<'hir>) {
self.0.into_visitor().visit_item(item);
}

fn visit_trait_item(&self, trait_item: &'hir TraitItem<'hir>) {
self.0.into_visitor().visit_trait_item(trait_item);
}

fn visit_impl_item(&self, impl_item: &'hir ImplItem<'hir>) {
self.0.into_visitor().visit_impl_item(impl_item);
}

fn visit_foreign_item(&self, foreign_item: &'hir ForeignItem<'hir>) {
self.0.into_visitor().visit_foreign_item(foreign_item);
}
}

#[derive(Copy, Clone, Debug)]
pub enum FnKind<'a> {
/// `#[xxx] pub async/const/extern "Abi" fn foo()`
Expand Down
9 changes: 0 additions & 9 deletions compiler/rustc_hir/src/itemlikevisit.rs

This file was deleted.

1 change: 0 additions & 1 deletion compiler/rustc_hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ pub use rustc_span::def_id;
mod hir;
pub mod hir_id;
pub mod intravisit;
pub mod itemlikevisit;
pub mod lang_items;
pub mod pat_util;
mod stable_hash_impls;
Expand Down
21 changes: 4 additions & 17 deletions compiler/rustc_middle/src/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,23 +612,6 @@ impl<'hir> Map<'hir> {
}
}

/// A parallel version of `visit_all_item_likes`.
pub fn par_visit_all_item_likes<V>(self, visitor: &V)
where
V: itemlikevisit::ParItemLikeVisitor<'hir> + Sync + Send,
{
let krate = self.krate();
par_for_each_in(&krate.owners.raw, |owner| match owner.map(OwnerInfo::node) {
MaybeOwner::Owner(OwnerNode::Item(item)) => visitor.visit_item(item),
MaybeOwner::Owner(OwnerNode::ForeignItem(item)) => visitor.visit_foreign_item(item),
MaybeOwner::Owner(OwnerNode::ImplItem(item)) => visitor.visit_impl_item(item),
MaybeOwner::Owner(OwnerNode::TraitItem(item)) => visitor.visit_trait_item(item),
MaybeOwner::Owner(OwnerNode::Crate(_))
| MaybeOwner::NonOwner(_)
| MaybeOwner::Phantom => {}
})
}

/// If you don't care about nesting, you should use the
/// `tcx.hir_module_items()` query or `module_items()` instead.
/// Please see notes in `deep_visit_all_item_likes`.
Expand Down Expand Up @@ -867,6 +850,10 @@ impl<'hir> Map<'hir> {
)
}

pub fn expect_owner(self, id: LocalDefId) -> OwnerNode<'hir> {
self.tcx.hir_owner(id).unwrap_or_else(|| bug!("expected owner for {:?}", id)).node
}

pub fn expect_item(self, id: LocalDefId) -> &'hir Item<'hir> {
match self.tcx.hir_owner(id) {
Some(Owner { node: OwnerNode::Item(item), .. }) => item,
Expand Down
17 changes: 17 additions & 0 deletions compiler/rustc_middle/src/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::ty::query::Providers;
use crate::ty::{DefIdTree, ImplSubject, TyCtxt};
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_data_structures::sync::{par_for_each_in, Send, Sync};
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::*;
use rustc_query_system::ich::StableHashingContext;
Expand Down Expand Up @@ -61,6 +62,22 @@ impl ModuleItems {
pub fn foreign_items(&self) -> impl Iterator<Item = ForeignItemId> + '_ {
self.foreign_items.iter().copied()
}

pub fn par_items(&self, f: impl Fn(ItemId) + Send + Sync) {
par_for_each_in(&self.items[..], |&id| f(id))
}

pub fn par_trait_items(&self, f: impl Fn(TraitItemId) + Send + Sync) {
par_for_each_in(&self.trait_items[..], |&id| f(id))
}

pub fn par_impl_items(&self, f: impl Fn(ImplItemId) + Send + Sync) {
par_for_each_in(&self.impl_items[..], |&id| f(id))
}

pub fn par_foreign_items(&self, f: impl Fn(ForeignItemId) + Send + Sync) {
par_for_each_in(&self.foreign_items[..], |&id| f(id))
}
}

impl<'tcx> TyCtxt<'tcx> {
Expand Down
12 changes: 5 additions & 7 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,10 @@ rustc_queries! {
desc { |tcx| "checking that impls are well-formed in {}", describe_as_module(key, tcx) }
}

query check_mod_type_wf(key: LocalDefId) -> () {
desc { |tcx| "checking that types are well-formed in {}", describe_as_module(key, tcx) }
}

query collect_mod_item_types(key: LocalDefId) -> () {
desc { |tcx| "collecting item types in {}", describe_as_module(key, tcx) }
}
Expand Down Expand Up @@ -1399,13 +1403,7 @@ rustc_queries! {
separate_provide_extern
}

query check_item_well_formed(key: LocalDefId) -> () {
desc { |tcx| "checking that `{}` is well-formed", tcx.def_path_str(key.to_def_id()) }
}
query check_trait_item_well_formed(key: LocalDefId) -> () {
desc { |tcx| "checking that `{}` is well-formed", tcx.def_path_str(key.to_def_id()) }
}
query check_impl_item_well_formed(key: LocalDefId) -> () {
query check_well_formed(key: LocalDefId) -> () {
desc { |tcx| "checking that `{}` is well-formed", tcx.def_path_str(key.to_def_id()) }
}

Expand Down
13 changes: 1 addition & 12 deletions compiler/rustc_typeck/src/check/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ use rustc_ty_utils::representability::{self, Representability};
use std::iter;
use std::ops::ControlFlow;

pub fn check_wf_new(tcx: TyCtxt<'_>) {
let visit = wfcheck::CheckTypeWellFormedVisitor::new(tcx);
tcx.hir().par_visit_all_item_likes(&visit);
}

pub(super) fn check_abi(tcx: TyCtxt<'_>, hir_id: hir::HirId, span: Span, abi: Abi) {
match tcx.sess.target.is_abi_supported(abi) {
Some(true) => (),
Expand Down Expand Up @@ -754,7 +749,7 @@ fn check_opaque_meets_bounds<'tcx>(
});
}

pub fn check_item_type<'tcx>(tcx: TyCtxt<'tcx>, id: hir::ItemId) {
fn check_item_type<'tcx>(tcx: TyCtxt<'tcx>, id: hir::ItemId) {
debug!(
"check_item_type(it.def_id={:?}, it.name={})",
id.def_id,
Expand Down Expand Up @@ -1543,12 +1538,6 @@ pub(super) fn check_mod_item_types(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
}
}

pub(super) use wfcheck::check_item_well_formed;

pub(super) use wfcheck::check_trait_item as check_trait_item_well_formed;

pub(super) use wfcheck::check_impl_item as check_impl_item_well_formed;

fn async_opaque_type_cycle_error(tcx: TyCtxt<'_>, span: Span) -> ErrorGuaranteed {
struct_span_err!(tcx.sess, span, E0733, "recursion in an `async fn` requires boxing")
.span_label(span, "recursive `async fn`")
Expand Down
10 changes: 2 additions & 8 deletions compiler/rustc_typeck/src/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,7 @@ mod upvar;
mod wfcheck;
pub mod writeback;

use check::{
check_abi, check_fn, check_impl_item_well_formed, check_item_well_formed, check_mod_item_types,
check_trait_item_well_formed,
};
pub use check::{check_item_type, check_wf_new};
use check::{check_abi, check_fn, check_mod_item_types};
pub use diverges::Diverges;
pub use expectation::Expectation;
pub use fn_ctxt::*;
Expand Down Expand Up @@ -245,6 +241,7 @@ impl<'tcx> EnclosingBreakables<'tcx> {

pub fn provide(providers: &mut Providers) {
method::provide(providers);
wfcheck::provide(providers);
*providers = Providers {
typeck_item_bodies,
typeck_const_arg,
Expand All @@ -253,9 +250,6 @@ pub fn provide(providers: &mut Providers) {
has_typeck_results,
adt_destructor,
used_trait_imports,
check_item_well_formed,
check_trait_item_well_formed,
check_impl_item_well_formed,
check_mod_item_types,
region_scope_tree,
..*providers
Expand Down
Loading

0 comments on commit 5ffa8f6

Please sign in to comment.