@@ -5,7 +5,7 @@ use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId};
5
5
use rustc_hir:: definitions:: DefPathData ;
6
6
use rustc_hir:: intravisit:: { self , Visitor } ;
7
7
use rustc_middle:: ty:: { self , ImplTraitInTraitData , InternalSubsts , TyCtxt } ;
8
- use rustc_span:: symbol :: kw ;
8
+ use rustc_span:: Symbol ;
9
9
10
10
pub fn provide ( providers : & mut ty:: query:: Providers ) {
11
11
* providers = ty:: query:: Providers {
@@ -266,7 +266,8 @@ fn associated_type_for_impl_trait_in_trait(
266
266
assert_eq ! ( tcx. def_kind( trait_def_id) , DefKind :: Trait ) ;
267
267
268
268
let span = tcx. def_span ( opaque_ty_def_id) ;
269
- let trait_assoc_ty = tcx. at ( span) . create_def ( trait_def_id, DefPathData :: ImplTraitAssocTy ) ;
269
+ let name = name_for_impl_trait_in_trait ( tcx, opaque_ty_def_id, trait_def_id) ;
270
+ let trait_assoc_ty = tcx. at ( span) . create_def ( trait_def_id, DefPathData :: ImplTraitAssocTy ( name) ) ;
270
271
271
272
let local_def_id = trait_assoc_ty. def_id ( ) ;
272
273
let def_id = local_def_id. to_def_id ( ) ;
@@ -281,7 +282,7 @@ fn associated_type_for_impl_trait_in_trait(
281
282
trait_assoc_ty. def_ident_span ( Some ( span) ) ;
282
283
283
284
trait_assoc_ty. associated_item ( ty:: AssocItem {
284
- name : kw :: Empty ,
285
+ name,
285
286
kind : ty:: AssocKind :: Type ,
286
287
def_id,
287
288
trait_item_def_id : None ,
@@ -351,6 +352,24 @@ fn associated_type_for_impl_trait_in_trait(
351
352
local_def_id
352
353
}
353
354
355
+ /// Create a stable path name for an associated type for an impl trait in trait
356
+ /// by appending the opaque type's path segments starting from the function name.
357
+ fn name_for_impl_trait_in_trait (
358
+ tcx : TyCtxt < ' _ > ,
359
+ opaque_ty_def_id : LocalDefId ,
360
+ trait_def_id : LocalDefId ,
361
+ ) -> Symbol {
362
+ let mut name = vec ! [ ] ;
363
+ let mut def_id = opaque_ty_def_id;
364
+ while def_id != trait_def_id {
365
+ name. push ( tcx. def_key ( def_id. to_def_id ( ) ) . disambiguated_data . to_string ( ) ) ;
366
+ def_id = tcx. local_parent ( def_id) ;
367
+ }
368
+ name. reverse ( ) ;
369
+ let name = Symbol :: intern ( & name. join ( "::" ) ) ;
370
+ name
371
+ }
372
+
354
373
/// Given an `trait_assoc_def_id` corresponding to an associated item synthesized
355
374
/// from an `impl Trait` in an associated function from a trait, and an
356
375
/// `impl_fn_def_id` that represents an implementation of the associated function
@@ -364,9 +383,11 @@ fn associated_type_for_impl_trait_in_impl(
364
383
let impl_local_def_id = tcx. local_parent ( impl_fn_def_id) ;
365
384
let impl_def_id = impl_local_def_id. to_def_id ( ) ;
366
385
386
+ let name = tcx. item_name ( trait_assoc_def_id) ;
367
387
// FIXME fix the span, we probably want the def_id of the return type of the function
368
388
let span = tcx. def_span ( impl_fn_def_id) ;
369
- let impl_assoc_ty = tcx. at ( span) . create_def ( impl_local_def_id, DefPathData :: ImplTraitAssocTy ) ;
389
+ let impl_assoc_ty =
390
+ tcx. at ( span) . create_def ( impl_local_def_id, DefPathData :: ImplTraitAssocTy ( name) ) ;
370
391
371
392
let local_def_id = impl_assoc_ty. def_id ( ) ;
372
393
let def_id = local_def_id. to_def_id ( ) ;
@@ -381,7 +402,7 @@ fn associated_type_for_impl_trait_in_impl(
381
402
impl_assoc_ty. def_ident_span ( Some ( span) ) ;
382
403
383
404
impl_assoc_ty. associated_item ( ty:: AssocItem {
384
- name : kw :: Empty ,
405
+ name,
385
406
kind : ty:: AssocKind :: Type ,
386
407
def_id,
387
408
trait_item_def_id : Some ( trait_assoc_def_id) ,
0 commit comments