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