@@ -210,6 +210,11 @@ impl<'a, 'tcx> AsMut<Resolver<'a, 'tcx>> for BuildReducedGraphVisitor<'a, '_, 't
210
210
}
211
211
212
212
impl < ' a , ' b , ' tcx > BuildReducedGraphVisitor < ' a , ' b , ' tcx > {
213
+ fn res ( & self , def_id : impl Into < DefId > ) -> Res {
214
+ let def_id = def_id. into ( ) ;
215
+ Res :: Def ( self . r . tcx . def_kind ( def_id) , def_id)
216
+ }
217
+
213
218
fn resolve_visibility ( & mut self , vis : & ast:: Visibility ) -> ty:: Visibility {
214
219
self . try_resolve_visibility ( vis, true ) . unwrap_or_else ( |err| {
215
220
self . r . report_vis_error ( err) ;
@@ -628,6 +633,8 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
628
633
let vis = self . resolve_visibility ( & item. vis ) ;
629
634
let local_def_id = self . r . local_def_id ( item. id ) ;
630
635
let def_id = local_def_id. to_def_id ( ) ;
636
+ let def_kind = self . r . tcx . def_kind ( def_id) ;
637
+ let res = Res :: Def ( def_kind, def_id) ;
631
638
632
639
self . r . visibilities . insert ( local_def_id, vis) ;
633
640
@@ -659,7 +666,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
659
666
ItemKind :: Mod ( ..) => {
660
667
let module = self . r . new_module (
661
668
Some ( parent) ,
662
- ModuleKind :: Def ( DefKind :: Mod , def_id, ident. name ) ,
669
+ ModuleKind :: Def ( def_kind , def_id, ident. name ) ,
663
670
expansion. to_expn_id ( ) ,
664
671
item. span ,
665
672
parent. no_implicit_prelude
@@ -672,16 +679,13 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
672
679
}
673
680
674
681
// These items live in the value namespace.
675
- ItemKind :: Static ( box ast:: StaticItem { mutability, .. } ) => {
676
- let res = Res :: Def ( DefKind :: Static ( mutability) , def_id) ;
682
+ ItemKind :: Static ( ..) => {
677
683
self . r . define ( parent, ident, ValueNS , ( res, vis, sp, expansion) ) ;
678
684
}
679
685
ItemKind :: Const ( ..) => {
680
- let res = Res :: Def ( DefKind :: Const , def_id) ;
681
686
self . r . define ( parent, ident, ValueNS , ( res, vis, sp, expansion) ) ;
682
687
}
683
688
ItemKind :: Fn ( ..) => {
684
- let res = Res :: Def ( DefKind :: Fn , def_id) ;
685
689
self . r . define ( parent, ident, ValueNS , ( res, vis, sp, expansion) ) ;
686
690
687
691
// Functions introducing procedural macros reserve a slot
@@ -691,14 +695,13 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
691
695
692
696
// These items live in the type namespace.
693
697
ItemKind :: TyAlias ( ..) => {
694
- let res = Res :: Def ( DefKind :: TyAlias , def_id) ;
695
698
self . r . define ( parent, ident, TypeNS , ( res, vis, sp, expansion) ) ;
696
699
}
697
700
698
701
ItemKind :: Enum ( _, _) => {
699
702
let module = self . r . new_module (
700
703
Some ( parent) ,
701
- ModuleKind :: Def ( DefKind :: Enum , def_id, ident. name ) ,
704
+ ModuleKind :: Def ( def_kind , def_id, ident. name ) ,
702
705
expansion. to_expn_id ( ) ,
703
706
item. span ,
704
707
parent. no_implicit_prelude ,
@@ -708,14 +711,12 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
708
711
}
709
712
710
713
ItemKind :: TraitAlias ( ..) => {
711
- let res = Res :: Def ( DefKind :: TraitAlias , def_id) ;
712
714
self . r . define ( parent, ident, TypeNS , ( res, vis, sp, expansion) ) ;
713
715
}
714
716
715
717
// These items live in both the type and value namespaces.
716
718
ItemKind :: Struct ( ref vdata, _) => {
717
719
// Define a name in the type namespace.
718
- let res = Res :: Def ( DefKind :: Struct , def_id) ;
719
720
self . r . define ( parent, ident, TypeNS , ( res, vis, sp, expansion) ) ;
720
721
721
722
// Record field names for error reporting.
@@ -724,7 +725,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
724
725
725
726
// If this is a tuple or unit struct, define a name
726
727
// in the value namespace as well.
727
- if let Some ( ( ctor_kind , ctor_node_id) ) = CtorKind :: from_ast ( vdata) {
728
+ if let Some ( ctor_node_id) = vdata. ctor_node_id ( ) {
728
729
// If the structure is marked as non_exhaustive then lower the visibility
729
730
// to within the crate.
730
731
let mut ctor_vis = if vis. is_public ( )
@@ -750,8 +751,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
750
751
ret_fields. push ( field_vis. to_def_id ( ) ) ;
751
752
}
752
753
let ctor_def_id = self . r . local_def_id ( ctor_node_id) ;
753
- let ctor_res =
754
- Res :: Def ( DefKind :: Ctor ( CtorOf :: Struct , ctor_kind) , ctor_def_id. to_def_id ( ) ) ;
754
+ let ctor_res = self . res ( ctor_def_id) ;
755
755
self . r . define ( parent, ident, ValueNS , ( ctor_res, ctor_vis, sp, expansion) ) ;
756
756
self . r . visibilities . insert ( ctor_def_id, ctor_vis) ;
757
757
// We need the field visibility spans also for the constructor for E0603.
@@ -764,7 +764,6 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
764
764
}
765
765
766
766
ItemKind :: Union ( ref vdata, _) => {
767
- let res = Res :: Def ( DefKind :: Union , def_id) ;
768
767
self . r . define ( parent, ident, TypeNS , ( res, vis, sp, expansion) ) ;
769
768
770
769
// Record field names for error reporting.
@@ -776,7 +775,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
776
775
// Add all the items within to a new module.
777
776
let module = self . r . new_module (
778
777
Some ( parent) ,
779
- ModuleKind :: Def ( DefKind :: Trait , def_id, ident. name ) ,
778
+ ModuleKind :: Def ( def_kind , def_id, ident. name ) ,
780
779
expansion. to_expn_id ( ) ,
781
780
item. span ,
782
781
parent. no_implicit_prelude ,
@@ -888,17 +887,16 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
888
887
fn build_reduced_graph_for_foreign_item ( & mut self , item : & ForeignItem ) {
889
888
let local_def_id = self . r . local_def_id ( item. id ) ;
890
889
let def_id = local_def_id. to_def_id ( ) ;
891
- let ( def_kind , ns ) = match item. kind {
892
- ForeignItemKind :: Fn ( ..) => ( DefKind :: Fn , ValueNS ) ,
893
- ForeignItemKind :: Static ( _ , mt , _ ) => ( DefKind :: Static ( mt ) , ValueNS ) ,
894
- ForeignItemKind :: TyAlias ( ..) => ( DefKind :: ForeignTy , TypeNS ) ,
895
- ForeignItemKind :: MacCall ( _ ) => unreachable ! ( ) ,
890
+ let ns = match item. kind {
891
+ ForeignItemKind :: Fn ( ..) => ValueNS ,
892
+ ForeignItemKind :: Static ( .. ) => ValueNS ,
893
+ ForeignItemKind :: TyAlias ( ..) => TypeNS ,
894
+ ForeignItemKind :: MacCall ( .. ) => unreachable ! ( ) ,
896
895
} ;
897
896
let parent = self . parent_scope . module ;
898
897
let expansion = self . parent_scope . expansion ;
899
898
let vis = self . resolve_visibility ( & item. vis ) ;
900
- let res = Res :: Def ( def_kind, def_id) ;
901
- self . r . define ( parent, item. ident , ns, ( res, vis, item. span , expansion) ) ;
899
+ self . r . define ( parent, item. ident , ns, ( self . res ( def_id) , vis, item. span , expansion) ) ;
902
900
self . r . visibilities . insert ( local_def_id, vis) ;
903
901
}
904
902
@@ -1180,24 +1178,21 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
1180
1178
let parent_scope = self . parent_scope ;
1181
1179
let expansion = parent_scope. expansion ;
1182
1180
let def_id = self . r . local_def_id ( item. id ) ;
1183
- let ( macro_kind, ident, span, macro_rules) = match & item. kind {
1184
- ItemKind :: MacroDef ( def) => {
1185
- let macro_kind = self . r . macro_map [ & def_id. to_def_id ( ) ] . ext . macro_kind ( ) ;
1186
- ( macro_kind, item. ident , item. span , def. macro_rules )
1187
- }
1181
+ let ( res, ident, span, macro_rules) = match & item. kind {
1182
+ ItemKind :: MacroDef ( def) => ( self . res ( def_id) , item. ident , item. span , def. macro_rules ) ,
1188
1183
ItemKind :: Fn ( ..) => match self . proc_macro_stub ( item) {
1189
1184
Some ( ( macro_kind, ident, span) ) => {
1185
+ let res = Res :: Def ( DefKind :: Macro ( macro_kind) , def_id. to_def_id ( ) ) ;
1190
1186
let macro_data = MacroData :: new ( self . r . dummy_ext ( macro_kind) ) ;
1191
1187
self . r . macro_map . insert ( def_id. to_def_id ( ) , macro_data) ;
1192
1188
self . r . proc_macro_stubs . insert ( def_id) ;
1193
- ( macro_kind , ident, span, false )
1189
+ ( res , ident, span, false )
1194
1190
}
1195
1191
None => return parent_scope. macro_rules ,
1196
1192
} ,
1197
1193
_ => unreachable ! ( ) ,
1198
1194
} ;
1199
1195
1200
- let res = Res :: Def ( DefKind :: Macro ( macro_kind) , def_id. to_def_id ( ) ) ;
1201
1196
self . r . local_macro_def_scopes . insert ( def_id, parent_scope. module ) ;
1202
1197
1203
1198
if macro_rules {
@@ -1363,22 +1358,21 @@ impl<'a, 'b, 'tcx> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b, 'tcx> {
1363
1358
}
1364
1359
1365
1360
if ctxt == AssocCtxt :: Trait {
1366
- let ( def_kind , ns ) = match item. kind {
1367
- AssocItemKind :: Const ( ..) => ( DefKind :: AssocConst , ValueNS ) ,
1361
+ let ns = match item. kind {
1362
+ AssocItemKind :: Const ( ..) => ValueNS ,
1368
1363
AssocItemKind :: Fn ( box Fn { ref sig, .. } ) => {
1369
1364
if sig. decl . has_self ( ) {
1370
1365
self . r . has_self . insert ( local_def_id) ;
1371
1366
}
1372
- ( DefKind :: AssocFn , ValueNS )
1367
+ ValueNS
1373
1368
}
1374
- AssocItemKind :: Type ( ..) => ( DefKind :: AssocTy , TypeNS ) ,
1369
+ AssocItemKind :: Type ( ..) => TypeNS ,
1375
1370
AssocItemKind :: MacCall ( _) => bug ! ( ) , // handled above
1376
1371
} ;
1377
1372
1378
1373
let parent = self . parent_scope . module ;
1379
1374
let expansion = self . parent_scope . expansion ;
1380
- let res = Res :: Def ( def_kind, def_id) ;
1381
- self . r . define ( parent, item. ident , ns, ( res, vis, item. span , expansion) ) ;
1375
+ self . r . define ( parent, item. ident , ns, ( self . res ( def_id) , vis, item. span , expansion) ) ;
1382
1376
}
1383
1377
1384
1378
visit:: walk_assoc_item ( self , item, ctxt) ;
@@ -1457,9 +1451,8 @@ impl<'a, 'b, 'tcx> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b, 'tcx> {
1457
1451
1458
1452
// Define a name in the type namespace.
1459
1453
let def_id = self . r . local_def_id ( variant. id ) ;
1460
- let res = Res :: Def ( DefKind :: Variant , def_id. to_def_id ( ) ) ;
1461
1454
let vis = self . resolve_visibility ( & variant. vis ) ;
1462
- self . r . define ( parent, ident, TypeNS , ( res, vis, variant. span , expn_id) ) ;
1455
+ self . r . define ( parent, ident, TypeNS , ( self . res ( def_id ) , vis, variant. span , expn_id) ) ;
1463
1456
self . r . visibilities . insert ( def_id, vis) ;
1464
1457
1465
1458
// If the variant is marked as non_exhaustive then lower the visibility to within the crate.
@@ -1471,10 +1464,9 @@ impl<'a, 'b, 'tcx> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b, 'tcx> {
1471
1464
} ;
1472
1465
1473
1466
// Define a constructor name in the value namespace.
1474
- if let Some ( ( ctor_kind , ctor_node_id) ) = CtorKind :: from_ast ( & variant. data ) {
1467
+ if let Some ( ctor_node_id) = variant. data . ctor_node_id ( ) {
1475
1468
let ctor_def_id = self . r . local_def_id ( ctor_node_id) ;
1476
- let ctor_res =
1477
- Res :: Def ( DefKind :: Ctor ( CtorOf :: Variant , ctor_kind) , ctor_def_id. to_def_id ( ) ) ;
1469
+ let ctor_res = self . res ( ctor_def_id) ;
1478
1470
self . r . define ( parent, ident, ValueNS , ( ctor_res, ctor_vis, variant. span , expn_id) ) ;
1479
1471
self . r . visibilities . insert ( ctor_def_id, ctor_vis) ;
1480
1472
}
0 commit comments