@@ -193,6 +193,13 @@ pub(crate) enum RibKind<'ra> {
193193 /// No restriction needs to be applied.
194194 Normal ,
195195
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+
196203 /// We passed through an impl or trait and are now in one of its
197204 /// methods or associated types. Allow references to ty params that impl or trait
198205 /// binds. Disallow any other upvars (including other ty params that are
@@ -211,7 +218,7 @@ pub(crate) enum RibKind<'ra> {
211218 /// All other constants aren't allowed to use generic params at all.
212219 ConstantItem ( ConstantHasGenerics , Option < ( Ident , ConstantItemKind ) > ) ,
213220
214- /// We passed through a module.
221+ /// We passed through a module item .
215222 Module ( Module < ' ra > ) ,
216223
217224 /// We passed through a `macro_rules!` statement
@@ -243,6 +250,7 @@ impl RibKind<'_> {
243250 pub ( crate ) fn contains_params ( & self ) -> bool {
244251 match self {
245252 RibKind :: Normal
253+ | RibKind :: Block ( ..)
246254 | RibKind :: FnOrCoroutine
247255 | RibKind :: ConstantItem ( ..)
248256 | RibKind :: Module ( _)
@@ -259,15 +267,8 @@ impl RibKind<'_> {
259267 fn is_label_barrier ( self ) -> bool {
260268 match self {
261269 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:?}" ) ,
271272 }
272273 }
273274}
@@ -2820,9 +2821,9 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
28202821 // We also can't shadow bindings from associated parent items.
28212822 for ns in [ ValueNS , TypeNS ] {
28222823 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
28242825 // allowed to shadow generic param names.
2825- if matches ! ( parent_rib. kind, RibKind :: Module ( ..) ) {
2826+ if matches ! ( parent_rib. kind, RibKind :: Module ( ..) | RibKind :: Block ( .. ) ) {
28262827 break ;
28272828 }
28282829
@@ -4663,16 +4664,16 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
46634664 debug ! ( "(resolving block) entering block" ) ;
46644665 // Move down in the graph, if there's an anonymous module rooted here.
46654666 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 ( ) ;
46674668
46684669 let mut num_macro_definition_ribs = 0 ;
46694670 if let Some ( anonymous_module) = anonymous_module {
46704671 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) ) ) ) ;
46734674 self . parent_scope . module = anonymous_module;
46744675 } else {
4675- self . ribs [ ValueNS ] . push ( Rib :: new ( RibKind :: Normal ) ) ;
4676+ self . ribs [ ValueNS ] . push ( Rib :: new ( RibKind :: Block ( None ) ) ) ;
46764677 }
46774678
46784679 // Descend into the block.
0 commit comments