@@ -888,48 +888,8 @@ pub(super) fn const_conditions<'tcx>(
888
888
tcx : TyCtxt < ' tcx > ,
889
889
def_id : LocalDefId ,
890
890
) -> ty:: ConstConditions < ' tcx > {
891
- // This logic is spaghetti, and should be cleaned up. The current methods that are
892
- // defined to deal with constness are very unintuitive.
893
- if tcx. is_const_fn_raw ( def_id. to_def_id ( ) ) {
894
- // Ok, const fn or method in const trait.
895
- } else {
896
- match tcx. def_kind ( def_id) {
897
- DefKind :: Trait => {
898
- if !tcx. is_const_trait ( def_id. to_def_id ( ) ) {
899
- return Default :: default ( ) ;
900
- }
901
- }
902
- DefKind :: Impl { .. } => {
903
- // FIXME(effects): Should be using a dedicated function to
904
- // test if this is a const trait impl.
905
- if tcx. constness ( def_id) != hir:: Constness :: Const {
906
- return Default :: default ( ) ;
907
- }
908
- }
909
- DefKind :: AssocTy | DefKind :: AssocFn => {
910
- let parent_def_id = tcx. local_parent ( def_id) . to_def_id ( ) ;
911
- match tcx. associated_item ( def_id) . container {
912
- ty:: AssocItemContainer :: TraitContainer => {
913
- if !tcx. is_const_trait ( parent_def_id) {
914
- return Default :: default ( ) ;
915
- }
916
- }
917
- ty:: AssocItemContainer :: ImplContainer => {
918
- // FIXME(effects): Should be using a dedicated function to
919
- // test if this is a const trait impl.
920
- if tcx. constness ( parent_def_id) != hir:: Constness :: Const {
921
- return Default :: default ( ) ;
922
- }
923
- }
924
- }
925
- }
926
- DefKind :: Closure | DefKind :: OpaqueTy => {
927
- // Closures and RPITs will eventually have const conditions
928
- // for `~const` bounds.
929
- return Default :: default ( ) ;
930
- }
931
- _ => return Default :: default ( ) ,
932
- }
891
+ if !tcx. is_conditionally_const ( def_id) {
892
+ bug ! ( "const_conditions invoked for item that is not conditionally const: {def_id:?}" ) ;
933
893
}
934
894
935
895
let ( generics, trait_def_id_and_supertraits, has_parent) = match tcx. hir_node_by_def_id ( def_id)
@@ -940,7 +900,7 @@ pub(super) fn const_conditions<'tcx>(
940
900
hir:: ItemKind :: Trait ( _, _, generics, supertraits, _) => {
941
901
( generics, Some ( ( item. owner_id . def_id , supertraits) ) , false )
942
902
}
943
- _ => return Default :: default ( ) ,
903
+ _ => bug ! ( "const_conditions called on wrong item: {def_id:?}" ) ,
944
904
} ,
945
905
// While associated types are not really const, we do allow them to have `~const`
946
906
// bounds and where clauses. `const_conditions` is responsible for gathering
@@ -950,13 +910,21 @@ pub(super) fn const_conditions<'tcx>(
950
910
hir:: TraitItemKind :: Fn ( _, _) | hir:: TraitItemKind :: Type ( _, _) => {
951
911
( item. generics , None , true )
952
912
}
953
- _ => return Default :: default ( ) ,
913
+ _ => bug ! ( "const_conditions called on wrong item: {def_id:?}" ) ,
954
914
} ,
955
915
Node :: ImplItem ( item) => match item. kind {
956
- hir:: ImplItemKind :: Fn ( _, _) | hir:: ImplItemKind :: Type ( _) => ( item. generics , None , true ) ,
957
- _ => return Default :: default ( ) ,
916
+ hir:: ImplItemKind :: Fn ( _, _) | hir:: ImplItemKind :: Type ( _) => {
917
+ ( item. generics , None , tcx. is_conditionally_const ( tcx. local_parent ( def_id) ) )
918
+ }
919
+ _ => bug ! ( "const_conditions called on wrong item: {def_id:?}" ) ,
958
920
} ,
959
- _ => return Default :: default ( ) ,
921
+ Node :: ForeignItem ( item) => match item. kind {
922
+ hir:: ForeignItemKind :: Fn ( _, _, generics) => ( generics, None , false ) ,
923
+ _ => bug ! ( "const_conditions called on wrong item: {def_id:?}" ) ,
924
+ } ,
925
+ // N.B. Tuple ctors are unconditionally constant.
926
+ Node :: Ctor ( hir:: VariantData :: Tuple { .. } ) => return Default :: default ( ) ,
927
+ _ => bug ! ( "const_conditions called on wrong item: {def_id:?}" ) ,
960
928
} ;
961
929
962
930
let icx = ItemCtxt :: new ( tcx, def_id) ;
@@ -1017,30 +985,22 @@ pub(super) fn implied_const_bounds<'tcx>(
1017
985
tcx : TyCtxt < ' tcx > ,
1018
986
def_id : LocalDefId ,
1019
987
) -> ty:: EarlyBinder < ' tcx , & ' tcx [ ( ty:: PolyTraitRef < ' tcx > , Span ) ] > {
988
+ if !tcx. is_conditionally_const ( def_id) {
989
+ bug ! ( "const_conditions invoked for item that is not conditionally const: {def_id:?}" ) ;
990
+ }
991
+
1020
992
let bounds = match tcx. hir_node_by_def_id ( def_id) {
1021
993
Node :: Item ( hir:: Item { kind : hir:: ItemKind :: Trait ( ..) , .. } ) => {
1022
- if !tcx. is_const_trait ( def_id. to_def_id ( ) ) {
1023
- return ty:: EarlyBinder :: bind ( & [ ] ) ;
1024
- }
1025
-
1026
994
implied_predicates_with_filter (
1027
995
tcx,
1028
996
def_id. to_def_id ( ) ,
1029
997
PredicateFilter :: SelfConstIfConst ,
1030
998
)
1031
999
}
1032
1000
Node :: TraitItem ( hir:: TraitItem { kind : hir:: TraitItemKind :: Type ( ..) , .. } ) => {
1033
- if !tcx. is_const_trait ( tcx. local_parent ( def_id) . to_def_id ( ) ) {
1034
- return ty:: EarlyBinder :: bind ( & [ ] ) ;
1035
- }
1036
-
1037
1001
explicit_item_bounds_with_filter ( tcx, def_id, PredicateFilter :: ConstIfConst )
1038
1002
}
1039
- Node :: OpaqueTy ( ..) => {
1040
- // We should eventually collect the `~const` bounds on opaques.
1041
- return ty:: EarlyBinder :: bind ( & [ ] ) ;
1042
- }
1043
- _ => return ty:: EarlyBinder :: bind ( & [ ] ) ,
1003
+ _ => bug ! ( "implied_const_bounds called on wrong item: {def_id:?}" ) ,
1044
1004
} ;
1045
1005
1046
1006
bounds. map_bound ( |bounds| {
0 commit comments