Rollup merge of #122523 - compiler-errors:ensure-associated-types, r=oli-obk

Ensure RPITITs are created before def-id freezing

From the test:

```rust
// `ty::Error` in a trait ref will silence any missing item errors, but will also
// prevent the `associated_items` query from being called before def ids are frozen.
```

Essentially, the code that checks that `impl`s have all their items (`check_impl_items_against_trait`) is also (implicitly) responsible for fetching the `associated_items` query before, but since we early return here:
c2901f5435/compiler/rustc_hir_analysis/src/check/check.rs (L732-L737)
...that means that this never happens for trait refs that reference errors.

Fixes #122518
r? oli-obk
This commit is contained in:
Matthias Krüger 2024-03-15 10:14:55 +01:00 committed by GitHub
commit ee940f87fc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 24 additions and 0 deletions

View File

@ -595,12 +595,14 @@ fn convert_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) {
tcx.ensure().type_of(def_id); tcx.ensure().type_of(def_id);
tcx.ensure().impl_trait_header(def_id); tcx.ensure().impl_trait_header(def_id);
tcx.ensure().predicates_of(def_id); tcx.ensure().predicates_of(def_id);
tcx.ensure().associated_items(def_id);
} }
hir::ItemKind::Trait(..) => { hir::ItemKind::Trait(..) => {
tcx.ensure().generics_of(def_id); tcx.ensure().generics_of(def_id);
tcx.ensure().trait_def(def_id); tcx.ensure().trait_def(def_id);
tcx.at(it.span).super_predicates_of(def_id); tcx.at(it.span).super_predicates_of(def_id);
tcx.ensure().predicates_of(def_id); tcx.ensure().predicates_of(def_id);
tcx.ensure().associated_items(def_id);
} }
hir::ItemKind::TraitAlias(..) => { hir::ItemKind::TraitAlias(..) => {
tcx.ensure().generics_of(def_id); tcx.ensure().generics_of(def_id);

View File

@ -0,0 +1,13 @@
trait Iterable {
type Item;
fn iter(&self) -> impl Sized;
}
// `ty::Error` in a trait ref will silence any missing item errors, but will also
// prevent the `associated_items` query from being called before def ids are frozen.
impl Iterable for Missing {
//~^ ERROR cannot find type `Missing` in this scope
fn iter(&self) -> Self::Item {}
}
fn main() {}

View File

@ -0,0 +1,9 @@
error[E0412]: cannot find type `Missing` in this scope
--> $DIR/ensure-rpitits-are-created-before-freezing.rs:8:19
|
LL | impl Iterable for Missing {
| ^^^^^^^ not found in this scope
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0412`.