@@ -888,48 +888,8 @@ pub(super) fn const_conditions<'tcx>(
888888 tcx : TyCtxt < ' tcx > ,
889889 def_id : LocalDefId ,
890890) -> 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:?}" ) ;
933893 }
934894
935895 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>(
940900 hir:: ItemKind :: Trait ( _, _, generics, supertraits, _) => {
941901 ( generics, Some ( ( item. owner_id . def_id , supertraits) ) , false )
942902 }
943- _ => return Default :: default ( ) ,
903+ _ => bug ! ( "const_conditions called on wrong item: {def_id:?}" ) ,
944904 } ,
945905 // While associated types are not really const, we do allow them to have `~const`
946906 // bounds and where clauses. `const_conditions` is responsible for gathering
@@ -950,13 +910,21 @@ pub(super) fn const_conditions<'tcx>(
950910 hir:: TraitItemKind :: Fn ( _, _) | hir:: TraitItemKind :: Type ( _, _) => {
951911 ( item. generics , None , true )
952912 }
953- _ => return Default :: default ( ) ,
913+ _ => bug ! ( "const_conditions called on wrong item: {def_id:?}" ) ,
954914 } ,
955915 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:?}" ) ,
958920 } ,
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:?}" ) ,
960928 } ;
961929
962930 let icx = ItemCtxt :: new ( tcx, def_id) ;
@@ -1017,30 +985,22 @@ pub(super) fn implied_const_bounds<'tcx>(
1017985 tcx : TyCtxt < ' tcx > ,
1018986 def_id : LocalDefId ,
1019987) -> 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+
1020992 let bounds = match tcx. hir_node_by_def_id ( def_id) {
1021993 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-
1026994 implied_predicates_with_filter (
1027995 tcx,
1028996 def_id. to_def_id ( ) ,
1029997 PredicateFilter :: SelfConstIfConst ,
1030998 )
1031999 }
10321000 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-
10371001 explicit_item_bounds_with_filter ( tcx, def_id, PredicateFilter :: ConstIfConst )
10381002 }
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:?}" ) ,
10441004 } ;
10451005
10461006 bounds. map_bound ( |bounds| {
0 commit comments