@@ -723,7 +723,7 @@ enum FallbackSuggestion {
723
723
}
724
724
725
725
#[ derive( Copy , Clone ) ]
726
- enum TypeParameters < ' a > {
726
+ enum TypeParameters < ' tcx , ' a > {
727
727
NoTypeParameters ,
728
728
HasTypeParameters ( // Type parameters.
729
729
& ' a Generics ,
@@ -733,13 +733,13 @@ enum TypeParameters<'a> {
733
733
ParamSpace ,
734
734
735
735
// The kind of the rib used for type parameters.
736
- RibKind ) ,
736
+ RibKind < ' tcx > ) ,
737
737
}
738
738
739
739
// The rib kind controls the translation of local
740
740
// definitions (`Def::Local`) to upvars (`Def::Upvar`).
741
741
#[ derive( Copy , Clone , Debug ) ]
742
- enum RibKind {
742
+ enum RibKind < ' a > {
743
743
// No translation needs to be applied.
744
744
NormalRibKind ,
745
745
@@ -758,6 +758,9 @@ enum RibKind {
758
758
759
759
// We're in a constant item. Can't refer to dynamic stuff.
760
760
ConstantItemRibKind ,
761
+
762
+ // We passed through an anonymous module.
763
+ AnonymousModuleRibKind ( Module < ' a > ) ,
761
764
}
762
765
763
766
#[ derive( Copy , Clone ) ]
@@ -799,13 +802,13 @@ enum BareIdentifierPatternResolution {
799
802
800
803
/// One local scope.
801
804
#[ derive( Debug ) ]
802
- struct Rib {
805
+ struct Rib < ' a > {
803
806
bindings : HashMap < Name , DefLike > ,
804
- kind : RibKind ,
807
+ kind : RibKind < ' a > ,
805
808
}
806
809
807
- impl Rib {
808
- fn new ( kind : RibKind ) -> Rib {
810
+ impl < ' a > Rib < ' a > {
811
+ fn new ( kind : RibKind < ' a > ) -> Rib < ' a > {
809
812
Rib {
810
813
bindings : HashMap :: new ( ) ,
811
814
kind : kind,
@@ -1180,13 +1183,13 @@ pub struct Resolver<'a, 'tcx: 'a> {
1180
1183
1181
1184
// The current set of local scopes, for values.
1182
1185
// FIXME #4948: Reuse ribs to avoid allocation.
1183
- value_ribs : Vec < Rib > ,
1186
+ value_ribs : Vec < Rib < ' a > > ,
1184
1187
1185
1188
// The current set of local scopes, for types.
1186
- type_ribs : Vec < Rib > ,
1189
+ type_ribs : Vec < Rib < ' a > > ,
1187
1190
1188
1191
// The current set of local scopes, for labels.
1189
- label_ribs : Vec < Rib > ,
1192
+ label_ribs : Vec < Rib < ' a > > ,
1190
1193
1191
1194
// The trait that the current context can refer to.
1192
1195
current_trait_ref : Option < ( DefId , TraitRef ) > ,
@@ -1304,6 +1307,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
1304
1307
self . arenas . modules . alloc ( ModuleS :: new ( parent_link, def, external, is_public) )
1305
1308
}
1306
1309
1310
+ fn get_ribs < ' b > ( & ' b mut self , ns : Namespace ) -> & ' b mut Vec < Rib < ' a > > {
1311
+ match ns { ValueNS => & mut self . value_ribs , TypeNS => & mut self . type_ribs }
1312
+ }
1313
+
1307
1314
#[ inline]
1308
1315
fn record_import_use ( & mut self , import_id : NodeId , name : Name ) {
1309
1316
if !self . make_glob_map {
@@ -2122,7 +2129,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
2122
2129
}
2123
2130
}
2124
2131
2125
- fn with_type_parameter_rib < F > ( & mut self , type_parameters : TypeParameters , f : F )
2132
+ fn with_type_parameter_rib < ' b , F > ( & ' b mut self , type_parameters : TypeParameters < ' a , ' b > , f : F )
2126
2133
where F : FnOnce ( & mut Resolver )
2127
2134
{
2128
2135
match type_parameters {
@@ -2191,7 +2198,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
2191
2198
}
2192
2199
}
2193
2200
2194
- fn resolve_function ( & mut self , rib_kind : RibKind , declaration : & FnDecl , block : & Block ) {
2201
+ fn resolve_function ( & mut self , rib_kind : RibKind < ' a > , declaration : & FnDecl , block : & Block ) {
2195
2202
// Create a value rib for the function.
2196
2203
self . value_ribs . push ( Rib :: new ( rib_kind) ) ;
2197
2204
@@ -2494,18 +2501,18 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
2494
2501
2495
2502
fn resolve_block ( & mut self , block : & Block ) {
2496
2503
debug ! ( "(resolving block) entering block" ) ;
2497
- self . value_ribs . push ( Rib :: new ( NormalRibKind ) ) ;
2498
-
2499
2504
// Move down in the graph, if there's an anonymous module rooted here.
2500
2505
let orig_module = self . current_module ;
2501
- match orig_module. anonymous_children . borrow ( ) . get ( & block. id ) {
2502
- None => {
2503
- // Nothing to do.
2504
- }
2505
- Some ( anonymous_module) => {
2506
- debug ! ( "(resolving block) found anonymous module, moving down" ) ;
2507
- self . current_module = anonymous_module;
2508
- }
2506
+ let anonymous_module =
2507
+ orig_module. anonymous_children . borrow ( ) . get ( & block. id ) . map ( |module| * module) ;
2508
+
2509
+ if let Some ( anonymous_module) = anonymous_module {
2510
+ debug ! ( "(resolving block) found anonymous module, moving down" ) ;
2511
+ self . value_ribs . push ( Rib :: new ( AnonymousModuleRibKind ( anonymous_module) ) ) ;
2512
+ self . type_ribs . push ( Rib :: new ( AnonymousModuleRibKind ( anonymous_module) ) ) ;
2513
+ self . current_module = anonymous_module;
2514
+ } else {
2515
+ self . value_ribs . push ( Rib :: new ( NormalRibKind ) ) ;
2509
2516
}
2510
2517
2511
2518
// Check for imports appearing after non-item statements.
@@ -2538,6 +2545,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
2538
2545
if !self . resolved {
2539
2546
self . current_module = orig_module;
2540
2547
self . value_ribs . pop ( ) ;
2548
+ if let Some ( _) = anonymous_module {
2549
+ self . type_ribs . pop ( ) ;
2550
+ }
2541
2551
}
2542
2552
debug ! ( "(resolving block) leaving block" ) ;
2543
2553
}
@@ -3072,7 +3082,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
3072
3082
Def :: Local ( _, node_id) => {
3073
3083
for rib in ribs {
3074
3084
match rib. kind {
3075
- NormalRibKind => {
3085
+ NormalRibKind | AnonymousModuleRibKind ( .. ) => {
3076
3086
// Nothing to do. Continue.
3077
3087
}
3078
3088
ClosureRibKind ( function_id) => {
@@ -3120,7 +3130,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
3120
3130
Def :: TyParam ( ..) | Def :: SelfTy ( ..) => {
3121
3131
for rib in ribs {
3122
3132
match rib. kind {
3123
- NormalRibKind | MethodRibKind | ClosureRibKind ( ..) => {
3133
+ NormalRibKind | MethodRibKind | ClosureRibKind ( ..) |
3134
+ AnonymousModuleRibKind ( ..) => {
3124
3135
// Nothing to do. Continue.
3125
3136
}
3126
3137
ItemRibKind => {
@@ -3271,13 +3282,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
3271
3282
namespace : Namespace )
3272
3283
-> Option < LocalDef > {
3273
3284
// Check the local set of ribs.
3274
- let ( name, ribs) = match namespace {
3275
- ValueNS => ( ident. name , & self . value_ribs ) ,
3276
- TypeNS => ( ident. unhygienic_name , & self . type_ribs ) ,
3277
- } ;
3285
+ let name = match namespace { ValueNS => ident. name , TypeNS => ident. unhygienic_name } ;
3278
3286
3279
- for ( i , rib ) in ribs . iter ( ) . enumerate ( ) . rev ( ) {
3280
- if let Some ( def_like) = rib . bindings . get ( & name) . cloned ( ) {
3287
+ for i in ( 0 .. self . get_ribs ( namespace ) . len ( ) ) . rev ( ) {
3288
+ if let Some ( def_like) = self . get_ribs ( namespace ) [ i ] . bindings . get ( & name) . cloned ( ) {
3281
3289
match def_like {
3282
3290
DlDef ( def) => {
3283
3291
debug ! ( "(resolving path in local ribs) resolved `{}` to {:?} at {}" ,
@@ -3297,6 +3305,18 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
3297
3305
}
3298
3306
}
3299
3307
}
3308
+
3309
+ if let AnonymousModuleRibKind ( module) = self . get_ribs ( namespace) [ i] . kind {
3310
+ if let Success ( ( target, _) ) = self . resolve_name_in_module ( module,
3311
+ ident. unhygienic_name ,
3312
+ namespace,
3313
+ PathSearch ,
3314
+ true ) {
3315
+ if let Some ( def) = target. binding . def ( ) {
3316
+ return Some ( LocalDef :: from_def ( def) ) ;
3317
+ }
3318
+ }
3319
+ }
3300
3320
}
3301
3321
3302
3322
None
0 commit comments