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