Skip to content

Commit b12225f

Browse files
Rollup merge of #77235 - petrochenkov:reparse, r=Aaron1011
pretty-print-reparse hack: Rename some variables for clarity This will also make it easier to make the comparisons asymmetric. Also one impossible case is removed. r? @Aaron1011
2 parents 96c5848 + fe3e5aa commit b12225f

File tree

1 file changed

+22
-20
lines changed
  • compiler/rustc_parse/src

1 file changed

+22
-20
lines changed

Diff for: compiler/rustc_parse/src/lib.rs

+22-20
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#![feature(or_patterns)]
88

99
use rustc_ast as ast;
10-
use rustc_ast::token::{self, DelimToken, Nonterminal, Token, TokenKind};
10+
use rustc_ast::token::{self, Nonterminal, Token, TokenKind};
1111
use rustc_ast::tokenstream::{self, Spacing, TokenStream, TokenTree};
1212
use rustc_ast_pretty::pprust;
1313
use rustc_data_structures::sync::Lrc;
@@ -299,7 +299,7 @@ pub fn nt_to_tokenstream(nt: &Nonterminal, sess: &ParseSess, span: Span) -> Toke
299299
// FIXME(#43081): Avoid this pretty-print + reparse hack
300300
let source = pprust::nonterminal_to_string(nt);
301301
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));
303303

304304
// During early phases of the compiler the AST could get modified
305305
// 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
325325
// modifications, including adding/removing typically non-semantic
326326
// tokens such as extra braces and commas, don't happen.
327327
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) {
329329
return tokens;
330330
}
331331
info!(
332332
"cached tokens found, but they're not \"probably equal\", \
333333
going with stringified version"
334334
);
335335
info!("cached tokens: {:?}", tokens);
336-
info!("reparsed tokens: {:?}", tokens_for_real);
336+
info!("reparsed tokens: {:?}", reparsed_tokens);
337337
}
338-
tokens_for_real
338+
reparsed_tokens
339339
}
340340

341341
// 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
344344
// This is otherwise the same as `eq_unspanned`, only recursing with a
345345
// different method.
346346
pub fn tokenstream_probably_equal_for_proc_macro(
347-
first: &TokenStream,
348-
other: &TokenStream,
347+
tokens: &TokenStream,
348+
reparsed_tokens: &TokenStream,
349349
sess: &ParseSess,
350350
) -> bool {
351351
// When checking for `probably_eq`, we ignore certain tokens that aren't
@@ -359,9 +359,6 @@ pub fn tokenstream_probably_equal_for_proc_macro(
359359
// The pretty printer tends to add trailing commas to
360360
// everything, and in particular, after struct fields.
361361
| token::Comma
362-
// The pretty printer emits `NoDelim` as whitespace.
363-
| token::OpenDelim(DelimToken::NoDelim)
364-
| token::CloseDelim(DelimToken::NoDelim)
365362
// The pretty printer collapses many semicolons into one.
366363
| token::Semi
367364
// We don't preserve leading `|` tokens in patterns, so
@@ -460,10 +457,11 @@ pub fn tokenstream_probably_equal_for_proc_macro(
460457

461458
// Break tokens after we expand any nonterminals, so that we break tokens
462459
// 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);
465463

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))
467465
}
468466

469467
// See comments in `Nonterminal::to_tokenstream` for why we care about
@@ -472,16 +470,20 @@ pub fn tokenstream_probably_equal_for_proc_macro(
472470
// This is otherwise the same as `eq_unspanned`, only recursing with a
473471
// different method.
474472
pub fn tokentree_probably_equal_for_proc_macro(
475-
first: &TokenTree,
476-
other: &TokenTree,
473+
token: &TokenTree,
474+
reparsed_token: &TokenTree,
477475
sess: &ParseSess,
478476
) -> 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)
482480
}
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)
485487
}
486488
_ => false,
487489
}

0 commit comments

Comments
 (0)