@@ -103,7 +103,7 @@ impl IntoDiagArg for PatternSource {
103103/// Denotes whether the context for the set of already bound bindings is a `Product`
104104/// or `Or` context. This is used in e.g., `fresh_binding` and `resolve_pattern_inner`.
105105/// See those functions for more information.
106- #[ derive( PartialEq ) ]
106+ #[ derive( PartialEq , Debug ) ]
107107enum PatBoundCtx {
108108 /// A product pattern context, e.g., `Variant(a, b)`.
109109 Product ,
@@ -217,6 +217,8 @@ pub(crate) enum RibKind<'ra> {
217217 /// We passed through a `macro_rules!` statement
218218 MacroDefinition ( DefId ) ,
219219
220+ LookAheadMacroDefinition ( DefId ) ,
221+
220222 /// All bindings in this rib are generic parameters that can't be used
221223 /// from the default of a generic parameter because they're not declared
222224 /// before said generic parameter. Also see the `visit_generics` override.
@@ -247,6 +249,7 @@ impl RibKind<'_> {
247249 | RibKind :: ConstantItem ( ..)
248250 | RibKind :: Module ( _)
249251 | RibKind :: MacroDefinition ( _)
252+ | RibKind :: LookAheadMacroDefinition ( _)
250253 | RibKind :: InlineAsmSym => false ,
251254 RibKind :: ConstParamTy
252255 | RibKind :: AssocItem
@@ -258,7 +261,9 @@ impl RibKind<'_> {
258261 /// This rib forbids referring to labels defined in upwards ribs.
259262 fn is_label_barrier ( self ) -> bool {
260263 match self {
261- RibKind :: Normal | RibKind :: MacroDefinition ( ..) => false ,
264+ RibKind :: Normal
265+ | RibKind :: MacroDefinition ( ..)
266+ | RibKind :: LookAheadMacroDefinition ( ..) => false ,
262267
263268 RibKind :: AssocItem
264269 | RibKind :: FnOrCoroutine
@@ -3793,17 +3798,21 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
37933798
37943799 /// Apply the bindings from a pattern to the innermost rib of the current scope.
37953800 fn apply_pattern_bindings ( & mut self , mut pat_bindings : PatternBindings ) {
3796- let rib_bindings = self . innermost_rib_bindings ( ValueNS ) ;
37973801 let Some ( ( _, pat_bindings) ) = pat_bindings. pop ( ) else {
37983802 bug ! ( "tried applying nonexistent bindings from pattern" ) ;
37993803 } ;
3800-
3801- if rib_bindings. is_empty ( ) {
3802- // Often, such as for match arms, the bindings are introduced into a new rib.
3803- // In this case, we can move the bindings over directly.
3804- * rib_bindings = pat_bindings;
3805- } else {
3806- rib_bindings. extend ( pat_bindings) ;
3804+ for rib in self . ribs [ ValueNS ] . iter_mut ( ) . rev ( ) {
3805+ let stop = !matches ! ( rib. kind, RibKind :: LookAheadMacroDefinition ( _) ) ;
3806+ if rib. bindings . is_empty ( ) {
3807+ // Often, such as for match arms, the bindings are introduced into a new rib.
3808+ // In this case, we can move the bindings over directly.
3809+ rib. bindings = pat_bindings. clone ( ) ;
3810+ } else {
3811+ rib. bindings . extend ( pat_bindings. clone ( ) ) ;
3812+ }
3813+ if stop {
3814+ break ;
3815+ }
38073816 }
38083817 }
38093818
@@ -4680,11 +4689,19 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
46804689 && let ItemKind :: MacroDef ( ..) = item. kind
46814690 {
46824691 num_macro_definition_ribs += 1 ;
4692+ let res = self . r . local_def_id ( item. id ) . to_def_id ( ) ;
4693+ self . ribs [ ValueNS ] . push ( Rib :: new ( RibKind :: LookAheadMacroDefinition ( res) ) ) ;
4694+ }
4695+ }
4696+
4697+ for stmt in & block. stmts {
4698+ if let StmtKind :: Item ( ref item) = stmt. kind
4699+ && let ItemKind :: MacroDef ( ..) = item. kind
4700+ {
46834701 let res = self . r . local_def_id ( item. id ) . to_def_id ( ) ;
46844702 self . ribs [ ValueNS ] . push ( Rib :: new ( RibKind :: MacroDefinition ( res) ) ) ;
46854703 self . label_ribs . push ( Rib :: new ( RibKind :: MacroDefinition ( res) ) ) ;
46864704 }
4687-
46884705 self . visit_stmt ( stmt) ;
46894706 }
46904707
@@ -4694,6 +4711,10 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
46944711 self . ribs [ ValueNS ] . pop ( ) ;
46954712 self . label_ribs . pop ( ) ;
46964713 }
4714+ for _ in 0 ..num_macro_definition_ribs {
4715+ // pop `RibKind::LookAheadMacroDefinition`
4716+ self . ribs [ ValueNS ] . pop ( ) ;
4717+ }
46974718 self . last_block_rib = self . ribs [ ValueNS ] . pop ( ) ;
46984719 if anonymous_module. is_some ( ) {
46994720 self . ribs [ TypeNS ] . pop ( ) ;
0 commit comments