David Tolnay | f83b6c9 | 2019-03-09 22:58:26 -0800 | [diff] [blame] | 1 | #![cfg(not(syn_disable_nightly_tests))] |
David Tolnay | ecd024d | 2018-07-21 09:07:56 -0700 | [diff] [blame] | 2 | #![recursion_limit = "1024"] |
Nika Layzell | a2a1a4a | 2017-11-19 11:33:17 -0500 | [diff] [blame] | 3 | #![feature(rustc_private)] |
| 4 | |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 5 | //! The tests in this module do the following: |
| 6 | //! |
Haibo Huang | 4fbebf9 | 2020-05-26 17:08:50 -0700 | [diff] [blame] | 7 | //! 1. Parse a given expression in both `syn` and `librustc`. |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 8 | //! 2. Fold over the expression adding brackets around each subexpression (with |
Haibo Huang | 4fbebf9 | 2020-05-26 17:08:50 -0700 | [diff] [blame] | 9 | //! some complications - see the `syn_brackets` and `librustc_brackets` |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 10 | //! methods). |
| 11 | //! 3. Serialize the `syn` expression back into a string, and re-parse it with |
Haibo Huang | 4fbebf9 | 2020-05-26 17:08:50 -0700 | [diff] [blame] | 12 | //! `librustc`. |
David Tolnay | 0ccb6d1 | 2018-08-14 22:43:00 -0700 | [diff] [blame] | 13 | //! 4. Respan all of the expressions, replacing the spans with the default |
| 14 | //! spans. |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 15 | //! 5. Compare the expressions with one another, if they are not equal fail. |
| 16 | |
Chih-Hung Hsieh | 725b6ec | 2020-04-16 17:09:06 -0700 | [diff] [blame] | 17 | extern crate rustc_ast; |
David Tolnay | c865992 | 2018-08-14 22:40:50 -0700 | [diff] [blame] | 18 | extern crate rustc_data_structures; |
Chih-Hung Hsieh | 218e269 | 2020-03-20 13:02:10 -0700 | [diff] [blame] | 19 | extern crate rustc_span; |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 20 | |
David Tolnay | e9b448f | 2019-03-10 22:46:38 -0700 | [diff] [blame] | 21 | use quote::quote; |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 22 | use rayon::iter::{IntoParallelIterator, ParallelIterator}; |
David Tolnay | d1c31cc | 2018-08-24 14:47:15 -0400 | [diff] [blame] | 23 | use regex::Regex; |
Chih-Hung Hsieh | 725b6ec | 2020-04-16 17:09:06 -0700 | [diff] [blame] | 24 | use rustc_ast::ast; |
| 25 | use rustc_ast::ptr::P; |
Chih-Hung Hsieh | 218e269 | 2020-03-20 13:02:10 -0700 | [diff] [blame] | 26 | use rustc_span::edition::Edition; |
Igor Gnatenko | 951a52b | 2018-03-12 10:33:33 +0100 | [diff] [blame] | 27 | use walkdir::{DirEntry, WalkDir}; |
David Tolnay | ee97dbf | 2017-11-19 14:24:38 -0800 | [diff] [blame] | 28 | |
| 29 | use std::fs::File; |
| 30 | use std::io::Read; |
David Tolnay | 3eaf7d8 | 2017-12-17 23:14:52 -0800 | [diff] [blame] | 31 | use std::process; |
David Tolnay | ee97dbf | 2017-11-19 14:24:38 -0800 | [diff] [blame] | 32 | use std::sync::atomic::{AtomicUsize, Ordering}; |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 33 | |
David Tolnay | ecd024d | 2018-07-21 09:07:56 -0700 | [diff] [blame] | 34 | use common::eq::SpanlessEq; |
| 35 | use common::parse; |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 36 | |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 37 | #[macro_use] |
David Tolnay | dd12556 | 2017-12-31 02:16:22 -0500 | [diff] [blame] | 38 | mod macros; |
| 39 | |
| 40 | #[allow(dead_code)] |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 41 | mod common; |
| 42 | |
David Tolnay | 4d156fd | 2019-06-30 11:54:14 -0700 | [diff] [blame] | 43 | mod repo; |
| 44 | |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 45 | /// Test some pre-set expressions chosen by us. |
| 46 | #[test] |
| 47 | fn test_simple_precedence() { |
| 48 | const EXPRS: &[&str] = &[ |
| 49 | "1 + 2 * 3 + 4", |
| 50 | "1 + 2 * ( 3 + 4 )", |
| 51 | "{ for i in r { } *some_ptr += 1; }", |
| 52 | "{ loop { break 5; } }", |
| 53 | "{ if true { () }.mthd() }", |
Nika Layzell | 3aa0dc7 | 2017-12-04 13:41:28 -0500 | [diff] [blame] | 54 | "{ for i in unsafe { 20 } { } }", |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 55 | ]; |
| 56 | |
| 57 | let mut failed = 0; |
| 58 | |
| 59 | for input in EXPRS { |
| 60 | let expr = if let Some(expr) = parse::syn_expr(input) { |
| 61 | expr |
| 62 | } else { |
| 63 | failed += 1; |
| 64 | continue; |
| 65 | }; |
| 66 | |
Haibo Huang | 4fbebf9 | 2020-05-26 17:08:50 -0700 | [diff] [blame] | 67 | let pf = match test_expressions(Edition::Edition2018, vec![expr]) { |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 68 | (1, 0) => "passed", |
| 69 | (0, 1) => { |
| 70 | failed += 1; |
| 71 | "failed" |
| 72 | } |
| 73 | _ => unreachable!(), |
| 74 | }; |
| 75 | errorf!("=== {}: {}\n", input, pf); |
| 76 | } |
| 77 | |
| 78 | if failed > 0 { |
| 79 | panic!("Failed {} tests", failed); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /// Test expressions from rustc, like in `test_round_trip`. |
| 84 | #[test] |
| 85 | fn test_rustc_precedence() { |
Haibo Huang | 548c6a3 | 2020-05-15 23:36:50 -0700 | [diff] [blame] | 86 | common::rayon_init(); |
David Tolnay | 4d156fd | 2019-06-30 11:54:14 -0700 | [diff] [blame] | 87 | repo::clone_rust(); |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 88 | let abort_after = common::abort_after(); |
| 89 | if abort_after == 0 { |
| 90 | panic!("Skipping all precedence tests"); |
| 91 | } |
| 92 | |
David Tolnay | ee97dbf | 2017-11-19 14:24:38 -0800 | [diff] [blame] | 93 | let passed = AtomicUsize::new(0); |
| 94 | let failed = AtomicUsize::new(0); |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 95 | |
David Tolnay | d1c31cc | 2018-08-24 14:47:15 -0400 | [diff] [blame] | 96 | // 2018 edition is hard |
| 97 | let edition_regex = Regex::new(r"\b(async|try)[!(]").unwrap(); |
| 98 | |
David Tolnay | ee97dbf | 2017-11-19 14:24:38 -0800 | [diff] [blame] | 99 | WalkDir::new("tests/rust") |
Igor Gnatenko | 951a52b | 2018-03-12 10:33:33 +0100 | [diff] [blame] | 100 | .sort_by(|a, b| a.file_name().cmp(b.file_name())) |
David Tolnay | ee97dbf | 2017-11-19 14:24:38 -0800 | [diff] [blame] | 101 | .into_iter() |
David Tolnay | 4d156fd | 2019-06-30 11:54:14 -0700 | [diff] [blame] | 102 | .filter_entry(repo::base_dir_filter) |
David Tolnay | ee97dbf | 2017-11-19 14:24:38 -0800 | [diff] [blame] | 103 | .collect::<Result<Vec<DirEntry>, walkdir::Error>>() |
| 104 | .unwrap() |
| 105 | .into_par_iter() |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 106 | .for_each(|entry| { |
| 107 | let path = entry.path(); |
| 108 | if path.is_dir() { |
| 109 | return; |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 110 | } |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 111 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 112 | let mut file = File::open(path).unwrap(); |
| 113 | let mut content = String::new(); |
| 114 | file.read_to_string(&mut content).unwrap(); |
David Tolnay | d1c31cc | 2018-08-24 14:47:15 -0400 | [diff] [blame] | 115 | let content = edition_regex.replace_all(&content, "_$0"); |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 116 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 117 | let (l_passed, l_failed) = match syn::parse_file(&content) { |
| 118 | Ok(file) => { |
Haibo Huang | 4fbebf9 | 2020-05-26 17:08:50 -0700 | [diff] [blame] | 119 | let edition = repo::edition(path).parse().unwrap(); |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 120 | let exprs = collect_exprs(file); |
Haibo Huang | 4fbebf9 | 2020-05-26 17:08:50 -0700 | [diff] [blame] | 121 | test_expressions(edition, exprs) |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 122 | } |
| 123 | Err(msg) => { |
| 124 | errorf!("syn failed to parse\n{:?}\n", msg); |
| 125 | (0, 1) |
| 126 | } |
| 127 | }; |
David Tolnay | ee97dbf | 2017-11-19 14:24:38 -0800 | [diff] [blame] | 128 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 129 | errorf!( |
| 130 | "=== {}: {} passed | {} failed\n", |
| 131 | path.display(), |
| 132 | l_passed, |
| 133 | l_failed |
| 134 | ); |
| 135 | |
| 136 | passed.fetch_add(l_passed, Ordering::SeqCst); |
| 137 | let prev_failed = failed.fetch_add(l_failed, Ordering::SeqCst); |
| 138 | |
| 139 | if prev_failed + l_failed >= abort_after { |
| 140 | process::exit(1); |
| 141 | } |
| 142 | }); |
David Tolnay | ee97dbf | 2017-11-19 14:24:38 -0800 | [diff] [blame] | 143 | |
| 144 | let passed = passed.load(Ordering::SeqCst); |
| 145 | let failed = failed.load(Ordering::SeqCst); |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 146 | |
| 147 | errorf!("\n===== Precedence Test Results =====\n"); |
| 148 | errorf!("{} passed | {} failed\n", passed, failed); |
| 149 | |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 150 | if failed > 0 { |
| 151 | panic!("{} failures", failed); |
| 152 | } |
| 153 | } |
| 154 | |
Haibo Huang | 4fbebf9 | 2020-05-26 17:08:50 -0700 | [diff] [blame] | 155 | fn test_expressions(edition: Edition, exprs: Vec<syn::Expr>) -> (usize, usize) { |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 156 | let mut passed = 0; |
| 157 | let mut failed = 0; |
| 158 | |
Haibo Huang | c7557ca | 2020-07-20 17:20:33 -0700 | [diff] [blame] | 159 | rustc_ast::with_session_globals(edition, || { |
David Tolnay | eb7d79b | 2018-03-31 22:52:17 +0200 | [diff] [blame] | 160 | for expr in exprs { |
| 161 | let raw = quote!(#expr).to_string(); |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 162 | |
Haibo Huang | 4fbebf9 | 2020-05-26 17:08:50 -0700 | [diff] [blame] | 163 | let librustc_ast = if let Some(e) = librustc_parse_and_rewrite(&raw) { |
David Tolnay | eb7d79b | 2018-03-31 22:52:17 +0200 | [diff] [blame] | 164 | e |
| 165 | } else { |
| 166 | failed += 1; |
Haibo Huang | 4fbebf9 | 2020-05-26 17:08:50 -0700 | [diff] [blame] | 167 | errorf!("\nFAIL - librustc failed to parse raw\n"); |
David Tolnay | eb7d79b | 2018-03-31 22:52:17 +0200 | [diff] [blame] | 168 | continue; |
| 169 | }; |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 170 | |
David Tolnay | eb7d79b | 2018-03-31 22:52:17 +0200 | [diff] [blame] | 171 | let syn_expr = syn_brackets(expr); |
Haibo Huang | 4fbebf9 | 2020-05-26 17:08:50 -0700 | [diff] [blame] | 172 | let syn_ast = if let Some(e) = parse::librustc_expr("e!(#syn_expr).to_string()) { |
David Tolnay | eb7d79b | 2018-03-31 22:52:17 +0200 | [diff] [blame] | 173 | e |
| 174 | } else { |
| 175 | failed += 1; |
Haibo Huang | 4fbebf9 | 2020-05-26 17:08:50 -0700 | [diff] [blame] | 176 | errorf!("\nFAIL - librustc failed to parse bracketed\n"); |
David Tolnay | eb7d79b | 2018-03-31 22:52:17 +0200 | [diff] [blame] | 177 | continue; |
| 178 | }; |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 179 | |
Haibo Huang | 4fbebf9 | 2020-05-26 17:08:50 -0700 | [diff] [blame] | 180 | if SpanlessEq::eq(&syn_ast, &librustc_ast) { |
David Tolnay | eb7d79b | 2018-03-31 22:52:17 +0200 | [diff] [blame] | 181 | passed += 1; |
| 182 | } else { |
| 183 | failed += 1; |
Haibo Huang | 4fbebf9 | 2020-05-26 17:08:50 -0700 | [diff] [blame] | 184 | errorf!("\nFAIL\n{:?}\n!=\n{:?}\n", syn_ast, librustc_ast); |
David Tolnay | eb7d79b | 2018-03-31 22:52:17 +0200 | [diff] [blame] | 185 | } |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 186 | } |
David Tolnay | 5d1a3ee | 2018-03-17 19:41:36 -0700 | [diff] [blame] | 187 | }); |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 188 | |
| 189 | (passed, failed) |
| 190 | } |
| 191 | |
Haibo Huang | 4fbebf9 | 2020-05-26 17:08:50 -0700 | [diff] [blame] | 192 | fn librustc_parse_and_rewrite(input: &str) -> Option<P<ast::Expr>> { |
| 193 | parse::librustc_expr(input).and_then(librustc_brackets) |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /// Wrap every expression which is not already wrapped in parens with parens, to |
David Tolnay | 0ccb6d1 | 2018-08-14 22:43:00 -0700 | [diff] [blame] | 197 | /// reveal the precidence of the parsed expressions, and produce a stringified |
| 198 | /// form of the resulting expression. |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 199 | /// |
Haibo Huang | 4fbebf9 | 2020-05-26 17:08:50 -0700 | [diff] [blame] | 200 | /// This method operates on librustc objects. |
| 201 | fn librustc_brackets(mut librustc_expr: P<ast::Expr>) -> Option<P<ast::Expr>> { |
Chih-Hung Hsieh | 725b6ec | 2020-04-16 17:09:06 -0700 | [diff] [blame] | 202 | use rustc_ast::ast::{ |
Haibo Huang | 4fbebf9 | 2020-05-26 17:08:50 -0700 | [diff] [blame] | 203 | Block, BorrowKind, Expr, ExprKind, Field, GenericArg, MacCall, Pat, Stmt, StmtKind, Ty, |
Chih-Hung Hsieh | 725b6ec | 2020-04-16 17:09:06 -0700 | [diff] [blame] | 204 | }; |
Haibo Huang | 4fbebf9 | 2020-05-26 17:08:50 -0700 | [diff] [blame] | 205 | use rustc_ast::mut_visit::{noop_visit_generic_arg, MutVisitor}; |
Haibo Huang | 3588bff | 2020-04-23 15:44:10 -0700 | [diff] [blame] | 206 | use rustc_data_structures::map_in_place::MapInPlace; |
David Tolnay | c865992 | 2018-08-14 22:40:50 -0700 | [diff] [blame] | 207 | use rustc_data_structures::thin_vec::ThinVec; |
Chih-Hung Hsieh | 218e269 | 2020-03-20 13:02:10 -0700 | [diff] [blame] | 208 | use rustc_span::DUMMY_SP; |
David Tolnay | 176838f | 2019-02-07 17:28:37 +0100 | [diff] [blame] | 209 | use std::mem; |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 210 | |
David Tolnay | 176838f | 2019-02-07 17:28:37 +0100 | [diff] [blame] | 211 | struct BracketsVisitor { |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 212 | failed: bool, |
| 213 | }; |
David Tolnay | e6b3709 | 2019-05-08 18:29:54 -0700 | [diff] [blame] | 214 | |
Chih-Hung Hsieh | 218e269 | 2020-03-20 13:02:10 -0700 | [diff] [blame] | 215 | fn flat_map_field<T: MutVisitor>(mut f: Field, vis: &mut T) -> Vec<Field> { |
| 216 | if f.is_shorthand { |
| 217 | noop_visit_expr(&mut f.expr, vis); |
| 218 | } else { |
| 219 | vis.visit_expr(&mut f.expr); |
| 220 | } |
| 221 | vec![f] |
| 222 | } |
| 223 | |
| 224 | fn flat_map_stmt<T: MutVisitor>(stmt: Stmt, vis: &mut T) -> Vec<Stmt> { |
| 225 | let kind = match stmt.kind { |
| 226 | // Don't wrap toplevel expressions in statements. |
| 227 | StmtKind::Expr(mut e) => { |
| 228 | noop_visit_expr(&mut e, vis); |
| 229 | StmtKind::Expr(e) |
| 230 | } |
| 231 | StmtKind::Semi(mut e) => { |
| 232 | noop_visit_expr(&mut e, vis); |
| 233 | StmtKind::Semi(e) |
| 234 | } |
| 235 | s => s, |
| 236 | }; |
| 237 | |
| 238 | vec![Stmt { kind, ..stmt }] |
| 239 | } |
| 240 | |
| 241 | fn noop_visit_expr<T: MutVisitor>(e: &mut Expr, vis: &mut T) { |
Chih-Hung Hsieh | 725b6ec | 2020-04-16 17:09:06 -0700 | [diff] [blame] | 242 | use rustc_ast::mut_visit::{noop_visit_expr, visit_opt, visit_thin_attrs}; |
Chih-Hung Hsieh | 218e269 | 2020-03-20 13:02:10 -0700 | [diff] [blame] | 243 | match &mut e.kind { |
| 244 | ExprKind::AddrOf(BorrowKind::Raw, ..) => {} |
| 245 | ExprKind::Struct(path, fields, expr) => { |
| 246 | vis.visit_path(path); |
| 247 | fields.flat_map_in_place(|field| flat_map_field(field, vis)); |
| 248 | visit_opt(expr, |expr| vis.visit_expr(expr)); |
| 249 | vis.visit_id(&mut e.id); |
| 250 | vis.visit_span(&mut e.span); |
| 251 | visit_thin_attrs(&mut e.attrs, vis); |
| 252 | } |
| 253 | _ => noop_visit_expr(e, vis), |
| 254 | } |
| 255 | } |
| 256 | |
David Tolnay | 176838f | 2019-02-07 17:28:37 +0100 | [diff] [blame] | 257 | impl MutVisitor for BracketsVisitor { |
| 258 | fn visit_expr(&mut self, e: &mut P<Expr>) { |
Chih-Hung Hsieh | 467ea21 | 2019-10-31 17:36:47 -0700 | [diff] [blame] | 259 | noop_visit_expr(e, self); |
| 260 | match e.kind { |
David Tolnay | c992d70 | 2019-06-23 18:48:23 -0700 | [diff] [blame] | 261 | ExprKind::If(..) | ExprKind::Block(..) | ExprKind::Let(..) => {} |
David Tolnay | 176838f | 2019-02-07 17:28:37 +0100 | [diff] [blame] | 262 | _ => { |
| 263 | let inner = mem::replace( |
| 264 | e, |
| 265 | P(Expr { |
| 266 | id: ast::DUMMY_NODE_ID, |
Chih-Hung Hsieh | 467ea21 | 2019-10-31 17:36:47 -0700 | [diff] [blame] | 267 | kind: ExprKind::Err, |
David Tolnay | 176838f | 2019-02-07 17:28:37 +0100 | [diff] [blame] | 268 | span: DUMMY_SP, |
| 269 | attrs: ThinVec::new(), |
Haibo Huang | 4fbebf9 | 2020-05-26 17:08:50 -0700 | [diff] [blame] | 270 | tokens: None, |
David Tolnay | 176838f | 2019-02-07 17:28:37 +0100 | [diff] [blame] | 271 | }), |
| 272 | ); |
Chih-Hung Hsieh | 467ea21 | 2019-10-31 17:36:47 -0700 | [diff] [blame] | 273 | e.kind = ExprKind::Paren(inner); |
David Tolnay | 5d314dc | 2018-07-21 16:40:01 -0700 | [diff] [blame] | 274 | } |
David Tolnay | 176838f | 2019-02-07 17:28:37 +0100 | [diff] [blame] | 275 | } |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 276 | } |
| 277 | |
Haibo Huang | 4fbebf9 | 2020-05-26 17:08:50 -0700 | [diff] [blame] | 278 | fn visit_generic_arg(&mut self, arg: &mut GenericArg) { |
| 279 | match arg { |
| 280 | // Don't wrap const generic arg as that's invalid syntax. |
| 281 | GenericArg::Const(arg) => noop_visit_expr(&mut arg.value, self), |
| 282 | _ => noop_visit_generic_arg(arg, self), |
| 283 | } |
| 284 | } |
| 285 | |
Chih-Hung Hsieh | 218e269 | 2020-03-20 13:02:10 -0700 | [diff] [blame] | 286 | fn visit_block(&mut self, block: &mut P<Block>) { |
| 287 | self.visit_id(&mut block.id); |
| 288 | block |
| 289 | .stmts |
| 290 | .flat_map_in_place(|stmt| flat_map_stmt(stmt, self)); |
| 291 | self.visit_span(&mut block.span); |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | // We don't want to look at expressions that might appear in patterns or |
| 295 | // types yet. We'll look into comparing those in the future. For now |
| 296 | // focus on expressions appearing in other places. |
David Tolnay | 176838f | 2019-02-07 17:28:37 +0100 | [diff] [blame] | 297 | fn visit_pat(&mut self, pat: &mut P<Pat>) { |
| 298 | let _ = pat; |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 299 | } |
| 300 | |
David Tolnay | 176838f | 2019-02-07 17:28:37 +0100 | [diff] [blame] | 301 | fn visit_ty(&mut self, ty: &mut P<Ty>) { |
| 302 | let _ = ty; |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 303 | } |
| 304 | |
Chih-Hung Hsieh | 725b6ec | 2020-04-16 17:09:06 -0700 | [diff] [blame] | 305 | fn visit_mac(&mut self, mac: &mut MacCall) { |
Haibo Huang | 4fbebf9 | 2020-05-26 17:08:50 -0700 | [diff] [blame] | 306 | // By default when folding over macros, librustc panics. This is |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 307 | // because it's usually not what you want, you want to run after |
| 308 | // macro expansion. We do want to do that (syn doesn't do macro |
David Tolnay | 176838f | 2019-02-07 17:28:37 +0100 | [diff] [blame] | 309 | // expansion), so we implement visit_mac to just return the macro |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 310 | // unchanged. |
David Tolnay | 176838f | 2019-02-07 17:28:37 +0100 | [diff] [blame] | 311 | let _ = mac; |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
David Tolnay | 176838f | 2019-02-07 17:28:37 +0100 | [diff] [blame] | 315 | let mut folder = BracketsVisitor { failed: false }; |
Haibo Huang | 4fbebf9 | 2020-05-26 17:08:50 -0700 | [diff] [blame] | 316 | folder.visit_expr(&mut librustc_expr); |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 317 | if folder.failed { |
| 318 | None |
| 319 | } else { |
Haibo Huang | 4fbebf9 | 2020-05-26 17:08:50 -0700 | [diff] [blame] | 320 | Some(librustc_expr) |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | |
| 324 | /// Wrap every expression which is not already wrapped in parens with parens, to |
David Tolnay | 0ccb6d1 | 2018-08-14 22:43:00 -0700 | [diff] [blame] | 325 | /// reveal the precedence of the parsed expressions, and produce a stringified |
| 326 | /// form of the resulting expression. |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 327 | fn syn_brackets(syn_expr: syn::Expr) -> syn::Expr { |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 328 | use syn::fold::*; |
David Tolnay | eb7d79b | 2018-03-31 22:52:17 +0200 | [diff] [blame] | 329 | use syn::*; |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 330 | |
David Tolnay | eb75206 | 2018-01-06 13:51:42 -0800 | [diff] [blame] | 331 | struct ParenthesizeEveryExpr; |
| 332 | impl Fold for ParenthesizeEveryExpr { |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 333 | fn fold_expr(&mut self, expr: Expr) -> Expr { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 334 | match expr { |
| 335 | Expr::Group(_) => unreachable!(), |
David Tolnay | 9c11912 | 2018-09-01 18:47:02 -0700 | [diff] [blame] | 336 | Expr::If(..) | Expr::Unsafe(..) | Expr::Block(..) | Expr::Let(..) => { |
David Tolnay | 3bc597f | 2017-12-31 02:31:11 -0500 | [diff] [blame] | 337 | fold_expr(self, expr) |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 338 | } |
Chih-Hung Hsieh | 467ea21 | 2019-10-31 17:36:47 -0700 | [diff] [blame] | 339 | _ => Expr::Paren(ExprParen { |
David Tolnay | 5d314dc | 2018-07-21 16:40:01 -0700 | [diff] [blame] | 340 | attrs: Vec::new(), |
Chih-Hung Hsieh | 467ea21 | 2019-10-31 17:36:47 -0700 | [diff] [blame] | 341 | expr: Box::new(fold_expr(self, expr)), |
David Tolnay | 5d314dc | 2018-07-21 16:40:01 -0700 | [diff] [blame] | 342 | paren_token: token::Paren::default(), |
| 343 | }), |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 344 | } |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 345 | } |
| 346 | |
Haibo Huang | 4fbebf9 | 2020-05-26 17:08:50 -0700 | [diff] [blame] | 347 | fn fold_generic_argument(&mut self, arg: GenericArgument) -> GenericArgument { |
| 348 | match arg { |
| 349 | // Don't wrap const generic arg as that's invalid syntax. |
| 350 | GenericArgument::Const(a) => GenericArgument::Const(fold_expr(self, a)), |
| 351 | _ => fold_generic_argument(self, arg), |
| 352 | } |
| 353 | } |
| 354 | |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 355 | fn fold_stmt(&mut self, stmt: Stmt) -> Stmt { |
| 356 | match stmt { |
| 357 | // Don't wrap toplevel expressions in statements. |
David Tolnay | 1f0b7b8 | 2018-01-06 16:07:14 -0800 | [diff] [blame] | 358 | Stmt::Expr(e) => Stmt::Expr(fold_expr(self, e)), |
| 359 | Stmt::Semi(e, semi) => Stmt::Semi(fold_expr(self, e), semi), |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 360 | s => s, |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | // We don't want to look at expressions that might appear in patterns or |
| 365 | // types yet. We'll look into comparing those in the future. For now |
| 366 | // focus on expressions appearing in other places. |
| 367 | fn fold_pat(&mut self, pat: Pat) -> Pat { |
| 368 | pat |
| 369 | } |
| 370 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 371 | fn fold_type(&mut self, ty: Type) -> Type { |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 372 | ty |
| 373 | } |
| 374 | } |
| 375 | |
David Tolnay | eb75206 | 2018-01-06 13:51:42 -0800 | [diff] [blame] | 376 | let mut folder = ParenthesizeEveryExpr; |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 377 | folder.fold_expr(syn_expr) |
| 378 | } |
| 379 | |
| 380 | /// Walk through a crate collecting all expressions we can find in it. |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 381 | fn collect_exprs(file: syn::File) -> Vec<syn::Expr> { |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 382 | use syn::fold::*; |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 383 | use syn::punctuated::Punctuated; |
David Tolnay | eb7d79b | 2018-03-31 22:52:17 +0200 | [diff] [blame] | 384 | use syn::*; |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 385 | |
David Tolnay | eb75206 | 2018-01-06 13:51:42 -0800 | [diff] [blame] | 386 | struct CollectExprs(Vec<Expr>); |
| 387 | impl Fold for CollectExprs { |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 388 | fn fold_expr(&mut self, expr: Expr) -> Expr { |
Chih-Hung Hsieh | 218e269 | 2020-03-20 13:02:10 -0700 | [diff] [blame] | 389 | match expr { |
| 390 | Expr::Verbatim(tokens) if tokens.is_empty() => {} |
| 391 | _ => self.0.push(expr), |
| 392 | } |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 393 | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 394 | Expr::Tuple(ExprTuple { |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 395 | attrs: vec![], |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 396 | elems: Punctuated::new(), |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 397 | paren_token: token::Paren::default(), |
| 398 | }) |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | |
David Tolnay | eb75206 | 2018-01-06 13:51:42 -0800 | [diff] [blame] | 402 | let mut folder = CollectExprs(vec![]); |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 403 | folder.fold_file(file); |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 404 | folder.0 |
| 405 | } |