@@ -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" ) ;
@@ -373,10 +380,18 @@ pub fn compile_declarative_macro(
373
380
node_id : NodeId ,
374
381
edition : Edition ,
375
382
) -> ( SyntaxExtension , usize ) {
376
- let is_local = node_id != DUMMY_NODE_ID ;
377
383
let mk_syn_ext = |expander| {
378
384
let kind = SyntaxExtensionKind :: LegacyBang ( expander) ;
379
- SyntaxExtension :: new ( sess, kind, span, Vec :: new ( ) , edition, ident. name , attrs, is_local)
385
+ SyntaxExtension :: new (
386
+ sess,
387
+ kind,
388
+ span,
389
+ Vec :: new ( ) ,
390
+ edition,
391
+ ident. name ,
392
+ attrs,
393
+ is_defined_in_current_crate ( node_id) ,
394
+ )
380
395
} ;
381
396
let dummy_syn_ext = |guar| ( mk_syn_ext ( Arc :: new ( DummyExpander ( guar) ) ) , 0 ) ;
382
397
@@ -439,7 +454,7 @@ pub fn compile_declarative_macro(
439
454
}
440
455
441
456
// Return the number of rules for unused rule linting, if this is a local macro.
442
- let nrules = if is_local { rules. len ( ) } else { 0 } ;
457
+ let nrules = if is_defined_in_current_crate ( node_id ) { rules. len ( ) } else { 0 } ;
443
458
444
459
let expander =
445
460
Arc :: new ( MacroRulesMacroExpander { name : ident, span, node_id, transparency, rules } ) ;
@@ -1034,9 +1049,7 @@ fn check_matcher_core<'tt>(
1034
1049
// definition of this macro_rules, not while (re)parsing
1035
1050
// the macro when compiling another crate that is using the
1036
1051
// macro. (See #86567.)
1037
- // Macros defined in the current crate have a real node id,
1038
- // whereas macros from an external crate have a dummy id.
1039
- if node_id != DUMMY_NODE_ID
1052
+ if is_defined_in_current_crate ( node_id)
1040
1053
&& matches ! ( kind, NonterminalKind :: Pat ( PatParam { inferred: true } ) )
1041
1054
&& matches ! (
1042
1055
next_token,
@@ -1296,6 +1309,12 @@ fn quoted_tt_to_string(tt: &mbe::TokenTree) -> String {
1296
1309
}
1297
1310
}
1298
1311
1312
+ fn is_defined_in_current_crate ( node_id : NodeId ) -> bool {
1313
+ // Macros defined in the current crate have a real node id,
1314
+ // whereas macros from an external crate have a dummy id.
1315
+ node_id != DUMMY_NODE_ID
1316
+ }
1317
+
1299
1318
pub ( super ) fn parser_from_cx (
1300
1319
psess : & ParseSess ,
1301
1320
mut tts : TokenStream ,
0 commit comments