Skip to content

Commit ae50e36

Browse files
committed
Merge collect_mod_item_types query into check_well_formed
1 parent 42ab88d commit ae50e36

File tree

67 files changed

+1033
-1104
lines changed

Some content is hidden

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

67 files changed

+1033
-1104
lines changed

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+20-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use crate::autoderef::Autoderef;
2+
use crate::collect::CollectItemTypesVisitor;
23
use crate::constrained_generic_params::{identify_constrained_generic_params, Parameter};
34
use crate::errors;
45

6+
use hir::intravisit::Visitor;
57
use rustc_ast as ast;
68
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet};
79
use rustc_errors::{codes::*, pluralize, struct_span_code_err, Applicability, ErrorGuaranteed};
@@ -225,6 +227,7 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
225227
?item.owner_id,
226228
item.name = ? tcx.def_path_str(def_id)
227229
);
230+
CollectItemTypesVisitor { tcx }.visit_item(item);
228231

229232
let res = match item.kind {
230233
// Right now we check that every default trait implementation
@@ -336,9 +339,14 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
336339
res
337340
}
338341

339-
fn check_foreign_item(tcx: TyCtxt<'_>, item: &hir::ForeignItem<'_>) -> Result<(), ErrorGuaranteed> {
342+
fn check_foreign_item<'tcx>(
343+
tcx: TyCtxt<'tcx>,
344+
item: &'tcx hir::ForeignItem<'tcx>,
345+
) -> Result<(), ErrorGuaranteed> {
340346
let def_id = item.owner_id.def_id;
341347

348+
CollectItemTypesVisitor { tcx }.visit_foreign_item(item);
349+
342350
debug!(
343351
?item.owner_id,
344352
item.name = ? tcx.def_path_str(def_id)
@@ -355,12 +363,14 @@ fn check_foreign_item(tcx: TyCtxt<'_>, item: &hir::ForeignItem<'_>) -> Result<()
355363
}
356364
}
357365

