@@ -459,122 +459,63 @@ fn const_evaluatable_predicates_of(
459459 collector. preds
460460}
461461
462- pub ( super ) fn trait_explicit_predicates_and_bounds (
463- tcx : TyCtxt < ' _ > ,
464- def_id : LocalDefId ,
465- ) -> ty:: GenericPredicates < ' _ > {
466- assert_eq ! ( tcx. def_kind( def_id) , DefKind :: Trait ) ;
467- gather_explicit_predicates_of ( tcx, def_id)
468- }
469-
470462pub ( super ) fn explicit_predicates_of < ' tcx > (
471463 tcx : TyCtxt < ' tcx > ,
472464 def_id : LocalDefId ,
473465) -> ty:: GenericPredicates < ' tcx > {
474466 let def_kind = tcx. def_kind ( def_id) ;
475- if let DefKind :: Trait = def_kind {
476- // Remove bounds on associated types from the predicates, they will be
477- // returned by `explicit_item_bounds`.
478- let predicates_and_bounds = tcx. trait_explicit_predicates_and_bounds ( def_id) ;
479- let trait_identity_args = ty:: GenericArgs :: identity_for_item ( tcx, def_id) ;
480-
481- let is_assoc_item_ty = |ty : Ty < ' tcx > | {
482- // For a predicate from a where clause to become a bound on an
483- // associated type:
484- // * It must use the identity args of the item.
485- // * We're in the scope of the trait, so we can't name any
486- // parameters of the GAT. That means that all we need to
487- // check are that the args of the projection are the
488- // identity args of the trait.
489- // * It must be an associated type for this trait (*not* a
490- // supertrait).
491- if let ty:: Alias ( ty:: Projection , projection) = ty. kind ( ) {
492- projection. args == trait_identity_args
493- // FIXME(return_type_notation): This check should be more robust
494- && !tcx. is_impl_trait_in_trait ( projection. def_id )
495- && tcx. associated_item ( projection. def_id ) . container_id ( tcx)
496- == def_id. to_def_id ( )
497- } else {
498- false
499- }
500- } ;
501-
502- let predicates: Vec < _ > = predicates_and_bounds
467+ if matches ! ( def_kind, DefKind :: AnonConst )
468+ && tcx. features ( ) . generic_const_exprs
469+ && let Some ( defaulted_param_def_id) =
470+ tcx. hir ( ) . opt_const_param_default_param_def_id ( tcx. local_def_id_to_hir_id ( def_id) )
471+ {
472+ // In `generics_of` we set the generics' parent to be our parent's parent which means that
473+ // we lose out on the predicates of our actual parent if we dont return those predicates here.
474+ // (See comment in `generics_of` for more information on why the parent shenanigans is necessary)
475+ //
476+ // struct Foo<T, const N: usize = { <T as Trait>::ASSOC }>(T) where T: Trait;
477+ // ^^^ ^^^^^^^^^^^^^^^^^^^^^^^ the def id we are calling
478+ // ^^^ explicit_predicates_of on
479+ // parent item we dont have set as the
480+ // parent of generics returned by `generics_of`
481+ //
482+ // In the above code we want the anon const to have predicates in its param env for `T: Trait`
483+ // and we would be calling `explicit_predicates_of(Foo)` here
484+ let parent_def_id = tcx. local_parent ( def_id) ;
485+ let parent_preds = tcx. explicit_predicates_of ( parent_def_id) ;
486+
487+ // If we dont filter out `ConstArgHasType` predicates then every single defaulted const parameter
488+ // will ICE because of #106994. FIXME(generic_const_exprs): remove this when a more general solution
489+ // to #106994 is implemented.
490+ let filtered_predicates = parent_preds
503491 . predicates
504- . iter ( )
505- . copied ( )
506- . filter ( |( pred, _) | match pred. kind ( ) . skip_binder ( ) {
507- ty:: ClauseKind :: Trait ( tr) => !is_assoc_item_ty ( tr. self_ty ( ) ) ,
508- ty:: ClauseKind :: Projection ( proj) => {
509- !is_assoc_item_ty ( proj. projection_term . self_ty ( ) )
510- }
511- ty:: ClauseKind :: TypeOutlives ( outlives) => !is_assoc_item_ty ( outlives. 0 ) ,
512- _ => true ,
513- } )
514- . collect ( ) ;
515- if predicates. len ( ) == predicates_and_bounds. predicates . len ( ) {
516- predicates_and_bounds
517- } else {
518- ty:: GenericPredicates {
519- parent : predicates_and_bounds. parent ,
520- predicates : tcx. arena . alloc_slice ( & predicates) ,
521- effects_min_tys : predicates_and_bounds. effects_min_tys ,
522- }
523- }
524- } else {
525- if matches ! ( def_kind, DefKind :: AnonConst )
526- && tcx. features ( ) . generic_const_exprs
527- && let Some ( defaulted_param_def_id) =
528- tcx. hir ( ) . opt_const_param_default_param_def_id ( tcx. local_def_id_to_hir_id ( def_id) )
529- {
530- // In `generics_of` we set the generics' parent to be our parent's parent which means that
531- // we lose out on the predicates of our actual parent if we dont return those predicates here.
532- // (See comment in `generics_of` for more information on why the parent shenanigans is necessary)
533- //
534- // struct Foo<T, const N: usize = { <T as Trait>::ASSOC }>(T) where T: Trait;
535- // ^^^ ^^^^^^^^^^^^^^^^^^^^^^^ the def id we are calling
536- // ^^^ explicit_predicates_of on
537- // parent item we dont have set as the
538- // parent of generics returned by `generics_of`
539- //
540- // In the above code we want the anon const to have predicates in its param env for `T: Trait`
541- // and we would be calling `explicit_predicates_of(Foo)` here
542- let parent_def_id = tcx. local_parent ( def_id) ;
543- let parent_preds = tcx. explicit_predicates_of ( parent_def_id) ;
544-
545- // If we dont filter out `ConstArgHasType` predicates then every single defaulted const parameter
546- // will ICE because of #106994. FIXME(generic_const_exprs): remove this when a more general solution
547- // to #106994 is implemented.
548- let filtered_predicates = parent_preds
549- . predicates
550- . into_iter ( )
551- . filter ( |( pred, _) | {
552- if let ty:: ClauseKind :: ConstArgHasType ( ct, _) = pred. kind ( ) . skip_binder ( ) {
553- match ct. kind ( ) {
554- ty:: ConstKind :: Param ( param_const) => {
555- let defaulted_param_idx = tcx
556- . generics_of ( parent_def_id)
557- . param_def_id_to_index [ & defaulted_param_def_id. to_def_id ( ) ] ;
558- param_const. index < defaulted_param_idx
559- }
560- _ => bug ! (
561- "`ConstArgHasType` in `predicates_of`\
562- that isn't a `Param` const"
563- ) ,
492+ . into_iter ( )
493+ . filter ( |( pred, _) | {
494+ if let ty:: ClauseKind :: ConstArgHasType ( ct, _) = pred. kind ( ) . skip_binder ( ) {
495+ match ct. kind ( ) {
496+ ty:: ConstKind :: Param ( param_const) => {
497+ let defaulted_param_idx = tcx
498+ . generics_of ( parent_def_id)
499+ . param_def_id_to_index [ & defaulted_param_def_id. to_def_id ( ) ] ;
500+ param_const. index < defaulted_param_idx
564501 }
565- } else {
566- true
502+ _ => bug ! (
503+ "`ConstArgHasType` in `predicates_of`\
504+ that isn't a `Param` const"
505+ ) ,
567506 }
568- } )
569- . cloned ( ) ;
570- return GenericPredicates {
571- parent : parent_preds. parent ,
572- predicates : { tcx. arena . alloc_from_iter ( filtered_predicates) } ,
573- effects_min_tys : parent_preds. effects_min_tys ,
574- } ;
575- }
576- gather_explicit_predicates_of ( tcx, def_id)
507+ } else {
508+ true
509+ }
510+ } )
511+ . cloned ( ) ;
512+ return GenericPredicates {
513+ parent : parent_preds. parent ,
514+ predicates : { tcx. arena . alloc_from_iter ( filtered_predicates) } ,
515+ effects_min_tys : parent_preds. effects_min_tys ,
516+ } ;
577517 }
518+ gather_explicit_predicates_of ( tcx, def_id)
578519}
579520
580521/// Ensures that the super-predicates of the trait with a `DefId`
0 commit comments