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