Skip to content

Commit cb158c2

Browse files
committed
Merge lower_item into check_item_type
1 parent 632a921 commit cb158c2

File tree

10 files changed

+101
-143
lines changed

10 files changed

+101
-143
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 75 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -758,17 +758,34 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
758758
}
759759

760760
match tcx.def_kind(def_id) {
761-
DefKind::Static { .. } => {
762-
check_static_inhabited(tcx, def_id);
763-
check_static_linkage(tcx, def_id);
764-
wfcheck::check_static_item(tcx, def_id)?;
761+
def_kind @ (DefKind::Static { .. } | DefKind::Const) => {
762+
tcx.ensure_ok().generics_of(def_id);
763+
tcx.ensure_ok().type_of(def_id);
764+
tcx.ensure_ok().predicates_of(def_id);
765+
match def_kind {
766+
DefKind::Static { .. } => {
767+
check_static_inhabited(tcx, def_id);
768+
check_static_linkage(tcx, def_id);
769+
wfcheck::check_static_item(tcx, def_id)?;
770+
}
771+
DefKind::Const => {}
772+
_ => unreachable!(),
773+
}
765774
}
766-
DefKind::Const => {}
767775
DefKind::Enum => {
776+
tcx.ensure_ok().generics_of(def_id);
777+
tcx.ensure_ok().type_of(def_id);
778+
tcx.ensure_ok().predicates_of(def_id);
779+
crate::collect::lower_enum_variant_types(tcx, def_id.to_def_id());
768780
check_enum(tcx, def_id);
769781
check_variances_for_type_defn(tcx, def_id);
770782
}
771783
DefKind::Fn => {
784+
tcx.ensure_ok().generics_of(def_id);
785+
tcx.ensure_ok().type_of(def_id);
786+
tcx.ensure_ok().predicates_of(def_id);
787+
tcx.ensure_ok().fn_sig(def_id);
788+
tcx.ensure_ok().codegen_fn_attrs(def_id);
772789
if let Some(i) = tcx.intrinsic(def_id) {
773790
intrinsic::check_intrinsic_type(
774791
tcx,
@@ -779,6 +796,11 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
779796
}
780797
}
781798
DefKind::Impl { of_trait } => {
799+
tcx.ensure_ok().generics_of(def_id);
800+
tcx.ensure_ok().type_of(def_id);
801+
tcx.ensure_ok().impl_trait_header(def_id);
802+
tcx.ensure_ok().predicates_of(def_id);
803+
tcx.ensure_ok().associated_items(def_id);
782804
if of_trait && let Some(impl_trait_header) = tcx.impl_trait_header(def_id) {
783805
tcx.ensure_ok()
784806
.coherent_trait(impl_trait_header.trait_ref.instantiate_identity().def_id)?;
@@ -787,6 +809,11 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
787809
}
788810
}
789811
DefKind::Trait => {
812+
tcx.ensure_ok().generics_of(def_id);
813+
tcx.ensure_ok().trait_def(def_id);
814+
tcx.ensure_ok().explicit_super_predicates_of(def_id);
815+
tcx.ensure_ok().predicates_of(def_id);
816+
tcx.ensure_ok().associated_items(def_id);
790817
let assoc_items = tcx.associated_items(def_id);
791818
check_on_unimplemented(tcx, def_id);
792819

@@ -805,12 +832,32 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
805832
}
806833
}
807834
}
808-
DefKind::Struct => {
809-
check_struct(tcx, def_id);
810-
check_variances_for_type_defn(tcx, def_id);
835+
DefKind::TraitAlias => {
836+
tcx.ensure_ok().generics_of(def_id);
837+
tcx.ensure_ok().explicit_implied_predicates_of(def_id);
838+
tcx.ensure_ok().explicit_super_predicates_of(def_id);
839+
tcx.ensure_ok().predicates_of(def_id);
811840
}
812-
DefKind::Union => {
813-
check_union(tcx, def_id);
841+
def_kind @ (DefKind::Struct | DefKind::Union) => {
842+
tcx.ensure_ok().generics_of(def_id);
843+
tcx.ensure_ok().type_of(def_id);
844+
tcx.ensure_ok().predicates_of(def_id);
845+
846+
let adt = tcx.adt_def(def_id).non_enum_variant();
847+
for f in adt.fields.iter() {
848+
tcx.ensure_ok().generics_of(f.did);
849+
tcx.ensure_ok().type_of(f.did);
850+
tcx.ensure_ok().predicates_of(f.did);
851+
}
852+
853+
if let Some((_, ctor_def_id)) = adt.ctor {
854+
crate::collect::lower_variant_ctor(tcx, ctor_def_id.expect_local());
855+
}
856+
match def_kind {
857+
DefKind::Struct => check_struct(tcx, def_id),
858+
DefKind::Union => check_union(tcx, def_id),
859+
_ => unreachable!(),
860+
}
814861
check_variances_for_type_defn(tcx, def_id);
815862
}
816863
DefKind::OpaqueTy => {
@@ -838,6 +885,9 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
838885
}
839886
}
840887
DefKind::TyAlias => {
888+
tcx.ensure_ok().generics_of(def_id);
889+
tcx.ensure_ok().type_of(def_id);
890+
tcx.ensure_ok().predicates_of(def_id);
841891
check_type_alias_type_params_are_used(tcx, def_id);
842892
if tcx.type_alias_is_lazy(def_id) {
843893
res = res.and(enter_wf_checking_ctxt(tcx, def_id, |wfcx| {
@@ -897,11 +947,23 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
897947
}
898948

899949
let item = tcx.hir_foreign_item(item.id);
900-
match &item.kind {
901-
hir::ForeignItemKind::Fn(sig, _, _) => {
950+
tcx.ensure_ok().generics_of(item.owner_id);
951+
tcx.ensure_ok().type_of(item.owner_id);
952+
tcx.ensure_ok().predicates_of(item.owner_id);
953+
if tcx.is_conditionally_const(def_id) {
954+
tcx.ensure_ok().explicit_implied_const_bounds(def_id);
955+
tcx.ensure_ok().const_conditions(def_id);
956+
}
957+
match item.kind {
958+
hir::ForeignItemKind::Fn(sig, ..) => {
959+
tcx.ensure_ok().codegen_fn_attrs(item.owner_id);
960+
tcx.ensure_ok().fn_sig(item.owner_id);
902961
require_c_abi_if_c_variadic(tcx, sig.decl, abi, item.span);
903962
}
904-
_ => {}
963+
hir::ForeignItemKind::Static(..) => {
964+
tcx.ensure_ok().codegen_fn_attrs(item.owner_id);
965+
}
966+
_ => (),
905967
}
906968
}
907969
}

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,6 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
230230
?item.owner_id,
231231
item.name = ? tcx.def_path_str(def_id)
232232
);
233-
crate::collect::lower_item(tcx, item.item_id());
234233

235234
match item.kind {
236235
// Right now we check that every default trait implementation

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 2 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -615,105 +615,6 @@ fn get_new_lifetime_name<'tcx>(
615615
(1..).flat_map(a_to_z_repeat_n).find(|lt| !existing_lifetimes.contains(lt.as_str())).unwrap()
616616
}
617617

618-
#[instrument(level = "debug", skip_all)]
619-
pub(super) fn lower_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) {
620-
let it = tcx.hir_item(item_id);
621-
debug!(item = ?it.kind.ident(), id = %it.hir_id());
622-
let def_id = item_id.owner_id.def_id;
623-
624-
match &it.kind {
625-
// These don't define types.
626-
hir::ItemKind::ExternCrate(..)
627-
| hir::ItemKind::Use(..)
628-
| hir::ItemKind::Macro(..)
629-
| hir::ItemKind::Mod(..)
630-
| hir::ItemKind::GlobalAsm { .. } => {}
631-
hir::ItemKind::ForeignMod { items, .. } => {
632-
for item in *items {
633-
let item = tcx.hir_foreign_item(item.id);
634-
tcx.ensure_ok().generics_of(item.owner_id);
635-
tcx.ensure_ok().type_of(item.owner_id);
636-
tcx.ensure_ok().predicates_of(item.owner_id);
637-
if tcx.is_conditionally_const(def_id) {
638-
tcx.ensure_ok().explicit_implied_const_bounds(def_id);
639-
tcx.ensure_ok().const_conditions(def_id);
640-
}
641-
match item.kind {
642-
hir::ForeignItemKind::Fn(..) => {
643-
tcx.ensure_ok().codegen_fn_attrs(item.owner_id);
644-
tcx.ensure_ok().fn_sig(item.owner_id)
645-
}
646-
hir::ForeignItemKind::Static(..) => {
647-
tcx.ensure_ok().codegen_fn_attrs(item.owner_id);
648-
}
649-
_ => (),
650-
}
651-
}
652-
}
653-
hir::ItemKind::Enum(..) => {
654-
tcx.ensure_ok().generics_of(def_id);
655-
tcx.ensure_ok().type_of(def_id);
656-
tcx.ensure_ok().predicates_of(def_id);
657-
lower_enum_variant_types(tcx, def_id.to_def_id());
658-
}
659-
hir::ItemKind::Impl { .. } => {
660-
tcx.ensure_ok().generics_of(def_id);
661-
tcx.ensure_ok().type_of(def_id);
662-
tcx.ensure_ok().impl_trait_header(def_id);
663-
tcx.ensure_ok().predicates_of(def_id);
664-
tcx.ensure_ok().associated_items(def_id);
665-
}
666-
hir::ItemKind::Trait(..) => {
667-
tcx.ensure_ok().generics_of(def_id);
668-
tcx.ensure_ok().trait_def(def_id);
669-
tcx.at(it.span).explicit_super_predicates_of(def_id);
670-
tcx.ensure_ok().predicates_of(def_id);
671-
tcx.ensure_ok().associated_items(def_id);
672-
}
673-
hir::ItemKind::TraitAlias(..) => {
674-
tcx.ensure_ok().generics_of(def_id);
675-
tcx.at(it.span).explicit_implied_predicates_of(def_id);
676-
tcx.at(it.span).explicit_super_predicates_of(def_id);
677-
tcx.ensure_ok().predicates_of(def_id);
678-
}
679-
hir::ItemKind::Struct(_, _, struct_def) | hir::ItemKind::Union(_, _, struct_def) => {
680-
tcx.ensure_ok().generics_of(def_id);
681-
tcx.ensure_ok().type_of(def_id);
682-
tcx.ensure_ok().predicates_of(def_id);
683-
684-
for f in struct_def.fields() {
685-
tcx.ensure_ok().generics_of(f.def_id);
686-
tcx.ensure_ok().type_of(f.def_id);
687-
tcx.ensure_ok().predicates_of(f.def_id);
688-
}
689-
690-
if let Some(ctor_def_id) = struct_def.ctor_def_id() {
691-
lower_variant_ctor(tcx, ctor_def_id);
692-
}
693-
}
694-
695-
hir::ItemKind::TyAlias(..) => {
696-
tcx.ensure_ok().generics_of(def_id);
697-
tcx.ensure_ok().type_of(def_id);
698-
tcx.ensure_ok().predicates_of(def_id);
699-
}
700-
701-
hir::ItemKind::Static(..) | hir::ItemKind::Const(..) => {
702-
tcx.ensure_ok().generics_of(def_id);
703-
tcx.ensure_ok().type_of(def_id);
704-
tcx.ensure_ok().predicates_of(def_id);
705-
}
706-
707-
hir::ItemKind::Fn { .. } => {
708-
tcx.ensure_ok().generics_of(def_id);
709-
tcx.ensure_ok().type_of(def_id);
710-
tcx.ensure_ok().predicates_of(def_id);
711-
tcx.ensure_ok().fn_sig(def_id);
712-
tcx.ensure_ok().codegen_fn_attrs(def_id);
713-
}
714-
}
715-
}
716-
717618
pub(crate) fn lower_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
718619
let trait_item = tcx.hir_trait_item(trait_item_id);
719620
let def_id = trait_item_id.owner_id;
@@ -761,13 +662,13 @@ pub(super) fn lower_impl_item(tcx: TyCtxt<'_>, impl_item_id: hir::ImplItemId) {
761662
}
762663
}
763664

764-
fn lower_variant_ctor(tcx: TyCtxt<'_>, def_id: LocalDefId) {
665+
pub(super) fn lower_variant_ctor(tcx: TyCtxt<'_>, def_id: LocalDefId) {
765666
tcx.ensure_ok().generics_of(def_id);
766667
tcx.ensure_ok().type_of(def_id);
767668
tcx.ensure_ok().predicates_of(def_id);
768669
}
769670

770-
fn lower_enum_variant_types(tcx: TyCtxt<'_>, def_id: DefId) {
671+
pub(super) fn lower_enum_variant_types(tcx: TyCtxt<'_>, def_id: DefId) {
771672
let def = tcx.adt_def(def_id);
772673
let repr_type = def.repr().discr_type();
773674
let initial = repr_type.initial_discriminant(tcx);

tests/incremental/cyclic-trait-hierarchy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
pub trait T2 {}
77
#[cfg(cfail2)]
88
pub trait T2: T1 {}
9-
//[cfail2]~^ ERROR cycle detected when computing the super predicates of `T2`
9+
//[cfail2]~^ ERROR cycle detected when computing the implied predicates of `T2`
1010

1111
pub trait T1: T2 {}
1212

tests/ui/cycle-trait/cycle-trait-supertrait-direct.stderr

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ LL | trait Chromosome: Chromosome {
88
note: cycle used when checking that `Chromosome` is well-formed
99
--> $DIR/cycle-trait-supertrait-direct.rs:3:1
1010
|
11-
LL | / trait Chromosome: Chromosome {
12-
LL | |
13-
LL | | }
14-
| |_^
11+
LL | trait Chromosome: Chromosome {
12+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1513
= 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
1614

1715
error: aborting due to 1 previous error

tests/ui/cycle-trait/issue-12511.stderr

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ LL | trait T2 : T1 {
1313
note: cycle used when checking that `T1` is well-formed
1414
--> $DIR/issue-12511.rs:1:1
1515
|
16-
LL | / trait T1 : T2 {
17-
LL | |
18-
LL | | }
19-
| |_^
16+
LL | trait T1 : T2 {
17+
| ^^^^^^^^^^^^^
2018
= 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
2119

2220
error: aborting due to 1 previous error

tests/ui/error-codes/E0081.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ LL |
7373
LL | V9,
7474
| -- `-2` assigned here
7575

76+
error[E0370]: enum discriminant overflowed
77+
--> $DIR/E0081.rs:87:5
78+
|
79+
LL | X256,
80+
| ^^^^ overflowed on value after 255
81+
|
82+
= note: explicitly set `X256 = 0` if that is desired outcome
83+
7684
error[E0081]: discriminant value `0` assigned more than once
7785
--> $DIR/E0081.rs:57:1
7886
|
@@ -88,14 +96,6 @@ LL | X000, X001, X002, X003, X004, X005, X006, X007, X008, X009,
8896
LL | X256,
8997
| ---- `0` assigned here
9098

91-
error[E0370]: enum discriminant overflowed
92-
--> $DIR/E0081.rs:87:5
93-
|
94-
LL | X256,
95-
| ^^^^ overflowed on value after 255
96-
|
97-
= note: explicitly set `X256 = 0` if that is desired outcome
98-
9999
error: aborting due to 7 previous errors
100100

101101
Some errors have detailed explanations: E0081, E0370.

tests/ui/infinite/infinite-trait-alias-recursion.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ note: cycle used when checking that `T1` is well-formed
2020
--> $DIR/infinite-trait-alias-recursion.rs:3:1
2121
|
2222
LL | trait T1 = T2;
23-
| ^^^^^^^^^^^^^^
23+
| ^^^^^^^^
2424
= 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
2525

2626
error: aborting due to 1 previous error

tests/ui/stability-attribute/generics-default-stability-where.stderr

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
error[E0210]: type parameter `T` must be used as the type parameter for some local type (e.g., `MyStruct<T>`)
2-
--> $DIR/generics-default-stability-where.rs:7:6
3-
|
4-
LL | impl<T> Trait3<usize> for T where T: Trait2<usize> {
5-
| ^ type parameter `T` must be used as the type parameter for some local type
6-
|
7-
= note: implementing a foreign trait is only possible if at least one of the types for which it is implemented is local
8-
= note: only traits defined in the current crate can be implemented for a type parameter
9-
101
error[E0658]: use of unstable library feature `unstable_default`
112
--> $DIR/generics-default-stability-where.rs:7:45
123
|
@@ -16,6 +7,15 @@ LL | impl<T> Trait3<usize> for T where T: Trait2<usize> {
167
= help: add `#![feature(unstable_default)]` to the crate attributes to enable
178
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
189

10+
error[E0210]: type parameter `T` must be used as the type parameter for some local type (e.g., `MyStruct<T>`)
11+
--> $DIR/generics-default-stability-where.rs:7:6
12+
|
13+
LL | impl<T> Trait3<usize> for T where T: Trait2<usize> {
14+
| ^ type parameter `T` must be used as the type parameter for some local type
15+
|
16+
= note: implementing a foreign trait is only possible if at least one of the types for which it is implemented is local
17+
= note: only traits defined in the current crate can be implemented for a type parameter
18+
1919
error: aborting due to 2 previous errors
2020

2121
Some errors have detailed explanations: E0210, E0658.

tests/ui/traits/trait-upcasting/cyclic-trait-resolution.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ note: cycle used when checking that `A` is well-formed
99
--> $DIR/cyclic-trait-resolution.rs:1:1
1010
|
1111
LL | trait A: B + A {}
12-
| ^^^^^^^^^^^^^^^^^
12+
| ^^^^^^^^^^^^^^
1313
= 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
1414

1515
error[E0391]: cycle detected when computing the implied predicates of `A`

0 commit comments

Comments
 (0)