1
+ use rustc_data_structures:: fx:: FxIndexSet ;
1
2
use rustc_hir as hir;
2
3
use rustc_hir:: def:: DefKind ;
3
4
use rustc_hir:: def_id:: { DefId , DefIdMap , LocalDefId } ;
@@ -196,20 +197,26 @@ fn associated_types_for_impl_traits_in_associated_fn(
196
197
197
198
match tcx. def_kind ( parent_def_id) {
198
199
DefKind :: Trait => {
199
- struct RPITVisitor {
200
- rpits : Vec < LocalDefId > ,
200
+ struct RPITVisitor < ' tcx > {
201
+ rpits : FxIndexSet < LocalDefId > ,
202
+ tcx : TyCtxt < ' tcx > ,
201
203
}
202
204
203
- impl < ' v > Visitor < ' v > for RPITVisitor {
204
- fn visit_ty ( & mut self , ty : & ' v hir:: Ty < ' v > ) {
205
- if let hir:: TyKind :: OpaqueDef ( item_id, _, _) = ty. kind {
206
- self . rpits . push ( item_id. owner_id . def_id )
205
+ impl < ' tcx > Visitor < ' tcx > for RPITVisitor < ' tcx > {
206
+ fn visit_ty ( & mut self , ty : & ' tcx hir:: Ty < ' tcx > ) {
207
+ if let hir:: TyKind :: OpaqueDef ( item_id, _, _) = ty. kind
208
+ && self . rpits . insert ( item_id. owner_id . def_id )
209
+ {
210
+ let opaque_item = self . tcx . hir ( ) . expect_item ( item_id. owner_id . def_id ) . expect_opaque_ty ( ) ;
211
+ for bound in opaque_item. bounds {
212
+ intravisit:: walk_param_bound ( self , bound) ;
213
+ }
207
214
}
208
215
intravisit:: walk_ty ( self , ty)
209
216
}
210
217
}
211
218
212
- let mut visitor = RPITVisitor { rpits : Vec :: new ( ) } ;
219
+ let mut visitor = RPITVisitor { tcx , rpits : FxIndexSet :: default ( ) } ;
213
220
214
221
if let Some ( output) = tcx. hir ( ) . get_fn_output ( fn_def_id) {
215
222
visitor. visit_fn_ret_ty ( output) ;
@@ -227,13 +234,9 @@ fn associated_types_for_impl_traits_in_associated_fn(
227
234
228
235
tcx. arena . alloc_from_iter (
229
236
tcx. associated_types_for_impl_traits_in_associated_fn ( trait_fn_def_id) . iter ( ) . map (
230
- move |trait_assoc_def_id| {
231
- associated_type_for_impl_trait_in_impl (
232
- tcx,
233
- trait_assoc_def_id. expect_local ( ) ,
234
- fn_def_id,
235
- )
236
- . to_def_id ( )
237
+ move |& trait_assoc_def_id| {
238
+ associated_type_for_impl_trait_in_impl ( tcx, trait_assoc_def_id, fn_def_id)
239
+ . to_def_id ( )
237
240
} ,
238
241
) ,
239
242
)
@@ -254,13 +257,16 @@ fn associated_type_for_impl_trait_in_trait(
254
257
tcx : TyCtxt < ' _ > ,
255
258
opaque_ty_def_id : LocalDefId ,
256
259
) -> LocalDefId {
257
- let fn_def_id = tcx. impl_trait_in_trait_parent_fn ( opaque_ty_def_id. to_def_id ( ) ) ;
258
- let trait_def_id = tcx. parent ( fn_def_id) ;
260
+ let ( hir:: OpaqueTyOrigin :: FnReturn ( fn_def_id) | hir:: OpaqueTyOrigin :: AsyncFn ( fn_def_id) ) =
261
+ tcx. hir ( ) . expect_item ( opaque_ty_def_id) . expect_opaque_ty ( ) . origin
262
+ else {
263
+ bug ! ( "expected opaque for {opaque_ty_def_id:?}" ) ;
264
+ } ;
265
+ let trait_def_id = tcx. local_parent ( fn_def_id) ;
259
266
assert_eq ! ( tcx. def_kind( trait_def_id) , DefKind :: Trait ) ;
260
267
261
268
let span = tcx. def_span ( opaque_ty_def_id) ;
262
- let trait_assoc_ty =
263
- tcx. at ( span) . create_def ( trait_def_id. expect_local ( ) , DefPathData :: ImplTraitAssocTy ) ;
269
+ let trait_assoc_ty = tcx. at ( span) . create_def ( trait_def_id, DefPathData :: ImplTraitAssocTy ) ;
264
270
265
271
let local_def_id = trait_assoc_ty. def_id ( ) ;
266
272
let def_id = local_def_id. to_def_id ( ) ;
@@ -282,7 +288,7 @@ fn associated_type_for_impl_trait_in_trait(
282
288
container : ty:: TraitContainer ,
283
289
fn_has_self_parameter : false ,
284
290
opt_rpitit_info : Some ( ImplTraitInTraitData :: Trait {
285
- fn_def_id,
291
+ fn_def_id : fn_def_id . to_def_id ( ) ,
286
292
opaque_def_id : opaque_ty_def_id. to_def_id ( ) ,
287
293
} ) ,
288
294
} ) ;
@@ -324,7 +330,7 @@ fn associated_type_for_impl_trait_in_trait(
324
330
params. iter ( ) . map ( |param| ( param. def_id , param. index ) ) . collect ( ) ;
325
331
326
332
ty:: Generics {
327
- parent : Some ( trait_def_id) ,
333
+ parent : Some ( trait_def_id. to_def_id ( ) ) ,
328
334
parent_count,
329
335
params,
330
336
param_def_id_to_index,
@@ -335,7 +341,7 @@ fn associated_type_for_impl_trait_in_trait(
335
341
336
342
// There are no predicates for the synthesized associated type.
337
343
trait_assoc_ty. explicit_predicates_of ( ty:: GenericPredicates {
338
- parent : Some ( trait_def_id) ,
344
+ parent : Some ( trait_def_id. to_def_id ( ) ) ,
339
345
predicates : & [ ] ,
340
346
} ) ;
341
347
@@ -352,7 +358,7 @@ fn associated_type_for_impl_trait_in_trait(
352
358
/// that inherits properties that we infer from the method and the associated type.
353
359
fn associated_type_for_impl_trait_in_impl (
354
360
tcx : TyCtxt < ' _ > ,
355
- trait_assoc_def_id : LocalDefId ,
361
+ trait_assoc_def_id : DefId ,
356
362
impl_fn_def_id : LocalDefId ,
357
363
) -> LocalDefId {
358
364
let impl_local_def_id = tcx. local_parent ( impl_fn_def_id) ;
@@ -378,7 +384,7 @@ fn associated_type_for_impl_trait_in_impl(
378
384
name : kw:: Empty ,
379
385
kind : ty:: AssocKind :: Type ,
380
386
def_id,
381
- trait_item_def_id : Some ( trait_assoc_def_id. to_def_id ( ) ) ,
387
+ trait_item_def_id : Some ( trait_assoc_def_id) ,
382
388
container : ty:: ImplContainer ,
383
389
fn_has_self_parameter : false ,
384
390
opt_rpitit_info : Some ( ImplTraitInTraitData :: Impl { fn_def_id : impl_fn_def_id. to_def_id ( ) } ) ,
0 commit comments