@@ -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
}
@@ -3076,7 +3086,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
3076
3086
Def :: Local ( _, node_id) => {
3077
3087
for rib in ribs {
3078
3088
match rib. kind {
3079
- NormalRibKind => {
3089
+ NormalRibKind | AnonymousModuleRibKind ( .. ) => {
3080
3090
// Nothing to do. Continue.
3081
3091
}
3082
3092
ClosureRibKind ( function_id) => {
@@ -3124,7 +3134,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
3124
3134
Def :: TyParam ( ..) | Def :: SelfTy ( ..) => {
3125
3135
for rib in ribs {
3126
3136
match rib. kind {
3127
- NormalRibKind | MethodRibKind | ClosureRibKind ( ..) => {
3137
+ NormalRibKind | MethodRibKind | ClosureRibKind ( ..) |
3138
+ AnonymousModuleRibKind ( ..) => {
3128
3139
// Nothing to do. Continue.
3129
3140
}
3130
3141
ItemRibKind => {
@@ -3275,13 +3286,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
3275
3286
namespace : Namespace )
3276
3287
-> Option < LocalDef > {
3277
3288
// Check the local set of ribs.
3278
- let ( name, ribs) = match namespace {
3279
- ValueNS => ( ident. name , & self . value_ribs ) ,
3280
- TypeNS => ( ident. unhygienic_name , & self . type_ribs ) ,
3281
- } ;
3289
+ let name = match namespace { ValueNS => ident. name , TypeNS => ident. unhygienic_name } ;
3282
3290
3283
- for ( i , rib ) in ribs . iter ( ) . enumerate ( ) . rev ( ) {
3284
- if let Some ( def_like) = rib . bindings . get ( & name) . cloned ( ) {
3291
+ for i in ( 0 .. self . get_ribs ( namespace ) . len ( ) ) . rev ( ) {
3292
+ if let Some ( def_like) = self . get_ribs ( namespace ) [ i ] . bindings . get ( & name) . cloned ( ) {
3285
3293
match def_like {
3286
3294
DlDef ( def) => {
3287
3295
debug ! ( "(resolving path in local ribs) resolved `{}` to {:?} at {}" ,
@@ -3301,6 +3309,18 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
3301
3309
}
3302
3310
}
3303
3311
}
3312
+
3313
+ if let AnonymousModuleRibKind ( module) = self . get_ribs ( namespace) [ i] . kind {
3314
+ if let Success ( ( target, _) ) = self . resolve_name_in_module ( module,
3315
+ ident. unhygienic_name ,
3316
+ namespace,
3317
+ PathSearch ,
3318
+ true ) {
3319
+ if let Some ( def) = target. binding . def ( ) {
3320
+ return Some ( LocalDef :: from_def ( def) ) ;
3321
+ }
3322
+ }
3323
+ }
3304
3324
}
3305
3325
3306
3326
None
0 commit comments