@@ -10,7 +10,7 @@ use rustc_errors::{EmissionGuarantee, MultiSpan};
10
10
use rustc_hir:: def:: { CtorKind , DefKind } ;
11
11
use rustc_hir:: { LangItem , Node , intravisit} ;
12
12
use rustc_infer:: infer:: { RegionVariableOrigin , TyCtxtInferExt } ;
13
- use rustc_infer:: traits:: { Obligation , ObligationCauseCode } ;
13
+ use rustc_infer:: traits:: { Obligation , ObligationCauseCode , WellFormedLoc } ;
14
14
use rustc_lint_defs:: builtin:: {
15
15
REPR_TRANSPARENT_EXTERNAL_PRIVATE_FIELDS , UNSUPPORTED_CALLING_CONVENTIONS ,
16
16
} ;
@@ -36,6 +36,10 @@ use {rustc_attr_data_structures as attrs, rustc_hir as hir};
36
36
37
37
use super :: compare_impl_item:: check_type_bounds;
38
38
use super :: * ;
39
+ use crate :: check:: wfcheck:: {
40
+ check_associated_item, check_trait_item, check_variances_for_type_defn, check_where_clauses,
41
+ enter_wf_checking_ctxt,
42
+ } ;
39
43
40
44
fn add_abi_diag_help < T : EmissionGuarantee > ( abi : ExternAbi , diag : & mut Diag < ' _ , T > ) {
41
45
if let ExternAbi :: Cdecl { unwind } = abi {
@@ -729,7 +733,8 @@ fn check_static_linkage(tcx: TyCtxt<'_>, def_id: LocalDefId) {
729
733
}
730
734
}
731
735
732
- pub ( crate ) fn check_item_type ( tcx : TyCtxt < ' _ > , def_id : LocalDefId ) {
736
+ pub ( crate ) fn check_item_type ( tcx : TyCtxt < ' _ > , def_id : LocalDefId ) -> Result < ( ) , ErrorGuaranteed > {
737
+ let mut res = Ok ( ( ) ) ;
733
738
let generics = tcx. generics_of ( def_id) ;
734
739
735
740
for param in & generics. own_params {
@@ -754,15 +759,35 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
754
759
}
755
760
756
761
match tcx. def_kind ( def_id) {
757
- DefKind :: Static { .. } => {
758
- check_static_inhabited ( tcx, def_id) ;
759
- check_static_linkage ( tcx, def_id) ;
762
+ def_kind @ ( DefKind :: Static { .. } | DefKind :: Const ) => {
763
+ tcx. ensure_ok ( ) . generics_of ( def_id) ;
764
+ tcx. ensure_ok ( ) . type_of ( def_id) ;
765
+ tcx. ensure_ok ( ) . predicates_of ( def_id) ;
766
+ match def_kind {
767
+ DefKind :: Static { .. } => {
768
+ check_static_inhabited ( tcx, def_id) ;
769
+ check_static_linkage ( tcx, def_id) ;
770
+ res = res. and ( wfcheck:: check_static_item ( tcx, def_id) ) ;
771
+ return res;
772
+ }
773
+ DefKind :: Const => { }
774
+ _ => unreachable ! ( ) ,
775
+ }
760
776
}
761
- DefKind :: Const => { }
762
777
DefKind :: Enum => {
778
+ tcx. ensure_ok ( ) . generics_of ( def_id) ;
779
+ tcx. ensure_ok ( ) . type_of ( def_id) ;
780
+ tcx. ensure_ok ( ) . predicates_of ( def_id) ;
781
+ crate :: collect:: lower_enum_variant_types ( tcx, def_id. to_def_id ( ) ) ;
763
782
check_enum ( tcx, def_id) ;
783
+ check_variances_for_type_defn ( tcx, def_id) ;
764
784
}
765
785
DefKind :: Fn => {
786
+ tcx. ensure_ok ( ) . generics_of ( def_id) ;
787
+ tcx. ensure_ok ( ) . type_of ( def_id) ;
788
+ tcx. ensure_ok ( ) . predicates_of ( def_id) ;
789
+ tcx. ensure_ok ( ) . fn_sig ( def_id) ;
790
+ tcx. ensure_ok ( ) . codegen_fn_attrs ( def_id) ;
766
791
if let Some ( i) = tcx. intrinsic ( def_id) {
767
792
intrinsic:: check_intrinsic_type (
768
793
tcx,
@@ -773,17 +798,31 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
773
798
}
774
799
}
775
800
DefKind :: Impl { of_trait } => {
801
+ tcx. ensure_ok ( ) . generics_of ( def_id) ;
802
+ tcx. ensure_ok ( ) . type_of ( def_id) ;
803
+ tcx. ensure_ok ( ) . impl_trait_header ( def_id) ;
804
+ tcx. ensure_ok ( ) . predicates_of ( def_id) ;
805
+ tcx. ensure_ok ( ) . associated_items ( def_id) ;
776
806
if of_trait && let Some ( impl_trait_header) = tcx. impl_trait_header ( def_id) {
777
- if tcx
778
- . ensure_ok ( )
779
- . coherent_trait ( impl_trait_header. trait_ref . instantiate_identity ( ) . def_id )
780
- . is_ok ( )
781
- {
807
+ res = res. and (
808
+ tcx. ensure_ok ( )
809
+ . coherent_trait ( impl_trait_header. trait_ref . instantiate_identity ( ) . def_id ) ,
810
+ ) ;
811
+
812
+ if res. is_ok ( ) {
813
+ // Checking this only makes sense if the all trait impls satisfy basic
814
+ // requirements (see `coherent_trait` query), otherwise
815
+ // we run into infinite recursions a lot.
782
816
check_impl_items_against_trait ( tcx, def_id, impl_trait_header) ;
783
817
}
784
818
}
785
819
}
786
820
DefKind :: Trait => {
821
+ tcx. ensure_ok ( ) . generics_of ( def_id) ;
822
+ tcx. ensure_ok ( ) . trait_def ( def_id) ;
823
+ tcx. ensure_ok ( ) . explicit_super_predicates_of ( def_id) ;
824
+ tcx. ensure_ok ( ) . predicates_of ( def_id) ;
825
+ tcx. ensure_ok ( ) . associated_items ( def_id) ;
787
826
let assoc_items = tcx. associated_items ( def_id) ;
788
827
check_on_unimplemented ( tcx, def_id) ;
789
828
@@ -802,11 +841,33 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
802
841
}
803
842
}
804
843
}
805
- DefKind :: Struct => {
806
- check_struct ( tcx, def_id) ;
844
+ DefKind :: TraitAlias => {
845
+ tcx. ensure_ok ( ) . generics_of ( def_id) ;
846
+ tcx. ensure_ok ( ) . explicit_implied_predicates_of ( def_id) ;
847
+ tcx. ensure_ok ( ) . explicit_super_predicates_of ( def_id) ;
848
+ tcx. ensure_ok ( ) . predicates_of ( def_id) ;
807
849
}
808
- DefKind :: Union => {
809
- check_union ( tcx, def_id) ;
850
+ def_kind @ ( DefKind :: Struct | DefKind :: Union ) => {
851
+ tcx. ensure_ok ( ) . generics_of ( def_id) ;
852
+ tcx. ensure_ok ( ) . type_of ( def_id) ;
853
+ tcx. ensure_ok ( ) . predicates_of ( def_id) ;
854
+
855
+ let adt = tcx. adt_def ( def_id) . non_enum_variant ( ) ;
856
+ for f in adt. fields . iter ( ) {
857
+ tcx. ensure_ok ( ) . generics_of ( f. did ) ;
858
+ tcx. ensure_ok ( ) . type_of ( f. did ) ;
859
+ tcx. ensure_ok ( ) . predicates_of ( f. did ) ;
860
+ }
861
+
862
+ if let Some ( ( _, ctor_def_id) ) = adt. ctor {
863
+ crate :: collect:: lower_variant_ctor ( tcx, ctor_def_id. expect_local ( ) ) ;
864
+ }
865
+ match def_kind {
866
+ DefKind :: Struct => check_struct ( tcx, def_id) ,
867
+ DefKind :: Union => check_union ( tcx, def_id) ,
868
+ _ => unreachable ! ( ) ,
869
+ }
870
+ check_variances_for_type_defn ( tcx, def_id) ;
810
871
}
811
872
DefKind :: OpaqueTy => {
812
873
check_opaque_precise_captures ( tcx, def_id) ;
@@ -831,14 +892,33 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
831
892
tcx. ensure_ok ( ) . explicit_implied_const_bounds ( def_id) ;
832
893
tcx. ensure_ok ( ) . const_conditions ( def_id) ;
833
894
}
895
+ return res;
834
896
}
835
897
DefKind :: TyAlias => {
898
+ tcx. ensure_ok ( ) . generics_of ( def_id) ;
899
+ tcx. ensure_ok ( ) . type_of ( def_id) ;
900
+ tcx. ensure_ok ( ) . predicates_of ( def_id) ;
836
901
check_type_alias_type_params_are_used ( tcx, def_id) ;
902
+ if tcx. type_alias_is_lazy ( def_id) {
903
+ res = res. and ( enter_wf_checking_ctxt ( tcx, def_id, |wfcx| {
904
+ let ty = tcx. type_of ( def_id) . instantiate_identity ( ) ;
905
+ let span = tcx. def_span ( def_id) ;
906
+ let item_ty = wfcx. deeply_normalize ( span, Some ( WellFormedLoc :: Ty ( def_id) ) , ty) ;
907
+ wfcx. register_wf_obligation (
908
+ span,
909
+ Some ( WellFormedLoc :: Ty ( def_id) ) ,
910
+ item_ty. into ( ) ,
911
+ ) ;
912
+ check_where_clauses ( wfcx, def_id) ;
913
+ Ok ( ( ) )
914
+ } ) ) ;
915
+ check_variances_for_type_defn ( tcx, def_id) ;
916
+ }
837
917
}
838
918
DefKind :: ForeignMod => {
839
919
let it = tcx. hir_expect_item ( def_id) ;
840
920
let hir:: ItemKind :: ForeignMod { abi, items } = it. kind else {
841
- return ;
921
+ return Ok ( ( ) ) ;
842
922
} ;
843
923
844
924
check_abi ( tcx, it. hir_id ( ) , it. span , abi) ;
@@ -877,15 +957,23 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
877
957
}
878
958
879
959
let item = tcx. hir_foreign_item ( item. id ) ;
880
- match & item. kind {
881
- hir:: ForeignItemKind :: Fn ( sig, _, _) => {
960
+ tcx. ensure_ok ( ) . generics_of ( item. owner_id ) ;
961
+ tcx. ensure_ok ( ) . type_of ( item. owner_id ) ;
962
+ tcx. ensure_ok ( ) . predicates_of ( item. owner_id ) ;
963
+ if tcx. is_conditionally_const ( def_id) {
964
+ tcx. ensure_ok ( ) . explicit_implied_const_bounds ( def_id) ;
965
+ tcx. ensure_ok ( ) . const_conditions ( def_id) ;
966
+ }
967
+ match item. kind {
968
+ hir:: ForeignItemKind :: Fn ( sig, ..) => {
969
+ tcx. ensure_ok ( ) . codegen_fn_attrs ( item. owner_id ) ;
970
+ tcx. ensure_ok ( ) . fn_sig ( item. owner_id ) ;
882
971
require_c_abi_if_c_variadic ( tcx, sig. decl , abi, item. span ) ;
883
972
}
884
973
hir:: ForeignItemKind :: Static ( ..) => {
885
- check_static_inhabited ( tcx, def_id) ;
886
- check_static_linkage ( tcx, def_id) ;
974
+ tcx. ensure_ok ( ) . codegen_fn_attrs ( item. owner_id ) ;
887
975
}
888
- _ => { }
976
+ _ => ( ) ,
889
977
}
890
978
}
891
979
}
@@ -897,9 +985,65 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
897
985
// We do not call `type_of` for closures here as that
898
986
// depends on typecheck and would therefore hide
899
987
// any further errors in case one typeck fails.
988
+ return res;
989
+ }
990
+ DefKind :: AssocFn => {
991
+ tcx. ensure_ok ( ) . codegen_fn_attrs ( def_id) ;
992
+ tcx. ensure_ok ( ) . type_of ( def_id) ;
993
+ tcx. ensure_ok ( ) . fn_sig ( def_id) ;
994
+ tcx. ensure_ok ( ) . predicates_of ( def_id) ;
995
+ res = res. and ( check_associated_item ( tcx, def_id) ) ;
996
+ let assoc_item = tcx. associated_item ( def_id) ;
997
+ match assoc_item. container {
998
+ ty:: AssocItemContainer :: Impl => { }
999
+ ty:: AssocItemContainer :: Trait => {
1000
+ res = res. and ( check_trait_item ( tcx, def_id) ) ;
1001
+ }
1002
+ }
1003
+ return res;
1004
+ }
1005
+ DefKind :: AssocConst => {
1006
+ tcx. ensure_ok ( ) . type_of ( def_id) ;
1007
+ tcx. ensure_ok ( ) . predicates_of ( def_id) ;
1008
+ res = res. and ( check_associated_item ( tcx, def_id) ) ;
1009
+ let assoc_item = tcx. associated_item ( def_id) ;
1010
+ match assoc_item. container {
1011
+ ty:: AssocItemContainer :: Impl => { }
1012
+ ty:: AssocItemContainer :: Trait => {
1013
+ res = res. and ( check_trait_item ( tcx, def_id) ) ;
1014
+ }
1015
+ }
1016
+ return res;
1017
+ }
1018
+ DefKind :: AssocTy => {
1019
+ tcx. ensure_ok ( ) . predicates_of ( def_id) ;
1020
+ res = res. and ( check_associated_item ( tcx, def_id) ) ;
1021
+
1022
+ let assoc_item = tcx. associated_item ( def_id) ;
1023
+ let has_type = match assoc_item. container {
1024
+ ty:: AssocItemContainer :: Impl => true ,
1025
+ ty:: AssocItemContainer :: Trait => {
1026
+ tcx. ensure_ok ( ) . item_bounds ( def_id) ;
1027
+ tcx. ensure_ok ( ) . item_self_bounds ( def_id) ;
1028
+ res = res. and ( check_trait_item ( tcx, def_id) ) ;
1029
+ assoc_item. defaultness ( tcx) . has_value ( )
1030
+ }
1031
+ } ;
1032
+ if has_type {
1033
+ tcx. ensure_ok ( ) . type_of ( def_id) ;
1034
+ }
1035
+ return res;
900
1036
}
1037
+ DefKind :: AnonConst | DefKind :: InlineConst => return res,
901
1038
_ => { }
902
1039
}
1040
+ let node = tcx. hir_node_by_def_id ( def_id) ;
1041
+ res. and ( match node {
1042
+ hir:: Node :: Crate ( _) => bug ! ( "check_well_formed cannot be applied to the crate root" ) ,
1043
+ hir:: Node :: Item ( item) => wfcheck:: check_item ( tcx, item) ,
1044
+ hir:: Node :: ForeignItem ( item) => wfcheck:: check_foreign_item ( tcx, item) ,
1045
+ _ => unreachable ! ( "{node:?}" ) ,
1046
+ } )
903
1047
}
904
1048
905
1049
pub ( super ) fn check_on_unimplemented ( tcx : TyCtxt < ' _ > , def_id : LocalDefId ) {
0 commit comments