Skip to content

Commit 9ae2546

Browse files
committed
Only keep a single well-formed query.
1 parent 21e9336 commit 9ae2546

27 files changed

+209
-430
lines changed

compiler/rustc_hir/src/intravisit.rs

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
//! example generator inference, and possibly also HIR borrowck.
6666
6767
use crate::hir::*;
68-
use crate::itemlikevisit::ParItemLikeVisitor;
6968
use rustc_ast::walk_list;
7069
use rustc_ast::{Attribute, Label};
7170
use rustc_span::symbol::{Ident, Symbol};
@@ -76,29 +75,6 @@ pub trait IntoVisitor<'hir> {
7675
fn into_visitor(&self) -> Self::Visitor;
7776
}
7877

79-
pub struct ParDeepVisitor<V>(pub V);
80-
81-
impl<'hir, V> ParItemLikeVisitor<'hir> for ParDeepVisitor<V>
82-
where
83-
V: IntoVisitor<'hir>,
84-
{
85-
fn visit_item(&self, item: &'hir Item<'hir>) {
86-
self.0.into_visitor().visit_item(item);
87-
}
88-
89-
fn visit_trait_item(&self, trait_item: &'hir TraitItem<'hir>) {
90-
self.0.into_visitor().visit_trait_item(trait_item);
91-
}
92-
93-
fn visit_impl_item(&self, impl_item: &'hir ImplItem<'hir>) {
94-
self.0.into_visitor().visit_impl_item(impl_item);
95-
}
96-
97-
fn visit_foreign_item(&self, foreign_item: &'hir ForeignItem<'hir>) {
98-
self.0.into_visitor().visit_foreign_item(foreign_item);
99-
}
100-
}
101-
10278
#[derive(Copy, Clone, Debug)]
10379
pub enum FnKind<'a> {
10480
/// `#[xxx] pub async/const/extern "Abi" fn foo()`

compiler/rustc_hir/src/itemlikevisit.rs

Lines changed: 0 additions & 9 deletions
This file was deleted.

compiler/rustc_hir/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ pub use rustc_span::def_id;
2727
mod hir;
2828
pub mod hir_id;
2929
pub mod intravisit;
30-
pub mod itemlikevisit;
3130
pub mod lang_items;
3231
pub mod pat_util;
3332
mod stable_hash_impls;

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

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -612,23 +612,6 @@ impl<'hir> Map<'hir> {
612612
}
613613
}
614614

615-
/// A parallel version of `visit_all_item_likes`.
616-
pub fn par_visit_all_item_likes<V>(self, visitor: &V)
617-
where
618-
V: itemlikevisit::ParItemLikeVisitor<'hir> + Sync + Send,
619-
{
620-
let krate = self.krate();
621-
par_for_each_in(&krate.owners.raw, |owner| match owner.map(OwnerInfo::node) {
622-
MaybeOwner::Owner(OwnerNode::Item(item)) => visitor.visit_item(item),
623-
MaybeOwner::Owner(OwnerNode::ForeignItem(item)) => visitor.visit_foreign_item(item),
624-
MaybeOwner::Owner(OwnerNode::ImplItem(item)) => visitor.visit_impl_item(item),
625-
MaybeOwner::Owner(OwnerNode::TraitItem(item)) => visitor.visit_trait_item(item),
626-
MaybeOwner::Owner(OwnerNode::Crate(_))
627-
| MaybeOwner::NonOwner(_)
628-
| MaybeOwner::Phantom => {}
629-
})
630-
}
631-
632615
/// If you don't care about nesting, you should use the
633616
/// `tcx.hir_module_items()` query or `module_items()` instead.
634617
/// Please see notes in `deep_visit_all_item_likes`.
@@ -867,6 +850,10 @@ impl<'hir> Map<'hir> {
867850
)
868851
}
869852

