@@ -192,6 +192,13 @@ pub(crate) enum RibKind<'ra> {
192
192
/// No restriction needs to be applied.
193
193
Normal ,
194
194
195
+ /// We passed through an `ast::Block`.
196
+ /// Behaves like `Normal`, but also partially like `Module` if the block contains items.
197
+ /// `Block(None)` must be always processed in the same way as `Block(Some(module))`
198
+ /// with empty `module`. The module can be `None` only because creation of some definitely
199
+ /// empty modules is skipped as an optimization.
200
+ Block ( Option < Module < ' ra > > ) ,
201
+
195
202
/// We passed through an impl or trait and are now in one of its
196
203
/// methods or associated types. Allow references to ty params that impl or trait
197
204
/// binds. Disallow any other upvars (including other ty params that are
@@ -210,7 +217,7 @@ pub(crate) enum RibKind<'ra> {
210
217
/// All other constants aren't allowed to use generic params at all.
211
218
ConstantItem ( ConstantHasGenerics , Option < ( Ident , ConstantItemKind ) > ) ,
212
219
213
- /// We passed through a module.
220
+ /// We passed through a module item .
214
221
Module ( Module < ' ra > ) ,
215
222
216
223
/// We passed through a `macro_rules!` statement
@@ -242,6 +249,7 @@ impl RibKind<'_> {
242
249
pub ( crate ) fn contains_params ( & self ) -> bool {
243
250
match self {
244
251
RibKind :: Normal
252
+ | RibKind :: Block ( ..)
245
253
| RibKind :: FnOrCoroutine
246
254
| RibKind :: ConstantItem ( ..)
247
255
| RibKind :: Module ( _)
@@ -258,15 +266,8 @@ impl RibKind<'_> {
258
266
fn is_label_barrier ( self ) -> bool {
259
267
match self {
260
268
RibKind :: Normal | RibKind :: MacroDefinition ( ..) => false ,
261
-
262
- RibKind :: AssocItem
263
- | RibKind :: FnOrCoroutine
264
- | RibKind :: Item ( ..)
265
- | RibKind :: ConstantItem ( ..)
266
- | RibKind :: Module ( ..)
267
- | RibKind :: ForwardGenericParamBan ( _)
268
- | RibKind :: ConstParamTy
269
- | RibKind :: InlineAsmSym => true ,
269
+ RibKind :: FnOrCoroutine | RibKind :: ConstantItem ( ..) => true ,
270
+ kind => bug ! ( "unexpected rib kind: {kind:?}" ) ,
270
271
}
271
272
}
272
273
}
@@ -1527,19 +1528,6 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
1527
1528
ret
1528
1529
}
1529
1530
1530
- fn with_mod_rib < T > ( & mut self , id : NodeId , f : impl FnOnce ( & mut Self ) -> T ) -> T {
1531
- let module = self . r . expect_module ( self . r . local_def_id ( id) . to_def_id ( ) ) ;
1532
- // Move down in the graph.
1533
- let orig_module = replace ( & mut self . parent_scope . module , module) ;
1534
- self . with_rib ( ValueNS , RibKind :: Module ( module) , |this| {
1535
- this. with_rib ( TypeNS , RibKind :: Module ( module) , |this| {
1536
- let ret = f ( this) ;
1537
- this. parent_scope . module = orig_module;
1538
- ret
1539
- } )
1540
- } )
1541
- }
1542
-
1543
1531
fn visit_generic_params ( & mut self , params : & ' ast [ GenericParam ] , add_self_upper : bool ) {
1544
1532
// For type parameter defaults, we have to ban access
1545
1533
// to following type parameters, as the GenericArgs can only
@@ -2677,20 +2665,25 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
2677
2665
}
2678
2666
2679
2667
ItemKind :: Mod ( ..) => {
2680
- self . with_mod_rib ( item. id , |this| {
2681
- if mod_inner_docs {
2682
- this. resolve_doc_links ( & item. attrs , MaybeExported :: Ok ( item. id ) ) ;
2683
- }
2684
- let old_macro_rules = this. parent_scope . macro_rules ;
2685
- visit:: walk_item ( this, item) ;
2686
- // Maintain macro_rules scopes in the same way as during early resolution
2687
- // for diagnostics and doc links.
2688
- if item. attrs . iter ( ) . all ( |attr| {
2689
- !attr. has_name ( sym:: macro_use) && !attr. has_name ( sym:: macro_escape)
2690
- } ) {
2691
- this. parent_scope . macro_rules = old_macro_rules;
2692
- }
2668
+ let module = self . r . expect_module ( self . r . local_def_id ( item. id ) . to_def_id ( ) ) ;
2669
+ let orig_module = replace ( & mut self . parent_scope . module , module) ;
2670
+ self . with_rib ( ValueNS , RibKind :: Module ( module) , |this| {
2671
+ this. with_rib ( TypeNS , RibKind :: Module ( module) , |this| {
2672
+ if mod_inner_docs {
2673
+ this. resolve_doc_links ( & item. attrs , MaybeExported :: Ok ( item. id ) ) ;
2674
+ }
2675
+ let old_macro_rules = this. parent_scope . macro_rules ;
2676
+ visit:: walk_item ( this, item) ;
2677
+ // Maintain macro_rules scopes in the same way as during early resolution
2678
+ // for diagnostics and doc links.
2679
+ if item. attrs . iter ( ) . all ( |attr| {
2680
+ !attr. has_name ( sym:: macro_use) && !attr. has_name ( sym:: macro_escape)
2681
+ } ) {
2682
+ this. parent_scope . macro_rules = old_macro_rules;
2683
+ }
2684
+ } )
2693
2685
} ) ;
2686
+ self . parent_scope . module = orig_module;
2694
2687
}
2695
2688
2696
2689
ItemKind :: Static ( box ast:: StaticItem {
@@ -2821,9 +2814,9 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
2821
2814
// We also can't shadow bindings from associated parent items.
2822
2815
for ns in [ ValueNS , TypeNS ] {
2823
2816
for parent_rib in self . ribs [ ns] . iter ( ) . rev ( ) {
2824
- // Break at mod level, to account for nested items which are
2817
+ // Break at module or block level, to account for nested items which are
2825
2818
// allowed to shadow generic param names.
2826
- if matches ! ( parent_rib. kind, RibKind :: Module ( ..) ) {
2819
+ if matches ! ( parent_rib. kind, RibKind :: Module ( ..) | RibKind :: Block ( .. ) ) {
2827
2820
break ;
2828
2821
}
2829
2822
@@ -4652,16 +4645,16 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
4652
4645
debug ! ( "(resolving block) entering block" ) ;
4653
4646
// Move down in the graph, if there's an anonymous module rooted here.
4654
4647
let orig_module = self . parent_scope . module ;
4655
- let anonymous_module = self . r . block_map . get ( & block. id ) . cloned ( ) ; // clones a reference
4648
+ let anonymous_module = self . r . block_map . get ( & block. id ) . copied ( ) ;
4656
4649
4657
4650
let mut num_macro_definition_ribs = 0 ;
4658
4651
if let Some ( anonymous_module) = anonymous_module {
4659
4652
debug ! ( "(resolving block) found anonymous module, moving down" ) ;
4660
- self . ribs [ ValueNS ] . push ( Rib :: new ( RibKind :: Module ( anonymous_module) ) ) ;
4661
- self . ribs [ TypeNS ] . push ( Rib :: new ( RibKind :: Module ( anonymous_module) ) ) ;
4653
+ self . ribs [ ValueNS ] . push ( Rib :: new ( RibKind :: Block ( Some ( anonymous_module) ) ) ) ;
4654
+ self . ribs [ TypeNS ] . push ( Rib :: new ( RibKind :: Block ( Some ( anonymous_module) ) ) ) ;
4662
4655
self . parent_scope . module = anonymous_module;
4663
4656
} else {
4664
- self . ribs [ ValueNS ] . push ( Rib :: new ( RibKind :: Normal ) ) ;
4657
+ self . ribs [ ValueNS ] . push ( Rib :: new ( RibKind :: Block ( None ) ) ) ;
4665
4658
}
4666
4659
4667
4660
// Descend into the block.
0 commit comments