1 | //! This test does the following for every file in the rust-lang/rust repo: |
2 | //! |
3 | //! 1. Parse the file using syn into a syn::File. |
4 | //! 2. Extract every syn::Expr from the file. |
5 | //! 3. Print each expr to a string of source code. |
6 | //! 4. Parse the source code using librustc_parse into a rustc_ast::Expr. |
7 | //! 5. For both the syn::Expr and rustc_ast::Expr, crawl the syntax tree to |
8 | //! insert parentheses surrounding every subexpression. |
9 | //! 6. Serialize the fully parenthesized syn::Expr to a string of source code. |
10 | //! 7. Parse the fully parenthesized source code using librustc_parse. |
11 | //! 8. Compare the rustc_ast::Expr resulting from parenthesizing using rustc |
12 | //! data structures vs syn data structures, ignoring spans. If they agree, |
13 | //! rustc's parser and syn's parser have identical handling of expression |
14 | //! precedence. |
15 | |
16 | #![cfg (not(syn_disable_nightly_tests))] |
17 | #![cfg (not(miri))] |
18 | #![recursion_limit = "1024" ] |
19 | #![feature (rustc_private)] |
20 | #![allow ( |
21 | clippy::doc_markdown, |
22 | clippy::explicit_deref_methods, |
23 | clippy::let_underscore_untyped, |
24 | clippy::manual_assert, |
25 | clippy::manual_let_else, |
26 | clippy::match_like_matches_macro, |
27 | clippy::match_wildcard_for_single_variants, |
28 | clippy::too_many_lines, |
29 | clippy::uninlined_format_args |
30 | )] |
31 | |
32 | extern crate rustc_ast; |
33 | extern crate rustc_ast_pretty; |
34 | extern crate rustc_data_structures; |
35 | extern crate rustc_driver; |
36 | extern crate rustc_span; |
37 | extern crate smallvec; |
38 | extern crate thin_vec; |
39 | |
40 | use crate::common::eq::SpanlessEq; |
41 | use crate::common::parse; |
42 | use quote::ToTokens; |
43 | use rustc_ast::ast; |
44 | use rustc_ast::ptr::P; |
45 | use rustc_ast_pretty::pprust; |
46 | use rustc_span::edition::Edition; |
47 | use std::fs; |
48 | use std::path::Path; |
49 | use std::process; |
50 | use std::sync::atomic::{AtomicUsize, Ordering}; |
51 | |
52 | #[macro_use ] |
53 | mod macros; |
54 | |
55 | #[allow (dead_code)] |
56 | mod common; |
57 | |
58 | mod repo; |
59 | |
60 | #[test] |
61 | fn test_rustc_precedence() { |
62 | common::rayon_init(); |
63 | repo::clone_rust(); |
64 | let abort_after = common::abort_after(); |
65 | if abort_after == 0 { |
66 | panic!("Skipping all precedence tests" ); |
67 | } |
68 | |
69 | let passed = AtomicUsize::new(0); |
70 | let failed = AtomicUsize::new(0); |
71 | |
72 | repo::for_each_rust_file(|path| { |
73 | let content = fs::read_to_string(path).unwrap(); |
74 | |
75 | let (l_passed, l_failed) = match syn::parse_file(&content) { |
76 | Ok(file) => { |
77 | let edition = repo::edition(path).parse().unwrap(); |
78 | let exprs = collect_exprs(file); |
79 | let (l_passed, l_failed) = test_expressions(path, edition, exprs); |
80 | errorf!( |
81 | "=== {}: {} passed | {} failed \n" , |
82 | path.display(), |
83 | l_passed, |
84 | l_failed, |
85 | ); |
86 | (l_passed, l_failed) |
87 | } |
88 | Err(msg) => { |
89 | errorf!(" \nFAIL {} - syn failed to parse: {} \n" , path.display(), msg); |
90 | (0, 1) |
91 | } |
92 | }; |
93 | |
94 | passed.fetch_add(l_passed, Ordering::Relaxed); |
95 | let prev_failed = failed.fetch_add(l_failed, Ordering::Relaxed); |
96 | |
97 | if prev_failed + l_failed >= abort_after { |
98 | process::exit(1); |
99 | } |
100 | }); |
101 | |
102 | let passed = passed.load(Ordering::Relaxed); |
103 | let failed = failed.load(Ordering::Relaxed); |
104 | |
105 | errorf!(" \n===== Precedence Test Results ===== \n" ); |
106 | errorf!("{} passed | {} failed \n" , passed, failed); |
107 | |
108 | if failed > 0 { |
109 | panic!("{} failures" , failed); |
110 | } |
111 | } |
112 | |
113 | fn test_expressions(path: &Path, edition: Edition, exprs: Vec<syn::Expr>) -> (usize, usize) { |
114 | let mut passed = 0; |
115 | let mut failed = 0; |
116 | |
117 | rustc_span::create_session_if_not_set_then(edition, |_| { |
118 | for expr in exprs { |
119 | let source_code = expr.to_token_stream().to_string(); |
120 | let librustc_ast = if let Some(e) = librustc_parse_and_rewrite(&source_code) { |
121 | e |
122 | } else { |
123 | failed += 1; |
124 | errorf!( |
125 | " \nFAIL {} - librustc failed to parse original \n" , |
126 | path.display(), |
127 | ); |
128 | continue; |
129 | }; |
130 | |
131 | let syn_parenthesized_code = |
132 | syn_parenthesize(expr.clone()).to_token_stream().to_string(); |
133 | let syn_ast = if let Some(e) = parse::librustc_expr(&syn_parenthesized_code) { |
134 | e |
135 | } else { |
136 | failed += 1; |
137 | errorf!( |
138 | " \nFAIL {} - librustc failed to parse parenthesized \n" , |
139 | path.display(), |
140 | ); |
141 | continue; |
142 | }; |
143 | |
144 | if !SpanlessEq::eq(&syn_ast, &librustc_ast) { |
145 | failed += 1; |
146 | let syn_pretty = pprust::expr_to_string(&syn_ast); |
147 | let librustc_pretty = pprust::expr_to_string(&librustc_ast); |
148 | errorf!( |
149 | " \nFAIL {} \n{} \nsyn != rustc \n{} \n" , |
150 | path.display(), |
151 | syn_pretty, |
152 | librustc_pretty, |
153 | ); |
154 | continue; |
155 | } |
156 | |
157 | let expr_invisible = make_parens_invisible(expr); |
158 | let Ok(reparsed_expr_invisible) = syn::parse2(expr_invisible.to_token_stream()) else { |
159 | failed += 1; |
160 | errorf!( |
161 | " \nFAIL {} - syn failed to parse invisible delimiters \n{} \n" , |
162 | path.display(), |
163 | source_code, |
164 | ); |
165 | continue; |
166 | }; |
167 | if expr_invisible != reparsed_expr_invisible { |
168 | failed += 1; |
169 | errorf!( |
170 | " \nFAIL {} - mismatch after parsing invisible delimiters \n{} \n" , |
171 | path.display(), |
172 | source_code, |
173 | ); |
174 | continue; |
175 | } |
176 | |
177 | passed += 1; |
178 | } |
179 | }); |
180 | |
181 | (passed, failed) |
182 | } |
183 | |
184 | fn librustc_parse_and_rewrite(input: &str) -> Option<P<ast::Expr>> { |
185 | parse::librustc_expr(input).map(librustc_parenthesize) |
186 | } |
187 | |
188 | fn librustc_parenthesize(mut librustc_expr: P<ast::Expr>) -> P<ast::Expr> { |
189 | use rustc_ast::ast::{ |
190 | AssocItem, AssocItemKind, Attribute, BinOpKind, Block, BorrowKind, Expr, ExprField, |
191 | ExprKind, GenericArg, GenericBound, ItemKind, Local, LocalKind, Pat, Stmt, StmtKind, |
192 | StructExpr, StructRest, TraitBoundModifier, Ty, |
193 | }; |
194 | use rustc_ast::mut_visit::{ |
195 | noop_flat_map_assoc_item, noop_visit_generic_arg, noop_visit_item_kind, noop_visit_local, |
196 | noop_visit_param_bound, MutVisitor, |
197 | }; |
198 | use rustc_data_structures::flat_map_in_place::FlatMapInPlace; |
199 | use rustc_span::DUMMY_SP; |
200 | use smallvec::SmallVec; |
201 | use std::mem; |
202 | use std::ops::DerefMut; |
203 | use thin_vec::ThinVec; |
204 | |
205 | struct FullyParenthesize; |
206 | |
207 | fn contains_let_chain(expr: &Expr) -> bool { |
208 | match &expr.kind { |
209 | ExprKind::Let(..) => true, |
210 | ExprKind::Binary(binop, left, right) => { |
211 | binop.node == BinOpKind::And |
212 | && (contains_let_chain(left) || contains_let_chain(right)) |
213 | } |
214 | _ => false, |
215 | } |
216 | } |
217 | |
218 | fn flat_map_field<T: MutVisitor>(mut f: ExprField, vis: &mut T) -> Vec<ExprField> { |
219 | if f.is_shorthand { |
220 | noop_visit_expr(&mut f.expr, vis); |
221 | } else { |
222 | vis.visit_expr(&mut f.expr); |
223 | } |
224 | vec![f] |
225 | } |
226 | |
227 | fn flat_map_stmt<T: MutVisitor>(stmt: Stmt, vis: &mut T) -> Vec<Stmt> { |
228 | let kind = match stmt.kind { |
229 | // Don't wrap toplevel expressions in statements. |
230 | StmtKind::Expr(mut e) => { |
231 | noop_visit_expr(&mut e, vis); |
232 | StmtKind::Expr(e) |
233 | } |
234 | StmtKind::Semi(mut e) => { |
235 | noop_visit_expr(&mut e, vis); |
236 | StmtKind::Semi(e) |
237 | } |
238 | s => s, |
239 | }; |
240 | |
241 | vec![Stmt { kind, ..stmt }] |
242 | } |
243 | |
244 | fn noop_visit_expr<T: MutVisitor>(e: &mut Expr, vis: &mut T) { |
245 | use rustc_ast::mut_visit::{noop_visit_expr, visit_attrs}; |
246 | match &mut e.kind { |
247 | ExprKind::AddrOf(BorrowKind::Raw, ..) => {} |
248 | ExprKind::Struct(expr) => { |
249 | let StructExpr { |
250 | qself, |
251 | path, |
252 | fields, |
253 | rest, |
254 | } = expr.deref_mut(); |
255 | vis.visit_qself(qself); |
256 | vis.visit_path(path); |
257 | fields.flat_map_in_place(|field| flat_map_field(field, vis)); |
258 | if let StructRest::Base(rest) = rest { |
259 | vis.visit_expr(rest); |
260 | } |
261 | vis.visit_id(&mut e.id); |
262 | vis.visit_span(&mut e.span); |
263 | visit_attrs(&mut e.attrs, vis); |
264 | } |
265 | _ => noop_visit_expr(e, vis), |
266 | } |
267 | } |
268 | |
269 | impl MutVisitor for FullyParenthesize { |
270 | fn visit_expr(&mut self, e: &mut P<Expr>) { |
271 | noop_visit_expr(e, self); |
272 | match e.kind { |
273 | ExprKind::Block(..) | ExprKind::If(..) | ExprKind::Let(..) => {} |
274 | ExprKind::Binary(..) if contains_let_chain(e) => {} |
275 | _ => { |
276 | let inner = mem::replace( |
277 | e, |
278 | P(Expr { |
279 | id: ast::DUMMY_NODE_ID, |
280 | kind: ExprKind::Err, |
281 | span: DUMMY_SP, |
282 | attrs: ThinVec::new(), |
283 | tokens: None, |
284 | }), |
285 | ); |
286 | e.kind = ExprKind::Paren(inner); |
287 | } |
288 | } |
289 | } |
290 | |
291 | fn visit_generic_arg(&mut self, arg: &mut GenericArg) { |
292 | match arg { |
293 | // Don't wrap unbraced const generic arg as that's invalid syntax. |
294 | GenericArg::Const(anon_const) => { |
295 | if let ExprKind::Block(..) = &mut anon_const.value.kind { |
296 | noop_visit_expr(&mut anon_const.value, self); |
297 | } |
298 | } |
299 | _ => noop_visit_generic_arg(arg, self), |
300 | } |
301 | } |
302 | |
303 | fn visit_param_bound(&mut self, bound: &mut GenericBound) { |
304 | match bound { |
305 | GenericBound::Trait( |
306 | _, |
307 | TraitBoundModifier::MaybeConst(_) | TraitBoundModifier::MaybeConstMaybe, |
308 | ) => {} |
309 | _ => noop_visit_param_bound(bound, self), |
310 | } |
311 | } |
312 | |
313 | fn visit_block(&mut self, block: &mut P<Block>) { |
314 | self.visit_id(&mut block.id); |
315 | block |
316 | .stmts |
317 | .flat_map_in_place(|stmt| flat_map_stmt(stmt, self)); |
318 | self.visit_span(&mut block.span); |
319 | } |
320 | |
321 | fn visit_local(&mut self, local: &mut P<Local>) { |
322 | match local.kind { |
323 | LocalKind::InitElse(..) => {} |
324 | _ => noop_visit_local(local, self), |
325 | } |
326 | } |
327 | |
328 | fn visit_item_kind(&mut self, item: &mut ItemKind) { |
329 | match item { |
330 | ItemKind::Const(const_item) |
331 | if !const_item.generics.params.is_empty() |
332 | || !const_item.generics.where_clause.predicates.is_empty() => {} |
333 | _ => noop_visit_item_kind(item, self), |
334 | } |
335 | } |
336 | |
337 | fn flat_map_trait_item(&mut self, item: P<AssocItem>) -> SmallVec<[P<AssocItem>; 1]> { |
338 | match &item.kind { |
339 | AssocItemKind::Const(const_item) |
340 | if !const_item.generics.params.is_empty() |
341 | || !const_item.generics.where_clause.predicates.is_empty() => |
342 | { |
343 | SmallVec::from([item]) |
344 | } |
345 | _ => noop_flat_map_assoc_item(item, self), |
346 | } |
347 | } |
348 | |
349 | fn flat_map_impl_item(&mut self, item: P<AssocItem>) -> SmallVec<[P<AssocItem>; 1]> { |
350 | match &item.kind { |
351 | AssocItemKind::Const(const_item) |
352 | if !const_item.generics.params.is_empty() |
353 | || !const_item.generics.where_clause.predicates.is_empty() => |
354 | { |
355 | SmallVec::from([item]) |
356 | } |
357 | _ => noop_flat_map_assoc_item(item, self), |
358 | } |
359 | } |
360 | |
361 | // We don't want to look at expressions that might appear in patterns or |
362 | // types yet. We'll look into comparing those in the future. For now |
363 | // focus on expressions appearing in other places. |
364 | fn visit_pat(&mut self, pat: &mut P<Pat>) { |
365 | let _ = pat; |
366 | } |
367 | |
368 | fn visit_ty(&mut self, ty: &mut P<Ty>) { |
369 | let _ = ty; |
370 | } |
371 | |
372 | fn visit_attribute(&mut self, attr: &mut Attribute) { |
373 | let _ = attr; |
374 | } |
375 | } |
376 | |
377 | let mut folder = FullyParenthesize; |
378 | folder.visit_expr(&mut librustc_expr); |
379 | librustc_expr |
380 | } |
381 | |
382 | fn syn_parenthesize(syn_expr: syn::Expr) -> syn::Expr { |
383 | use syn::fold::{fold_expr, fold_generic_argument, Fold}; |
384 | use syn::{token, BinOp, Expr, ExprParen, GenericArgument, MetaNameValue, Pat, Stmt, Type}; |
385 | |
386 | struct FullyParenthesize; |
387 | |
388 | fn parenthesize(expr: Expr) -> Expr { |
389 | Expr::Paren(ExprParen { |
390 | attrs: Vec::new(), |
391 | expr: Box::new(expr), |
392 | paren_token: token::Paren::default(), |
393 | }) |
394 | } |
395 | |
396 | fn needs_paren(expr: &Expr) -> bool { |
397 | match expr { |
398 | Expr::Group(_) => unreachable!(), |
399 | Expr::If(_) | Expr::Unsafe(_) | Expr::Block(_) | Expr::Let(_) => false, |
400 | Expr::Binary(_) => !contains_let_chain(expr), |
401 | _ => true, |
402 | } |
403 | } |
404 | |
405 | fn contains_let_chain(expr: &Expr) -> bool { |
406 | match expr { |
407 | Expr::Let(_) => true, |
408 | Expr::Binary(expr) => { |
409 | matches!(expr.op, BinOp::And(_)) |
410 | && (contains_let_chain(&expr.left) || contains_let_chain(&expr.right)) |
411 | } |
412 | _ => false, |
413 | } |
414 | } |
415 | |
416 | impl Fold for FullyParenthesize { |
417 | fn fold_expr(&mut self, expr: Expr) -> Expr { |
418 | let needs_paren = needs_paren(&expr); |
419 | let folded = fold_expr(self, expr); |
420 | if needs_paren { |
421 | parenthesize(folded) |
422 | } else { |
423 | folded |
424 | } |
425 | } |
426 | |
427 | fn fold_generic_argument(&mut self, arg: GenericArgument) -> GenericArgument { |
428 | match arg { |
429 | GenericArgument::Const(arg) => GenericArgument::Const(match arg { |
430 | Expr::Block(_) => fold_expr(self, arg), |
431 | // Don't wrap unbraced const generic arg as that's invalid syntax. |
432 | _ => arg, |
433 | }), |
434 | _ => fold_generic_argument(self, arg), |
435 | } |
436 | } |
437 | |
438 | fn fold_stmt(&mut self, stmt: Stmt) -> Stmt { |
439 | match stmt { |
440 | // Don't wrap toplevel expressions in statements. |
441 | Stmt::Expr(Expr::Verbatim(_), Some(_)) => stmt, |
442 | Stmt::Expr(e, semi) => Stmt::Expr(fold_expr(self, e), semi), |
443 | s => s, |
444 | } |
445 | } |
446 | |
447 | fn fold_meta_name_value(&mut self, meta: MetaNameValue) -> MetaNameValue { |
448 | // Don't turn #[p = "..."] into #[p = ("...")]. |
449 | meta |
450 | } |
451 | |
452 | // We don't want to look at expressions that might appear in patterns or |
453 | // types yet. We'll look into comparing those in the future. For now |
454 | // focus on expressions appearing in other places. |
455 | fn fold_pat(&mut self, pat: Pat) -> Pat { |
456 | pat |
457 | } |
458 | |
459 | fn fold_type(&mut self, ty: Type) -> Type { |
460 | ty |
461 | } |
462 | } |
463 | |
464 | let mut folder = FullyParenthesize; |
465 | folder.fold_expr(syn_expr) |
466 | } |
467 | |
468 | fn make_parens_invisible(expr: syn::Expr) -> syn::Expr { |
469 | use syn::fold::{fold_expr, fold_stmt, Fold}; |
470 | use syn::{token, Expr, ExprGroup, ExprParen, Stmt}; |
471 | |
472 | struct MakeParensInvisible; |
473 | |
474 | impl Fold for MakeParensInvisible { |
475 | fn fold_expr(&mut self, mut expr: Expr) -> Expr { |
476 | if let Expr::Paren(paren) = expr { |
477 | expr = Expr::Group(ExprGroup { |
478 | attrs: paren.attrs, |
479 | group_token: token::Group(paren.paren_token.span.join()), |
480 | expr: paren.expr, |
481 | }); |
482 | } |
483 | fold_expr(self, expr) |
484 | } |
485 | |
486 | fn fold_stmt(&mut self, stmt: Stmt) -> Stmt { |
487 | if let Stmt::Expr(expr @ (Expr::Binary(_) | Expr::Cast(_)), None) = stmt { |
488 | Stmt::Expr( |
489 | Expr::Paren(ExprParen { |
490 | attrs: Vec::new(), |
491 | paren_token: token::Paren::default(), |
492 | expr: Box::new(fold_expr(self, expr)), |
493 | }), |
494 | None, |
495 | ) |
496 | } else { |
497 | fold_stmt(self, stmt) |
498 | } |
499 | } |
500 | } |
501 | |
502 | let mut folder = MakeParensInvisible; |
503 | folder.fold_expr(expr) |
504 | } |
505 | |
506 | /// Walk through a crate collecting all expressions we can find in it. |
507 | fn collect_exprs(file: syn::File) -> Vec<syn::Expr> { |
508 | use syn::fold::Fold; |
509 | use syn::punctuated::Punctuated; |
510 | use syn::{token, ConstParam, Expr, ExprTuple, Pat, Path}; |
511 | |
512 | struct CollectExprs(Vec<Expr>); |
513 | impl Fold for CollectExprs { |
514 | fn fold_expr(&mut self, expr: Expr) -> Expr { |
515 | match expr { |
516 | Expr::Verbatim(_) => {} |
517 | _ => self.0.push(expr), |
518 | } |
519 | |
520 | Expr::Tuple(ExprTuple { |
521 | attrs: vec![], |
522 | elems: Punctuated::new(), |
523 | paren_token: token::Paren::default(), |
524 | }) |
525 | } |
526 | |
527 | fn fold_pat(&mut self, pat: Pat) -> Pat { |
528 | pat |
529 | } |
530 | |
531 | fn fold_path(&mut self, path: Path) -> Path { |
532 | // Skip traversing into const generic path arguments |
533 | path |
534 | } |
535 | |
536 | fn fold_const_param(&mut self, const_param: ConstParam) -> ConstParam { |
537 | const_param |
538 | } |
539 | } |
540 | |
541 | let mut folder = CollectExprs(vec![]); |
542 | folder.fold_file(file); |
543 | folder.0 |
544 | } |
545 | |