diff options
| author | Nicholas Nethercote <n.nethercote@gmail.com> | 2024-05-03 09:26:34 +1000 | 
|---|---|---|
| committer | Nicholas Nethercote <n.nethercote@gmail.com> | 2024-05-06 09:06:02 +1000 | 
| commit | 2acbe9c7438478b022fa52e54ba3ceb8ca570c20 (patch) | |
| tree | 7f15534ca6ba98686bfce608a293379046a14f14 /compiler/rustc_parse/src/parser/mut_visit/tests.rs | |
| parent | 9c9b568792ef20d8459c745345dd3e79b7c7fa8c (diff) | |
| download | rust-2acbe9c7438478b022fa52e54ba3ceb8ca570c20.tar.gz rust-2acbe9c7438478b022fa52e54ba3ceb8ca570c20.zip | |
Move some tests from `rustc_expand` to `rustc_parse`.
There are some test cases involving `parse` and `tokenstream` and `mut_visit` that are located in `rustc_expand`. Because it used to be the case that constructing a `ParseSess` required the involvement of `rustc_expand`. However, since #64197 merged (a long time ago) `rust_expand` no longer needs to be involved. This commit moves the tests into `rustc_parse`. This is the optimal place for the `parse` tests. It's not ideal for the `tokenstream` and `mut_visit` tests -- they would be better in `rustc_ast` -- but they still rely on parsing, which is not available in `rustc_ast`. But `rustc_parse` is lower down in the crate graph and closer to `rustc_ast` than `rust_expand`, so it's still an improvement for them. The exact renaming is as follows: - rustc_expand/src/mut_visit/tests.rs -> rustc_parse/src/parser/mut_visit/tests.rs - rustc_expand/src/tokenstream/tests.rs -> rustc_parse/src/parser/tokenstream/tests.rs - rustc_expand/src/tests.rs + rustc_expand/src/parse/tests.rs -> compiler/rustc_parse/src/parser/tests.rs The latter two test files are combined because there's no need for them to be separate, and having a `rustc_parse::parser::parse` module would be weird. This also means some `pub(crate)`s can be removed.
Diffstat (limited to 'compiler/rustc_parse/src/parser/mut_visit/tests.rs')
| -rw-r--r-- | compiler/rustc_parse/src/parser/mut_visit/tests.rs | 71 | 
1 files changed, 71 insertions, 0 deletions
| diff --git a/compiler/rustc_parse/src/parser/mut_visit/tests.rs b/compiler/rustc_parse/src/parser/mut_visit/tests.rs new file mode 100644 index 00000000000..b3cb28af657 --- /dev/null +++ b/compiler/rustc_parse/src/parser/mut_visit/tests.rs @@ -0,0 +1,71 @@ +use crate::parser::tests::{matches_codepattern, string_to_crate}; +use rustc_ast as ast; +use rustc_ast::mut_visit::MutVisitor; +use rustc_ast_pretty::pprust; +use rustc_span::create_default_session_globals_then; +use rustc_span::symbol::Ident; + +// This version doesn't care about getting comments or doc-strings in. +fn print_crate_items(krate: &ast::Crate) -> String { + krate.items.iter().map(|i| pprust::item_to_string(i)).collect::<Vec<_>>().join(" ") +} + +// Change every identifier to "zz". +struct ToZzIdentMutVisitor; + +impl MutVisitor for ToZzIdentMutVisitor { + const VISIT_TOKENS: bool = true; + + fn visit_ident(&mut self, ident: &mut Ident) { + *ident = Ident::from_str("zz"); + } +} + +// Maybe add to `expand.rs`. +macro_rules! assert_pred { + ($pred:expr, $predname:expr, $a:expr , $b:expr) => {{ + let pred_val = $pred; + let a_val = $a; + let b_val = $b; + if !(pred_val(&a_val, &b_val)) { + panic!("expected args satisfying {}, got {} and {}", $predname, a_val, b_val); + } + }}; +} + +// Make sure idents get transformed everywhere. +#[test] +fn ident_transformation() { + create_default_session_globals_then(|| { + let mut zz_visitor = ToZzIdentMutVisitor; + let mut krate = + string_to_crate("#[a] mod b {fn c (d : e, f : g) {h!(i,j,k);l;m}}".to_string()); + zz_visitor.visit_crate(&mut krate); + assert_pred!( + matches_codepattern, + "matches_codepattern", + print_crate_items(&krate), + "#[zz]mod zz{fn zz(zz:zz,zz:zz){zz!(zz,zz,zz);zz;zz}}".to_string() + ); + }) +} + +// Make sure idents get transformed even inside macro defs. +#[test] +fn ident_transformation_in_defs() { + create_default_session_globals_then(|| { + let mut zz_visitor = ToZzIdentMutVisitor; + let mut krate = string_to_crate( + "macro_rules! a {(b $c:expr $(d $e:token)f+ => \ + (g $(d $d $e)+))} " + .to_string(), + ); + zz_visitor.visit_crate(&mut krate); + assert_pred!( + matches_codepattern, + "matches_codepattern", + print_crate_items(&krate), + "macro_rules! zz{(zz$zz:zz$(zz $zz:zz)zz+=>(zz$(zz$zz$zz)+))}".to_string() + ); + }) +} | 
