@@ -689,7 +689,7 @@ pub enum TypeOwnerId {
689
689
}
690
690
691
691
impl TypeOwnerId {
692
- fn as_generic_def_id ( self ) -> Option < GenericDefId > {
692
+ fn as_generic_def_id ( self , db : & dyn DefDatabase ) -> Option < GenericDefId > {
693
693
Some ( match self {
694
694
TypeOwnerId :: FunctionId ( it) => GenericDefId :: FunctionId ( it) ,
695
695
TypeOwnerId :: ConstId ( it) => GenericDefId :: ConstId ( it) ,
@@ -698,7 +698,9 @@ impl TypeOwnerId {
698
698
TypeOwnerId :: TraitAliasId ( it) => GenericDefId :: TraitAliasId ( it) ,
699
699
TypeOwnerId :: TypeAliasId ( it) => GenericDefId :: TypeAliasId ( it) ,
700
700
TypeOwnerId :: ImplId ( it) => GenericDefId :: ImplId ( it) ,
701
- TypeOwnerId :: EnumVariantId ( it) => GenericDefId :: EnumVariantId ( it) ,
701
+ TypeOwnerId :: EnumVariantId ( it) => {
702
+ GenericDefId :: AdtId ( AdtId :: EnumId ( it. lookup ( db) . parent ) )
703
+ }
702
704
TypeOwnerId :: InTypeConstId ( _) | TypeOwnerId :: StaticId ( _) => return None ,
703
705
} )
704
706
}
@@ -740,7 +742,6 @@ impl From<GenericDefId> for TypeOwnerId {
740
742
GenericDefId :: TraitAliasId ( it) => it. into ( ) ,
741
743
GenericDefId :: TypeAliasId ( it) => it. into ( ) ,
742
744
GenericDefId :: ImplId ( it) => it. into ( ) ,
743
- GenericDefId :: EnumVariantId ( it) => it. into ( ) ,
744
745
GenericDefId :: ConstId ( it) => it. into ( ) ,
745
746
}
746
747
}
@@ -849,8 +850,8 @@ impl GeneralConstId {
849
850
pub fn generic_def ( self , db : & dyn DefDatabase ) -> Option < GenericDefId > {
850
851
match self {
851
852
GeneralConstId :: ConstId ( it) => Some ( it. into ( ) ) ,
852
- GeneralConstId :: ConstBlockId ( it) => it. lookup ( db) . parent . as_generic_def_id ( ) ,
853
- GeneralConstId :: InTypeConstId ( it) => it. lookup ( db) . owner . as_generic_def_id ( ) ,
853
+ GeneralConstId :: ConstBlockId ( it) => it. lookup ( db) . parent . as_generic_def_id ( db ) ,
854
+ GeneralConstId :: InTypeConstId ( it) => it. lookup ( db) . owner . as_generic_def_id ( db ) ,
854
855
}
855
856
}
856
857
@@ -888,12 +889,12 @@ impl From<EnumVariantId> for DefWithBodyId {
888
889
}
889
890
890
891
impl DefWithBodyId {
891
- pub fn as_generic_def_id ( self ) -> Option < GenericDefId > {
892
+ pub fn as_generic_def_id ( self , db : & dyn DefDatabase ) -> Option < GenericDefId > {
892
893
match self {
893
894
DefWithBodyId :: FunctionId ( f) => Some ( f. into ( ) ) ,
894
895
DefWithBodyId :: StaticId ( _) => None ,
895
896
DefWithBodyId :: ConstId ( c) => Some ( c. into ( ) ) ,
896
- DefWithBodyId :: VariantId ( c) => Some ( c. into ( ) ) ,
897
+ DefWithBodyId :: VariantId ( c) => Some ( c. lookup ( db ) . parent . into ( ) ) ,
897
898
// FIXME: stable rust doesn't allow generics in constants, but we should
898
899
// use `TypeOwnerId::as_generic_def_id` when it does.
899
900
DefWithBodyId :: InTypeConstId ( _) => None ,
@@ -921,10 +922,6 @@ pub enum GenericDefId {
921
922
TraitAliasId ( TraitAliasId ) ,
922
923
TypeAliasId ( TypeAliasId ) ,
923
924
ImplId ( ImplId ) ,
924
- // enum variants cannot have generics themselves, but their parent enums
925
- // can, and this makes some code easier to write
926
- // FIXME: Try to remove this as that will reduce the amount of query slots generated per enum?
927
- EnumVariantId ( EnumVariantId ) ,
928
925
// consts can have type parameters from their parents (i.e. associated consts of traits)
929
926
ConstId ( ConstId ) ,
930
927
}
@@ -935,7 +932,6 @@ impl_from!(
935
932
TraitAliasId ,
936
933
TypeAliasId ,
937
934
ImplId ,
938
- EnumVariantId ,
939
935
ConstId
940
936
for GenericDefId
941
937
) ;
@@ -967,7 +963,6 @@ impl GenericDefId {
967
963
GenericDefId :: TraitAliasId ( it) => file_id_and_params_of_item_loc ( db, it) ,
968
964
GenericDefId :: ImplId ( it) => file_id_and_params_of_item_loc ( db, it) ,
969
965
GenericDefId :: ConstId ( it) => ( it. lookup ( db) . id . file_id ( ) , None ) ,
970
- GenericDefId :: EnumVariantId ( it) => ( it. lookup ( db) . id . file_id ( ) , None ) ,
971
966
}
972
967
}
973
968
@@ -982,6 +977,14 @@ impl GenericDefId {
982
977
_ => None ,
983
978
}
984
979
}
980
+
981
+ pub fn from_callable ( db : & dyn DefDatabase , def : CallableDefId ) -> GenericDefId {
982
+ match def {
983
+ CallableDefId :: FunctionId ( f) => f. into ( ) ,
984
+ CallableDefId :: StructId ( s) => s. into ( ) ,
985
+ CallableDefId :: EnumVariantId ( e) => e. lookup ( db) . parent . into ( ) ,
986
+ }
987
+ }
985
988
}
986
989
987
990
impl From < AssocItemId > for GenericDefId {
@@ -994,6 +997,36 @@ impl From<AssocItemId> for GenericDefId {
994
997
}
995
998
}
996
999
1000
+ #[ derive( Clone , Copy , Debug , PartialEq , Eq , Hash ) ]
1001
+ pub enum CallableDefId {
1002
+ FunctionId ( FunctionId ) ,
1003
+ StructId ( StructId ) ,
1004
+ EnumVariantId ( EnumVariantId ) ,
1005
+ }
1006
+
1007
+ impl InternValueTrivial for CallableDefId { }
1008
+
1009
+ impl_from ! ( FunctionId , StructId , EnumVariantId for CallableDefId ) ;
1010
+ impl From < CallableDefId > for ModuleDefId {
1011
+ fn from ( def : CallableDefId ) -> ModuleDefId {
1012
+ match def {
1013
+ CallableDefId :: FunctionId ( f) => ModuleDefId :: FunctionId ( f) ,
1014
+ CallableDefId :: StructId ( s) => ModuleDefId :: AdtId ( AdtId :: StructId ( s) ) ,
1015
+ CallableDefId :: EnumVariantId ( e) => ModuleDefId :: EnumVariantId ( e) ,
1016
+ }
1017
+ }
1018
+ }
1019
+
1020
+ impl CallableDefId {
1021
+ pub fn krate ( self , db : & dyn DefDatabase ) -> CrateId {
1022
+ match self {
1023
+ CallableDefId :: FunctionId ( f) => f. krate ( db) ,
1024
+ CallableDefId :: StructId ( s) => s. krate ( db) ,
1025
+ CallableDefId :: EnumVariantId ( e) => e. krate ( db) ,
1026
+ }
1027
+ }
1028
+ }
1029
+
997
1030
#[ derive( Clone , Copy , Debug , PartialEq , Eq , Hash ) ]
998
1031
pub enum AttrDefId {
999
1032
ModuleId ( ModuleId ) ,
@@ -1310,7 +1343,6 @@ impl HasModule for GenericDefId {
1310
1343
GenericDefId :: TraitAliasId ( it) => it. module ( db) ,
1311
1344
GenericDefId :: TypeAliasId ( it) => it. module ( db) ,
1312
1345
GenericDefId :: ImplId ( it) => it. module ( db) ,
1313
- GenericDefId :: EnumVariantId ( it) => it. module ( db) ,
1314
1346
GenericDefId :: ConstId ( it) => it. module ( db) ,
1315
1347
}
1316
1348
}
0 commit comments