7
7
#![ feature( or_patterns) ]
8
8
9
9
use rustc_ast as ast;
10
- use rustc_ast:: token:: { self , DelimToken , Nonterminal , Token , TokenKind } ;
10
+ use rustc_ast:: token:: { self , Nonterminal , Token , TokenKind } ;
11
11
use rustc_ast:: tokenstream:: { self , Spacing , TokenStream , TokenTree } ;
12
12
use rustc_ast_pretty:: pprust;
13
13
use rustc_data_structures:: sync:: Lrc ;
@@ -299,7 +299,7 @@ pub fn nt_to_tokenstream(nt: &Nonterminal, sess: &ParseSess, span: Span) -> Toke
299
299
// FIXME(#43081): Avoid this pretty-print + reparse hack
300
300
let source = pprust:: nonterminal_to_string ( nt) ;
301
301
let filename = FileName :: macro_expansion_source_code ( & source) ;
302
- let tokens_for_real = parse_stream_from_source_str ( filename, source, sess, Some ( span) ) ;
302
+ let reparsed_tokens = parse_stream_from_source_str ( filename, source, sess, Some ( span) ) ;
303
303
304
304
// During early phases of the compiler the AST could get modified
305
305
// directly (e.g., attributes added or removed) and the internal cache
@@ -325,17 +325,17 @@ pub fn nt_to_tokenstream(nt: &Nonterminal, sess: &ParseSess, span: Span) -> Toke
325
325
// modifications, including adding/removing typically non-semantic
326
326
// tokens such as extra braces and commas, don't happen.
327
327
if let Some ( tokens) = tokens {
328
- if tokenstream_probably_equal_for_proc_macro ( & tokens, & tokens_for_real , sess) {
328
+ if tokenstream_probably_equal_for_proc_macro ( & tokens, & reparsed_tokens , sess) {
329
329
return tokens;
330
330
}
331
331
info ! (
332
332
"cached tokens found, but they're not \" probably equal\" , \
333
333
going with stringified version"
334
334
) ;
335
335
info ! ( "cached tokens: {:?}" , tokens) ;
336
- info ! ( "reparsed tokens: {:?}" , tokens_for_real ) ;
336
+ info ! ( "reparsed tokens: {:?}" , reparsed_tokens ) ;
337
337
}
338
- tokens_for_real
338
+ reparsed_tokens
339
339
}
340
340
341
341
// See comments in `Nonterminal::to_tokenstream` for why we care about
@@ -344,8 +344,8 @@ pub fn nt_to_tokenstream(nt: &Nonterminal, sess: &ParseSess, span: Span) -> Toke
344
344
// This is otherwise the same as `eq_unspanned`, only recursing with a
345
345
// different method.
346
346
pub fn tokenstream_probably_equal_for_proc_macro (
347
- first : & TokenStream ,
348
- other : & TokenStream ,
347
+ tokens : & TokenStream ,
348
+ reparsed_tokens : & TokenStream ,
349
349
sess : & ParseSess ,
350
350
) -> bool {
351
351
// When checking for `probably_eq`, we ignore certain tokens that aren't
@@ -359,9 +359,6 @@ pub fn tokenstream_probably_equal_for_proc_macro(
359
359
// The pretty printer tends to add trailing commas to
360
360
// everything, and in particular, after struct fields.
361
361
| token:: Comma
362
- // The pretty printer emits `NoDelim` as whitespace.
363
- | token:: OpenDelim ( DelimToken :: NoDelim )
364
- | token:: CloseDelim ( DelimToken :: NoDelim )
365
362
// The pretty printer collapses many semicolons into one.
366
363
| token:: Semi
367
364
// We don't preserve leading `|` tokens in patterns, so
@@ -460,10 +457,11 @@ pub fn tokenstream_probably_equal_for_proc_macro(
460
457
461
458
// Break tokens after we expand any nonterminals, so that we break tokens
462
459
// that are produced as a result of nonterminal expansion.
463
- let t1 = first. trees ( ) . filter ( semantic_tree) . flat_map ( expand_nt) . flat_map ( break_tokens) ;
464
- let t2 = other. trees ( ) . filter ( semantic_tree) . flat_map ( expand_nt) . flat_map ( break_tokens) ;
460
+ let tokens = tokens. trees ( ) . filter ( semantic_tree) . flat_map ( expand_nt) . flat_map ( break_tokens) ;
461
+ let reparsed_tokens =
462
+ reparsed_tokens. trees ( ) . filter ( semantic_tree) . flat_map ( expand_nt) . flat_map ( break_tokens) ;
465
463
466
- t1 . eq_by ( t2 , |t1 , t2 | tokentree_probably_equal_for_proc_macro ( & t1 , & t2 , sess) )
464
+ tokens . eq_by ( reparsed_tokens , |t , rt | tokentree_probably_equal_for_proc_macro ( & t , & rt , sess) )
467
465
}
468
466
469
467
// See comments in `Nonterminal::to_tokenstream` for why we care about
@@ -472,16 +470,20 @@ pub fn tokenstream_probably_equal_for_proc_macro(
472
470
// This is otherwise the same as `eq_unspanned`, only recursing with a
473
471
// different method.
474
472
pub fn tokentree_probably_equal_for_proc_macro (
475
- first : & TokenTree ,
476
- other : & TokenTree ,
473
+ token : & TokenTree ,
474
+ reparsed_token : & TokenTree ,
477
475
sess : & ParseSess ,
478
476
) -> bool {
479
- match ( first , other ) {
480
- ( TokenTree :: Token ( token) , TokenTree :: Token ( token2 ) ) => {
481
- token_probably_equal_for_proc_macro ( token, token2 )
477
+ match ( token , reparsed_token ) {
478
+ ( TokenTree :: Token ( token) , TokenTree :: Token ( reparsed_token ) ) => {
479
+ token_probably_equal_for_proc_macro ( token, reparsed_token )
482
480
}
483
- ( TokenTree :: Delimited ( _, delim, tts) , TokenTree :: Delimited ( _, delim2, tts2) ) => {
484
- delim == delim2 && tokenstream_probably_equal_for_proc_macro ( & tts, & tts2, sess)
481
+ (
482
+ TokenTree :: Delimited ( _, delim, tokens) ,
483
+ TokenTree :: Delimited ( _, reparsed_delim, reparsed_tokens) ,
484
+ ) => {
485
+ delim == reparsed_delim
486
+ && tokenstream_probably_equal_for_proc_macro ( tokens, reparsed_tokens, sess)
485
487
}
486
488
_ => false ,
487
489
}
0 commit comments