358-
fn check_trait_item(
359-
tcx: TyCtxt<'_>,
360-
trait_item: &hir::TraitItem<'_>,
366+
fn check_trait_item<'tcx>(
367+
tcx: TyCtxt<'tcx>,
368+
trait_item: &'tcx hir::TraitItem<'tcx>,
361369
) -> Result<(), ErrorGuaranteed> {
362370
let def_id = trait_item.owner_id.def_id;
363371

372+
CollectItemTypesVisitor { tcx }.visit_trait_item(trait_item);
373+
364374
let (method_sig, span) = match trait_item.kind {
365375
hir::TraitItemKind::Fn(ref sig, _) => (Some(sig), trait_item.span),
366376
hir::TraitItemKind::Type(_bounds, Some(ty)) => (None, ty.span),
@@ -895,7 +905,12 @@ fn check_object_unsafe_self_trait_by_name(tcx: TyCtxt<'_>, item: &hir::TraitItem
895905
}
896906
}
897907

898-
fn check_impl_item(tcx: TyCtxt<'_>, impl_item: &hir::ImplItem<'_>) -> Result<(), ErrorGuaranteed> {
908+
fn check_impl_item<'tcx>(
909+
tcx: TyCtxt<'tcx>,
910+
impl_item: &'tcx hir::ImplItem<'tcx>,
911+
) -> Result<(), ErrorGuaranteed> {
912+
CollectItemTypesVisitor { tcx }.visit_impl_item(impl_item);
913+
899914
let (method_sig, span) = match impl_item.kind {
900915
hir::ImplItemKind::Fn(ref sig, _) => (Some(sig), impl_item.span),
901916
// Constrain binding and overflow error spans to `<Ty>` in `type foo = <Ty>`.

compiler/rustc_hir_analysis/src/collect.rs

+3-20
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc_data_structures::unord::UnordMap;
2020
use rustc_errors::{Applicability, Diag, ErrorGuaranteed, StashKey};
2121
use rustc_hir as hir;
2222
use rustc_hir::def::DefKind;
23-
use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId};
23+
use rustc_hir::def_id::{DefId, LocalDefId};
2424
use rustc_hir::intravisit::{self, Visitor};
2525
use rustc_hir::{GenericParamKind, Node};
2626
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
@@ -52,22 +52,6 @@ mod resolve_bound_vars;
5252
mod type_of;
5353

5454
///////////////////////////////////////////////////////////////////////////
55-
// Main entry point
56-
57-
fn collect_mod_item_types(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) {
58-
let items = tcx.hir_module_items(module_def_id);
59-
let hir = tcx.hir();
60-
let _ = items.par_items(|item| Ok(CollectItemTypesVisitor { tcx }.visit_item(hir.item(item))));
61-
let _ = items.par_impl_items(|item| {
62-
Ok(CollectItemTypesVisitor { tcx }.visit_impl_item(hir.impl_item(item)))
63-
});
64-
let _ = items.par_trait_items(|item| {
65-
Ok(CollectItemTypesVisitor { tcx }.visit_trait_item(hir.trait_item(item)))
66-
});
67-
let _ = items.par_foreign_items(|item| {
68-
Ok(CollectItemTypesVisitor { tcx }.visit_foreign_item(hir.foreign_item(item)))
69-
});
70-
}
7155

7256
pub fn provide(providers: &mut Providers) {
7357
resolve_bound_vars::provide(providers);
@@ -93,7 +77,6 @@ pub fn provide(providers: &mut Providers) {
9377
impl_trait_header,
9478
coroutine_kind,
9579
coroutine_for_closure,
96-
collect_mod_item_types,
9780
is_type_alias_impl_trait,
9881
find_field,
9982
..*providers
@@ -166,8 +149,8 @@ impl<'v> Visitor<'v> for HirPlaceholderCollector {
166149
}
167150
}
168151

169-
struct CollectItemTypesVisitor<'tcx> {
170-
tcx: TyCtxt<'tcx>,
152+
pub struct CollectItemTypesVisitor<'tcx> {
153+
pub tcx: TyCtxt<'tcx>,
171154
}
172155

173156
/// If there are any placeholder types (`_`), emit an error explaining that this is not allowed

compiler/rustc_hir_analysis/src/lib.rs

-6
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,6 @@ pub fn provide(providers: &mut Providers) {
159159
pub fn check_crate(tcx: TyCtxt<'_>) -> Result<(), ErrorGuaranteed> {
160160
let _prof_timer = tcx.sess.timer("type_check_crate");
161161

162-
// this ensures that later parts of type checking can assume that items
163-
// have valid types and not error
164-
tcx.sess.time("type_collecting", || {
165-
tcx.hir().for_each_module(|module| tcx.ensure().collect_mod_item_types(module))
166-
});
167-
168162
if tcx.features().rustc_attrs {
169163
tcx.sess.time("outlives_testing", || outlives::test::test_inferred_outlives(tcx))?;
170164
}

compiler/rustc_middle/src/query/mod.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,7 @@ rustc_queries! {
763763
desc { |tcx| "computing the variances of `{}`", tcx.def_path_str(def_id) }
764764
cache_on_disk_if { def_id.is_local() }
765765
separate_provide_extern
766+
cycle_delay_bug
766767
}
767768

768769
/// Maps from thee `DefId` of a type to its (inferred) outlives.
@@ -960,10 +961,6 @@ rustc_queries! {
960961
ensure_forwards_result_if_red
961962
}
962963

963-
query collect_mod_item_types(key: LocalModDefId) {
964-
desc { |tcx| "collecting item types in {}", describe_as_module(key, tcx) }
965-
}
966-
967964
/// Caches `CoerceUnsized` kinds for impls on custom types.
968965
query coerce_unsized_info(key: DefId) -> Result<ty::adjustment::CoerceUnsizedInfo, ErrorGuaranteed> {
969966
desc { |tcx| "computing CoerceUnsized info for `{}`", tcx.def_path_str(key) }

compiler/rustc_middle/src/values.rs

+21
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,27 @@ impl<'tcx> Value<TyCtxt<'tcx>> for ty::EarlyBinder<ty::Binder<'_, ty::FnSig<'_>>
131131
}
132132
}
133133

134+
impl<'tcx> Value<TyCtxt<'tcx>> for &[ty::Variance] {
135+
fn from_cycle_error(
136+
tcx: TyCtxt<'tcx>,
137+
cycle_error: &CycleError,
138+
_guar: ErrorGuaranteed,
139+
) -> Self {
140+
if let Some(frame) = cycle_error.cycle.get(0)
141+
&& frame.query.dep_kind == dep_kinds::variances_of
142+
&& let Some(def_id) = frame.query.def_id
143+
{
144+
let n = tcx.generics_of(def_id).params.len();
145+
vec![ty::Variance::Bivariant; n].leak()
146+
} else {
147+
span_bug!(
148+
cycle_error.usage.as_ref().unwrap().0,
149+
"only `variances_of` returns `&[ty::Variance]`"
150+
);
151+
}
152+
}
153+
}
154+
134155
// Take a cycle of `Q` and try `try_cycle` on every permutation, falling back to `otherwise`.
135156
fn search_for_cycle_permutation<Q, T>(
136157
cycle: &[Q],

src/librustdoc/core.rs

-6
Original file line numberDiff line numberDiff line change
@@ -314,12 +314,6 @@ pub(crate) fn run_global_ctxt(
314314
// typeck function bodies or run the default rustc lints.
315315
// (see `override_queries` in the `config`)
316316

317-
// HACK(jynelson) this calls an _extremely_ limited subset of `typeck`
318-
// and might break if queries change their assumptions in the future.
319-
tcx.sess.time("type_collecting", || {
320-
tcx.hir().for_each_module(|module| tcx.ensure().collect_mod_item_types(module))
321-
});
322-
323317
// NOTE: These are copy/pasted from typeck/lib.rs and should be kept in sync with those changes.
324318
let _ = tcx.sess.time("wf_checking", || {
325319
tcx.hir().try_par_for_each_module(|module| tcx.ensure().check_mod_type_wf(module))

tests/rustdoc-ui/issue-110629-private-type-cycle-dyn.stderr

+3-9
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,11 @@ LL | type Bar<'a, 'b> = Box<dyn PartialEq<Bar<'a, 'b>>>;
88
= note: type aliases cannot be recursive
99
= help: consider using a struct, enum, or union instead to break the cycle
1010
= help: see <https://doc.rust-lang.org/reference/types.html#recursive-types> for more information
11-
note: cycle used when collecting item types in top-level module
11+
note: cycle used when checking that `Bar` is well-formed
1212
--> $DIR/issue-110629-private-type-cycle-dyn.rs:1:1
1313
|
14-
LL | / type Bar<'a, 'b> = Box<dyn PartialEq<Bar<'a, 'b>>>;
15-
LL | |
16-
LL | |
17-
LL | | fn bar<'a, 'b>(i: &'a i32) -> Bar<'a, 'b> {
18-
... |
19-
LL | | assert!(bar(&meh) == bar(&muh));
20-
LL | | }
21-
| |_^
14+
LL | type Bar<'a, 'b> = Box<dyn PartialEq<Bar<'a, 'b>>>;
15+
| ^^^^^^^^^^^^^^^^
2216
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
2317

2418
error: aborting due to 1 previous error

tests/rustdoc-ui/issues/issue-105742.stderr

+32-32
Original file line numberDiff line numberDiff line change
@@ -464,38 +464,6 @@ help: add missing generic argument
464464
LL | Output = <Self as SVec>::Item> as SVec>::Item<T>,
465465
| +++
466466

467-
error[E0107]: missing generics for associated type `SVec::Item`
468-
--> $DIR/issue-105742.rs:61:38
469-
|
470-
LL | fn len(&self) -> <Self as SVec>::Item;
471-
| ^^^^ expected 1 lifetime argument
472-
|
473-
note: associated type defined here, with 1 lifetime parameter: `'a`
474-
--> $DIR/issue-105742.rs:59:10
475-
|
476-
LL | type Item<'a, T>;
477-
| ^^^^ --
478-
help: add missing lifetime argument
479-
|
480-
LL | fn len(&self) -> <Self as SVec>::Item<'_>;
481-
| ++++
482-
483-
error[E0107]: missing generics for associated type `SVec::Item`
484-
--> $DIR/issue-105742.rs:61:38
485-
|
486-
LL | fn len(&self) -> <Self as SVec>::Item;
487-
| ^^^^ expected 1 generic argument
488-
|
489-
note: associated type defined here, with 1 generic parameter: `T`
490-
--> $DIR/issue-105742.rs:59:10
491-
|
492-
LL | type Item<'a, T>;
493-
| ^^^^ -
494-
help: add missing generic argument
495-
|
496-
LL | fn len(&self) -> <Self as SVec>::Item<T>;
497-
| +++
498-
499467
error[E0107]: missing generics for associated type `SVec::Item`
500468
--> $DIR/issue-105742.rs:15:21
501469
|
@@ -632,6 +600,38 @@ help: add missing generic argument
632600
LL | Output = <Self as SVec>::Item> as SVec>::Item<T>,
633601
| +++
634602

603+
error[E0107]: missing generics for associated type `SVec::Item`
604+
--> $DIR/issue-105742.rs:61:38
605+
|
606+
LL | fn len(&self) -> <Self as SVec>::Item;
607+
| ^^^^ expected 1 lifetime argument
608+
|
609+
note: associated type defined here, with 1 lifetime parameter: `'a`
610+
--> $DIR/issue-105742.rs:59:10
611+
|
612+
LL | type Item<'a, T>;
613+
| ^^^^ --
614+
help: add missing lifetime argument
615+
|
616+
LL | fn len(&self) -> <Self as SVec>::Item<'_>;
617+
| ++++
618+
619+
error[E0107]: missing generics for associated type `SVec::Item`
620+
--> $DIR/issue-105742.rs:61:38
621+
|
622+
LL | fn len(&self) -> <Self as SVec>::Item;
623+
| ^^^^ expected 1 generic argument
624+
|
625+
note: associated type defined here, with 1 generic parameter: `T`
626+
--> $DIR/issue-105742.rs:59:10
627+
|
628+
LL | type Item<'a, T>;
629+
| ^^^^ -
630+
help: add missing generic argument
631+
|
632+
LL | fn len(&self) -> <Self as SVec>::Item<T>;
633+
| +++
634+
635635
error: aborting due to 37 previous errors
636636

637637
Some errors have detailed explanations: E0038, E0107.

tests/ui/associated-inherent-types/bugs/cycle-iat-inside-of-adt.stderr

+4-10
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,11 @@ note: ...which requires computing normalized predicates of `Foo`...
2626
LL | struct Foo {
2727
| ^^^^^^^^^^
2828
= note: ...which again requires computing predicates of `Foo`, completing the cycle
29-
note: cycle used when collecting item types in top-level module
30-
--> $DIR/cycle-iat-inside-of-adt.rs:3:1
29+
note: cycle used when checking that `Foo` is well-formed
30+
--> $DIR/cycle-iat-inside-of-adt.rs:7:1
3131
|
32-
LL | / #![feature(inherent_associated_types)]
33-
LL | | #![allow(incomplete_features)]
34-
LL | | // FIXME(inherent_associated_types): This should pass.
35-
LL | |
36-
... |
37-
LL | |
38-
LL | | fn main() {}
39-
| |____________^
32+
LL | struct Foo {
33+
| ^^^^^^^^^^
4034
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
4135

4236
error: aborting due to 1 previous error

tests/ui/associated-inherent-types/bugs/cycle-iat-inside-of-where-predicate.stderr

+17-13
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,23 @@ note: ...which requires computing normalized predicates of `user`...
2020
LL | fn user<T>() where S<T>::P: std::fmt::Debug {}
2121
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2222
= note: ...which again requires computing predicates of `user`, completing the cycle
23-
note: cycle used when collecting item types in top-level module
24-
--> $DIR/cycle-iat-inside-of-where-predicate.rs:3:1
25-
|
26-
LL | / #![feature(inherent_associated_types)]
27-
LL | | #![allow(incomplete_features)]
28-
LL | |
29-
LL | | // FIXME(inherent_associated_types): This shouldn't lead to a cycle error.
30-
... |
31-
LL | |
32-
LL | | fn main() {}
33-
| |____________^
23+
note: cycle used when checking that `user` is well-formed
24+
--> $DIR/cycle-iat-inside-of-where-predicate.rs:8:1
25+
|
26+
LL | fn user<T>() where S<T>::P: std::fmt::Debug {}
27+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3428
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
3529

36-
error: aborting due to 1 previous error
30+
error[E0392]: type parameter `T` is never used
31+
--> $DIR/cycle-iat-inside-of-where-predicate.rs:10:10
32+
|
33+
LL | struct S<T>;
34+
| ^ unused type parameter
35+
|
36+
= help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`
37+
= help: if you intended `T` to be a const parameter, use `const T: /* Type */` instead
38+
39+
error: aborting due to 2 previous errors
3740

38-
For more information about this error, try `rustc --explain E0391`.
41+
Some errors have detailed explanations: E0391, E0392.
42+
For more information about an error, try `rustc --explain E0391`.

tests/ui/associated-inherent-types/issue-109071.no_gate.stderr

+10-10
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,6 @@ help: add missing generic argument
2020
LL | impl<T> Windows<T> {
2121
| +++
2222

23-
error[E0658]: inherent associated types are unstable
24-
--> $DIR/issue-109071.rs:8:5
25-
|
26-
LL | type Item = &[T];
27-
| ^^^^^^^^^^^^^^^^^
28-
|
29-
= note: see issue #8995 <https://github.com/rust-lang/rust/issues/8995> for more information
30-
= help: add `#![feature(inherent_associated_types)]` to the crate attributes to enable
31-
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
32-
3323
error[E0223]: ambiguous associated type
3424
--> $DIR/issue-109071.rs:15:22
3525
|
@@ -43,6 +33,16 @@ LL | fn T() -> Option<<Windows<T> as IntoAsyncIterator>::Item> {}
4333
LL | fn T() -> Option<<Windows<T> as IntoIterator>::Item> {}
4434
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4535

36+
error[E0658]: inherent associated types are unstable
37+
--> $DIR/issue-109071.rs:8:5
38+
|
39+
LL | type Item = &[T];
40+
| ^^^^^^^^^^^^^^^^^
41+
|
42+
= note: see issue #8995 <https://github.com/rust-lang/rust/issues/8995> for more information
43+
= help: add `#![feature(inherent_associated_types)]` to the crate attributes to enable
44+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
45+
4646
error: aborting due to 4 previous errors
4747

4848
Some errors have detailed explanations: E0107, E0223, E0637, E0658.

0 commit comments

Comments
 (0)