@@ -72,7 +72,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
7272 let mut is_default_impl_trait = None ;
7373
7474 // FIXME: Should ItemCtxt take a LocalDefId?
75- let icx = ItemCtxt :: new ( tcx, def_id. to_def_id ( ) ) ;
75+ let icx = ItemCtxt :: new ( tcx, def_id) ;
7676
7777 const NO_GENERICS : & hir:: Generics < ' _ > = hir:: Generics :: empty ( ) ;
7878
@@ -551,80 +551,78 @@ pub(super) fn super_predicates_that_define_assoc_type(
551551 tcx : TyCtxt < ' _ > ,
552552 ( trait_def_id, assoc_name) : ( DefId , Option < Ident > ) ,
553553) -> ty:: GenericPredicates < ' _ > {
554- if trait_def_id. is_local ( ) {
555- debug ! ( "local trait" ) ;
556- let trait_hir_id = tcx. hir ( ) . local_def_id_to_hir_id ( trait_def_id. expect_local ( ) ) ;
554+ let Some ( trait_def_id) = trait_def_id. as_local ( ) else {
555+ // if `assoc_name` is None, then the query should've been redirected to an
556+ // external provider
557+ assert ! ( assoc_name. is_some( ) ) ;
558+ return tcx. super_predicates_of ( trait_def_id) ;
559+ } ;
557560
558- let Node :: Item ( item) = tcx. hir ( ) . get ( trait_hir_id) else {
559- bug ! ( "trait_node_id {} is not an item" , trait_hir_id) ;
560- } ;
561+ debug ! ( "local trait" ) ;
562+ let trait_hir_id = tcx. hir ( ) . local_def_id_to_hir_id ( trait_def_id) ;
561563
562- let ( generics, bounds) = match item. kind {
563- hir:: ItemKind :: Trait ( .., generics, supertraits, _) => ( generics, supertraits) ,
564- hir:: ItemKind :: TraitAlias ( generics, supertraits) => ( generics, supertraits) ,
565- _ => span_bug ! ( item. span, "super_predicates invoked on non-trait" ) ,
566- } ;
564+ let Node :: Item ( item) = tcx. hir ( ) . get ( trait_hir_id) else {
565+ bug ! ( "trait_node_id {} is not an item" , trait_hir_id) ;
566+ } ;
567567
568- let icx = ItemCtxt :: new ( tcx, trait_def_id) ;
568+ let ( generics, bounds) = match item. kind {
569+ hir:: ItemKind :: Trait ( .., generics, supertraits, _) => ( generics, supertraits) ,
570+ hir:: ItemKind :: TraitAlias ( generics, supertraits) => ( generics, supertraits) ,
571+ _ => span_bug ! ( item. span, "super_predicates invoked on non-trait" ) ,
572+ } ;
569573
570- // Convert the bounds that follow the colon, e.g., `Bar + Zed` in `trait Foo: Bar + Zed`.
571- let self_param_ty = tcx. types . self_param ;
572- let superbounds1 = if let Some ( assoc_name) = assoc_name {
573- icx. astconv ( ) . compute_bounds_that_match_assoc_type ( self_param_ty, bounds, assoc_name)
574- } else {
575- icx. astconv ( ) . compute_bounds ( self_param_ty, bounds)
576- } ;
574+ let icx = ItemCtxt :: new ( tcx, trait_def_id) ;
577575
578- let superbounds1 = superbounds1. predicates ( ) ;
579-
580- // Convert any explicit superbounds in the where-clause,
581- // e.g., `trait Foo where Self: Bar`.
582- // In the case of trait aliases, however, we include all bounds in the where-clause,
583- // so e.g., `trait Foo = where u32: PartialEq<Self>` would include `u32: PartialEq<Self>`
584- // as one of its "superpredicates".
585- let is_trait_alias = tcx. is_trait_alias ( trait_def_id) ;
586- let superbounds2 = icx. type_parameter_bounds_in_generics (
587- generics,
588- item. owner_id . def_id ,
589- self_param_ty,
590- OnlySelfBounds ( !is_trait_alias) ,
591- assoc_name,
592- ) ;
576+ // Convert the bounds that follow the colon, e.g., `Bar + Zed` in `trait Foo: Bar + Zed`.
577+ let self_param_ty = tcx. types . self_param ;
578+ let superbounds1 = if let Some ( assoc_name) = assoc_name {
579+ icx. astconv ( ) . compute_bounds_that_match_assoc_type ( self_param_ty, bounds, assoc_name)
580+ } else {
581+ icx. astconv ( ) . compute_bounds ( self_param_ty, bounds)
582+ } ;
583+
584+ let superbounds1 = superbounds1. predicates ( ) ;
585+
586+ // Convert any explicit superbounds in the where-clause,
587+ // e.g., `trait Foo where Self: Bar`.
588+ // In the case of trait aliases, however, we include all bounds in the where-clause,
589+ // so e.g., `trait Foo = where u32: PartialEq<Self>` would include `u32: PartialEq<Self>`
590+ // as one of its "superpredicates".
591+ let is_trait_alias = tcx. is_trait_alias ( trait_def_id. to_def_id ( ) ) ;
592+ let superbounds2 = icx. type_parameter_bounds_in_generics (
593+ generics,
594+ item. owner_id . def_id ,
595+ self_param_ty,
596+ OnlySelfBounds ( !is_trait_alias) ,
597+ assoc_name,
598+ ) ;
593599
594- // Combine the two lists to form the complete set of superbounds:
595- let superbounds = & * tcx. arena . alloc_from_iter ( superbounds1. into_iter ( ) . chain ( superbounds2) ) ;
596- debug ! ( ?superbounds) ;
600+ // Combine the two lists to form the complete set of superbounds:
601+ let superbounds = & * tcx. arena . alloc_from_iter ( superbounds1. into_iter ( ) . chain ( superbounds2) ) ;
602+ debug ! ( ?superbounds) ;
597603
604+ // Now require that immediate supertraits are converted,
605+ // which will, in turn, reach indirect supertraits.
606+ if assoc_name. is_none ( ) {
598607 // Now require that immediate supertraits are converted,
599608 // which will, in turn, reach indirect supertraits.
600- if assoc_name. is_none ( ) {
601- // Now require that immediate supertraits are converted,
602- // which will, in turn, reach indirect supertraits.
603- for & ( pred, span) in superbounds {
604- debug ! ( "superbound: {:?}" , pred) ;
605- if let ty:: PredicateKind :: Clause ( ty:: Clause :: Trait ( bound) ) =
606- pred. kind ( ) . skip_binder ( )
607- {
608- tcx. at ( span) . super_predicates_of ( bound. def_id ( ) ) ;
609- }
609+ for & ( pred, span) in superbounds {
610+ debug ! ( "superbound: {:?}" , pred) ;
611+ if let ty:: PredicateKind :: Clause ( ty:: Clause :: Trait ( bound) ) = pred. kind ( ) . skip_binder ( ) {
612+ tcx. at ( span) . super_predicates_of ( bound. def_id ( ) ) ;
610613 }
611614 }
612-
613- ty:: GenericPredicates { parent : None , predicates : superbounds }
614- } else {
615- // if `assoc_name` is None, then the query should've been redirected to an
616- // external provider
617- assert ! ( assoc_name. is_some( ) ) ;
618- tcx. super_predicates_of ( trait_def_id)
619615 }
616+
617+ ty:: GenericPredicates { parent : None , predicates : superbounds }
620618}
621619
622620/// Returns the predicates defined on `item_def_id` of the form
623621/// `X: Foo` where `X` is the type parameter `def_id`.
624622#[ instrument( level = "trace" , skip( tcx) ) ]
625623pub ( super ) fn type_param_predicates (
626624 tcx : TyCtxt < ' _ > ,
627- ( item_def_id, def_id, assoc_name) : ( DefId , LocalDefId , Ident ) ,
625+ ( item_def_id, def_id, assoc_name) : ( LocalDefId , LocalDefId , Ident ) ,
628626) -> ty:: GenericPredicates < ' _ > {
629627 use rustc_hir:: * ;
630628
@@ -639,21 +637,21 @@ pub(super) fn type_param_predicates(
639637 let ty = tcx. mk_ty_param ( index, tcx. hir ( ) . ty_param_name ( def_id) ) ;
640638
641639 // Don't look for bounds where the type parameter isn't in scope.
642- let parent = if item_def_id == param_owner. to_def_id ( ) {
640+ let parent = if item_def_id == param_owner {
643641 None
644642 } else {
645- tcx. generics_of ( item_def_id) . parent
643+ tcx. generics_of ( item_def_id) . parent . map ( |def_id| def_id . expect_local ( ) )
646644 } ;
647645
648646 let mut result = parent
649647 . map ( |parent| {
650648 let icx = ItemCtxt :: new ( tcx, parent) ;
651- icx. get_type_parameter_bounds ( DUMMY_SP , def_id. to_def_id ( ) , assoc_name)
649+ icx. get_type_parameter_bounds ( DUMMY_SP , def_id, assoc_name)
652650 } )
653651 . unwrap_or_default ( ) ;
654652 let mut extend = None ;
655653
656- let item_hir_id = tcx. hir ( ) . local_def_id_to_hir_id ( item_def_id. expect_local ( ) ) ;
654+ let item_hir_id = tcx. hir ( ) . local_def_id_to_hir_id ( item_def_id) ;
657655 let ast_generics = match tcx. hir ( ) . get ( item_hir_id) {
658656 Node :: TraitItem ( item) => & item. generics ,
659657
@@ -675,7 +673,8 @@ pub(super) fn type_param_predicates(
675673 ItemKind :: Trait ( _, _, generics, ..) => {
676674 // Implied `Self: Trait` and supertrait bounds.
677675 if param_id == item_hir_id {
678- let identity_trait_ref = ty:: TraitRef :: identity ( tcx, item_def_id) ;
676+ let identity_trait_ref =
677+ ty:: TraitRef :: identity ( tcx, item_def_id. to_def_id ( ) ) ;
679678 extend =
680679 Some ( ( identity_trait_ref. without_const ( ) . to_predicate ( tcx) , item. span ) ) ;
681680 }
0 commit comments