@@ -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
}
@@ -2821,9 +2822,9 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
2821
2822
// We also can't shadow bindings from associated parent items.
2822
2823
for ns in [ ValueNS , TypeNS ] {
2823
2824
for parent_rib in self . ribs [ ns] . iter ( ) . rev ( ) {
2824
- // Break at mod level, to account for nested items which are
2825
+ // Break at module or block level, to account for nested items which are
2825
2826
// allowed to shadow generic param names.
2826
- if matches ! ( parent_rib. kind, RibKind :: Module ( ..) ) {
2827
+ if matches ! ( parent_rib. kind, RibKind :: Module ( ..) | RibKind :: Block ( .. ) ) {
2827
2828
break ;
2828
2829
}
2829
2830
@@ -4664,16 +4665,16 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
4664
4665
debug ! ( "(resolving block) entering block" ) ;
4665
4666
// Move down in the graph, if there's an anonymous module rooted here.
4666
4667
let orig_module = self . parent_scope . module ;
4667
- let anonymous_module = self . r . block_map . get ( & block. id ) . cloned ( ) ; // clones a reference
4668
+ let anonymous_module = self . r . block_map . get ( & block. id ) . copied ( ) ;
4668
4669
4669
4670
let mut num_macro_definition_ribs = 0 ;
4670
4671
if let Some ( anonymous_module) = anonymous_module {
4671
4672
debug ! ( "(resolving block) found anonymous module, moving down" ) ;
4672
- self . ribs [ ValueNS ] . push ( Rib :: new ( RibKind :: Module ( anonymous_module) ) ) ;
4673
- self . ribs [ TypeNS ] . push ( Rib :: new ( RibKind :: Module ( anonymous_module) ) ) ;
4673
+ self . ribs [ ValueNS ] . push ( Rib :: new ( RibKind :: Block ( Some ( anonymous_module) ) ) ) ;
4674
+ self . ribs [ TypeNS ] . push ( Rib :: new ( RibKind :: Block ( Some ( anonymous_module) ) ) ) ;
4674
4675
self . parent_scope . module = anonymous_module;
4675
4676
} else {
4676
- self . ribs [ ValueNS ] . push ( Rib :: new ( RibKind :: Normal ) ) ;
4677
+ self . ribs [ ValueNS ] . push ( Rib :: new ( RibKind :: Block ( None ) ) ) ;
4677
4678
}
4678
4679
4679
4680
// Descend into the block.
0 commit comments