@@ -19,6 +19,7 @@ use rustc::middle::cstore::CrateStore;
19
19
use rustc_metadata:: cstore:: LoadedMacro ;
20
20
21
21
use std:: cell:: Cell ;
22
+ use std:: ops:: { Deref , DerefMut } ;
22
23
use std:: ptr;
23
24
use rustc_data_structures:: sync:: Lrc ;
24
25
@@ -115,7 +116,7 @@ impl<'a> Resolver<'a> {
115
116
parent_prefix : & [ Segment ] ,
116
117
nested : bool ,
117
118
// The whole `use` item
118
- parent_scope : ParentScope < ' a > ,
119
+ parent_scope : & ParentScope < ' a > ,
119
120
item : & Item ,
120
121
vis : ty:: Visibility ,
121
122
root_span : Span ,
@@ -249,7 +250,7 @@ impl<'a> Resolver<'a> {
249
250
root_span,
250
251
item. id ,
251
252
vis,
252
- parent_scope,
253
+ parent_scope. clone ( ) ,
253
254
) ;
254
255
}
255
256
ast:: UseTreeKind :: Glob => {
@@ -266,7 +267,7 @@ impl<'a> Resolver<'a> {
266
267
root_span,
267
268
item. id ,
268
269
vis,
269
- parent_scope,
270
+ parent_scope. clone ( ) ,
270
271
) ;
271
272
}
272
273
ast:: UseTreeKind :: Nested ( ref items) => {
@@ -297,7 +298,7 @@ impl<'a> Resolver<'a> {
297
298
// This particular use tree
298
299
tree, id, & prefix, true ,
299
300
// The whole `use` item
300
- parent_scope. clone ( ) , item, vis, root_span,
301
+ parent_scope, item, vis, root_span,
301
302
) ;
302
303
}
303
304
@@ -327,14 +328,16 @@ impl<'a> Resolver<'a> {
327
328
}
328
329
}
329
330
}
331
+ }
330
332
333
+ impl < ' a > BuildReducedGraphVisitor < ' _ , ' a > {
331
334
/// Constructs the reduced graph for one item.
332
- fn build_reduced_graph_for_item ( & mut self , item : & Item , parent_scope : ParentScope < ' a > ) {
335
+ fn build_reduced_graph_for_item ( & mut self , item : & Item , parent_scope : & ParentScope < ' a > ) {
333
336
let parent = parent_scope. module ;
334
337
let expansion = parent_scope. expansion ;
335
338
let ident = item. ident . gensym_if_underscore ( ) ;
336
339
let sp = item. span ;
337
- let vis = self . resolve_visibility ( & item. vis ) ;
340
+ let vis = self . resolve_visibility ( & item. vis , parent_scope ) ;
338
341
339
342
match item. node {
340
343
ItemKind :: Use ( ref use_tree) => {
@@ -361,7 +364,9 @@ impl<'a> Resolver<'a> {
361
364
} else if orig_name == Some ( kw:: SelfLower ) {
362
365
self . graph_root
363
366
} else {
364
- let crate_id = self . crate_loader . process_extern_crate ( item, & self . definitions ) ;
367
+ let crate_id = self . resolver . crate_loader . process_extern_crate (
368
+ item, & self . resolver . definitions
369
+ ) ;
365
370
self . get_module ( DefId { krate : crate_id, index : CRATE_DEF_INDEX } )
366
371
} ;
367
372
@@ -372,13 +377,13 @@ impl<'a> Resolver<'a> {
372
377
}
373
378
}
374
379
375
- let used = self . process_legacy_macro_imports ( item, module, & parent_scope) ;
380
+ let used = self . process_legacy_macro_imports ( item, module, parent_scope) ;
376
381
let binding =
377
382
( module, ty:: Visibility :: Public , sp, expansion) . to_name_binding ( self . arenas ) ;
378
383
let directive = self . arenas . alloc_import_directive ( ImportDirective {
379
384
root_id : item. id ,
380
385
id : item. id ,
381
- parent_scope,
386
+ parent_scope : parent_scope . clone ( ) ,
382
387
imported_module : Cell :: new ( Some ( ModuleOrUniformRoot :: Module ( module) ) ) ,
383
388
subclass : ImportDirectiveSubclass :: ExternCrate {
384
389
source : orig_name,
@@ -395,7 +400,7 @@ impl<'a> Resolver<'a> {
395
400
} ) ;
396
401
self . potentially_unused_imports . push ( directive) ;
397
402
let imported_binding = self . import ( binding, directive) ;
398
- if ptr:: eq ( self . current_module , self . graph_root ) {
403
+ if ptr:: eq ( parent , self . graph_root ) {
399
404
if let Some ( entry) = self . extern_prelude . get ( & ident. modern ( ) ) {
400
405
if expansion != ExpnId :: root ( ) && orig_name. is_some ( ) &&
401
406
entry. extern_crate_item . is_none ( ) {
@@ -455,7 +460,7 @@ impl<'a> Resolver<'a> {
455
460
456
461
// Functions introducing procedural macros reserve a slot
457
462
// in the macro namespace as well (see #52225).
458
- self . define_macro ( item, expansion , & mut LegacyScope :: Empty ) ;
463
+ self . define_macro ( item, parent_scope ) ;
459
464
}
460
465
461
466
// These items live in the type namespace.
@@ -511,8 +516,8 @@ impl<'a> Resolver<'a> {
511
516
512
517
// Record field names for error reporting.
513
518
let field_names = struct_def. fields ( ) . iter ( ) . filter_map ( |field| {
514
- let field_vis = self . resolve_visibility ( & field. vis ) ;
515
- if ctor_vis. is_at_least ( field_vis, & * self ) {
519
+ let field_vis = self . resolve_visibility ( & field. vis , parent_scope ) ;
520
+ if ctor_vis. is_at_least ( field_vis, & * self . resolver ) {
516
521
ctor_vis = field_vis;
517
522
}
518
523
field. ident . map ( |ident| ident. name )
@@ -538,7 +543,7 @@ impl<'a> Resolver<'a> {
538
543
539
544
// Record field names for error reporting.
540
545
let field_names = vdata. fields ( ) . iter ( ) . filter_map ( |field| {
541
- self . resolve_visibility ( & field. vis ) ;
546
+ self . resolve_visibility ( & field. vis , parent_scope ) ;
542
547
field. ident . map ( |ident| ident. name )
543
548
} ) . collect ( ) ;
544
549
let item_def_id = self . definitions . local_def_id ( item. id ) ;
@@ -614,7 +619,13 @@ impl<'a> Resolver<'a> {
614
619
ForeignItemKind :: Macro ( _) => unreachable ! ( ) ,
615
620
} ;
616
621
let parent = self . current_module ;
617
- let vis = self . resolve_visibility ( & item. vis ) ;
622
+ let parent_scope = & ParentScope {
623
+ module : self . current_module ,
624
+ expansion : self . expansion ,
625
+ legacy : self . current_legacy_scope ,
626
+ derives : Vec :: new ( ) ,
627
+ } ;
628
+ let vis = self . resolver . resolve_visibility ( & item. vis , parent_scope) ;
618
629
self . define ( parent, item. ident , ns, ( res, vis, item. span , expn_id) ) ;
619
630
}
620
631
@@ -630,7 +641,9 @@ impl<'a> Resolver<'a> {
630
641
self . current_module = module; // Descend into the block.
631
642
}
632
643
}
644
+ }
633
645
646
+ impl < ' a > Resolver < ' a > {
634
647
/// Builds the reduced graph for a single item in an external crate.
635
648
fn build_reduced_graph_for_external_crate_res (
636
649
& mut self ,
@@ -804,7 +817,9 @@ impl<'a> Resolver<'a> {
804
817
self . session . struct_span_err ( span, & msg) . note ( note) . emit ( ) ;
805
818
}
806
819
}
820
+ }
807
821
822
+ impl < ' a > BuildReducedGraphVisitor < ' _ , ' a > {
808
823
/// Returns `true` if we should consider the underlying `extern crate` to be used.
809
824
fn process_legacy_macro_imports ( & mut self , item : & Item , module : Module < ' a > ,
810
825
parent_scope : & ParentScope < ' a > ) -> bool {
@@ -873,7 +888,7 @@ impl<'a> Resolver<'a> {
873
888
ModuleOrUniformRoot :: Module ( module) ,
874
889
ident,
875
890
MacroNS ,
876
- None ,
891
+ parent_scope ,
877
892
false ,
878
893
ident. span ,
879
894
) ;
@@ -918,22 +933,36 @@ impl<'a> Resolver<'a> {
918
933
919
934
pub struct BuildReducedGraphVisitor < ' a , ' b > {
920
935
pub resolver : & ' a mut Resolver < ' b > ,
936
+ pub current_module : Module < ' b > ,
921
937
pub current_legacy_scope : LegacyScope < ' b > ,
922
938
pub expansion : ExpnId ,
923
939
}
924
940
941
+ impl < ' b > Deref for BuildReducedGraphVisitor < ' _ , ' b > {
942
+ type Target = Resolver < ' b > ;
943
+ fn deref ( & self ) -> & Self :: Target {
944
+ self . resolver
945
+ }
946
+ }
947
+
948
+ impl < ' b > DerefMut for BuildReducedGraphVisitor < ' _ , ' b > {
949
+ fn deref_mut ( & mut self ) -> & mut Self :: Target {
950
+ self . resolver
951
+ }
952
+ }
953
+
925
954
impl < ' a , ' b > BuildReducedGraphVisitor < ' a , ' b > {
926
955
fn visit_invoc ( & mut self , id : ast:: NodeId ) -> & ' b InvocationData < ' b > {
927
956
let invoc_id = id. placeholder_to_expn_id ( ) ;
928
957
929
- self . resolver . current_module . unresolved_invocations . borrow_mut ( ) . insert ( invoc_id) ;
958
+ self . current_module . unresolved_invocations . borrow_mut ( ) . insert ( invoc_id) ;
930
959
931
- let invocation_data = self . resolver . arenas . alloc_invocation_data ( InvocationData {
932
- module : self . resolver . current_module ,
960
+ let invocation_data = self . arenas . alloc_invocation_data ( InvocationData {
961
+ module : self . current_module ,
933
962
parent_legacy_scope : self . current_legacy_scope ,
934
963
output_legacy_scope : Cell :: new ( None ) ,
935
964
} ) ;
936
- let old_invocation_data = self . resolver . invocations . insert ( invoc_id, invocation_data) ;
965
+ let old_invocation_data = self . invocations . insert ( invoc_id, invocation_data) ;
937
966
assert ! ( old_invocation_data. is_none( ) , "invocation data is reset for an invocation" ) ;
938
967
939
968
invocation_data
@@ -959,30 +988,30 @@ impl<'a, 'b> Visitor<'a> for BuildReducedGraphVisitor<'a, 'b> {
959
988
method ! ( visit_ty: ast:: Ty , ast:: TyKind :: Mac , walk_ty) ;
960
989
961
990
fn visit_item ( & mut self , item : & ' a Item ) {
991
+ let parent_scope = & ParentScope {
992
+ module : self . current_module ,
993
+ expansion : self . expansion ,
994
+ legacy : self . current_legacy_scope ,
995
+ derives : Vec :: new ( ) ,
996
+ } ;
962
997
let macro_use = match item. node {
963
998
ItemKind :: MacroDef ( ..) => {
964
- self . resolver . define_macro ( item, self . expansion , & mut self . current_legacy_scope ) ;
999
+ self . current_legacy_scope = self . resolver . define_macro ( item, parent_scope ) ;
965
1000
return
966
1001
}
967
1002
ItemKind :: Mac ( ..) => {
968
1003
self . current_legacy_scope = LegacyScope :: Invocation ( self . visit_invoc ( item. id ) ) ;
969
1004
return
970
1005
}
971
- ItemKind :: Mod ( ..) => self . resolver . contains_macro_use ( & item. attrs ) ,
1006
+ ItemKind :: Mod ( ..) => self . contains_macro_use ( & item. attrs ) ,
972
1007
_ => false ,
973
1008
} ;
974
1009
975
- let orig_current_module = self . resolver . current_module ;
1010
+ let orig_current_module = self . current_module ;
976
1011
let orig_current_legacy_scope = self . current_legacy_scope ;
977
- let parent_scope = ParentScope {
978
- module : self . resolver . current_module ,
979
- expansion : self . expansion ,
980
- legacy : self . current_legacy_scope ,
981
- derives : Vec :: new ( ) ,
982
- } ;
983
- self . resolver . build_reduced_graph_for_item ( item, parent_scope) ;
1012
+ self . build_reduced_graph_for_item ( item, parent_scope) ;
984
1013
visit:: walk_item ( self , item) ;
985
- self . resolver . current_module = orig_current_module;
1014
+ self . current_module = orig_current_module;
986
1015
if !macro_use {
987
1016
self . current_legacy_scope = orig_current_legacy_scope;
988
1017
}
@@ -1002,34 +1031,34 @@ impl<'a, 'b> Visitor<'a> for BuildReducedGraphVisitor<'a, 'b> {
1002
1031
return ;
1003
1032
}
1004
1033
1005
- self . resolver . build_reduced_graph_for_foreign_item ( foreign_item, self . expansion ) ;
1034
+ self . build_reduced_graph_for_foreign_item ( foreign_item, self . expansion ) ;
1006
1035
visit:: walk_foreign_item ( self , foreign_item) ;
1007
1036
}
1008
1037
1009
1038
fn visit_block ( & mut self , block : & ' a Block ) {
1010
- let orig_current_module = self . resolver . current_module ;
1039
+ let orig_current_module = self . current_module ;
1011
1040
let orig_current_legacy_scope = self . current_legacy_scope ;
1012
- self . resolver . build_reduced_graph_for_block ( block, self . expansion ) ;
1041
+ self . build_reduced_graph_for_block ( block, self . expansion ) ;
1013
1042
visit:: walk_block ( self , block) ;
1014
- self . resolver . current_module = orig_current_module;
1043
+ self . current_module = orig_current_module;
1015
1044
self . current_legacy_scope = orig_current_legacy_scope;
1016
1045
}
1017
1046
1018
1047
fn visit_trait_item ( & mut self , item : & ' a TraitItem ) {
1019
- let parent = self . resolver . current_module ;
1048
+ let parent = self . current_module ;
1020
1049
1021
1050
if let TraitItemKind :: Macro ( _) = item. node {
1022
1051
self . visit_invoc ( item. id ) ;
1023
1052
return
1024
1053
}
1025
1054
1026
1055
// Add the item to the trait info.
1027
- let item_def_id = self . resolver . definitions . local_def_id ( item. id ) ;
1056
+ let item_def_id = self . definitions . local_def_id ( item. id ) ;
1028
1057
let ( res, ns) = match item. node {
1029
1058
TraitItemKind :: Const ( ..) => ( Res :: Def ( DefKind :: AssocConst , item_def_id) , ValueNS ) ,
1030
1059
TraitItemKind :: Method ( ref sig, _) => {
1031
1060
if sig. decl . has_self ( ) {
1032
- self . resolver . has_self . insert ( item_def_id) ;
1061
+ self . has_self . insert ( item_def_id) ;
1033
1062
}
1034
1063
( Res :: Def ( DefKind :: Method , item_def_id) , ValueNS )
1035
1064
}
@@ -1040,9 +1069,9 @@ impl<'a, 'b> Visitor<'a> for BuildReducedGraphVisitor<'a, 'b> {
1040
1069
let vis = ty:: Visibility :: Public ;
1041
1070
self . resolver . define ( parent, item. ident , ns, ( res, vis, item. span , self . expansion ) ) ;
1042
1071
1043
- self . resolver . current_module = parent. parent . unwrap ( ) ; // nearest normal ancestor
1072
+ self . current_module = parent. parent . unwrap ( ) ; // nearest normal ancestor
1044
1073
visit:: walk_trait_item ( self , item) ;
1045
- self . resolver . current_module = parent;
1074
+ self . current_module = parent;
1046
1075
}
1047
1076
1048
1077
fn visit_token ( & mut self , t : Token ) {
@@ -1058,7 +1087,7 @@ impl<'a, 'b> Visitor<'a> for BuildReducedGraphVisitor<'a, 'b> {
1058
1087
fn visit_attribute ( & mut self , attr : & ' a ast:: Attribute ) {
1059
1088
if !attr. is_sugared_doc && is_builtin_attr ( attr) {
1060
1089
let parent_scope = ParentScope {
1061
- module : self . resolver . current_module . nearest_item_scope ( ) ,
1090
+ module : self . current_module . nearest_item_scope ( ) ,
1062
1091
expansion : self . expansion ,
1063
1092
legacy : self . current_legacy_scope ,
1064
1093
// Let's hope discerning built-in attributes from derive helpers is not necessary
0 commit comments