Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 1 | #![cfg(all(feature = "full", feature = "fold"))] |
| 2 | |
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 | //! |
David Tolnay | cfa5cc0 | 2017-11-13 01:05:11 -0800 | [diff] [blame] | 7 | //! 1. Parse a given expression in both `syn` and `libsyntax`. |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 8 | //! 2. Fold over the expression adding brackets around each subexpression (with |
David Tolnay | cfa5cc0 | 2017-11-13 01:05:11 -0800 | [diff] [blame] | 9 | //! some complications - see the `syn_brackets` and `libsyntax_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 |
David Tolnay | cfa5cc0 | 2017-11-13 01:05:11 -0800 | [diff] [blame] | 12 | //! `libsyntax`. |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 13 | //! 4. Respan all of the expressions, replacing the spans with the default spans. |
| 14 | //! 5. Compare the expressions with one another, if they are not equal fail. |
| 15 | |
| 16 | #[macro_use] |
| 17 | extern crate quote; |
David Tolnay | ee97dbf | 2017-11-19 14:24:38 -0800 | [diff] [blame^] | 18 | extern crate rayon; |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 19 | extern crate syn; |
| 20 | extern crate synom; |
David Tolnay | cfa5cc0 | 2017-11-13 01:05:11 -0800 | [diff] [blame] | 21 | extern crate syntax; |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 22 | extern crate walkdir; |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 23 | |
David Tolnay | ee97dbf | 2017-11-19 14:24:38 -0800 | [diff] [blame^] | 24 | use rayon::iter::{ParallelIterator, IntoParallelIterator}; |
David Tolnay | cfa5cc0 | 2017-11-13 01:05:11 -0800 | [diff] [blame] | 25 | use syntax::ast; |
| 26 | use syntax::ptr::P; |
David Tolnay | ee97dbf | 2017-11-19 14:24:38 -0800 | [diff] [blame^] | 27 | use walkdir::{WalkDir, WalkDirIterator, DirEntry}; |
| 28 | |
| 29 | use std::fs::File; |
| 30 | use std::io::Read; |
| 31 | use std::sync::atomic::{AtomicUsize, Ordering}; |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 32 | |
| 33 | use common::{respan, parse}; |
| 34 | |
| 35 | #[allow(dead_code)] |
| 36 | #[macro_use] |
| 37 | mod common; |
| 38 | |
| 39 | /// Test some pre-set expressions chosen by us. |
| 40 | #[test] |
| 41 | fn test_simple_precedence() { |
| 42 | const EXPRS: &[&str] = &[ |
| 43 | "1 + 2 * 3 + 4", |
| 44 | "1 + 2 * ( 3 + 4 )", |
| 45 | "{ for i in r { } *some_ptr += 1; }", |
| 46 | "{ loop { break 5; } }", |
| 47 | "{ if true { () }.mthd() }", |
| 48 | ]; |
| 49 | |
| 50 | let mut failed = 0; |
| 51 | |
| 52 | for input in EXPRS { |
| 53 | let expr = if let Some(expr) = parse::syn_expr(input) { |
| 54 | expr |
| 55 | } else { |
| 56 | failed += 1; |
| 57 | continue; |
| 58 | }; |
| 59 | |
| 60 | let pf = match test_expressions(vec![expr]) { |
| 61 | (1, 0) => "passed", |
| 62 | (0, 1) => { |
| 63 | failed += 1; |
| 64 | "failed" |
| 65 | } |
| 66 | _ => unreachable!(), |
| 67 | }; |
| 68 | errorf!("=== {}: {}\n", input, pf); |
| 69 | } |
| 70 | |
| 71 | if failed > 0 { |
| 72 | panic!("Failed {} tests", failed); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /// Test expressions from rustc, like in `test_round_trip`. |
| 77 | #[test] |
| 78 | fn test_rustc_precedence() { |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 79 | common::check_min_stack(); |
Alex Crichton | 8637477 | 2017-07-07 20:39:28 -0700 | [diff] [blame] | 80 | common::clone_rust(); |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 81 | let abort_after = common::abort_after(); |
| 82 | if abort_after == 0 { |
| 83 | panic!("Skipping all precedence tests"); |
| 84 | } |
| 85 | |
David Tolnay | ee97dbf | 2017-11-19 14:24:38 -0800 | [diff] [blame^] | 86 | let passed = AtomicUsize::new(0); |
| 87 | let failed = AtomicUsize::new(0); |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 88 | |
David Tolnay | ee97dbf | 2017-11-19 14:24:38 -0800 | [diff] [blame^] | 89 | WalkDir::new("tests/rust") |
| 90 | .sort_by(|a, b| a.cmp(b)) |
| 91 | .into_iter() |
| 92 | .filter_entry(common::base_dir_filter) |
| 93 | .collect::<Result<Vec<DirEntry>, walkdir::Error>>() |
| 94 | .unwrap() |
| 95 | .into_par_iter() |
| 96 | .for_each(|entry| |
| 97 | { |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 98 | let path = entry.path(); |
| 99 | if path.is_dir() { |
David Tolnay | ee97dbf | 2017-11-19 14:24:38 -0800 | [diff] [blame^] | 100 | return; |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 101 | } |
| 102 | |
David Tolnay | cfa5cc0 | 2017-11-13 01:05:11 -0800 | [diff] [blame] | 103 | // Our version of `libsyntax` can't parse this tests |
Alex Crichton | 10143d0 | 2017-08-28 10:02:00 -0700 | [diff] [blame] | 104 | if path.to_str().unwrap().ends_with("optional_comma_in_match_arm.rs") { |
David Tolnay | ee97dbf | 2017-11-19 14:24:38 -0800 | [diff] [blame^] | 105 | return; |
Alex Crichton | 10143d0 | 2017-08-28 10:02:00 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 108 | let mut file = File::open(path).unwrap(); |
| 109 | let mut content = String::new(); |
| 110 | file.read_to_string(&mut content).unwrap(); |
| 111 | |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 112 | let (l_passed, l_failed) = match syn::parse_file(&content) { |
| 113 | Ok(file) => { |
| 114 | let exprs = collect_exprs(file); |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 115 | test_expressions(exprs) |
| 116 | } |
| 117 | Err(msg) => { |
| 118 | errorf!("syn failed to parse\n{:?}\n", msg); |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 119 | (0, 1) |
| 120 | } |
| 121 | }; |
| 122 | |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 123 | errorf!("=== {}: {} passed | {} failed\n", path.display(), l_passed, l_failed); |
| 124 | |
David Tolnay | ee97dbf | 2017-11-19 14:24:38 -0800 | [diff] [blame^] | 125 | passed.fetch_add(l_passed, Ordering::SeqCst); |
| 126 | let prev_failed = failed.fetch_add(l_failed, Ordering::SeqCst); |
| 127 | |
| 128 | if prev_failed + l_failed >= abort_after { |
| 129 | panic!("Aborting Immediately due to ABORT_AFTER_FAILURE"); |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 130 | } |
David Tolnay | ee97dbf | 2017-11-19 14:24:38 -0800 | [diff] [blame^] | 131 | }); |
| 132 | |
| 133 | let passed = passed.load(Ordering::SeqCst); |
| 134 | let failed = failed.load(Ordering::SeqCst); |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 135 | |
| 136 | errorf!("\n===== Precedence Test Results =====\n"); |
| 137 | errorf!("{} passed | {} failed\n", passed, failed); |
| 138 | |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 139 | if failed > 0 { |
| 140 | panic!("{} failures", failed); |
| 141 | } |
| 142 | } |
| 143 | |
David Tolnay | ee97dbf | 2017-11-19 14:24:38 -0800 | [diff] [blame^] | 144 | fn test_expressions(exprs: Vec<syn::Expr>) -> (usize, usize) { |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 145 | let mut passed = 0; |
| 146 | let mut failed = 0; |
| 147 | |
| 148 | for expr in exprs { |
| 149 | let raw = quote!(#expr).to_string(); |
| 150 | |
David Tolnay | cfa5cc0 | 2017-11-13 01:05:11 -0800 | [diff] [blame] | 151 | let libsyntax_ast = if let Some(e) = libsyntax_parse_and_rewrite(&raw) { |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 152 | e |
| 153 | } else { |
| 154 | failed += 1; |
David Tolnay | cfa5cc0 | 2017-11-13 01:05:11 -0800 | [diff] [blame] | 155 | errorf!("\nFAIL - libsyntax failed to parse raw\n"); |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 156 | continue; |
| 157 | }; |
| 158 | |
| 159 | let syn_expr = syn_brackets(expr); |
David Tolnay | cfa5cc0 | 2017-11-13 01:05:11 -0800 | [diff] [blame] | 160 | let syn_ast = if let Some(e) = parse::libsyntax_expr("e!(#syn_expr).to_string()) { |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 161 | e |
| 162 | } else { |
| 163 | failed += 1; |
David Tolnay | cfa5cc0 | 2017-11-13 01:05:11 -0800 | [diff] [blame] | 164 | errorf!("\nFAIL - libsyntax failed to parse bracketed\n"); |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 165 | continue; |
| 166 | }; |
| 167 | |
| 168 | let syn_ast = respan::respan_expr(syn_ast); |
David Tolnay | cfa5cc0 | 2017-11-13 01:05:11 -0800 | [diff] [blame] | 169 | let libsyntax_ast = respan::respan_expr(libsyntax_ast); |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 170 | |
David Tolnay | cfa5cc0 | 2017-11-13 01:05:11 -0800 | [diff] [blame] | 171 | if syn_ast == libsyntax_ast { |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 172 | passed += 1; |
| 173 | } else { |
| 174 | failed += 1; |
David Tolnay | cfa5cc0 | 2017-11-13 01:05:11 -0800 | [diff] [blame] | 175 | errorf!("\nFAIL\n{:?}\n!=\n{:?}\n", syn_ast, libsyntax_ast); |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
| 179 | (passed, failed) |
| 180 | } |
| 181 | |
David Tolnay | cfa5cc0 | 2017-11-13 01:05:11 -0800 | [diff] [blame] | 182 | fn libsyntax_parse_and_rewrite(input: &str) -> Option<P<ast::Expr>> { |
| 183 | parse::libsyntax_expr(input).and_then(|e| libsyntax_brackets(e)) |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /// Wrap every expression which is not already wrapped in parens with parens, to |
| 187 | /// reveal the precidence of the parsed expressions, and produce a stringified form |
| 188 | /// of the resulting expression. |
| 189 | /// |
David Tolnay | cfa5cc0 | 2017-11-13 01:05:11 -0800 | [diff] [blame] | 190 | /// This method operates on libsyntax objects. |
| 191 | fn libsyntax_brackets(libsyntax_expr: P<ast::Expr>) -> Option<P<ast::Expr>> { |
| 192 | use syntax::ast::{Expr, ExprKind, Mac, Stmt, StmtKind, Pat, Ty, Field}; |
| 193 | use syntax::fold::{self, Folder}; |
| 194 | use syntax::util::ThinVec; |
| 195 | use syntax::util::small_vector::SmallVector; |
| 196 | use syntax::ext::quote::rt::DUMMY_SP; |
| 197 | use syntax::codemap; |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 198 | |
| 199 | fn expr(node: ExprKind) -> P<Expr> { |
| 200 | P(Expr { |
| 201 | id: ast::DUMMY_NODE_ID, |
| 202 | node: node, |
| 203 | span: DUMMY_SP, |
| 204 | attrs: ThinVec::new(), |
| 205 | }) |
| 206 | } |
| 207 | |
| 208 | struct BracketsFolder { |
| 209 | failed: bool, |
| 210 | }; |
| 211 | impl Folder for BracketsFolder { |
| 212 | fn fold_expr(&mut self, e: P<Expr>) -> P<Expr> { |
| 213 | e.map(|e| { |
| 214 | Expr { |
| 215 | node: match e.node { |
| 216 | ExprKind::Paren(inner) => { |
| 217 | ExprKind::Paren(inner.map(|e| { |
| 218 | fold::noop_fold_expr(e, self) |
| 219 | })) |
| 220 | } |
| 221 | ExprKind::If(..) | |
| 222 | ExprKind::Block(..) | |
| 223 | ExprKind::IfLet(..) => { |
| 224 | return fold::noop_fold_expr(e, self); |
| 225 | } |
| 226 | node => { |
| 227 | ExprKind::Paren(expr(node).map(|e| { |
| 228 | fold::noop_fold_expr(e, self) |
| 229 | })) |
| 230 | } |
| 231 | }, |
| 232 | ..e |
| 233 | } |
| 234 | }) |
| 235 | } |
| 236 | |
| 237 | fn fold_field(&mut self, f: Field) -> Field { |
| 238 | Field { |
| 239 | ident: codemap::respan(f.ident.span, self.fold_ident(f.ident.node)), |
| 240 | expr: if f.is_shorthand { |
| 241 | f.expr.map(|e| fold::noop_fold_expr(e, self)) |
| 242 | } else { |
| 243 | self.fold_expr(f.expr) |
| 244 | }, |
| 245 | span: self.new_span(f.span), |
| 246 | is_shorthand: f.is_shorthand, |
| 247 | attrs: fold::fold_thin_attrs(f.attrs, self), |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | // We don't want to look at expressions that might appear in patterns or |
| 252 | // types yet. We'll look into comparing those in the future. For now |
| 253 | // focus on expressions appearing in other places. |
| 254 | fn fold_pat(&mut self, pat: P<Pat>) -> P<Pat> { |
| 255 | pat |
| 256 | } |
| 257 | |
| 258 | fn fold_ty(&mut self, ty: P<Ty>) -> P<Ty> { |
| 259 | ty |
| 260 | } |
| 261 | |
| 262 | fn fold_stmt(&mut self, stmt: Stmt) -> SmallVector<Stmt> { |
| 263 | let node = match stmt.node { |
| 264 | // Don't wrap toplevel expressions in statements. |
| 265 | StmtKind::Expr(e) => { |
| 266 | StmtKind::Expr(e.map(|e| fold::noop_fold_expr(e, self))) |
| 267 | } |
| 268 | StmtKind::Semi(e) => { |
| 269 | StmtKind::Semi(e.map(|e| fold::noop_fold_expr(e, self))) |
| 270 | } |
| 271 | s => s, |
| 272 | }; |
| 273 | |
| 274 | SmallVector::one(Stmt { |
| 275 | node: node, |
| 276 | ..stmt |
| 277 | }) |
| 278 | } |
| 279 | |
| 280 | fn fold_mac(&mut self, mac: Mac) -> Mac { |
David Tolnay | cfa5cc0 | 2017-11-13 01:05:11 -0800 | [diff] [blame] | 281 | // By default when folding over macros, libsyntax panics. This is |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 282 | // because it's usually not what you want, you want to run after |
| 283 | // macro expansion. We do want to do that (syn doesn't do macro |
| 284 | // expansion), so we implement fold_mac to just return the macro |
| 285 | // unchanged. |
| 286 | mac |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | let mut folder = BracketsFolder { |
| 291 | failed: false, |
| 292 | }; |
David Tolnay | cfa5cc0 | 2017-11-13 01:05:11 -0800 | [diff] [blame] | 293 | let e = folder.fold_expr(libsyntax_expr); |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 294 | if folder.failed { |
| 295 | None |
| 296 | } else { |
| 297 | Some(e) |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | /// Wrap every expression which is not already wrapped in parens with parens, to |
| 302 | /// reveal the precidence of the parsed expressions, and produce a stringified form |
| 303 | /// of the resulting expression. |
| 304 | fn syn_brackets(syn_expr: syn::Expr) -> syn::Expr { |
| 305 | use syn::*; |
| 306 | use syn::fold::*; |
| 307 | |
| 308 | fn paren(folder: &mut BracketsFolder, node: ExprKind) -> ExprKind { |
| 309 | ExprKind::Paren(ExprParen { |
Nika Layzell | a6f46c4 | 2017-10-26 15:26:16 -0400 | [diff] [blame] | 310 | expr: Box::new(fold_expr(folder, Expr { |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 311 | node: node, |
| 312 | attrs: vec![], |
| 313 | })), |
| 314 | paren_token: tokens::Paren::default(), |
| 315 | }) |
| 316 | } |
| 317 | |
| 318 | struct BracketsFolder; |
| 319 | impl Folder for BracketsFolder { |
| 320 | fn fold_expr(&mut self, expr: Expr) -> Expr { |
| 321 | let kind = match expr.node { |
| 322 | ExprKind::Group(_) => unreachable!(), |
| 323 | ExprKind::Paren(p) => paren(self, p.expr.node), |
| 324 | ExprKind::If(..) | |
| 325 | ExprKind::Block(..) | |
| 326 | ExprKind::IfLet(..) => { |
Nika Layzell | a6f46c4 | 2017-10-26 15:26:16 -0400 | [diff] [blame] | 327 | return fold_expr(self, expr); |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 328 | } |
| 329 | node => paren(self, node), |
| 330 | }; |
| 331 | |
| 332 | Expr { |
| 333 | node: kind, |
| 334 | ..expr |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | fn fold_stmt(&mut self, stmt: Stmt) -> Stmt { |
| 339 | match stmt { |
| 340 | // Don't wrap toplevel expressions in statements. |
| 341 | Stmt::Expr(e) => { |
Nika Layzell | a6f46c4 | 2017-10-26 15:26:16 -0400 | [diff] [blame] | 342 | Stmt::Expr(Box::new(fold_expr(self, *e))) |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 343 | } |
| 344 | Stmt::Semi(e, semi) => { |
Nika Layzell | a6f46c4 | 2017-10-26 15:26:16 -0400 | [diff] [blame] | 345 | Stmt::Semi(Box::new(fold_expr(self, *e)), semi) |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 346 | } |
| 347 | s => s, |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | // We don't want to look at expressions that might appear in patterns or |
| 352 | // types yet. We'll look into comparing those in the future. For now |
| 353 | // focus on expressions appearing in other places. |
| 354 | fn fold_pat(&mut self, pat: Pat) -> Pat { |
| 355 | pat |
| 356 | } |
| 357 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 358 | fn fold_type(&mut self, ty: Type) -> Type { |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 359 | ty |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | let mut folder = BracketsFolder; |
| 364 | folder.fold_expr(syn_expr) |
| 365 | } |
| 366 | |
| 367 | /// Walk through a crate collecting all expressions we can find in it. |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 368 | fn collect_exprs(file: syn::File) -> Vec<syn::Expr> { |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 369 | use synom::delimited::Delimited; |
| 370 | use syn::*; |
| 371 | use syn::fold::*; |
| 372 | |
| 373 | struct CollectExprsFolder(Vec<Expr>); |
| 374 | impl Folder for CollectExprsFolder { |
| 375 | fn fold_expr(&mut self, expr: Expr) -> Expr { |
| 376 | self.0.push(expr); |
| 377 | |
| 378 | Expr { |
| 379 | node: ExprKind::Tup(ExprTup { |
| 380 | args: Delimited::new(), |
| 381 | paren_token: tokens::Paren::default(), |
| 382 | lone_comma: None |
| 383 | }), |
| 384 | attrs: vec![], |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | let mut folder = CollectExprsFolder(vec![]); |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 390 | folder.fold_file(file); |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 391 | folder.0 |
| 392 | } |