@@ -55,6 +55,7 @@ pub mod visibility;
55
55
56
56
use intern:: Interned ;
57
57
pub use rustc_abi as layout;
58
+ use src:: HasSource ;
58
59
use triomphe:: Arc ;
59
60
60
61
#[ cfg( test) ]
@@ -77,6 +78,7 @@ use hir_expand::{
77
78
builtin:: { BuiltinAttrExpander , BuiltinDeriveExpander , BuiltinFnLikeExpander , EagerExpander } ,
78
79
db:: ExpandDatabase ,
79
80
eager:: expand_eager_macro_input,
81
+ files:: InFileWrapper ,
80
82
impl_intern_lookup,
81
83
name:: Name ,
82
84
proc_macro:: { CustomProcMacroExpander , ProcMacroKind } ,
@@ -519,6 +521,41 @@ pub struct FieldId {
519
521
pub local_id : LocalFieldId ,
520
522
}
521
523
524
+ impl FieldId {
525
+ pub fn record_field_source (
526
+ & self ,
527
+ db : & dyn DefDatabase ,
528
+ ) -> InFileWrapper < HirFileId , Option < ast:: RecordField > > {
529
+ let field_list = match self . parent {
530
+ crate :: VariantId :: EnumVariantId ( it) => {
531
+ let s = it. lookup ( db) ;
532
+ s. source ( db) . map ( |it| {
533
+ it. field_list ( ) . and_then ( |it| match it {
534
+ ast:: FieldList :: RecordFieldList ( it) => Some ( it) ,
535
+ _ => None ,
536
+ } )
537
+ } )
538
+ }
539
+ crate :: VariantId :: StructId ( it) => {
540
+ let s = it. lookup ( db) ;
541
+ s. source ( db) . map ( |it| {
542
+ it. field_list ( ) . and_then ( |it| match it {
543
+ ast:: FieldList :: RecordFieldList ( it) => Some ( it) ,
544
+ _ => None ,
545
+ } )
546
+ } )
547
+ }
548
+ crate :: VariantId :: UnionId ( it) => {
549
+ let s = it. lookup ( db) ;
550
+ s. source ( db) . map ( |it| it. record_field_list ( ) )
551
+ }
552
+ } ;
553
+ field_list. map ( |it| {
554
+ it. and_then ( |it| it. fields ( ) . nth ( self . local_id . into_raw ( ) . into_u32 ( ) as usize ) )
555
+ } )
556
+ }
557
+ }
558
+
522
559
pub type LocalFieldId = Idx < data:: adt:: FieldData > ;
523
560
524
561
#[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash ) ]
@@ -686,6 +723,7 @@ pub enum TypeOwnerId {
686
723
TypeAliasId ( TypeAliasId ) ,
687
724
ImplId ( ImplId ) ,
688
725
EnumVariantId ( EnumVariantId ) ,
726
+ FieldId ( FieldId ) ,
689
727
}
690
728
691
729
impl TypeOwnerId {
@@ -703,6 +741,11 @@ impl TypeOwnerId {
703
741
GenericDefId :: AdtId ( AdtId :: EnumId ( it. lookup ( db) . parent ) )
704
742
}
705
743
TypeOwnerId :: InTypeConstId ( _) => return None ,
744
+ TypeOwnerId :: FieldId ( it) => GenericDefId :: AdtId ( match it. parent {
745
+ VariantId :: EnumVariantId ( it) => AdtId :: EnumId ( it. lookup ( db) . parent ) ,
746
+ VariantId :: StructId ( it) => it. into ( ) ,
747
+ VariantId :: UnionId ( it) => it. into ( ) ,
748
+ } ) ,
706
749
} )
707
750
}
708
751
}
@@ -717,7 +760,8 @@ impl_from!(
717
760
TraitAliasId ,
718
761
TypeAliasId ,
719
762
ImplId ,
720
- EnumVariantId
763
+ EnumVariantId ,
764
+ FieldId
721
765
for TypeOwnerId
722
766
) ;
723
767
@@ -730,6 +774,7 @@ impl From<DefWithBodyId> for TypeOwnerId {
730
774
DefWithBodyId :: ConstId ( it) => it. into ( ) ,
731
775
DefWithBodyId :: InTypeConstId ( it) => it. into ( ) ,
732
776
DefWithBodyId :: VariantId ( it) => it. into ( ) ,
777
+ DefWithBodyId :: FieldId ( it) => it. into ( ) ,
733
778
}
734
779
}
735
780
}
@@ -885,6 +930,7 @@ pub enum DefWithBodyId {
885
930
ConstId ( ConstId ) ,
886
931
InTypeConstId ( InTypeConstId ) ,
887
932
VariantId ( EnumVariantId ) ,
933
+ FieldId ( FieldId ) ,
888
934
}
889
935
890
936
impl_from ! ( FunctionId , ConstId , StaticId , InTypeConstId for DefWithBodyId ) ;
@@ -905,6 +951,7 @@ impl DefWithBodyId {
905
951
// FIXME: stable rust doesn't allow generics in constants, but we should
906
952
// use `TypeOwnerId::as_generic_def_id` when it does.
907
953
DefWithBodyId :: InTypeConstId ( _) => None ,
954
+ DefWithBodyId :: FieldId ( _) => None ,
908
955
}
909
956
}
910
957
}
@@ -1309,6 +1356,12 @@ impl HasModule for VariantId {
1309
1356
}
1310
1357
}
1311
1358
1359
+ impl HasModule for FieldId {
1360
+ fn module ( & self , db : & dyn DefDatabase ) -> ModuleId {
1361
+ self . parent . module ( db)
1362
+ }
1363
+ }
1364
+
1312
1365
impl HasModule for MacroId {
1313
1366
fn module ( & self , db : & dyn DefDatabase ) -> ModuleId {
1314
1367
match * self {
@@ -1332,6 +1385,7 @@ impl HasModule for TypeOwnerId {
1332
1385
TypeOwnerId :: ImplId ( it) => it. module ( db) ,
1333
1386
TypeOwnerId :: EnumVariantId ( it) => it. module ( db) ,
1334
1387
TypeOwnerId :: InTypeConstId ( it) => it. lookup ( db) . owner . module ( db) ,
1388
+ TypeOwnerId :: FieldId ( it) => it. module ( db) ,
1335
1389
}
1336
1390
}
1337
1391
}
@@ -1344,6 +1398,7 @@ impl HasModule for DefWithBodyId {
1344
1398
DefWithBodyId :: ConstId ( it) => it. module ( db) ,
1345
1399
DefWithBodyId :: VariantId ( it) => it. module ( db) ,
1346
1400
DefWithBodyId :: InTypeConstId ( it) => it. lookup ( db) . owner . module ( db) ,
1401
+ DefWithBodyId :: FieldId ( it) => it. module ( db) ,
1347
1402
}
1348
1403
}
1349
1404
}
0 commit comments