@@ -193,6 +193,13 @@ pub(crate) enum RibKind<'ra> {
193
193
/// No restriction needs to be applied.
194
194
Normal ,
195
195
196
+ /// We passed through an `ast::Block`.
197
+ /// Behaves like `Normal`, but also partially like `Module` if the block contains items.
198
+ /// `Block(None)` must be always processed in the same way as `Block(Some(module))`
199
+ /// with empty `module`. The module can be `None` only because creation of some definitely
200
+ /// empty modules is skipped as an optimization.
201
+ Block ( Option < Module < ' ra > > ) ,
202
+
196
203
/// We passed through an impl or trait and are now in one of its
197
204
/// methods or associated types. Allow references to ty params that impl or trait
198
205
/// binds. Disallow any other upvars (including other ty params that are
@@ -211,7 +218,7 @@ pub(crate) enum RibKind<'ra> {
211
218
/// All other constants aren't allowed to use generic params at all.
212
219
ConstantItem ( ConstantHasGenerics , Option < ( Ident , ConstantItemKind ) > ) ,
213
220
214
- /// We passed through a module.
221
+ /// We passed through a module item .
215
222
Module ( Module < ' ra > ) ,
216
223
217
224
/// We passed through a `macro_rules!` statement
@@ -243,6 +250,7 @@ impl RibKind<'_> {
243
250
pub ( crate ) fn contains_params ( & self ) -> bool {
244
251
match self {
245
252
RibKind :: Normal
253
+ | RibKind :: Block ( ..)
246
254
| RibKind :: FnOrCoroutine
247
255
| RibKind :: ConstantItem ( ..)
248
256
| RibKind :: Module ( _)
@@ -259,15 +267,8 @@ impl RibKind<'_> {
259
267
fn is_label_barrier ( self ) -> bool {
260
268
match self {
261
269
RibKind :: Normal | RibKind :: MacroDefinition ( ..) => false ,
262
-
263
- RibKind :: AssocItem
264
- | RibKind :: FnOrCoroutine
265
- | RibKind :: Item ( ..)
266
- | RibKind :: ConstantItem ( ..)
267
- | RibKind :: Module ( ..)
268
- | RibKind :: ForwardGenericParamBan ( _)
269
- | RibKind :: ConstParamTy
270
- | RibKind :: InlineAsmSym => true ,
270
+ RibKind :: FnOrCoroutine | RibKind :: ConstantItem ( ..) => true ,
271
+ kind => bug ! ( "unexpected rib kind: {kind:?}" ) ,
271
272
}
272
273
}
273
274
}
@@ -2820,9 +2821,9 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
2820
2821
// We also can't shadow bindings from associated parent items.
2821
2822
for ns in [ ValueNS , TypeNS ] {
2822
2823
for parent_rib in self . ribs [ ns] . iter ( ) . rev ( ) {
2823
- // Break at mod level, to account for nested items which are
2824
+ // Break at module or block level, to account for nested items which are
2824
2825
// allowed to shadow generic param names.
2825
- if matches ! ( parent_rib. kind, RibKind :: Module ( ..) ) {
2826
+ if matches ! ( parent_rib. kind, RibKind :: Module ( ..) | RibKind :: Block ( .. ) ) {
2826
2827
break ;
2827
2828
}
2828
2829
@@ -4663,16 +4664,16 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
4663
4664
debug ! ( "(resolving block) entering block" ) ;
4664
4665
// Move down in the graph, if there's an anonymous module rooted here.
4665
4666
let orig_module = self . parent_scope . module ;
4666
- let anonymous_module = self . r . block_map . get ( & block. id ) . cloned ( ) ; // clones a reference
4667
+ let anonymous_module = self . r . block_map . get ( & block. id ) . copied ( ) ;
4667
4668
4668
4669
let mut num_macro_definition_ribs = 0 ;
4669
4670
if let Some ( anonymous_module) = anonymous_module {
4670
4671
debug ! ( "(resolving block) found anonymous module, moving down" ) ;
4671
- self . ribs [ ValueNS ] . push ( Rib :: new ( RibKind :: Module ( anonymous_module) ) ) ;
4672
- self . ribs [ TypeNS ] . push ( Rib :: new ( RibKind :: Module ( anonymous_module) ) ) ;
4672
+ self . ribs [ ValueNS ] . push ( Rib :: new ( RibKind :: Block ( Some ( anonymous_module) ) ) ) ;
4673
+ self . ribs [ TypeNS ] . push ( Rib :: new ( RibKind :: Block ( Some ( anonymous_module) ) ) ) ;
4673
4674
self . parent_scope . module = anonymous_module;
4674
4675
} else {
4675
- self . ribs [ ValueNS ] . push ( Rib :: new ( RibKind :: Normal ) ) ;
4676
+ self . ribs [ ValueNS ] . push ( Rib :: new ( RibKind :: Block ( None ) ) ) ;
4676
4677
}
4677
4678
4678
4679
// Descend into the block.
0 commit comments