]> git.proxmox.com Git - rustc.git/blob - src/librustc_expand/mut_visit/tests.rs
New upstream version 1.44.1+dfsg1
[rustc.git] / src / librustc_expand / mut_visit / tests.rs
1 use crate::tests::{matches_codepattern, string_to_crate};
2
3 use rustc_ast::ast::{self, Ident};
4 use rustc_ast::mut_visit::{self, MutVisitor};
5 use rustc_ast::with_default_globals;
6 use rustc_ast_pretty::pprust;
7
8 // This version doesn't care about getting comments or doc-strings in.
9 fn fake_print_crate(s: &mut pprust::State<'_>, krate: &ast::Crate) {
10 s.print_mod(&krate.module, &krate.attrs)
11 }
12
13 // Change every identifier to "zz".
14 struct ToZzIdentMutVisitor;
15
16 impl MutVisitor for ToZzIdentMutVisitor {
17 fn visit_ident(&mut self, ident: &mut ast::Ident) {
18 *ident = Ident::from_str("zz");
19 }
20 fn visit_mac(&mut self, mac: &mut ast::MacCall) {
21 mut_visit::noop_visit_mac(mac, self)
22 }
23 }
24
25 // Maybe add to `expand.rs`.
26 macro_rules! assert_pred {
27 ($pred:expr, $predname:expr, $a:expr , $b:expr) => {{
28 let pred_val = $pred;
29 let a_val = $a;
30 let b_val = $b;
31 if !(pred_val(&a_val, &b_val)) {
32 panic!("expected args satisfying {}, got {} and {}", $predname, a_val, b_val);
33 }
34 }};
35 }
36
37 // Make sure idents get transformed everywhere.
38 #[test]
39 fn ident_transformation() {
40 with_default_globals(|| {
41 let mut zz_visitor = ToZzIdentMutVisitor;
42 let mut krate =
43 string_to_crate("#[a] mod b {fn c (d : e, f : g) {h!(i,j,k);l;m}}".to_string());
44 zz_visitor.visit_crate(&mut krate);
45 assert_pred!(
46 matches_codepattern,
47 "matches_codepattern",
48 pprust::to_string(|s| fake_print_crate(s, &krate)),
49 "#[zz]mod zz{fn zz(zz:zz,zz:zz){zz!(zz,zz,zz);zz;zz}}".to_string()
50 );
51 })
52 }
53
54 // Make sure idents get transformed even inside macro defs.
55 #[test]
56 fn ident_transformation_in_defs() {
57 with_default_globals(|| {
58 let mut zz_visitor = ToZzIdentMutVisitor;
59 let mut krate = string_to_crate(
60 "macro_rules! a {(b $c:expr $(d $e:token)f+ => \
61 (g $(d $d $e)+))} "
62 .to_string(),
63 );
64 zz_visitor.visit_crate(&mut krate);
65 assert_pred!(
66 matches_codepattern,
67 "matches_codepattern",
68 pprust::to_string(|s| fake_print_crate(s, &krate)),
69 "macro_rules! zz{(zz$zz:zz$(zz $zz:zz)zz+=>(zz$(zz$zz$zz)+))}".to_string()
70 );
71 })
72 }