853+
pub fn expect_owner(self, id: LocalDefId) -> OwnerNode<'hir> {
854+
self.tcx.hir_owner(id).unwrap_or_else(|| bug!("expected owner for {:?}", id)).node
855+
}
856+
870857
pub fn expect_item(self, id: LocalDefId) -> &'hir Item<'hir> {
871858
match self.tcx.hir_owner(id) {
872859
Some(Owner { node: OwnerNode::Item(item), .. }) => item,

compiler/rustc_middle/src/query/mod.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,13 +1398,7 @@ rustc_queries! {
13981398
separate_provide_extern
13991399
}
14001400

1401-
query check_item_well_formed(key: LocalDefId) -> () {
1402-
desc { |tcx| "checking that `{}` is well-formed", tcx.def_path_str(key.to_def_id()) }
1403-
}
1404-
query check_trait_item_well_formed(key: LocalDefId) -> () {
1405-
desc { |tcx| "checking that `{}` is well-formed", tcx.def_path_str(key.to_def_id()) }
1406-
}
1407-
query check_impl_item_well_formed(key: LocalDefId) -> () {
1401+
query check_well_formed(key: LocalDefId) -> () {
14081402
desc { |tcx| "checking that `{}` is well-formed", tcx.def_path_str(key.to_def_id()) }
14091403
}
14101404

compiler/rustc_typeck/src/check/check.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@ use rustc_ty_utils::representability::{self, Representability};
3232
use std::iter;
3333
use std::ops::ControlFlow;
3434

35-
pub fn check_wf_new(tcx: TyCtxt<'_>) {
36-
let visit = wfcheck::CheckTypeWellFormedVisitor::new(tcx);
37-
tcx.hir().par_visit_all_item_likes(&visit);
38-
}
39-
4035
pub(super) fn check_abi(tcx: TyCtxt<'_>, hir_id: hir::HirId, span: Span, abi: Abi) {
4136
match tcx.sess.target.is_abi_supported(abi) {
4237
Some(true) => (),
@@ -749,7 +744,7 @@ fn check_opaque_meets_bounds<'tcx>(
749744
});
750745
}
751746

752-
pub fn check_item_type<'tcx>(tcx: TyCtxt<'tcx>, id: hir::ItemId) {
747+
fn check_item_type<'tcx>(tcx: TyCtxt<'tcx>, id: hir::ItemId) {
753748
debug!(
754749
"check_item_type(it.def_id={:?}, it.name={})",
755750
id.def_id,
@@ -1538,12 +1533,6 @@ pub(super) fn check_mod_item_types(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
15381533
}
15391534
}
15401535

1541-
pub(super) use wfcheck::check_item_well_formed;
1542-
1543-
pub(super) use wfcheck::check_trait_item as check_trait_item_well_formed;
1544-
1545-
pub(super) use wfcheck::check_impl_item as check_impl_item_well_formed;
1546-
15471536
fn async_opaque_type_cycle_error(tcx: TyCtxt<'_>, span: Span) -> ErrorGuaranteed {
15481537
struct_span_err!(tcx.sess, span, E0733, "recursion in an `async fn` requires boxing")
15491538
.span_label(span, "recursive `async fn`")

compiler/rustc_typeck/src/check/mod.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,14 @@ mod upvar;
9393
mod wfcheck;
9494
pub mod writeback;
9595

96-
use check::{
97-
check_abi, check_fn, check_impl_item_well_formed, check_item_well_formed, check_mod_item_types,
98-
check_trait_item_well_formed,
99-
};
100-
pub use check::{check_item_type, check_wf_new};
96+
use check::{check_abi, check_fn, check_mod_item_types};
10197
pub use diverges::Diverges;
10298
pub use expectation::Expectation;
10399
pub use fn_ctxt::*;
104100
use hir::def::CtorOf;
105101
pub use inherited::{Inherited, InheritedBuilder};
102+
use wfcheck::check_well_formed;
103+
pub(crate) use wfcheck::check_wf_new;
106104

107105
use crate::astconv::AstConv;
108106
use crate::check::gather_locals::GatherLocalsVisitor;
@@ -253,9 +251,7 @@ pub fn provide(providers: &mut Providers) {
253251
has_typeck_results,
254252
adt_destructor,
255253
used_trait_imports,
256-
check_item_well_formed,
257-
check_trait_item_well_formed,
258-
check_impl_item_well_formed,
254+
check_well_formed,
259255
check_mod_item_types,
260256
region_scope_tree,
261257
..*providers

compiler/rustc_typeck/src/check/wfcheck.rs

Lines changed: 53 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,21 @@ use crate::constrained_generic_params::{identify_constrained_generic_params, Par
44

55
use rustc_ast as ast;
66
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
7+
use rustc_data_structures::sync::par_for_each_in;
78
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, ErrorGuaranteed};
89
use rustc_hir as hir;
910
use rustc_hir::def_id::{DefId, LocalDefId};
10-
use rustc_hir::intravisit as hir_visit;
11-
use rustc_hir::intravisit::Visitor;
12-
use rustc_hir::itemlikevisit::ParItemLikeVisitor;
1311
use rustc_hir::lang_items::LangItem;
1412
use rustc_hir::ItemKind;
1513
use rustc_infer::infer::outlives::env::OutlivesEnvironment;
1614
use rustc_infer::infer::outlives::obligations::TypeOutlives;
1715
use rustc_infer::infer::region_constraints::GenericKind;
1816
use rustc_infer::infer::{self, InferCtxt, TyCtxtInferExt};
19-
use rustc_middle::hir::nested_filter;
2017
use rustc_middle::ty::subst::{GenericArgKind, InternalSubsts, Subst};
2118
use rustc_middle::ty::trait_def::TraitSpecializationKind;
2219
use rustc_middle::ty::{
23-
self, AdtKind, EarlyBinder, GenericParamDefKind, ToPredicate, Ty, TyCtxt, TypeFoldable,
24-
TypeSuperFoldable, TypeVisitor,
20+
self, AdtKind, DefIdTree, EarlyBinder, GenericParamDefKind, ToPredicate, Ty, TyCtxt,
21+
TypeFoldable, TypeSuperFoldable, TypeVisitor,
2522
};
2623
use rustc_session::parse::feature_err;
2724
use rustc_span::symbol::{sym, Ident, Symbol};
@@ -70,6 +67,23 @@ impl<'tcx> CheckWfFcxBuilder<'tcx> {
7067
}
7168
}
7269

70+
pub(crate) fn check_well_formed(tcx: TyCtxt<'_>, def_id: LocalDefId) {
71+
let node = tcx.hir().expect_owner(def_id);
72+
match node {
73+
hir::OwnerNode::Crate(_) => {}
74+
hir::OwnerNode::Item(item) => check_item(tcx, item),
75+
hir::OwnerNode::TraitItem(item) => check_trait_item(tcx, item),
76+
hir::OwnerNode::ImplItem(item) => check_impl_item(tcx, item),
77+
hir::OwnerNode::ForeignItem(item) => check_foreign_item(tcx, item),
78+
}
79+
80+
if let Some(generics) = node.generics() {
81+
for param in generics.params {
82+
check_param_wf(tcx, param)
83+
}
84+
}
85+
}
86+
7387
/// Checks that the field types (in a struct def'n) or argument types (in an enum def'n) are
7488
/// well-formed, meaning that they do not require any constraints not declared in the struct
7589
/// definition itself. For example, this definition would be illegal:
@@ -84,8 +98,8 @@ impl<'tcx> CheckWfFcxBuilder<'tcx> {
8498
/// not included it frequently leads to confusing errors in fn bodies. So it's better to check
8599
/// the types first.
86100
#[instrument(skip(tcx), level = "debug")]
87-
pub fn check_item_well_formed(tcx: TyCtxt<'_>, def_id: LocalDefId) {
88-
let item = tcx.hir().expect_item(def_id);
101+
fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) {
102+
let def_id = item.def_id;
89103

90104
debug!(
91105
?item.def_id,
@@ -156,20 +170,6 @@ pub fn check_item_well_formed(tcx: TyCtxt<'_>, def_id: LocalDefId) {
156170
hir::ItemKind::Const(ty, ..) => {
157171
check_item_type(tcx, item.def_id, ty.span, false);
158172
}
159-
hir::ItemKind::ForeignMod { items, .. } => {
160-
for it in items.iter() {
161-
let it = tcx.hir().foreign_item(it.id);
162-
match it.kind {
163-
hir::ForeignItemKind::Fn(decl, ..) => {
164-
check_item_fn(tcx, it.def_id, it.ident, it.span, decl)
165-
}
166-
hir::ForeignItemKind::Static(ty, ..) => {
167-
check_item_type(tcx, it.def_id, ty.span, true)
168-
}
169-
hir::ForeignItemKind::Type => (),
170-
}
171-
}
172-
}
173173
hir::ItemKind::Struct(ref struct_def, ref ast_generics) => {
174174
check_type_defn(tcx, item, false, |fcx| vec![fcx.non_enum_variant(struct_def)]);
175175

@@ -191,13 +191,31 @@ pub fn check_item_well_formed(tcx: TyCtxt<'_>, def_id: LocalDefId) {
191191
hir::ItemKind::TraitAlias(..) => {
192192
check_trait(tcx, item);
193193
}
194+
// `ForeignItem`s are handled separately.
195+
hir::ItemKind::ForeignMod { .. } => {}
194196
_ => {}
195197
}
196198
}
197199

198-
pub fn check_trait_item(tcx: TyCtxt<'_>, def_id: LocalDefId) {
199-
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
200-
let trait_item = tcx.hir().expect_trait_item(def_id);
200+
fn check_foreign_item(tcx: TyCtxt<'_>, item: &hir::ForeignItem<'_>) {
201+
let def_id = item.def_id;
202+
203+
debug!(
204+
?item.def_id,
205+
item.name = ? tcx.def_path_str(def_id.to_def_id())
206+
);
207+
208+
match item.kind {
209+
hir::ForeignItemKind::Fn(decl, ..) => {
210+
check_item_fn(tcx, item.def_id, item.ident, item.span, decl)
211+
}
212+
hir::ForeignItemKind::Static(ty, ..) => check_item_type(tcx, item.def_id, ty.span, true),
213+
hir::ForeignItemKind::Type => (),
214+
}
215+
}
216+
217+
fn check_trait_item(tcx: TyCtxt<'_>, trait_item: &hir::TraitItem<'_>) {
218+
let def_id = trait_item.def_id;
201219

202220
let (method_sig, span) = match trait_item.kind {
203221
hir::TraitItemKind::Fn(ref sig, _) => (Some(sig), trait_item.span),
@@ -207,7 +225,7 @@ pub fn check_trait_item(tcx: TyCtxt<'_>, def_id: LocalDefId) {
207225
check_object_unsafe_self_trait_by_name(tcx, trait_item);
208226
check_associated_item(tcx, trait_item.def_id, span, method_sig);
209227

210-
let encl_trait_def_id = tcx.hir().get_parent_item(hir_id);
228+
let encl_trait_def_id = tcx.local_parent(def_id);
211229
let encl_trait = tcx.hir().expect_item(encl_trait_def_id);
212230
let encl_trait_def_id = encl_trait.def_id.to_def_id();
213231
let fn_lang_item_name = if Some(encl_trait_def_id) == tcx.lang_items().fn_trait() {
@@ -783,8 +801,8 @@ fn check_object_unsafe_self_trait_by_name(tcx: TyCtxt<'_>, item: &hir::TraitItem
783801
}
784802
}
785803

786-
pub fn check_impl_item(tcx: TyCtxt<'_>, def_id: LocalDefId) {
787-
let impl_item = tcx.hir().expect_impl_item(def_id);
804+
fn check_impl_item(tcx: TyCtxt<'_>, impl_item: &hir::ImplItem<'_>) {
805+
let def_id = impl_item.def_id;
788806

789807
let (method_sig, span) = match impl_item.kind {
790808
hir::ImplItemKind::Fn(ref sig, _) => (Some(sig), impl_item.span),
@@ -793,7 +811,7 @@ pub fn check_impl_item(tcx: TyCtxt<'_>, def_id: LocalDefId) {
793811
_ => (None, impl_item.span),
794812
};
795813

796-
check_associated_item(tcx, impl_item.def_id, span, method_sig);
814+
check_associated_item(tcx, def_id, span, method_sig);
797815
}
798816

799817
fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) {
@@ -1840,67 +1858,12 @@ fn check_false_global_bounds(fcx: &FnCtxt<'_, '_>, mut span: Span, id: hir::HirI
18401858
fcx.select_all_obligations_or_error();
18411859
}
18421860

1843-
#[derive(Clone, Copy)]
1844-
pub struct CheckTypeWellFormedVisitor<'tcx> {
1845-
tcx: TyCtxt<'tcx>,
1846-
}
1847-
1848-
impl<'tcx> CheckTypeWellFormedVisitor<'tcx> {
1849-
pub fn new(tcx: TyCtxt<'tcx>) -> CheckTypeWellFormedVisitor<'tcx> {
1850-
CheckTypeWellFormedVisitor { tcx }
1851-
}
1852-
}
1853-
1854-
impl<'tcx> ParItemLikeVisitor<'tcx> for CheckTypeWellFormedVisitor<'tcx> {
1855-
fn visit_item(&self, i: &'tcx hir::Item<'tcx>) {
1856-
Visitor::visit_item(&mut self.clone(), i);
1857-
}
1858-
1859-
fn visit_trait_item(&self, trait_item: &'tcx hir::TraitItem<'tcx>) {
1860-
Visitor::visit_trait_item(&mut self.clone(), trait_item);
1861-
}
1862-
1863-
fn visit_impl_item(&self, impl_item: &'tcx hir::ImplItem<'tcx>) {
1864-
Visitor::visit_impl_item(&mut self.clone(), impl_item);
1865-
}
1866-
1867-
fn visit_foreign_item(&self, foreign_item: &'tcx hir::ForeignItem<'tcx>) {
1868-
Visitor::visit_foreign_item(&mut self.clone(), foreign_item)
1869-
}
1870-
}
1871-
1872-
impl<'tcx> Visitor<'tcx> for CheckTypeWellFormedVisitor<'tcx> {
1873-
type NestedFilter = nested_filter::OnlyBodies;
1874-
1875-
fn nested_visit_map(&mut self) -> Self::Map {
1876-
self.tcx.hir()
1877-
}
1878-
1879-
#[instrument(skip(self, i), level = "debug")]
1880-
fn visit_item(&mut self, i: &'tcx hir::Item<'tcx>) {
1881-
trace!(?i);
1882-
self.tcx.ensure().check_item_well_formed(i.def_id);
1883-
hir_visit::walk_item(self, i);
1884-
}
1885-
1886-
#[instrument(skip(self, trait_item), level = "debug")]
1887-
fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>) {
1888-
trace!(?trait_item);
1889-
self.tcx.ensure().check_trait_item_well_formed(trait_item.def_id);
1890-
hir_visit::walk_trait_item(self, trait_item);
1891-
}
1892-
1893-
#[instrument(skip(self, impl_item), level = "debug")]
1894-
fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) {
1895-
trace!(?impl_item);
1896-
self.tcx.ensure().check_impl_item_well_formed(impl_item.def_id);
1897-
hir_visit::walk_impl_item(self, impl_item);
1898-
}
1899-
1900-
fn visit_generic_param(&mut self, p: &'tcx hir::GenericParam<'tcx>) {
1901-
check_param_wf(self.tcx, p);
1902-
hir_visit::walk_generic_param(self, p);
1903-
}
1861+
pub(crate) fn check_wf_new(tcx: TyCtxt<'_>) {
1862+
let items = tcx.hir_crate_items(());
1863+
par_for_each_in(items.items(), |item| tcx.ensure().check_well_formed(item.def_id));
1864+
par_for_each_in(items.impl_items(), |item| tcx.ensure().check_well_formed(item.def_id));
1865+
par_for_each_in(items.trait_items(), |item| tcx.ensure().check_well_formed(item.def_id));
1866+
par_for_each_in(items.foreign_items(), |item| tcx.ensure().check_well_formed(item.def_id));
19041867
}
19051868

19061869
///////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)