@@ -3,7 +3,7 @@ use crate::astconv::{AstConv, PredicateFilter};
3
3
use rustc_hir as hir;
4
4
use rustc_infer:: traits:: util;
5
5
use rustc_middle:: ty:: GenericArgs ;
6
- use rustc_middle:: ty:: { self , Ty , TyCtxt } ;
6
+ use rustc_middle:: ty:: { self , Ty , TyCtxt , TypeFoldable , TypeFolder } ;
7
7
use rustc_span:: def_id:: { DefId , LocalDefId } ;
8
8
use rustc_span:: Span ;
9
9
@@ -113,14 +113,35 @@ pub(super) fn explicit_item_bounds(
113
113
..
114
114
} ) => associated_type_bounds ( tcx, def_id, bounds, * span) ,
115
115
hir:: Node :: Item ( hir:: Item {
116
- kind : hir:: ItemKind :: OpaqueTy ( hir:: OpaqueTy { bounds, .. } ) ,
116
+ kind : hir:: ItemKind :: OpaqueTy ( hir:: OpaqueTy { bounds, in_trait : false , .. } ) ,
117
117
span,
118
118
..
119
119
} ) => {
120
120
let args = GenericArgs :: identity_for_item ( tcx, def_id) ;
121
121
let item_ty = Ty :: new_opaque ( tcx, def_id. to_def_id ( ) , args) ;
122
122
opaque_type_bounds ( tcx, def_id, bounds, item_ty, * span)
123
123
}
124
+ // Since RPITITs are astconv'd as projections in `ast_ty_to_ty`, when we're asking
125
+ // for the item bounds of the *opaques* in a trait's default method signature, we
126
+ // need to map these projections back to opaques.
127
+ hir:: Node :: Item ( hir:: Item {
128
+ kind : hir:: ItemKind :: OpaqueTy ( hir:: OpaqueTy { bounds, in_trait : true , origin, .. } ) ,
129
+ span,
130
+ ..
131
+ } ) => {
132
+ let ( hir:: OpaqueTyOrigin :: FnReturn ( fn_def_id)
133
+ | hir:: OpaqueTyOrigin :: AsyncFn ( fn_def_id) ) = * origin
134
+ else {
135
+ bug ! ( )
136
+ } ;
137
+ let args = GenericArgs :: identity_for_item ( tcx, def_id) ;
138
+ let item_ty = Ty :: new_opaque ( tcx, def_id. to_def_id ( ) , args) ;
139
+ tcx. arena . alloc_slice (
140
+ & opaque_type_bounds ( tcx, def_id, bounds, item_ty, * span)
141
+ . to_vec ( )
142
+ . fold_with ( & mut AssocTyToOpaque { tcx, fn_def_id : fn_def_id. to_def_id ( ) } ) ,
143
+ )
144
+ }
124
145
hir:: Node :: Item ( hir:: Item { kind : hir:: ItemKind :: TyAlias ( ..) , .. } ) => & [ ] ,
125
146
_ => bug ! ( "item_bounds called on {:?}" , def_id) ,
126
147
} ;
@@ -135,3 +156,26 @@ pub(super) fn item_bounds(
135
156
tcx. mk_clauses_from_iter ( util:: elaborate ( tcx, bounds. iter ( ) . map ( |& ( bound, _span) | bound) ) )
136
157
} )
137
158
}
159
+
160
+ struct AssocTyToOpaque < ' tcx > {
161
+ tcx : TyCtxt < ' tcx > ,
162
+ fn_def_id : DefId ,
163
+ }
164
+
165
+ impl < ' tcx > TypeFolder < TyCtxt < ' tcx > > for AssocTyToOpaque < ' tcx > {
166
+ fn interner ( & self ) -> TyCtxt < ' tcx > {
167
+ self . tcx
168
+ }
169
+
170
+ fn fold_ty ( & mut self , ty : Ty < ' tcx > ) -> Ty < ' tcx > {
171
+ if let ty:: Alias ( ty:: Projection , projection_ty) = ty. kind ( )
172
+ && let Some ( ty:: ImplTraitInTraitData :: Trait { fn_def_id, .. } )
173
+ = self . tcx . opt_rpitit_info ( projection_ty. def_id )
174
+ && fn_def_id == self . fn_def_id
175
+ {
176
+ self . tcx . type_of ( projection_ty. def_id ) . instantiate ( self . tcx , projection_ty. args )
177
+ } else {
178
+ ty
179
+ }
180
+ }
181
+ }
0 commit comments