1
- use rustc_data_structures :: fx :: FxIndexSet ;
1
+ use rustc_hir as hir ;
2
2
use rustc_hir:: def:: DefKind ;
3
3
use rustc_hir:: def_id:: { DefId , DefIdMap , LocalDefId } ;
4
4
use rustc_hir:: definitions:: { DefPathData , DisambiguatorState } ;
5
5
use rustc_hir:: intravisit:: { self , Visitor } ;
6
- use rustc_hir:: { self as hir, AmbigArg } ;
7
6
use rustc_middle:: query:: Providers ;
8
7
use rustc_middle:: ty:: { self , ImplTraitInTraitData , TyCtxt } ;
9
8
use rustc_middle:: { bug, span_bug} ;
@@ -14,7 +13,6 @@ pub(crate) fn provide(providers: &mut Providers) {
14
13
associated_item_def_ids,
15
14
associated_items,
16
15
associated_types_for_impl_traits_in_associated_fn,
17
- associated_type_for_impl_trait_in_trait,
18
16
impl_item_implementor_ids,
19
17
..* providers
20
18
} ;
@@ -160,20 +158,22 @@ fn associated_item_from_impl_item_ref(impl_item_ref: &hir::ImplItemRef) -> ty::A
160
158
container : ty:: AssocItemContainer :: Impl ,
161
159
}
162
160
}
163
- struct RPITVisitor {
164
- rpits : FxIndexSet < LocalDefId > ,
161
+ struct RPITVisitor < ' tcx > {
162
+ tcx : TyCtxt < ' tcx > ,
163
+ synthetics : Vec < LocalDefId > ,
164
+ data : DefPathData ,
165
+ disambiguator : DisambiguatorState ,
165
166
}
166
167
167
- impl < ' tcx > Visitor < ' tcx > for RPITVisitor {
168
- fn visit_ty ( & mut self , ty : & ' tcx hir:: Ty < ' tcx , AmbigArg > ) {
169
- if let hir:: TyKind :: OpaqueDef ( opaq) = ty. kind
170
- && self . rpits . insert ( opaq. def_id )
171
- {
172
- for bound in opaq. bounds {
173
- intravisit:: walk_param_bound ( self , bound) ;
174
- }
175
- }
176
- intravisit:: walk_ty ( self , ty)
168
+ impl < ' tcx > Visitor < ' tcx > for RPITVisitor < ' tcx > {
169
+ fn visit_opaque_ty ( & mut self , opaque : & ' tcx hir:: OpaqueTy < ' tcx > ) -> Self :: Result {
170
+ self . synthetics . push ( associated_type_for_impl_trait_in_trait (
171
+ self . tcx ,
172
+ opaque. def_id ,
173
+ self . data ,
174
+ & mut self . disambiguator ,
175
+ ) ) ;
176
+ intravisit:: walk_opaque_ty ( self , opaque)
177
177
}
178
178
}
179
179
@@ -194,14 +194,18 @@ fn associated_types_for_impl_traits_in_associated_fn(
194
194
195
195
match tcx. def_kind ( parent_def_id) {
196
196
DefKind :: Trait => {
197
- let mut visitor = RPITVisitor { rpits : FxIndexSet :: default ( ) } ;
198
-
199
197
if let Some ( output) = tcx. hir_get_fn_output ( fn_def_id) {
198
+ let data = DefPathData :: AnonAssocTy ( tcx. item_name ( fn_def_id. to_def_id ( ) ) ) ;
199
+ let mut visitor = RPITVisitor {
200
+ tcx,
201
+ synthetics : vec ! [ ] ,
202
+ data,
203
+ disambiguator : DisambiguatorState :: with ( parent_def_id, data, 0 ) ,
204
+ } ;
200
205
visitor. visit_fn_ret_ty ( output) ;
201
-
202
- tcx. arena . alloc_from_iter ( visitor. rpits . iter ( ) . map ( |opaque_ty_def_id| {
203
- tcx. associated_type_for_impl_trait_in_trait ( opaque_ty_def_id) . to_def_id ( )
204
- } ) )
206
+ tcx. arena . alloc_from_iter (
207
+ visitor. synthetics . into_iter ( ) . map ( |def_id| def_id. to_def_id ( ) ) ,
208
+ )
205
209
} else {
206
210
& [ ]
207
211
}
@@ -211,7 +215,6 @@ fn associated_types_for_impl_traits_in_associated_fn(
211
215
let Some ( trait_fn_def_id) = tcx. associated_item ( fn_def_id) . trait_item_def_id else {
212
216
return & [ ] ;
213
217
} ;
214
-
215
218
tcx. arena . alloc_from_iter (
216
219
tcx. associated_types_for_impl_traits_in_associated_fn ( trait_fn_def_id) . iter ( ) . map (
217
220
move |& trait_assoc_def_id| {
@@ -236,6 +239,8 @@ fn associated_types_for_impl_traits_in_associated_fn(
236
239
fn associated_type_for_impl_trait_in_trait (
237
240
tcx : TyCtxt < ' _ > ,
238
241
opaque_ty_def_id : LocalDefId ,
242
+ data : DefPathData ,
243
+ disambiguator : & mut DisambiguatorState ,
239
244
) -> LocalDefId {
240
245
let ( hir:: OpaqueTyOrigin :: FnReturn { parent : fn_def_id, .. }
241
246
| hir:: OpaqueTyOrigin :: AsyncFn { parent : fn_def_id, .. } ) =
@@ -246,22 +251,15 @@ fn associated_type_for_impl_trait_in_trait(
246
251
let trait_def_id = tcx. local_parent ( fn_def_id) ;
247
252
assert_eq ! ( tcx. def_kind( trait_def_id) , DefKind :: Trait ) ;
248
253
249
- // Collect all opaque types in return position for the method and use
250
- // the index as the disambiguator to make an unique def path.
251
- let mut visitor = RPITVisitor { rpits : FxIndexSet :: default ( ) } ;
252
- visitor. visit_fn_ret_ty ( tcx. hir_get_fn_output ( fn_def_id) . unwrap ( ) ) ;
253
- let disambiguator = visitor. rpits . get_index_of ( & opaque_ty_def_id) . unwrap ( ) . try_into ( ) . unwrap ( ) ;
254
-
255
254
let span = tcx. def_span ( opaque_ty_def_id) ;
256
255
// Also use the method name to create an unique def path.
257
- let data = DefPathData :: AnonAssocTy ( tcx. item_name ( fn_def_id. to_def_id ( ) ) ) ;
258
256
let trait_assoc_ty = tcx. at ( span) . create_def (
259
257
trait_def_id,
260
258
// No name because this is an anonymous associated type.
261
259
None ,
262
260
DefKind :: AssocTy ,
263
261
Some ( data) ,
264
- & mut DisambiguatorState :: with ( trait_def_id , data , disambiguator) ,
262
+ disambiguator,
265
263
) ;
266
264
267
265
let local_def_id = trait_assoc_ty. def_id ( ) ;
0 commit comments