@@ -194,6 +194,30 @@ impl<'matcher> Tracker<'matcher> for NoopTracker {
194
194
}
195
195
}
196
196
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
+
197
221
/// Expands the rules based macro defined by `rules` for a given input `arg`.
198
222
#[ instrument( skip( cx, transparency, arg, rules) ) ]
199
223
fn expand_macro < ' cx > (
@@ -207,9 +231,6 @@ fn expand_macro<'cx>(
207
231
rules : & [ MacroRule ] ,
208
232
) -> Box < dyn MacResult + ' cx > {
209
233
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 ;
213
234
214
235
if cx. trace_macros ( ) {
215
236
let msg = format ! ( "expanding `{}! {{ {} }}`" , name, pprust:: tts_to_string( & arg) ) ;
@@ -220,7 +241,7 @@ fn expand_macro<'cx>(
220
241
let try_success_result = try_match_macro ( psess, name, & arg, rules, & mut NoopTracker ) ;
221
242
222
243
match try_success_result {
223
- Ok ( ( i , rule, named_matches) ) => {
244
+ Ok ( ( rule_index , rule, named_matches) ) => {
224
245
let mbe:: TokenTree :: Delimited ( rhs_span, _, ref rhs) = rule. rhs else {
225
246
cx. dcx ( ) . span_bug ( sp, "malformed macro rhs" ) ;
226
247
} ;
@@ -241,27 +262,13 @@ fn expand_macro<'cx>(
241
262
trace_macros_note ( & mut cx. expansions , sp, msg) ;
242
263
}
243
264
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) ;
248
267
}
249
268
250
269
// Let the context choose how to interpret the result.
251
270
// 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)
265
272
}
266
273
Err ( CanRetry :: No ( guar) ) => {
267
274
debug ! ( "Will not retry matching as an error was emitted already" ) ;
@@ -382,7 +389,7 @@ pub fn compile_declarative_macro(
382
389
edition,
383
390
ident. name ,
384
391
attrs,
385
- node_id != DUMMY_NODE_ID ,
392
+ is_defined_in_current_crate ( node_id) ,
386
393
)
387
394
} ;
388
395
let dummy_syn_ext = |guar| ( mk_syn_ext ( Arc :: new ( DummyExpander ( guar) ) ) , 0 ) ;
@@ -454,7 +461,7 @@ pub fn compile_declarative_macro(
454
461
}
455
462
456
463
// 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 } ;
458
465
459
466
let expander =
460
467
Arc :: new ( MacroRulesMacroExpander { name : ident, span, node_id, transparency, rules } ) ;
@@ -1030,9 +1037,7 @@ fn check_matcher_core<'tt>(
1030
1037
// definition of this macro_rules, not while (re)parsing
1031
1038
// the macro when compiling another crate that is using the
1032
1039
// 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)
1036
1041
&& matches ! ( kind, NonterminalKind :: Pat ( PatParam { inferred: true } ) )
1037
1042
&& matches ! (
1038
1043
next_token,
@@ -1292,6 +1297,12 @@ fn quoted_tt_to_string(tt: &mbe::TokenTree) -> String {
1292
1297
}
1293
1298
}
1294
1299
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
+
1295
1306
pub ( super ) fn parser_from_cx (
1296
1307
psess : & ParseSess ,
1297
1308
mut tts : TokenStream ,
0 commit comments