11use super :: ItemCtxt ;
22use crate :: astconv:: { AstConv , PredicateFilter } ;
3+ use rustc_data_structures:: fx:: FxIndexSet ;
34use rustc_hir as hir;
45use rustc_infer:: traits:: util;
56use rustc_middle:: ty:: GenericArgs ;
@@ -19,6 +20,7 @@ fn associated_type_bounds<'tcx>(
1920 assoc_item_def_id : LocalDefId ,
2021 ast_bounds : & ' tcx [ hir:: GenericBound < ' tcx > ] ,
2122 span : Span ,
23+ filter : PredicateFilter ,
2224) -> & ' tcx [ ( ty:: Clause < ' tcx > , Span ) ] {
2325 let item_ty = Ty :: new_projection (
2426 tcx,
@@ -27,7 +29,7 @@ fn associated_type_bounds<'tcx>(
2729 ) ;
2830
2931 let icx = ItemCtxt :: new ( tcx, assoc_item_def_id) ;
30- let mut bounds = icx. astconv ( ) . compute_bounds ( item_ty, ast_bounds, PredicateFilter :: All ) ;
32+ let mut bounds = icx. astconv ( ) . compute_bounds ( item_ty, ast_bounds, filter ) ;
3133 // Associated types are implicitly sized unless a `?Sized` bound is found
3234 icx. astconv ( ) . add_implicitly_sized ( & mut bounds, item_ty, ast_bounds, None , span) ;
3335
@@ -63,10 +65,11 @@ fn opaque_type_bounds<'tcx>(
6365 ast_bounds : & ' tcx [ hir:: GenericBound < ' tcx > ] ,
6466 item_ty : Ty < ' tcx > ,
6567 span : Span ,
68+ filter : PredicateFilter ,
6669) -> & ' tcx [ ( ty:: Clause < ' tcx > , Span ) ] {
6770 ty:: print:: with_no_queries!( {
6871 let icx = ItemCtxt :: new( tcx, opaque_def_id) ;
69- let mut bounds = icx. astconv( ) . compute_bounds( item_ty, ast_bounds, PredicateFilter :: All ) ;
72+ let mut bounds = icx. astconv( ) . compute_bounds( item_ty, ast_bounds, filter ) ;
7073 // Opaque types are implicitly sized unless a `?Sized` bound is found
7174 icx. astconv( ) . add_implicitly_sized( & mut bounds, item_ty, ast_bounds, None , span) ;
7275 debug!( ?bounds) ;
@@ -78,6 +81,21 @@ fn opaque_type_bounds<'tcx>(
7881pub ( super ) fn explicit_item_bounds (
7982 tcx : TyCtxt < ' _ > ,
8083 def_id : LocalDefId ,
84+ ) -> ty:: EarlyBinder < & ' _ [ ( ty:: Clause < ' _ > , Span ) ] > {
85+ explicit_item_bounds_with_filter ( tcx, def_id, PredicateFilter :: All )
86+ }
87+
88+ pub ( super ) fn explicit_item_own_assumptions (
89+ tcx : TyCtxt < ' _ > ,
90+ def_id : LocalDefId ,
91+ ) -> ty:: EarlyBinder < & ' _ [ ( ty:: Clause < ' _ > , Span ) ] > {
92+ explicit_item_bounds_with_filter ( tcx, def_id, PredicateFilter :: SelfOnly )
93+ }
94+
95+ pub ( super ) fn explicit_item_bounds_with_filter (
96+ tcx : TyCtxt < ' _ > ,
97+ def_id : LocalDefId ,
98+ filter : PredicateFilter ,
8199) -> ty:: EarlyBinder < & ' _ [ ( ty:: Clause < ' _ > , Span ) ] > {
82100 match tcx. opt_rpitit_info ( def_id. to_def_id ( ) ) {
83101 // RPITIT's bounds are the same as opaque type bounds, but with
@@ -95,6 +113,7 @@ pub(super) fn explicit_item_bounds(
95113 ty:: GenericArgs :: identity_for_item ( tcx, def_id) ,
96114 ) ,
97115 item. span ,
116+ filter,
98117 ) ) ;
99118 }
100119 Some ( ty:: ImplTraitInTraitData :: Impl { .. } ) => span_bug ! (
@@ -109,15 +128,15 @@ pub(super) fn explicit_item_bounds(
109128 kind : hir:: TraitItemKind :: Type ( bounds, _) ,
110129 span,
111130 ..
112- } ) => associated_type_bounds ( tcx, def_id, bounds, * span) ,
131+ } ) => associated_type_bounds ( tcx, def_id, bounds, * span, filter ) ,
113132 hir:: Node :: Item ( hir:: Item {
114133 kind : hir:: ItemKind :: OpaqueTy ( hir:: OpaqueTy { bounds, in_trait : false , .. } ) ,
115134 span,
116135 ..
117136 } ) => {
118137 let args = GenericArgs :: identity_for_item ( tcx, def_id) ;
119138 let item_ty = Ty :: new_opaque ( tcx, def_id. to_def_id ( ) , args) ;
120- opaque_type_bounds ( tcx, def_id, bounds, item_ty, * span)
139+ opaque_type_bounds ( tcx, def_id, bounds, item_ty, * span, filter )
121140 }
122141 // Since RPITITs are astconv'd as projections in `ast_ty_to_ty`, when we're asking
123142 // for the item bounds of the *opaques* in a trait's default method signature, we
@@ -135,7 +154,7 @@ pub(super) fn explicit_item_bounds(
135154 let args = GenericArgs :: identity_for_item ( tcx, def_id) ;
136155 let item_ty = Ty :: new_opaque ( tcx, def_id. to_def_id ( ) , args) ;
137156 tcx. arena . alloc_slice (
138- & opaque_type_bounds ( tcx, def_id, bounds, item_ty, * span)
157+ & opaque_type_bounds ( tcx, def_id, bounds, item_ty, * span, filter )
139158 . to_vec ( )
140159 . fold_with ( & mut AssocTyToOpaque { tcx, fn_def_id : fn_def_id. to_def_id ( ) } ) ,
141160 )
@@ -155,6 +174,30 @@ pub(super) fn item_bounds(
155174 } )
156175}
157176
177+ pub ( super ) fn item_own_assumptions (
178+ tcx : TyCtxt < ' _ > ,
179+ def_id : DefId ,
180+ ) -> ty:: EarlyBinder < & ' _ ty:: List < ty:: Clause < ' _ > > > {
181+ tcx. explicit_item_own_assumptions ( def_id) . map_bound ( |bounds| {
182+ tcx. mk_clauses_from_iter (
183+ util:: elaborate ( tcx, bounds. iter ( ) . map ( |& ( bound, _span) | bound) ) . filter_only_self ( ) ,
184+ )
185+ } )
186+ }
187+
188+ pub ( super ) fn item_non_self_assumptions (
189+ tcx : TyCtxt < ' _ > ,
190+ def_id : DefId ,
191+ ) -> ty:: EarlyBinder < & ' _ ty:: List < ty:: Clause < ' _ > > > {
192+ let all_bounds: FxIndexSet < _ > = tcx. item_bounds ( def_id) . skip_binder ( ) . iter ( ) . collect ( ) ;
193+ let own_bounds: FxIndexSet < _ > = tcx. item_own_assumptions ( def_id) . skip_binder ( ) . iter ( ) . collect ( ) ;
194+ if all_bounds. len ( ) == own_bounds. len ( ) {
195+ ty:: EarlyBinder :: bind ( ty:: List :: empty ( ) )
196+ } else {
197+ ty:: EarlyBinder :: bind ( tcx. mk_clauses_from_iter ( all_bounds. difference ( & own_bounds) . copied ( ) ) )
198+ }
199+ }
200+
158201struct AssocTyToOpaque < ' tcx > {
159202 tcx : TyCtxt < ' tcx > ,
160203 fn_def_id : DefId ,
0 commit comments