@@ -4,19 +4,19 @@ use chalk_ir::{
4
4
FloatTy , IntTy , Mutability , Scalar , TyVariableKind , TypeOutlives , UintTy , cast:: Cast ,
5
5
} ;
6
6
use hir_def:: {
7
- DefWithBodyId , FunctionId , GenericDefId , HasModule , ItemContainerId , Lookup , TraitId ,
7
+ DefWithBodyId , FunctionId , HasModule , ItemContainerId , Lookup , TraitId ,
8
8
builtin_type:: { BuiltinFloat , BuiltinInt , BuiltinType , BuiltinUint } ,
9
9
generics:: TypeOrConstParamData ,
10
10
lang_item:: LangItem ,
11
11
type_ref:: Rawness ,
12
12
} ;
13
13
14
14
use crate :: {
15
- AdtId , AliasEq , AliasTy , Binders , CallableDefId , CallableSig , Canonical , CanonicalVarKinds ,
16
- ClosureId , DynTy , FnPointer , ImplTraitId , InEnvironment , Interner , Lifetime , ProjectionTy ,
17
- QuantifiedWhereClause , Substitution , TraitRef , Ty , TyBuilder , TyKind , TypeFlags , WhereClause ,
18
- db:: HirDatabase , from_assoc_type_id, from_chalk_trait_id, from_foreign_def_id ,
19
- from_placeholder_idx , generics:: generics, to_chalk_trait_id, utils:: ClosureSubst ,
15
+ AdtId , AliasEq , AliasTy , AssocTypeId , Binders , CallableDefId , CallableSig , Canonical ,
16
+ CanonicalVarKinds , ClosureId , DynTy , FnPointer , ImplTraitId , InEnvironment , Interner , Lifetime ,
17
+ ProjectionTy , QuantifiedWhereClause , Substitution , TraitRef , Ty , TyBuilder , TyKind , TypeFlags ,
18
+ WhereClause , db:: HirDatabase , from_assoc_type_id, from_chalk_trait_id, from_placeholder_idx ,
19
+ generics:: generics, mapping :: AnyTraitAssocType , to_chalk_trait_id, utils:: ClosureSubst ,
20
20
} ;
21
21
22
22
pub trait TyExt {
@@ -39,7 +39,6 @@ pub trait TyExt {
39
39
fn as_reference ( & self ) -> Option < ( & Ty , Lifetime , Mutability ) > ;
40
40
fn as_raw_ptr ( & self ) -> Option < ( & Ty , Mutability ) > ;
41
41
fn as_reference_or_ptr ( & self ) -> Option < ( & Ty , Rawness , Mutability ) > ;
42
- fn as_generic_def ( & self , db : & dyn HirDatabase ) -> Option < GenericDefId > ;
43
42
44
43
fn callable_def ( & self , db : & dyn HirDatabase ) -> Option < CallableDefId > ;
45
44
fn callable_sig ( & self , db : & dyn HirDatabase ) -> Option < CallableSig > ;
@@ -187,19 +186,6 @@ impl TyExt for Ty {
187
186
}
188
187
}
189
188
190
- fn as_generic_def ( & self , db : & dyn HirDatabase ) -> Option < GenericDefId > {
191
- match * self . kind ( Interner ) {
192
- TyKind :: Adt ( AdtId ( adt) , ..) => Some ( adt. into ( ) ) ,
193
- TyKind :: FnDef ( callable, ..) => Some ( GenericDefId :: from_callable (
194
- db. upcast ( ) ,
195
- db. lookup_intern_callable_def ( callable. into ( ) ) ,
196
- ) ) ,
197
- TyKind :: AssociatedType ( type_alias, ..) => Some ( from_assoc_type_id ( type_alias) . into ( ) ) ,
198
- TyKind :: Foreign ( type_alias, ..) => Some ( from_foreign_def_id ( type_alias) . into ( ) ) ,
199
- _ => None ,
200
- }
201
- }
202
-
203
189
fn callable_def ( & self , db : & dyn HirDatabase ) -> Option < CallableDefId > {
204
190
match self . kind ( Interner ) {
205
191
& TyKind :: FnDef ( def, ..) => Some ( db. lookup_intern_callable_def ( def. into ( ) ) ) ,
@@ -348,20 +334,9 @@ impl TyExt for Ty {
348
334
349
335
fn associated_type_parent_trait ( & self , db : & dyn HirDatabase ) -> Option < TraitId > {
350
336
match self . kind ( Interner ) {
351
- TyKind :: AssociatedType ( id, ..) => {
352
- match from_assoc_type_id ( * id) . lookup ( db. upcast ( ) ) . container {
353
- ItemContainerId :: TraitId ( trait_id) => Some ( trait_id) ,
354
- _ => None ,
355
- }
356
- }
337
+ TyKind :: AssociatedType ( id, ..) => Some ( assoc_type_parent_trait ( db, * id) ) ,
357
338
TyKind :: Alias ( AliasTy :: Projection ( projection_ty) ) => {
358
- match from_assoc_type_id ( projection_ty. associated_ty_id )
359
- . lookup ( db. upcast ( ) )
360
- . container
361
- {
362
- ItemContainerId :: TraitId ( trait_id) => Some ( trait_id) ,
363
- _ => None ,
364
- }
339
+ Some ( assoc_type_parent_trait ( db, projection_ty. associated_ty_id ) )
365
340
}
366
341
_ => None ,
367
342
}
@@ -413,6 +388,16 @@ impl TyExt for Ty {
413
388
}
414
389
}
415
390
391
+ fn assoc_type_parent_trait ( db : & dyn HirDatabase , id : AssocTypeId ) -> TraitId {
392
+ match from_assoc_type_id ( db, id) {
393
+ AnyTraitAssocType :: Normal ( type_alias) => match type_alias. lookup ( db. upcast ( ) ) . container {
394
+ ItemContainerId :: TraitId ( trait_id) => trait_id,
395
+ _ => panic ! ( "`AssocTypeId` without parent trait" ) ,
396
+ } ,
397
+ AnyTraitAssocType :: Rpitit ( assoc_type) => assoc_type. loc ( db) . trait_id ,
398
+ }
399
+ }
400
+
416
401
pub trait ProjectionTyExt {
417
402
fn trait_ref ( & self , db : & dyn HirDatabase ) -> TraitRef ;
418
403
fn trait_ ( & self , db : & dyn HirDatabase ) -> TraitId ;
@@ -422,7 +407,15 @@ pub trait ProjectionTyExt {
422
407
impl ProjectionTyExt for ProjectionTy {
423
408
fn trait_ref ( & self , db : & dyn HirDatabase ) -> TraitRef {
424
409
// FIXME: something like `Split` trait from chalk-solve might be nice.
425
- let generics = generics ( db. upcast ( ) , from_assoc_type_id ( self . associated_ty_id ) . into ( ) ) ;
410
+ let generic_def = match from_assoc_type_id ( db, self . associated_ty_id ) {
411
+ AnyTraitAssocType :: Normal ( type_alias) => type_alias. into ( ) ,
412
+ // FIXME: This isn't entirely correct, the generics of the RPITIT assoc type may differ from its method
413
+ // wrt. lifetimes, but we don't handle that currently. See https://rustc-dev-guide.rust-lang.org/return-position-impl-trait-in-trait.html.
414
+ AnyTraitAssocType :: Rpitit ( assoc_type) => {
415
+ assoc_type. loc ( db) . synthesized_from_method . into ( )
416
+ }
417
+ } ;
418
+ let generics = generics ( db. upcast ( ) , generic_def) ;
426
419
let substitution = Substitution :: from_iter (
427
420
Interner ,
428
421
self . substitution . iter ( Interner ) . skip ( generics. len_self ( ) ) ,
@@ -431,10 +424,7 @@ impl ProjectionTyExt for ProjectionTy {
431
424
}
432
425
433
426
fn trait_ ( & self , db : & dyn HirDatabase ) -> TraitId {
434
- match from_assoc_type_id ( self . associated_ty_id ) . lookup ( db. upcast ( ) ) . container {
435
- ItemContainerId :: TraitId ( it) => it,
436
- _ => panic ! ( "projection ty without parent trait" ) ,
437
- }
427
+ assoc_type_parent_trait ( db, self . associated_ty_id )
438
428
}
439
429
440
430
fn self_type_parameter ( & self , db : & dyn HirDatabase ) -> Ty {
0 commit comments