1
1
use super :: ItemCtxt ;
2
2
use crate :: astconv:: { AstConv , PredicateFilter } ;
3
+ use rustc_data_structures:: fx:: FxIndexSet ;
3
4
use rustc_hir as hir;
4
5
use rustc_infer:: traits:: util;
5
6
use rustc_middle:: ty:: GenericArgs ;
@@ -19,6 +20,7 @@ fn associated_type_bounds<'tcx>(
19
20
assoc_item_def_id : LocalDefId ,
20
21
ast_bounds : & ' tcx [ hir:: GenericBound < ' tcx > ] ,
21
22
span : Span ,
23
+ filter : PredicateFilter ,
22
24
) -> & ' tcx [ ( ty:: Clause < ' tcx > , Span ) ] {
23
25
let item_ty = Ty :: new_projection (
24
26
tcx,
@@ -27,7 +29,7 @@ fn associated_type_bounds<'tcx>(
27
29
) ;
28
30
29
31
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 ) ;
31
33
// Associated types are implicitly sized unless a `?Sized` bound is found
32
34
icx. astconv ( ) . add_implicitly_sized ( & mut bounds, item_ty, ast_bounds, None , span) ;
33
35
@@ -63,10 +65,11 @@ fn opaque_type_bounds<'tcx>(
63
65
ast_bounds : & ' tcx [ hir:: GenericBound < ' tcx > ] ,
64
66
item_ty : Ty < ' tcx > ,
65
67
span : Span ,
68
+ filter : PredicateFilter ,
66
69
) -> & ' tcx [ ( ty:: Clause < ' tcx > , Span ) ] {
67
70
ty:: print:: with_no_queries!( {
68
71
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 ) ;
70
73
// Opaque types are implicitly sized unless a `?Sized` bound is found
71
74
icx. astconv( ) . add_implicitly_sized( & mut bounds, item_ty, ast_bounds, None , span) ;
72
75
debug!( ?bounds) ;
@@ -78,6 +81,21 @@ fn opaque_type_bounds<'tcx>(
78
81
pub ( super ) fn explicit_item_bounds (
79
82
tcx : TyCtxt < ' _ > ,
80
83
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 ,
81
99
) -> ty:: EarlyBinder < & ' _ [ ( ty:: Clause < ' _ > , Span ) ] > {
82
100
match tcx. opt_rpitit_info ( def_id. to_def_id ( ) ) {
83
101
// RPITIT's bounds are the same as opaque type bounds, but with
@@ -95,6 +113,7 @@ pub(super) fn explicit_item_bounds(
95
113
ty:: GenericArgs :: identity_for_item ( tcx, def_id) ,
96
114
) ,
97
115
item. span ,
116
+ filter,
98
117
) ) ;
99
118
}
100
119
Some ( ty:: ImplTraitInTraitData :: Impl { .. } ) => span_bug ! (
@@ -109,15 +128,15 @@ pub(super) fn explicit_item_bounds(
109
128
kind : hir:: TraitItemKind :: Type ( bounds, _) ,
110
129
span,
111
130
..
112
- } ) => associated_type_bounds ( tcx, def_id, bounds, * span) ,
131
+ } ) => associated_type_bounds ( tcx, def_id, bounds, * span, filter ) ,
113
132
hir:: Node :: Item ( hir:: Item {
114
133
kind : hir:: ItemKind :: OpaqueTy ( hir:: OpaqueTy { bounds, in_trait : false , .. } ) ,
115
134
span,
116
135
..
117
136
} ) => {
118
137
let args = GenericArgs :: identity_for_item ( tcx, def_id) ;
119
138
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 )
121
140
}
122
141
// Since RPITITs are astconv'd as projections in `ast_ty_to_ty`, when we're asking
123
142
// 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(
135
154
let args = GenericArgs :: identity_for_item ( tcx, def_id) ;
136
155
let item_ty = Ty :: new_opaque ( tcx, def_id. to_def_id ( ) , args) ;
137
156
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 )
139
158
. to_vec ( )
140
159
. fold_with ( & mut AssocTyToOpaque { tcx, fn_def_id : fn_def_id. to_def_id ( ) } ) ,
141
160
)
@@ -155,6 +174,30 @@ pub(super) fn item_bounds(
155
174
} )
156
175
}
157
176
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
+
158
201
struct AssocTyToOpaque < ' tcx > {
159
202
tcx : TyCtxt < ' tcx > ,
160
203
fn_def_id : DefId ,
0 commit comments