Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/vendor/syn/tests/test_expr.rs # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / vendor / syn / tests / test_expr.rs` 1718 lines ยท 53247 bytes Raw Blame History 1 #![cfg(not(miri))] 2 #![recursion_limit = "1024"] 3 #![feature(rustc_private)] 4 #![allow( 5 clippy::assert_is_empty, 6 clippy::elidable_lifetime_names, 7 clippy::match_like_matches_macro, 8 clippy::needless_lifetimes, 9 clippy::single_element_loop, 10 clippy::too_many_lines, 11 clippy::uninlined_format_args, 12 clippy::unreadable_literal 13 )] 14 15 #[macro_use] 16 mod macros; 17 #[macro_use] 18 mod snapshot; 19 20 mod common; 21 mod debug; 22 23 use crate::common::visit::{AsIfPrinted, FlattenParens}; 24 use proc_macro2::{Delimiter, Group, Ident, Span, TokenStream}; 25 use quote::{quote, ToTokens as _}; 26 use std::process::ExitCode; 27 use syn::punctuated::Punctuated; 28 use syn::visit_mut::VisitMut as _; 29 use syn::{ 30 parse_quote, token, AngleBracketedGenericArguments, Arm, BinOp, Block, BlockModifiers, 31 ClosureModifiers, Expr, ExprArray, ExprAssign, ExprAsync, ExprAwait, ExprBinary, ExprBlock, 32 ExprBreak, ExprCall, ExprCast, ExprClosure, ExprConst, ExprContinue, ExprField, ExprForLoop, 33 ExprIf, ExprIndex, ExprLet, ExprLit, ExprLoop, ExprMacro, ExprMatch, ExprMethodCall, ExprPath, 34 ExprRange, ExprRawAddr, ExprReference, ExprReturn, ExprStruct, ExprTry, ExprTryBlock, 35 ExprTuple, ExprUnary, ExprUnsafe, ExprWhile, ExprYield, GenericArgument, Label, Lifetime, Lit, 36 LitInt, Macro, MacroDelimiter, Member, Pat, PatGuard, PatWild, Path, PathArguments, 37 PathSegment, PointerMutability, QSelf, RangeLimits, ReturnType, Stmt, Token, Type, TypePath, 38 UnOp, 39 }; 40 41 #[test] 42 fn test_expr_parse() { 43 let tokens = quote!(..100u32); 44 snapshot!(tokens as Expr, @r#" 45 Expr::Range { 46 limits: RangeLimits::HalfOpen, 47 end: Some(Expr::Lit { 48 lit: 100u32, 49 }), 50 } 51 "#); 52 53 let tokens = quote!(..100u32); 54 snapshot!(tokens as ExprRange, @r#" 55 ExprRange { 56 limits: RangeLimits::HalfOpen, 57 end: Some(Expr::Lit { 58 lit: 100u32, 59 }), 60 } 61 "#); 62 } 63 64 #[test] 65 fn test_await() { 66 // Must not parse as Expr::Field. 67 let tokens = quote!(fut.await); 68 69 snapshot!(tokens as Expr, @r#" 70 Expr::Await { 71 base: Expr::Path { 72 path: Path { 73 segments: [ 74 PathSegment { 75 ident: "fut", 76 }, 77 ], 78 }, 79 }, 80 } 81 "#); 82 } 83 84 #[rustfmt::skip] 85 #[test] 86 fn test_tuple_multi_index() { 87 let expected = snapshot!("tuple.0.0" as Expr, @r#" 88 Expr::Field { 89 base: Expr::Field { 90 base: Expr::Path { 91 path: Path { 92 segments: [ 93 PathSegment { 94 ident: "tuple", 95 }, 96 ], 97 }, 98 }, 99 member: Member::Unnamed(Index { 100 index: 0, 101 }), 102 }, 103 member: Member::Unnamed(Index { 104 index: 0, 105 }), 106 } 107 "#); 108 109 for &input in &[ 110 "tuple .0.0", 111 "tuple. 0.0", 112 "tuple.0 .0", 113 "tuple.0. 0", 114 "tuple . 0 . 0", 115 ] { 116 assert_eq!(expected, syn::parse_str(input).unwrap()); 117 } 118 119 for tokens in [ 120 quote!(tuple.0.0), 121 quote!(tuple .0.0), 122 quote!(tuple. 0.0), 123 quote!(tuple.0 .0), 124 quote!(tuple.0. 0), 125 quote!(tuple . 0 . 0), 126 ] { 127 assert_eq!(expected, syn::parse2(tokens).unwrap()); 128 } 129 } 130 131 #[test] 132 fn test_macro_variable_func() { 133 // mimics the token stream corresponding to `$fn()` 134 let path = Group::new(Delimiter::None, quote!(f)); 135 let tokens = quote!(#path()); 136 137 snapshot!(tokens as Expr, @r#" 138 Expr::Call { 139 func: Expr::Group { 140 expr: Expr::Path { 141 path: Path { 142 segments: [ 143 PathSegment { 144 ident: "f", 145 }, 146 ], 147 }, 148 }, 149 }, 150 } 151 "#); 152 153 let path = Group::new(Delimiter::None, quote! { #[inside] f }); 154 let tokens = quote!(#[outside] #path()); 155 156 snapshot!(tokens as Expr, @r#" 157 Expr::Call { 158 attrs: [ 159 Attribute { 160 style: AttrStyle::Outer, 161 meta: Meta::Path { 162 segments: [ 163 PathSegment { 164 ident: "outside", 165 }, 166 ], 167 }, 168 }, 169 ], 170 func: Expr::Group { 171 expr: Expr::Path { 172 attrs: [ 173 Attribute { 174 style: AttrStyle::Outer, 175 meta: Meta::Path { 176 segments: [ 177 PathSegment { 178 ident: "inside", 179 }, 180 ], 181 }, 182 }, 183 ], 184 path: Path { 185 segments: [ 186 PathSegment { 187 ident: "f", 188 }, 189 ], 190 }, 191 }, 192 }, 193 } 194 "#); 195 } 196 197 #[test] 198 fn test_macro_variable_macro() { 199 // mimics the token stream corresponding to `$macro!()` 200 let mac = Group::new(Delimiter::None, quote!(m)); 201 let tokens = quote!(#mac!()); 202 203 snapshot!(tokens as Expr, @r#" 204 Expr::Macro { 205 mac: Macro { 206 path: Path { 207 segments: [ 208 PathSegment { 209 ident: "m", 210 }, 211 ], 212 }, 213 delimiter: MacroDelimiter::Paren, 214 tokens: TokenStream(``), 215 }, 216 } 217 "#); 218 } 219 220 #[test] 221 fn test_macro_variable_struct() { 222 // mimics the token stream corresponding to `$struct {}` 223 let s = Group::new(Delimiter::None, quote! { S }); 224 let tokens = quote!(#s {}); 225 226 snapshot!(tokens as Expr, @r#" 227 Expr::Struct { 228 path: Path { 229 segments: [ 230 PathSegment { 231 ident: "S", 232 }, 233 ], 234 }, 235 } 236 "#); 237 } 238 239 #[test] 240 fn test_macro_variable_unary() { 241 // mimics the token stream corresponding to `$expr.method()` where expr is `&self` 242 let inner = Group::new(Delimiter::None, quote!(&self)); 243 let tokens = quote!(#inner.method()); 244 snapshot!(tokens as Expr, @r#" 245 Expr::MethodCall { 246 receiver: Expr::Group { 247 expr: Expr::Reference { 248 expr: Expr::Path { 249 path: Path { 250 segments: [ 251 PathSegment { 252 ident: "self", 253 }, 254 ], 255 }, 256 }, 257 }, 258 }, 259 method: "method", 260 } 261 "#); 262 } 263 264 #[test] 265 fn test_macro_variable_match_arm() { 266 // mimics the token stream corresponding to `match v { _ => $expr }` 267 let expr = Group::new(Delimiter::None, quote! { #[a] () }); 268 let tokens = quote!(match v { _ => #expr }); 269 snapshot!(tokens as Expr, @r#" 270 Expr::Match { 271 expr: Expr::Path { 272 path: Path { 273 segments: [ 274 PathSegment { 275 ident: "v", 276 }, 277 ], 278 }, 279 }, 280 arms: [ 281 Arm { 282 pat: Pat::Wild, 283 body: Expr::Group { 284 expr: Expr::Tuple { 285 attrs: [ 286 Attribute { 287 style: AttrStyle::Outer, 288 meta: Meta::Path { 289 segments: [ 290 PathSegment { 291 ident: "a", 292 }, 293 ], 294 }, 295 }, 296 ], 297 }, 298 }, 299 }, 300 ], 301 } 302 "#); 303 304 let expr = Group::new(Delimiter::None, quote!(loop {} + 1)); 305 let tokens = quote!(match v { _ => #expr }); 306 snapshot!(tokens as Expr, @r#" 307 Expr::Match { 308 expr: Expr::Path { 309 path: Path { 310 segments: [ 311 PathSegment { 312 ident: "v", 313 }, 314 ], 315 }, 316 }, 317 arms: [ 318 Arm { 319 pat: Pat::Wild, 320 body: Expr::Group { 321 expr: Expr::Binary { 322 left: Expr::Loop { 323 body: Block { 324 stmts: [], 325 }, 326 }, 327 op: BinOp::Add, 328 right: Expr::Lit { 329 lit: 1, 330 }, 331 }, 332 }, 333 }, 334 ], 335 } 336 "#); 337 } 338 339 // https://github.com/dtolnay/syn/issues/1019 340 #[test] 341 fn test_closure_vs_rangefull() { 342 #[rustfmt::skip] // rustfmt bug: https://github.com/rust-lang/rustfmt/issues/4808 343 let tokens = quote!(|| .. .method()); 344 snapshot!(tokens as Expr, @r#" 345 Expr::MethodCall { 346 receiver: Expr::Closure { 347 modifiers: ClosureModifiers, 348 output: ReturnType::Default, 349 body: Expr::Range { 350 limits: RangeLimits::HalfOpen, 351 }, 352 }, 353 method: "method", 354 } 355 "#); 356 } 357 358 #[test] 359 fn test_postfix_operator_after_cast() { 360 syn::parse_str::("|| &x as T[0]").unwrap_err(); 361 syn::parse_str::("|| () as ()()").unwrap_err(); 362 } 363 364 #[test] 365 fn test_range_kinds() { 366 syn::parse_str::("..").unwrap(); 367 syn::parse_str::("..hi").unwrap(); 368 syn::parse_str::("lo..").unwrap(); 369 syn::parse_str::("lo..hi").unwrap(); 370 371 syn::parse_str::("..=").unwrap_err(); 372 syn::parse_str::("..=hi").unwrap(); 373 syn::parse_str::("lo..=").unwrap_err(); 374 syn::parse_str::("lo..=hi").unwrap(); 375 376 syn::parse_str::("...").unwrap_err(); 377 syn::parse_str::("...hi").unwrap_err(); 378 syn::parse_str::("lo...").unwrap_err(); 379 syn::parse_str::("lo...hi").unwrap_err(); 380 } 381 382 #[test] 383 fn test_range_precedence() { 384 snapshot!(".. .." as Expr, @r#" 385 Expr::Range { 386 limits: RangeLimits::HalfOpen, 387 end: Some(Expr::Range { 388 limits: RangeLimits::HalfOpen, 389 }), 390 } 391 "#); 392 393 snapshot!(".. .. ()" as Expr, @r#" 394 Expr::Range { 395 limits: RangeLimits::HalfOpen, 396 end: Some(Expr::Range { 397 limits: RangeLimits::HalfOpen, 398 end: Some(Expr::Tuple), 399 }), 400 } 401 "#); 402 403 snapshot!("() .. .." as Expr, @r#" 404 Expr::Range { 405 start: Some(Expr::Tuple), 406 limits: RangeLimits::HalfOpen, 407 end: Some(Expr::Range { 408 limits: RangeLimits::HalfOpen, 409 }), 410 } 411 "#); 412 413 snapshot!("() = .. + ()" as Expr, @r" 414 Expr::Binary { 415 left: Expr::Assign { 416 left: Expr::Tuple, 417 right: Expr::Range { 418 limits: RangeLimits::HalfOpen, 419 }, 420 }, 421 op: BinOp::Add, 422 right: Expr::Tuple, 423 } 424 "); 425 426 // A range with a lower bound cannot be the upper bound of another range, 427 // and a range with an upper bound cannot be the lower bound of another 428 // range. 429 syn::parse_str::(".. x ..").unwrap_err(); 430 syn::parse_str::("x .. x ..").unwrap_err(); 431 } 432 433 #[test] 434 fn test_range_attrs() { 435 // Attributes are not allowed on range expressions starting with `..` 436 syn::parse_str::("#[allow()] ..").unwrap_err(); 437 syn::parse_str::("#[allow()] .. hi").unwrap_err(); 438 439 snapshot!("#[allow()] lo .. hi" as Expr, @r#" 440 Expr::Range { 441 start: Some(Expr::Path { 442 attrs: [ 443 Attribute { 444 style: AttrStyle::Outer, 445 meta: Meta::List { 446 path: Path { 447 segments: [ 448 PathSegment { 449 ident: "allow", 450 }, 451 ], 452 }, 453 delimiter: MacroDelimiter::Paren, 454 tokens: TokenStream(``), 455 }, 456 }, 457 ], 458 path: Path { 459 segments: [ 460 PathSegment { 461 ident: "lo", 462 }, 463 ], 464 }, 465 }), 466 limits: RangeLimits::HalfOpen, 467 end: Some(Expr::Path { 468 path: Path { 469 segments: [ 470 PathSegment { 471 ident: "hi", 472 }, 473 ], 474 }, 475 }), 476 } 477 "#); 478 } 479 480 #[test] 481 fn test_ranges_bailout() { 482 syn::parse_str::(".. ?").unwrap_err(); 483 syn::parse_str::(".. .field").unwrap_err(); 484 485 snapshot!("return .. ?" as Expr, @r" 486 Expr::Try { 487 expr: Expr::Return { 488 expr: Some(Expr::Range { 489 limits: RangeLimits::HalfOpen, 490 }), 491 }, 492 } 493 "); 494 495 snapshot!("break .. ?" as Expr, @r" 496 Expr::Try { 497 expr: Expr::Break { 498 expr: Some(Expr::Range { 499 limits: RangeLimits::HalfOpen, 500 }), 501 }, 502 } 503 "); 504 505 snapshot!("|| .. ?" as Expr, @" 506 Expr::Try { 507 expr: Expr::Closure { 508 modifiers: ClosureModifiers, 509 output: ReturnType::Default, 510 body: Expr::Range { 511 limits: RangeLimits::HalfOpen, 512 }, 513 }, 514 } 515 "); 516 517 snapshot!("return .. .field" as Expr, @r#" 518 Expr::Field { 519 base: Expr::Return { 520 expr: Some(Expr::Range { 521 limits: RangeLimits::HalfOpen, 522 }), 523 }, 524 member: Member::Named("field"), 525 } 526 "#); 527 528 snapshot!("break .. .field" as Expr, @r#" 529 Expr::Field { 530 base: Expr::Break { 531 expr: Some(Expr::Range { 532 limits: RangeLimits::HalfOpen, 533 }), 534 }, 535 member: Member::Named("field"), 536 } 537 "#); 538 539 snapshot!("|| .. .field" as Expr, @r#" 540 Expr::Field { 541 base: Expr::Closure { 542 modifiers: ClosureModifiers, 543 output: ReturnType::Default, 544 body: Expr::Range { 545 limits: RangeLimits::HalfOpen, 546 }, 547 }, 548 member: Member::Named("field"), 549 } 550 "#); 551 552 snapshot!("return .. = ()" as Expr, @r" 553 Expr::Assign { 554 left: Expr::Return { 555 expr: Some(Expr::Range { 556 limits: RangeLimits::HalfOpen, 557 }), 558 }, 559 right: Expr::Tuple, 560 } 561 "); 562 563 snapshot!("return .. += ()" as Expr, @r" 564 Expr::Binary { 565 left: Expr::Return { 566 expr: Some(Expr::Range { 567 limits: RangeLimits::HalfOpen, 568 }), 569 }, 570 op: BinOp::AddAssign, 571 right: Expr::Tuple, 572 } 573 "); 574 } 575 576 #[test] 577 fn test_ambiguous_label() { 578 for stmt in [ 579 quote! { 580 return 'label: loop { break 'label 42; }; 581 }, 582 quote! { 583 break ('label: loop { break 'label 42; }); 584 }, 585 quote! { 586 break 1 + 'label: loop { break 'label 42; }; 587 }, 588 quote! { 589 break 'outer 'inner: loop { break 'inner 42; }; 590 }, 591 ] { 592 syn::parse2::(stmt).unwrap(); 593 } 594 595 for stmt in [ 596 // Parentheses required. See https://github.com/rust-lang/rust/pull/87026. 597 quote! { 598 break 'label: loop { break 'label 42; }; 599 }, 600 ] { 601 syn::parse2::(stmt).unwrap_err(); 602 } 603 } 604 605 #[test] 606 fn test_extended_interpolated_path() { 607 let path = Group::new(Delimiter::None, quote!(a::b)); 608 609 let tokens = quote!(if #path {}); 610 snapshot!(tokens as Expr, @r#" 611 Expr::If { 612 cond: Expr::Group { 613 expr: Expr::Path { 614 path: Path { 615 segments: [ 616 PathSegment { 617 ident: "a", 618 }, 619 Token![::], 620 PathSegment { 621 ident: "b", 622 }, 623 ], 624 }, 625 }, 626 }, 627 then_branch: Block { 628 stmts: [], 629 }, 630 } 631 "#); 632 633 let tokens = quote!(#path {}); 634 snapshot!(tokens as Expr, @r#" 635 Expr::Struct { 636 path: Path { 637 segments: [ 638 PathSegment { 639 ident: "a", 640 }, 641 Token![::], 642 PathSegment { 643 ident: "b", 644 }, 645 ], 646 }, 647 } 648 "#); 649 650 let tokens = quote!(#path :: c); 651 snapshot!(tokens as Expr, @r#" 652 Expr::Path { 653 path: Path { 654 segments: [ 655 PathSegment { 656 ident: "a", 657 }, 658 Token![::], 659 PathSegment { 660 ident: "b", 661 }, 662 Token![::], 663 PathSegment { 664 ident: "c", 665 }, 666 ], 667 }, 668 } 669 "#); 670 671 let nested = Group::new(Delimiter::None, quote!(a::b || true)); 672 let tokens = quote!(if #nested && false {}); 673 snapshot!(tokens as Expr, @r#" 674 Expr::If { 675 cond: Expr::Binary { 676 left: Expr::Group { 677 expr: Expr::Binary { 678 left: Expr::Path { 679 path: Path { 680 segments: [ 681 PathSegment { 682 ident: "a", 683 }, 684 Token![::], 685 PathSegment { 686 ident: "b", 687 }, 688 ], 689 }, 690 }, 691 op: BinOp::Or, 692 right: Expr::Lit { 693 lit: Lit::Bool { 694 value: true, 695 }, 696 }, 697 }, 698 }, 699 op: BinOp::And, 700 right: Expr::Lit { 701 lit: Lit::Bool { 702 value: false, 703 }, 704 }, 705 }, 706 then_branch: Block { 707 stmts: [], 708 }, 709 } 710 "#); 711 } 712 713 #[test] 714 fn test_tuple_comma() { 715 let mut expr = ExprTuple { 716 attrs: Vec::new(), 717 paren_token: token::Paren::default(), 718 elems: Punctuated::new(), 719 }; 720 snapshot!(expr.to_token_stream() as Expr, @"Expr::Tuple"); 721 722 expr.elems.push_value(parse_quote!(continue)); 723 // Must not parse to Expr::Paren 724 snapshot!(expr.to_token_stream() as Expr, @r#" 725 Expr::Tuple { 726 elems: [ 727 Expr::Continue, 728 Token![,], 729 ], 730 } 731 "#); 732 733 expr.elems.push_punct(::default()); 734 snapshot!(expr.to_token_stream() as Expr, @r#" 735 Expr::Tuple { 736 elems: [ 737 Expr::Continue, 738 Token![,], 739 ], 740 } 741 "#); 742 743 expr.elems.push_value(parse_quote!(continue)); 744 snapshot!(expr.to_token_stream() as Expr, @r#" 745 Expr::Tuple { 746 elems: [ 747 Expr::Continue, 748 Token![,], 749 Expr::Continue, 750 ], 751 } 752 "#); 753 754 expr.elems.push_punct(::default()); 755 snapshot!(expr.to_token_stream() as Expr, @r#" 756 Expr::Tuple { 757 elems: [ 758 Expr::Continue, 759 Token![,], 760 Expr::Continue, 761 Token![,], 762 ], 763 } 764 "#); 765 } 766 767 #[test] 768 fn test_binop_associativity() { 769 // Left to right. 770 snapshot!("() + () + ()" as Expr, @r#" 771 Expr::Binary { 772 left: Expr::Binary { 773 left: Expr::Tuple, 774 op: BinOp::Add, 775 right: Expr::Tuple, 776 }, 777 op: BinOp::Add, 778 right: Expr::Tuple, 779 } 780 "#); 781 782 // Right to left. 783 snapshot!("() += () += ()" as Expr, @r#" 784 Expr::Binary { 785 left: Expr::Tuple, 786 op: BinOp::AddAssign, 787 right: Expr::Binary { 788 left: Expr::Tuple, 789 op: BinOp::AddAssign, 790 right: Expr::Tuple, 791 }, 792 } 793 "#); 794 795 // Parenthesization is required. 796 syn::parse_str::("() == () == ()").unwrap_err(); 797 } 798 799 #[test] 800 fn test_assign_range_precedence() { 801 // Range has higher precedence as the right-hand of an assignment, but 802 // ambiguous precedence as the left-hand of an assignment. 803 snapshot!("() = () .. ()" as Expr, @r#" 804 Expr::Assign { 805 left: Expr::Tuple, 806 right: Expr::Range { 807 start: Some(Expr::Tuple), 808 limits: RangeLimits::HalfOpen, 809 end: Some(Expr::Tuple), 810 }, 811 } 812 "#); 813 814 snapshot!("() += () .. ()" as Expr, @r#" 815 Expr::Binary { 816 left: Expr::Tuple, 817 op: BinOp::AddAssign, 818 right: Expr::Range { 819 start: Some(Expr::Tuple), 820 limits: RangeLimits::HalfOpen, 821 end: Some(Expr::Tuple), 822 }, 823 } 824 "#); 825 826 syn::parse_str::("() .. () = ()").unwrap_err(); 827 syn::parse_str::("() .. () += ()").unwrap_err(); 828 } 829 830 #[test] 831 fn test_chained_comparison() { 832 // https://github.com/dtolnay/syn/issues/1738 833 let _ = syn::parse_str::("a = a < a <"); 834 let _ = syn::parse_str::("a = a .. a .."); 835 let _ = syn::parse_str::("a = a .. a +="); 836 837 let err = syn::parse_str::("a < a < a").unwrap_err(); 838 assert_eq!("comparison operators cannot be chained", err.to_string()); 839 840 let err = syn::parse_str::("a .. a .. a").unwrap_err(); 841 assert_eq!("unexpected token", err.to_string()); 842 843 let err = syn::parse_str::("a .. a += a").unwrap_err(); 844 assert_eq!("unexpected token", err.to_string()); 845 } 846 847 #[test] 848 fn test_fixup() { 849 for tokens in [ 850 quote! { 2 * (1 + 1) }, 851 quote! { 0 + (0 + 0) }, 852 quote! { (a = b) = c }, 853 quote! { (x as i32) < 0 }, 854 quote! { 1 + (x as i32) < 0 }, 855 quote! { (1 + 1).abs() }, 856 quote! { (lo..hi)[..] }, 857 quote! { (a..b)..(c..d) }, 858 quote! { (x > ..) > x }, 859 quote! { (&mut fut).await }, 860 quote! { &mut (x as i32) }, 861 quote! { -(x as i32) }, 862 quote! { if (S {}) == 1 {} }, 863 quote! { { (m! {}) - 1 } }, 864 quote! { match m { _ => ({}) - 1 } }, 865 quote! { if let _ = (a && b) && c {} }, 866 quote! { if let _ = (S {}) {} }, 867 quote! { if (S {}) == 0 && let Some(_) = x {} }, 868 quote! { break ('a: loop { break 'a 1 } + 1) }, 869 quote! { a + (|| b) + c }, 870 quote! { if let _ = ((break) - 1 || true) {} }, 871 quote! { if let _ = (break + 1 || true) {} }, 872 quote! { if break (break) {} }, 873 quote! { if break break {} {} }, 874 quote! { if return (..) {} }, 875 quote! { if return .. {} {} }, 876 quote! { if || (Struct {}) {} }, 877 quote! { if || (Struct {}).await {} }, 878 quote! { if break || Struct {}.await {} }, 879 quote! { if break 'outer 'block: {} {} }, 880 quote! { if ..'block: {} {} }, 881 quote! { if break ({}).await {} }, 882 quote! { (break)() }, 883 quote! { (..) = () }, 884 quote! { (..) += () }, 885 quote! { (1 < 2) == (3 < 4) }, 886 quote! { { (let _ = ()) } }, 887 quote! { (#[attr] thing).field }, 888 quote! { #[attr] (1 + 1) }, 889 quote! { #[attr] (x = 1) }, 890 quote! { #[attr] (x += 1) }, 891 quote! { #[attr] (1 as T) }, 892 quote! { (return #[attr] (x + ..)).field }, 893 quote! { (self.f)() }, 894 quote! { (return)..=return }, 895 quote! { 1 + (return)..=1 + return }, 896 quote! { .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. }, 897 ] { 898 let original: Expr = syn::parse2(tokens).unwrap(); 899 900 let mut flat = original.clone(); 901 FlattenParens::combine_attrs().visit_expr_mut(&mut flat); 902 let reconstructed: Expr = match syn::parse2(flat.to_token_stream()) { 903 Ok(reconstructed) => reconstructed, 904 Err(err) => panic!("failed to parse `{}`: {}", flat.to_token_stream(), err), 905 }; 906 907 assert!( 908 original == reconstructed, 909 "original: {}\n{:#?}\nreconstructed: {}\n{:#?}", 910 original.to_token_stream(), 911 crate::debug::Lite(&original), 912 reconstructed.to_token_stream(), 913 crate::debug::Lite(&reconstructed), 914 ); 915 } 916 } 917 918 #[test] 919 fn test_permutations() -> ExitCode { 920 fn iter(depth: usize, f: &mut dyn FnMut(Expr)) { 921 let span = Span::call_site(); 922 923 // Expr::Path 924 f(Expr::Path(ExprPath { 925 // `x` 926 attrs: Vec::new(), 927 qself: None, 928 path: Path::from(Ident::new("x", span)), 929 })); 930 if false { 931 f(Expr::Path(ExprPath { 932 // `x::` 933 attrs: Vec::new(), 934 qself: None, 935 path: Path { 936 leading_colon: None, 937 segments: Punctuated::from_iter([PathSegment { 938 ident: Ident::new("x", span), 939 arguments: PathArguments::AngleBracketed(AngleBracketedGenericArguments { 940 colon2_token: Some(Token![::](span)), 941 lt_token: Token![<](span), 942 args: Punctuated::from_iter([GenericArgument::Type(Type::Path( 943 TypePath { 944 attrs: Vec::new(), 945 qself: None, 946 path: Path::from(Ident::new("T", span)), 947 }, 948 ))]), 949 gt_token: Token![>](span), 950 }), 951 }]), 952 }, 953 })); 954 f(Expr::Path(ExprPath { 955 // `::CONST` 956 attrs: Vec::new(), 957 qself: Some(QSelf { 958 lt_token: Token![<](span), 959 ty: Box::new(Type::Path(TypePath { 960 attrs: Vec::new(), 961 qself: None, 962 path: Path::from(Ident::new("T", span)), 963 })), 964 position: 1, 965 as_token: Some(Token![as](span)), 966 gt_token: Token![>](span), 967 }), 968 path: Path { 969 leading_colon: None, 970 segments: Punctuated::from_iter([ 971 PathSegment::from(Ident::new("Trait", span)), 972 PathSegment::from(Ident::new("CONST", span)), 973 ]), 974 }, 975 })); 976 } 977 978 let Some(depth) = depth.checked_sub(1) else { 979 return; 980 }; 981 982 // Expr::Assign 983 iter(depth, &mut |expr| { 984 iter(0, &mut |simple| { 985 f(Expr::Assign(ExprAssign { 986 // `x = $expr` 987 attrs: Vec::new(), 988 left: Box::new(simple.clone()), 989 eq_token: Token![=](span), 990 right: Box::new(expr.clone()), 991 })); 992 f(Expr::Assign(ExprAssign { 993 // `$expr = x` 994 attrs: Vec::new(), 995 left: Box::new(expr.clone()), 996 eq_token: Token![=](span), 997 right: Box::new(simple), 998 })); 999 }); 1000 }); 1001 1002 // Expr::Binary 1003 iter(depth, &mut |expr| { 1004 iter(0, &mut |simple| { 1005 for op in [ 1006 BinOp::Add(Token![+](span)), 1007 //BinOp::Sub(Token![-](span)), 1008 //BinOp::Mul(Token![*](span)), 1009 //BinOp::Div(Token![/](span)), 1010 //BinOp::Rem(Token![%](span)), 1011 //BinOp::And(Token![&&](span)), 1012 //BinOp::Or(Token![||](span)), 1013 //BinOp::BitXor(Token![^](span)), 1014 //BinOp::BitAnd(Token![&](span)), 1015 //BinOp::BitOr(Token![|](span)), 1016 //BinOp::Shl(Token![<<](span)), 1017 //BinOp::Shr(Token![>>](span)), 1018 //BinOp::Eq(Token![==](span)), 1019 BinOp::Lt(Token![<](span)), 1020 //BinOp::Le(Token![<=](span)), 1021 //BinOp::Ne(Token![!=](span)), 1022 //BinOp::Ge(Token![>=](span)), 1023 //BinOp::Gt(Token![>](span)), 1024 BinOp::ShlAssign(Token![<<=](span)), 1025 ] { 1026 f(Expr::Binary(ExprBinary { 1027 // `x + $expr` 1028 attrs: Vec::new(), 1029 left: Box::new(simple.clone()), 1030 op, 1031 right: Box::new(expr.clone()), 1032 })); 1033 f(Expr::Binary(ExprBinary { 1034 // `$expr + x` 1035 attrs: Vec::new(), 1036 left: Box::new(expr.clone()), 1037 op, 1038 right: Box::new(simple.clone()), 1039 })); 1040 } 1041 }); 1042 }); 1043 1044 // Expr::Block 1045 f(Expr::Block(ExprBlock { 1046 // `{}` 1047 attrs: Vec::new(), 1048 label: None, 1049 block: Block { 1050 brace_token: token::Brace(span), 1051 stmts: Vec::new(), 1052 }, 1053 })); 1054 1055 // Expr::Break 1056 f(Expr::Break(ExprBreak { 1057 // `break` 1058 attrs: Vec::new(), 1059 break_token: Token![break](span), 1060 label: None, 1061 expr: None, 1062 })); 1063 iter(depth, &mut |expr| { 1064 f(Expr::Break(ExprBreak { 1065 // `break $expr` 1066 attrs: Vec::new(), 1067 break_token: Token![break](span), 1068 label: None, 1069 expr: Some(Box::new(expr)), 1070 })); 1071 }); 1072 1073 // Expr::Call 1074 iter(depth, &mut |expr| { 1075 f(Expr::Call(ExprCall { 1076 // `$expr()` 1077 attrs: Vec::new(), 1078 func: Box::new(expr), 1079 paren_token: token::Paren(span), 1080 args: Punctuated::new(), 1081 })); 1082 }); 1083 1084 // Expr::Cast 1085 iter(depth, &mut |expr| { 1086 f(Expr::Cast(ExprCast { 1087 // `$expr as T` 1088 attrs: Vec::new(), 1089 expr: Box::new(expr), 1090 as_token: Token![as](span), 1091 ty: Box::new(Type::Path(TypePath { 1092 attrs: Vec::new(), 1093 qself: None, 1094 path: Path::from(Ident::new("T", span)), 1095 })), 1096 })); 1097 }); 1098 1099 // Expr::Closure 1100 iter(depth, &mut |expr| { 1101 f(Expr::Closure(ExprClosure { 1102 // `|| $expr` 1103 attrs: Vec::new(), 1104 lifetimes: None, 1105 modifiers: ClosureModifiers::default(), 1106 constness: None, 1107 asyncness: None, 1108 capture: None, 1109 inputs_begin: Token![|](span), 1110 inputs: Punctuated::new(), 1111 inputs_end: Token![|](span), 1112 output: ReturnType::Default, 1113 body: Box::new(expr), 1114 })); 1115 }); 1116 1117 // Expr::Field 1118 iter(depth, &mut |expr| { 1119 f(Expr::Field(ExprField { 1120 // `$expr.field` 1121 attrs: Vec::new(), 1122 base: Box::new(expr), 1123 dot_token: Token![.](span), 1124 member: Member::Named(Ident::new("field", span)), 1125 })); 1126 }); 1127 1128 // Expr::If 1129 iter(depth, &mut |expr| { 1130 f(Expr::If(ExprIf { 1131 // `if $expr {}` 1132 attrs: Vec::new(), 1133 if_token: Token![if](span), 1134 cond: Box::new(expr), 1135 then_branch: Block { 1136 brace_token: token::Brace(span), 1137 stmts: Vec::new(), 1138 }, 1139 else_branch: None, 1140 })); 1141 }); 1142 1143 // Expr::Let 1144 iter(depth, &mut |expr| { 1145 f(Expr::Let(ExprLet { 1146 attrs: Vec::new(), 1147 let_token: Token![let](span), 1148 pat: Box::new(Pat::Wild(PatWild { 1149 attrs: Vec::new(), 1150 underscore_token: Token![_](span), 1151 })), 1152 eq_token: Token![=](span), 1153 expr: Box::new(expr), 1154 })); 1155 }); 1156 1157 // Expr::Range 1158 f(Expr::Range(ExprRange { 1159 // `..` 1160 attrs: Vec::new(), 1161 start: None, 1162 limits: RangeLimits::HalfOpen(Token![..](span)), 1163 end: None, 1164 })); 1165 iter(depth, &mut |expr| { 1166 f(Expr::Range(ExprRange { 1167 // `..$expr` 1168 attrs: Vec::new(), 1169 start: None, 1170 limits: RangeLimits::HalfOpen(Token![..](span)), 1171 end: Some(Box::new(expr.clone())), 1172 })); 1173 f(Expr::Range(ExprRange { 1174 // `$expr..` 1175 attrs: Vec::new(), 1176 start: Some(Box::new(expr)), 1177 limits: RangeLimits::HalfOpen(Token![..](span)), 1178 end: None, 1179 })); 1180 }); 1181 1182 // Expr::Reference 1183 iter(depth, &mut |expr| { 1184 f(Expr::Reference(ExprReference { 1185 // `&$expr` 1186 attrs: Vec::new(), 1187 and_token: Token![&](span), 1188 mutability: None, 1189 expr: Box::new(expr), 1190 })); 1191 }); 1192 1193 // Expr::Return 1194 f(Expr::Return(ExprReturn { 1195 // `return` 1196 attrs: Vec::new(), 1197 return_token: Token![return](span), 1198 expr: None, 1199 })); 1200 iter(depth, &mut |expr| { 1201 f(Expr::Return(ExprReturn { 1202 // `return $expr` 1203 attrs: Vec::new(), 1204 return_token: Token![return](span), 1205 expr: Some(Box::new(expr)), 1206 })); 1207 }); 1208 1209 // Expr::Try 1210 iter(depth, &mut |expr| { 1211 f(Expr::Try(ExprTry { 1212 // `$expr?` 1213 attrs: Vec::new(), 1214 expr: Box::new(expr), 1215 question_token: Token![?](span), 1216 })); 1217 }); 1218 1219 // Expr::Unary 1220 iter(depth, &mut |expr| { 1221 for op in [ 1222 UnOp::Deref(Token![*](span)), 1223 //UnOp::Not(Token![!](span)), 1224 //UnOp::Neg(Token![-](span)), 1225 ] { 1226 f(Expr::Unary(ExprUnary { 1227 // `*$expr` 1228 attrs: Vec::new(), 1229 op, 1230 expr: Box::new(expr.clone()), 1231 })); 1232 } 1233 }); 1234 1235 if false { 1236 // Expr::Array 1237 f(Expr::Array(ExprArray { 1238 // `[]` 1239 attrs: Vec::new(), 1240 bracket_token: token::Bracket(span), 1241 elems: Punctuated::new(), 1242 })); 1243 1244 // Expr::Async 1245 f(Expr::Async(ExprAsync { 1246 // `async {}` 1247 attrs: Vec::new(), 1248 async_token: Token![async](span), 1249 capture: None, 1250 modifiers: BlockModifiers::default(), 1251 block: Block { 1252 brace_token: token::Brace(span), 1253 stmts: Vec::new(), 1254 }, 1255 })); 1256 1257 // Expr::Await 1258 iter(depth, &mut |expr| { 1259 f(Expr::Await(ExprAwait { 1260 // `$expr.await` 1261 attrs: Vec::new(), 1262 base: Box::new(expr), 1263 dot_token: Token![.](span), 1264 await_token: Token![await](span), 1265 })); 1266 }); 1267 1268 // Expr::Block 1269 f(Expr::Block(ExprBlock { 1270 // `'a: {}` 1271 attrs: Vec::new(), 1272 label: Some(Label { 1273 name: Lifetime::new("'a", span), 1274 colon_token: Token![:](span), 1275 }), 1276 block: Block { 1277 brace_token: token::Brace(span), 1278 stmts: Vec::new(), 1279 }, 1280 })); 1281 iter(depth, &mut |expr| { 1282 f(Expr::Block(ExprBlock { 1283 // `{ $expr }` 1284 attrs: Vec::new(), 1285 label: None, 1286 block: Block { 1287 brace_token: token::Brace(span), 1288 stmts: Vec::from([Stmt::Expr(expr.clone(), None)]), 1289 }, 1290 })); 1291 f(Expr::Block(ExprBlock { 1292 // `{ $expr; }` 1293 attrs: Vec::new(), 1294 label: None, 1295 block: Block { 1296 brace_token: token::Brace(span), 1297 stmts: Vec::from([Stmt::Expr(expr, Some(Token![;](span)))]), 1298 }, 1299 })); 1300 }); 1301 1302 // Expr::Break 1303 f(Expr::Break(ExprBreak { 1304 // `break 'a` 1305 attrs: Vec::new(), 1306 break_token: Token![break](span), 1307 label: Some(Lifetime::new("'a", span)), 1308 expr: None, 1309 })); 1310 iter(depth, &mut |expr| { 1311 f(Expr::Break(ExprBreak { 1312 // `break 'a $expr` 1313 attrs: Vec::new(), 1314 break_token: Token![break](span), 1315 label: Some(Lifetime::new("'a", span)), 1316 expr: Some(Box::new(expr)), 1317 })); 1318 }); 1319 1320 // Expr::Closure 1321 f(Expr::Closure(ExprClosure { 1322 // `|| -> T {}` 1323 attrs: Vec::new(), 1324 lifetimes: None, 1325 modifiers: ClosureModifiers::default(), 1326 constness: None, 1327 asyncness: None, 1328 capture: None, 1329 inputs_begin: Token![|](span), 1330 inputs: Punctuated::new(), 1331 inputs_end: Token![|](span), 1332 output: ReturnType::Type( 1333 Token![->](span), 1334 Box::new(Type::Path(TypePath { 1335 attrs: Vec::new(), 1336 qself: None, 1337 path: Path::from(Ident::new("T", span)), 1338 })), 1339 ), 1340 body: Box::new(Expr::Block(ExprBlock { 1341 attrs: Vec::new(), 1342 label: None, 1343 block: Block { 1344 brace_token: token::Brace(span), 1345 stmts: Vec::new(), 1346 }, 1347 })), 1348 })); 1349 1350 // Expr::Const 1351 f(Expr::Const(ExprConst { 1352 // `const {}` 1353 attrs: Vec::new(), 1354 const_token: Token![const](span), 1355 modifiers: BlockModifiers::default(), 1356 block: Block { 1357 brace_token: token::Brace(span), 1358 stmts: Vec::new(), 1359 }, 1360 })); 1361 1362 // Expr::Continue 1363 f(Expr::Continue(ExprContinue { 1364 // `continue` 1365 attrs: Vec::new(), 1366 continue_token: Token![continue](span), 1367 label: None, 1368 })); 1369 f(Expr::Continue(ExprContinue { 1370 // `continue 'a` 1371 attrs: Vec::new(), 1372 continue_token: Token![continue](span), 1373 label: Some(Lifetime::new("'a", span)), 1374 })); 1375 1376 // Expr::ForLoop 1377 iter(depth, &mut |expr| { 1378 f(Expr::ForLoop(ExprForLoop { 1379 // `for _ in $expr {}` 1380 attrs: Vec::new(), 1381 label: None, 1382 for_token: Token![for](span), 1383 pat: Box::new(Pat::Wild(PatWild { 1384 attrs: Vec::new(), 1385 underscore_token: Token![_](span), 1386 })), 1387 in_token: Token![in](span), 1388 expr: Box::new(expr.clone()), 1389 body: Block { 1390 brace_token: token::Brace(span), 1391 stmts: Vec::new(), 1392 }, 1393 })); 1394 f(Expr::ForLoop(ExprForLoop { 1395 // `'a: for _ in $expr {}` 1396 attrs: Vec::new(), 1397 label: Some(Label { 1398 name: Lifetime::new("'a", span), 1399 colon_token: Token![:](span), 1400 }), 1401 for_token: Token![for](span), 1402 pat: Box::new(Pat::Wild(PatWild { 1403 attrs: Vec::new(), 1404 underscore_token: Token![_](span), 1405 })), 1406 in_token: Token![in](span), 1407 expr: Box::new(expr), 1408 body: Block { 1409 brace_token: token::Brace(span), 1410 stmts: Vec::new(), 1411 }, 1412 })); 1413 }); 1414 1415 // Expr::Index 1416 iter(depth, &mut |expr| { 1417 f(Expr::Index(ExprIndex { 1418 // `$expr[0]` 1419 attrs: Vec::new(), 1420 expr: Box::new(expr), 1421 bracket_token: token::Bracket(span), 1422 index: Box::new(Expr::Lit(ExprLit { 1423 attrs: Vec::new(), 1424 lit: Lit::Int(LitInt::new("0", span)), 1425 })), 1426 })); 1427 }); 1428 1429 // Expr::Loop 1430 f(Expr::Loop(ExprLoop { 1431 // `loop {}` 1432 attrs: Vec::new(), 1433 label: None, 1434 loop_token: Token![loop](span), 1435 body: Block { 1436 brace_token: token::Brace(span), 1437 stmts: Vec::new(), 1438 }, 1439 })); 1440 f(Expr::Loop(ExprLoop { 1441 // `'a: loop {}` 1442 attrs: Vec::new(), 1443 label: Some(Label { 1444 name: Lifetime::new("'a", span), 1445 colon_token: Token![:](span), 1446 }), 1447 loop_token: Token![loop](span), 1448 body: Block { 1449 brace_token: token::Brace(span), 1450 stmts: Vec::new(), 1451 }, 1452 })); 1453 1454 // Expr::Macro 1455 f(Expr::Macro(ExprMacro { 1456 // `m!()` 1457 attrs: Vec::new(), 1458 mac: Macro { 1459 path: Path::from(Ident::new("m", span)), 1460 bang_token: Token![!](span), 1461 delimiter: MacroDelimiter::Paren(token::Paren(span)), 1462 tokens: TokenStream::new(), 1463 }, 1464 })); 1465 f(Expr::Macro(ExprMacro { 1466 // `m! {}` 1467 attrs: Vec::new(), 1468 mac: Macro { 1469 path: Path::from(Ident::new("m", span)), 1470 bang_token: Token![!](span), 1471 delimiter: MacroDelimiter::Brace(token::Brace(span)), 1472 tokens: TokenStream::new(), 1473 }, 1474 })); 1475 1476 // Expr::Match 1477 iter(depth, &mut |expr| { 1478 f(Expr::Match(ExprMatch { 1479 // `match $expr {}` 1480 attrs: Vec::new(), 1481 match_token: Token![match](span), 1482 expr: Box::new(expr.clone()), 1483 brace_token: token::Brace(span), 1484 arms: Vec::new(), 1485 })); 1486 f(Expr::Match(ExprMatch { 1487 // `match x { _ => $expr }` 1488 attrs: Vec::new(), 1489 match_token: Token![match](span), 1490 expr: Box::new(Expr::Path(ExprPath { 1491 attrs: Vec::new(), 1492 qself: None, 1493 path: Path::from(Ident::new("x", span)), 1494 })), 1495 brace_token: token::Brace(span), 1496 arms: Vec::from([Arm { 1497 attrs: Vec::new(), 1498 pat: Pat::Wild(PatWild { 1499 attrs: Vec::new(), 1500 underscore_token: Token![_](span), 1501 }), 1502 fat_arrow_token: Token![=>](span), 1503 body: Box::new(expr.clone()), 1504 comma: None, 1505 }]), 1506 })); 1507 f(Expr::Match(ExprMatch { 1508 // `match x { _ if $expr => {} }` 1509 attrs: Vec::new(), 1510 match_token: Token![match](span), 1511 expr: Box::new(Expr::Path(ExprPath { 1512 attrs: Vec::new(), 1513 qself: None, 1514 path: Path::from(Ident::new("x", span)), 1515 })), 1516 brace_token: token::Brace(span), 1517 arms: Vec::from([Arm { 1518 attrs: Vec::new(), 1519 pat: Pat::Guard(PatGuard { 1520 attrs: Vec::new(), 1521 pat: Box::new(Pat::Wild(PatWild { 1522 attrs: Vec::new(), 1523 underscore_token: Token![_](span), 1524 })), 1525 if_token: Token![if](span), 1526 guard: Box::new(expr), 1527 }), 1528 fat_arrow_token: Token![=>](span), 1529 body: Box::new(Expr::Block(ExprBlock { 1530 attrs: Vec::new(), 1531 label: None, 1532 block: Block { 1533 brace_token: token::Brace(span), 1534 stmts: Vec::new(), 1535 }, 1536 })), 1537 comma: None, 1538 }]), 1539 })); 1540 }); 1541 1542 // Expr::MethodCall 1543 iter(depth, &mut |expr| { 1544 f(Expr::MethodCall(ExprMethodCall { 1545 // `$expr.method()` 1546 attrs: Vec::new(), 1547 receiver: Box::new(expr.clone()), 1548 dot_token: Token![.](span), 1549 method: Ident::new("method", span), 1550 turbofish: None, 1551 paren_token: token::Paren(span), 1552 args: Punctuated::new(), 1553 })); 1554 f(Expr::MethodCall(ExprMethodCall { 1555 // `$expr.method::()` 1556 attrs: Vec::new(), 1557 receiver: Box::new(expr), 1558 dot_token: Token![.](span), 1559 method: Ident::new("method", span), 1560 turbofish: Some(AngleBracketedGenericArguments { 1561 colon2_token: Some(Token![::](span)), 1562 lt_token: Token![<](span), 1563 args: Punctuated::from_iter([GenericArgument::Type(Type::Path( 1564 TypePath { 1565 attrs: Vec::new(), 1566 qself: None, 1567 path: Path::from(Ident::new("T", span)), 1568 }, 1569 ))]), 1570 gt_token: Token![>](span), 1571 }), 1572 paren_token: token::Paren(span), 1573 args: Punctuated::new(), 1574 })); 1575 }); 1576 1577 // Expr::RawAddr 1578 iter(depth, &mut |expr| { 1579 f(Expr::RawAddr(ExprRawAddr { 1580 // `&raw const $expr` 1581 attrs: Vec::new(), 1582 and_token: Token![&](span), 1583 raw: Token![raw](span), 1584 mutability: PointerMutability::Const(Token![const](span)), 1585 expr: Box::new(expr), 1586 })); 1587 }); 1588 1589 // Expr::Struct 1590 f(Expr::Struct(ExprStruct { 1591 // `Struct {}` 1592 attrs: Vec::new(), 1593 qself: None, 1594 path: Path::from(Ident::new("Struct", span)), 1595 brace_token: token::Brace(span), 1596 fields: Punctuated::new(), 1597 dot2_token: None, 1598 rest: None, 1599 })); 1600 1601 // Expr::TryBlock 1602 f(Expr::TryBlock(ExprTryBlock { 1603 // `try {}` 1604 attrs: Vec::new(), 1605 try_token: Token![try](span), 1606 modifiers: BlockModifiers::default(), 1607 block: Block { 1608 brace_token: token::Brace(span), 1609 stmts: Vec::new(), 1610 }, 1611 })); 1612 1613 // Expr::Unsafe 1614 f(Expr::Unsafe(ExprUnsafe { 1615 // `unsafe {}` 1616 attrs: Vec::new(), 1617 unsafe_token: Token![unsafe](span), 1618 block: Block { 1619 brace_token: token::Brace(span), 1620 stmts: Vec::new(), 1621 }, 1622 })); 1623 1624 // Expr::While 1625 iter(depth, &mut |expr| { 1626 f(Expr::While(ExprWhile { 1627 // `while $expr {}` 1628 attrs: Vec::new(), 1629 label: None, 1630 while_token: Token![while](span), 1631 cond: Box::new(expr.clone()), 1632 body: Block { 1633 brace_token: token::Brace(span), 1634 stmts: Vec::new(), 1635 }, 1636 })); 1637 f(Expr::While(ExprWhile { 1638 // `'a: while $expr {}` 1639 attrs: Vec::new(), 1640 label: Some(Label { 1641 name: Lifetime::new("'a", span), 1642 colon_token: Token![:](span), 1643 }), 1644 while_token: Token![while](span), 1645 cond: Box::new(expr), 1646 body: Block { 1647 brace_token: token::Brace(span), 1648 stmts: Vec::new(), 1649 }, 1650 })); 1651 }); 1652 1653 // Expr::Yield 1654 f(Expr::Yield(ExprYield { 1655 // `yield` 1656 attrs: Vec::new(), 1657 yield_token: Token![yield](span), 1658 expr: None, 1659 })); 1660 iter(depth, &mut |expr| { 1661 f(Expr::Yield(ExprYield { 1662 // `yield $expr` 1663 attrs: Vec::new(), 1664 yield_token: Token![yield](span), 1665 expr: Some(Box::new(expr)), 1666 })); 1667 }); 1668 } 1669 } 1670 1671 let mut failures = 0; 1672 macro_rules! fail { 1673 ($($message:tt)*) => {{ 1674 eprintln!($($message)*); 1675 failures += 1; 1676 return; 1677 }}; 1678 } 1679 let mut assert = |mut original: Expr| { 1680 let tokens = original.to_token_stream(); 1681 let Ok(mut parsed) = syn::parse2::(tokens.clone()) else { 1682 fail!( 1683 "failed to parse: {}\n{:#?}", 1684 tokens, 1685 crate::debug::Lite(&original), 1686 ); 1687 }; 1688 AsIfPrinted.visit_expr_mut(&mut original); 1689 FlattenParens::combine_attrs().visit_expr_mut(&mut parsed); 1690 if original != parsed { 1691 fail!( 1692 "before: {}\n{:#?}\nafter: {}\n{:#?}", 1693 tokens, 1694 crate::debug::Lite(&original), 1695 parsed.to_token_stream(), 1696 crate::debug::Lite(&parsed), 1697 ); 1698 } 1699 let mut tokens_no_paren = tokens.clone(); 1700 FlattenParens::visit_token_stream_mut(&mut tokens_no_paren); 1701 if tokens.to_string() != tokens_no_paren.to_string() { 1702 if let Ok(mut parsed2) = syn::parse2::(tokens_no_paren) { 1703 FlattenParens::combine_attrs().visit_expr_mut(&mut parsed2); 1704 if original == parsed2 { 1705 fail!("redundant parens: {}", tokens); 1706 } 1707 } 1708 } 1709 }; 1710 1711 iter(4, &mut assert); 1712 if failures > 0 { 1713 eprintln!("FAILURES: {failures}"); 1714 ExitCode::FAILURE 1715 } else { 1716 ExitCode::SUCCESS 1717 } 1718 }