blob: bbc60234cf56fd3a918c20ae24638b294009da39 [file] [log] [blame]
Michael Layzell53fc31a2017-06-07 09:21:53 -04001#![cfg(all(feature = "full", feature = "fold"))]
2
Nika Layzella2a1a4a2017-11-19 11:33:17 -05003#![feature(rustc_private)]
4
Michael Layzell53fc31a2017-06-07 09:21:53 -04005//! The tests in this module do the following:
6//!
David Tolnaycfa5cc02017-11-13 01:05:11 -08007//! 1. Parse a given expression in both `syn` and `libsyntax`.
Michael Layzell53fc31a2017-06-07 09:21:53 -04008//! 2. Fold over the expression adding brackets around each subexpression (with
David Tolnaycfa5cc02017-11-13 01:05:11 -08009//! some complications - see the `syn_brackets` and `libsyntax_brackets`
Michael Layzell53fc31a2017-06-07 09:21:53 -040010//! methods).
11//! 3. Serialize the `syn` expression back into a string, and re-parse it with
David Tolnaycfa5cc02017-11-13 01:05:11 -080012//! `libsyntax`.
Michael Layzell53fc31a2017-06-07 09:21:53 -040013//! 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]
17extern crate quote;
David Tolnayee97dbf2017-11-19 14:24:38 -080018extern crate rayon;
Michael Layzell53fc31a2017-06-07 09:21:53 -040019extern crate syn;
20extern crate synom;
David Tolnaycfa5cc02017-11-13 01:05:11 -080021extern crate syntax;
Michael Layzell53fc31a2017-06-07 09:21:53 -040022extern crate walkdir;
Michael Layzell53fc31a2017-06-07 09:21:53 -040023
David Tolnayee97dbf2017-11-19 14:24:38 -080024use rayon::iter::{ParallelIterator, IntoParallelIterator};
David Tolnaycfa5cc02017-11-13 01:05:11 -080025use syntax::ast;
26use syntax::ptr::P;
David Tolnayee97dbf2017-11-19 14:24:38 -080027use walkdir::{WalkDir, WalkDirIterator, DirEntry};
28
29use std::fs::File;
30use std::io::Read;
31use std::sync::atomic::{AtomicUsize, Ordering};
Michael Layzell53fc31a2017-06-07 09:21:53 -040032
33use common::{respan, parse};
34
35#[allow(dead_code)]
36#[macro_use]
37mod common;
38
39/// Test some pre-set expressions chosen by us.
40#[test]
41fn 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]
78fn test_rustc_precedence() {
Michael Layzell53fc31a2017-06-07 09:21:53 -040079 common::check_min_stack();
Alex Crichton86374772017-07-07 20:39:28 -070080 common::clone_rust();
Michael Layzell53fc31a2017-06-07 09:21:53 -040081 let abort_after = common::abort_after();
82 if abort_after == 0 {
83 panic!("Skipping all precedence tests");
84 }
85
David Tolnayee97dbf2017-11-19 14:24:38 -080086 let passed = AtomicUsize::new(0);
87 let failed = AtomicUsize::new(0);
Michael Layzell53fc31a2017-06-07 09:21:53 -040088
David Tolnayee97dbf2017-11-19 14:24:38 -080089 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 Layzell53fc31a2017-06-07 09:21:53 -040098 let path = entry.path();
99 if path.is_dir() {
David Tolnayee97dbf2017-11-19 14:24:38 -0800100 return;
Michael Layzell53fc31a2017-06-07 09:21:53 -0400101 }
102
David Tolnaycfa5cc02017-11-13 01:05:11 -0800103 // Our version of `libsyntax` can't parse this tests
Alex Crichton10143d02017-08-28 10:02:00 -0700104 if path.to_str().unwrap().ends_with("optional_comma_in_match_arm.rs") {
David Tolnayee97dbf2017-11-19 14:24:38 -0800105 return;
Alex Crichton10143d02017-08-28 10:02:00 -0700106 }
107
Michael Layzell53fc31a2017-06-07 09:21:53 -0400108 let mut file = File::open(path).unwrap();
109 let mut content = String::new();
110 file.read_to_string(&mut content).unwrap();
111
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700112 let (l_passed, l_failed) = match syn::parse_file(&content) {
113 Ok(file) => {
114 let exprs = collect_exprs(file);
Michael Layzell53fc31a2017-06-07 09:21:53 -0400115 test_expressions(exprs)
116 }
117 Err(msg) => {
118 errorf!("syn failed to parse\n{:?}\n", msg);
Michael Layzell53fc31a2017-06-07 09:21:53 -0400119 (0, 1)
120 }
121 };
122
Michael Layzell53fc31a2017-06-07 09:21:53 -0400123 errorf!("=== {}: {} passed | {} failed\n", path.display(), l_passed, l_failed);
124
David Tolnayee97dbf2017-11-19 14:24:38 -0800125 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 Layzell53fc31a2017-06-07 09:21:53 -0400130 }
David Tolnayee97dbf2017-11-19 14:24:38 -0800131 });
132
133 let passed = passed.load(Ordering::SeqCst);
134 let failed = failed.load(Ordering::SeqCst);
Michael Layzell53fc31a2017-06-07 09:21:53 -0400135
136 errorf!("\n===== Precedence Test Results =====\n");
137 errorf!("{} passed | {} failed\n", passed, failed);
138
Michael Layzell53fc31a2017-06-07 09:21:53 -0400139 if failed > 0 {
140 panic!("{} failures", failed);
141 }
142}
143
David Tolnayee97dbf2017-11-19 14:24:38 -0800144fn test_expressions(exprs: Vec<syn::Expr>) -> (usize, usize) {
Michael Layzell53fc31a2017-06-07 09:21:53 -0400145 let mut passed = 0;
146 let mut failed = 0;
147
148 for expr in exprs {
149 let raw = quote!(#expr).to_string();
150
David Tolnaycfa5cc02017-11-13 01:05:11 -0800151 let libsyntax_ast = if let Some(e) = libsyntax_parse_and_rewrite(&raw) {
Michael Layzell53fc31a2017-06-07 09:21:53 -0400152 e
153 } else {
154 failed += 1;
David Tolnaycfa5cc02017-11-13 01:05:11 -0800155 errorf!("\nFAIL - libsyntax failed to parse raw\n");
Michael Layzell53fc31a2017-06-07 09:21:53 -0400156 continue;
157 };
158
159 let syn_expr = syn_brackets(expr);
David Tolnaycfa5cc02017-11-13 01:05:11 -0800160 let syn_ast = if let Some(e) = parse::libsyntax_expr(&quote!(#syn_expr).to_string()) {
Michael Layzell53fc31a2017-06-07 09:21:53 -0400161 e
162 } else {
163 failed += 1;
David Tolnaycfa5cc02017-11-13 01:05:11 -0800164 errorf!("\nFAIL - libsyntax failed to parse bracketed\n");
Michael Layzell53fc31a2017-06-07 09:21:53 -0400165 continue;
166 };
167
168 let syn_ast = respan::respan_expr(syn_ast);
David Tolnaycfa5cc02017-11-13 01:05:11 -0800169 let libsyntax_ast = respan::respan_expr(libsyntax_ast);
Michael Layzell53fc31a2017-06-07 09:21:53 -0400170
David Tolnaycfa5cc02017-11-13 01:05:11 -0800171 if syn_ast == libsyntax_ast {
Michael Layzell53fc31a2017-06-07 09:21:53 -0400172 passed += 1;
173 } else {
174 failed += 1;
David Tolnaycfa5cc02017-11-13 01:05:11 -0800175 errorf!("\nFAIL\n{:?}\n!=\n{:?}\n", syn_ast, libsyntax_ast);
Michael Layzell53fc31a2017-06-07 09:21:53 -0400176 }
177 }
178
179 (passed, failed)
180}
181
David Tolnaycfa5cc02017-11-13 01:05:11 -0800182fn libsyntax_parse_and_rewrite(input: &str) -> Option<P<ast::Expr>> {
183 parse::libsyntax_expr(input).and_then(|e| libsyntax_brackets(e))
Michael Layzell53fc31a2017-06-07 09:21:53 -0400184}
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 Tolnaycfa5cc02017-11-13 01:05:11 -0800190/// This method operates on libsyntax objects.
191fn 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 Layzell53fc31a2017-06-07 09:21:53 -0400198
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 Tolnaycfa5cc02017-11-13 01:05:11 -0800281 // By default when folding over macros, libsyntax panics. This is
Michael Layzell53fc31a2017-06-07 09:21:53 -0400282 // 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 Tolnaycfa5cc02017-11-13 01:05:11 -0800293 let e = folder.fold_expr(libsyntax_expr);
Michael Layzell53fc31a2017-06-07 09:21:53 -0400294 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.
304fn 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 Layzella6f46c42017-10-26 15:26:16 -0400310 expr: Box::new(fold_expr(folder, Expr {
Michael Layzell53fc31a2017-06-07 09:21:53 -0400311 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 Layzella6f46c42017-10-26 15:26:16 -0400327 return fold_expr(self, expr);
Michael Layzell53fc31a2017-06-07 09:21:53 -0400328 }
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 Layzella6f46c42017-10-26 15:26:16 -0400342 Stmt::Expr(Box::new(fold_expr(self, *e)))
Michael Layzell53fc31a2017-06-07 09:21:53 -0400343 }
344 Stmt::Semi(e, semi) => {
Nika Layzella6f46c42017-10-26 15:26:16 -0400345 Stmt::Semi(Box::new(fold_expr(self, *e)), semi)
Michael Layzell53fc31a2017-06-07 09:21:53 -0400346 }
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 Tolnayfd6bf5c2017-11-12 09:41:14 -0800358 fn fold_type(&mut self, ty: Type) -> Type {
Michael Layzell53fc31a2017-06-07 09:21:53 -0400359 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 Tolnayc7a5d3d2017-06-04 12:11:05 -0700368fn collect_exprs(file: syn::File) -> Vec<syn::Expr> {
Michael Layzell53fc31a2017-06-07 09:21:53 -0400369 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 Tolnayc7a5d3d2017-06-04 12:11:05 -0700390 folder.fold_file(file);
Michael Layzell53fc31a2017-06-07 09:21:53 -0400391 folder.0
392}