@@ -487,22 +487,81 @@ impl_intern_key!(AnonymousConstId);
487
487
488
488
#[ derive( Debug , Clone , Copy , Eq , PartialEq , Hash ) ]
489
489
pub enum TypeOwnerId {
490
+ FunctionId ( FunctionId ) ,
491
+ StaticId ( StaticId ) ,
492
+ ConstId ( ConstId ) ,
493
+ InTypeConstId ( InTypeConstId ) ,
494
+ AdtId ( AdtId ) ,
495
+ TraitId ( TraitId ) ,
496
+ TraitAliasId ( TraitAliasId ) ,
497
+ TypeAliasId ( TypeAliasId ) ,
498
+ ImplId ( ImplId ) ,
499
+ EnumVariantId ( EnumVariantId ) ,
500
+ // FIXME(const-generic-body): ModuleId should not be a type owner. This needs to be fixed to make `TypeOwnerId` actually
501
+ // useful for assigning ids to in type consts.
490
502
ModuleId ( ModuleId ) ,
491
- DefWithBodyId ( DefWithBodyId ) ,
492
- GenericDefId ( GenericDefId ) ,
493
503
}
494
504
495
505
impl TypeOwnerId {
496
506
fn as_generic_def_id ( self ) -> Option < GenericDefId > {
497
- match self {
498
- TypeOwnerId :: ModuleId ( _) => None ,
499
- TypeOwnerId :: DefWithBodyId ( x) => x. as_generic_def_id ( ) ,
500
- TypeOwnerId :: GenericDefId ( x) => Some ( x) ,
507
+ Some ( match self {
508
+ TypeOwnerId :: FunctionId ( x) => GenericDefId :: FunctionId ( x) ,
509
+ TypeOwnerId :: ConstId ( x) => GenericDefId :: ConstId ( x) ,
510
+ TypeOwnerId :: AdtId ( x) => GenericDefId :: AdtId ( x) ,
511
+ TypeOwnerId :: TraitId ( x) => GenericDefId :: TraitId ( x) ,
512
+ TypeOwnerId :: TraitAliasId ( x) => GenericDefId :: TraitAliasId ( x) ,
513
+ TypeOwnerId :: TypeAliasId ( x) => GenericDefId :: TypeAliasId ( x) ,
514
+ TypeOwnerId :: ImplId ( x) => GenericDefId :: ImplId ( x) ,
515
+ TypeOwnerId :: EnumVariantId ( x) => GenericDefId :: EnumVariantId ( x) ,
516
+ TypeOwnerId :: InTypeConstId ( _) | TypeOwnerId :: ModuleId ( _) | TypeOwnerId :: StaticId ( _) => {
517
+ return None
518
+ }
519
+ } )
520
+ }
521
+ }
522
+
523
+ impl_from ! (
524
+ FunctionId ,
525
+ StaticId ,
526
+ ConstId ,
527
+ InTypeConstId ,
528
+ AdtId ,
529
+ TraitId ,
530
+ TraitAliasId ,
531
+ TypeAliasId ,
532
+ ImplId ,
533
+ EnumVariantId ,
534
+ ModuleId
535
+ for TypeOwnerId
536
+ ) ;
537
+
538
+ // Every `DefWithBodyId` is a type owner, since bodies can contain type (e.g. `{ let x: Type = _; }`)
539
+ impl From < DefWithBodyId > for TypeOwnerId {
540
+ fn from ( value : DefWithBodyId ) -> Self {
541
+ match value {
542
+ DefWithBodyId :: FunctionId ( x) => x. into ( ) ,
543
+ DefWithBodyId :: StaticId ( x) => x. into ( ) ,
544
+ DefWithBodyId :: ConstId ( x) => x. into ( ) ,
545
+ DefWithBodyId :: InTypeConstId ( x) => x. into ( ) ,
546
+ DefWithBodyId :: VariantId ( x) => x. into ( ) ,
501
547
}
502
548
}
503
549
}
504
550
505
- impl_from ! ( ModuleId , DefWithBodyId ( FunctionId , ConstId , StaticId ) , GenericDefId ( AdtId , TypeAliasId , ImplId ) for TypeOwnerId ) ;
551
+ impl From < GenericDefId > for TypeOwnerId {
552
+ fn from ( value : GenericDefId ) -> Self {
553
+ match value {
554
+ GenericDefId :: FunctionId ( x) => x. into ( ) ,
555
+ GenericDefId :: AdtId ( x) => x. into ( ) ,
556
+ GenericDefId :: TraitId ( x) => x. into ( ) ,
557
+ GenericDefId :: TraitAliasId ( x) => x. into ( ) ,
558
+ GenericDefId :: TypeAliasId ( x) => x. into ( ) ,
559
+ GenericDefId :: ImplId ( x) => x. into ( ) ,
560
+ GenericDefId :: EnumVariantId ( x) => x. into ( ) ,
561
+ GenericDefId :: ConstId ( x) => x. into ( ) ,
562
+ }
563
+ }
564
+ }
506
565
507
566
/// A thing that we want to store in interned ids, but we don't know its type in `hir-def`
508
567
pub trait OpaqueInternableThing :
@@ -815,9 +874,17 @@ impl HasModule for MacroId {
815
874
impl HasModule for TypeOwnerId {
816
875
fn module ( & self , db : & dyn db:: DefDatabase ) -> ModuleId {
817
876
match self {
877
+ TypeOwnerId :: FunctionId ( x) => x. lookup ( db) . module ( db) ,
878
+ TypeOwnerId :: StaticId ( x) => x. lookup ( db) . module ( db) ,
879
+ TypeOwnerId :: ConstId ( x) => x. lookup ( db) . module ( db) ,
880
+ TypeOwnerId :: InTypeConstId ( x) => x. lookup ( db) . 1 . module ( db) ,
881
+ TypeOwnerId :: AdtId ( x) => x. module ( db) ,
882
+ TypeOwnerId :: TraitId ( x) => x. lookup ( db) . container ,
883
+ TypeOwnerId :: TraitAliasId ( x) => x. lookup ( db) . container ,
884
+ TypeOwnerId :: TypeAliasId ( x) => x. lookup ( db) . module ( db) ,
885
+ TypeOwnerId :: ImplId ( x) => x. lookup ( db) . container ,
886
+ TypeOwnerId :: EnumVariantId ( x) => x. parent . lookup ( db) . container ,
818
887
TypeOwnerId :: ModuleId ( x) => * x,
819
- TypeOwnerId :: DefWithBodyId ( x) => x. module ( db) ,
820
- TypeOwnerId :: GenericDefId ( x) => x. module ( db) ,
821
888
}
822
889
}
823
890
}
0 commit comments