@@ -194,6 +194,30 @@ impl<'matcher> Tracker<'matcher> for NoopTracker {
194194 }
195195}
196196
197+ #[ instrument( skip( cx, tts) ) ]
198+ pub fn expand_token_stream < ' cx > (
199+ cx : & ' cx mut ExtCtxt < ' _ > ,
200+ sp : Span ,
201+ arm_span : Span ,
202+ node_id : NodeId ,
203+ name : Ident ,
204+ tts : TokenStream ,
205+ ) -> Box < dyn MacResult + ' cx > {
206+ Box :: new ( ParserAnyMacro {
207+ parser : Parser :: new ( & cx. sess . psess , tts, None ) ,
208+
209+ // Pass along the original expansion site and the name of the macro
210+ // so we can print a useful error message if the parse of the expanded
211+ // macro leaves unparsed tokens.
212+ site_span : sp,
213+ macro_ident : name,
214+ lint_node_id : cx. current_expansion . lint_node_id ,
215+ is_trailing_mac : cx. current_expansion . is_trailing_mac ,
216+ arm_span,
217+ is_local : is_defined_in_current_crate ( node_id) ,
218+ } )
219+ }
220+
197221/// Expands the rules based macro defined by `rules` for a given input `arg`.
198222#[ instrument( skip( cx, transparency, arg, rules) ) ]
199223fn expand_macro < ' cx > (
@@ -207,9 +231,6 @@ fn expand_macro<'cx>(
207231 rules : & [ MacroRule ] ,
208232) -> Box < dyn MacResult + ' cx > {
209233 let psess = & cx. sess . psess ;
210- // Macros defined in the current crate have a real node id,
211- // whereas macros from an external crate have a dummy id.
212- let is_local = node_id != DUMMY_NODE_ID ;
213234
214235 if cx. trace_macros ( ) {
215236 let msg = format ! ( "expanding `{}! {{ {} }}`" , name, pprust:: tts_to_string( & arg) ) ;
@@ -220,7 +241,7 @@ fn expand_macro<'cx>(
220241 let try_success_result = try_match_macro ( psess, name, & arg, rules, & mut NoopTracker ) ;
221242
222243 match try_success_result {
223- Ok ( ( i , rule, named_matches) ) => {
244+ Ok ( ( rule_index , rule, named_matches) ) => {
224245 let mbe:: TokenTree :: Delimited ( rhs_span, _, ref rhs) = rule. rhs else {
225246 cx. dcx ( ) . span_bug ( sp, "malformed macro rhs" ) ;
226247 } ;
@@ -241,27 +262,13 @@ fn expand_macro<'cx>(
241262 trace_macros_note ( & mut cx. expansions , sp, msg) ;
242263 }
243264
244- let p = Parser :: new ( psess, tts, None ) ;
245-
246- if is_local {
247- cx. resolver . record_macro_rule_usage ( node_id, i) ;
265+ if is_defined_in_current_crate ( node_id) {
266+ cx. resolver . record_macro_rule_usage ( node_id, rule_index) ;
248267 }
249268
250269 // Let the context choose how to interpret the result.
251270 // Weird, but useful for X-macros.
252- Box :: new ( ParserAnyMacro {
253- parser : p,
254-
255- // Pass along the original expansion site and the name of the macro
256- // so we can print a useful error message if the parse of the expanded
257- // macro leaves unparsed tokens.
258- site_span : sp,
259- macro_ident : name,
260- lint_node_id : cx. current_expansion . lint_node_id ,
261- is_trailing_mac : cx. current_expansion . is_trailing_mac ,
262- arm_span,
263- is_local,
264- } )
271+ expand_token_stream ( cx, sp, arm_span, node_id, name, tts)
265272 }
266273 Err ( CanRetry :: No ( guar) ) => {
267274 debug ! ( "Will not retry matching as an error was emitted already" ) ;
@@ -382,7 +389,7 @@ pub fn compile_declarative_macro(
382389 edition,
383390 ident. name ,
384391 attrs,
385- node_id != DUMMY_NODE_ID ,
392+ is_defined_in_current_crate ( node_id) ,
386393 )
387394 } ;
388395 let dummy_syn_ext = |guar| ( mk_syn_ext ( Arc :: new ( DummyExpander ( guar) ) ) , 0 ) ;
@@ -454,7 +461,7 @@ pub fn compile_declarative_macro(
454461 }
455462
456463 // Return the number of rules for unused rule linting, if this is a local macro.
457- let nrules = if node_id != DUMMY_NODE_ID { rules. len ( ) } else { 0 } ;
464+ let nrules = if is_defined_in_current_crate ( node_id) { rules. len ( ) } else { 0 } ;
458465
459466 let expander =
460467 Arc :: new ( MacroRulesMacroExpander { name : ident, span, node_id, transparency, rules } ) ;
@@ -1030,9 +1037,7 @@ fn check_matcher_core<'tt>(
10301037 // definition of this macro_rules, not while (re)parsing
10311038 // the macro when compiling another crate that is using the
10321039 // macro. (See #86567.)
1033- // Macros defined in the current crate have a real node id,
1034- // whereas macros from an external crate have a dummy id.
1035- if node_id != DUMMY_NODE_ID
1040+ if is_defined_in_current_crate ( node_id)
10361041 && matches ! ( kind, NonterminalKind :: Pat ( PatParam { inferred: true } ) )
10371042 && matches ! (
10381043 next_token,
@@ -1292,6 +1297,12 @@ fn quoted_tt_to_string(tt: &mbe::TokenTree) -> String {
12921297 }
12931298}
12941299
1300+ fn is_defined_in_current_crate ( node_id : NodeId ) -> bool {
1301+ // Macros defined in the current crate have a real node id,
1302+ // whereas macros from an external crate have a dummy id.
1303+ node_id != DUMMY_NODE_ID
1304+ }
1305+
12951306pub ( super ) fn parser_from_cx (
12961307 psess : & ParseSess ,
12971308 mut tts : TokenStream ,
0 commit comments