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_derive_input.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_derive_input.rs` 801 lines ยท 22641 bytes Raw Blame History 1 #![allow( 2 clippy::assertions_on_result_states, 3 clippy::elidable_lifetime_names, 4 clippy::manual_let_else, 5 clippy::needless_lifetimes, 6 clippy::too_many_lines, 7 clippy::uninlined_format_args 8 )] 9 10 #[macro_use] 11 mod snapshot; 12 13 mod debug; 14 15 use quote::quote; 16 use syn::{Data, DeriveInput}; 17 18 #[test] 19 fn test_unit() { 20 let input = quote! { 21 struct Unit; 22 }; 23 24 snapshot!(input as DeriveInput, @r#" 25 DeriveInput { 26 vis: Visibility::Inherited, 27 ident: "Unit", 28 generics: Generics, 29 data: Data::Struct { 30 fields: Fields::Unit, 31 semi_token: Some, 32 }, 33 } 34 "#); 35 } 36 37 #[test] 38 fn test_struct() { 39 let input = quote! { 40 #[derive(Debug, Clone)] 41 pub struct Item { 42 pub ident: Ident, 43 pub attrs: Vec 44 } 45 }; 46 47 snapshot!(input as DeriveInput, @r#" 48 DeriveInput { 49 attrs: [ 50 Attribute { 51 style: AttrStyle::Outer, 52 meta: Meta::List { 53 path: Path { 54 segments: [ 55 PathSegment { 56 ident: "derive", 57 }, 58 ], 59 }, 60 delimiter: MacroDelimiter::Paren, 61 tokens: TokenStream(`Debug , Clone`), 62 }, 63 }, 64 ], 65 vis: Visibility::Public, 66 ident: "Item", 67 generics: Generics, 68 data: Data::Struct { 69 fields: Fields::Named { 70 named: [ 71 Field { 72 vis: Visibility::Public, 73 modifiers: FieldModifiers, 74 ident: Some("ident"), 75 colon_token: Some, 76 ty: Type::Path { 77 path: Path { 78 segments: [ 79 PathSegment { 80 ident: "Ident", 81 }, 82 ], 83 }, 84 }, 85 }, 86 Token![,], 87 Field { 88 vis: Visibility::Public, 89 modifiers: FieldModifiers, 90 ident: Some("attrs"), 91 colon_token: Some, 92 ty: Type::Path { 93 path: Path { 94 segments: [ 95 PathSegment { 96 ident: "Vec", 97 arguments: PathArguments::AngleBracketed { 98 args: [ 99 GenericArgument::Type(Type::Path { 100 path: Path { 101 segments: [ 102 PathSegment { 103 ident: "Attribute", 104 }, 105 ], 106 }, 107 }), 108 ], 109 }, 110 }, 111 ], 112 }, 113 }, 114 }, 115 ], 116 }, 117 }, 118 } 119 "#); 120 121 snapshot!(&input.attrs[0].meta, @r#" 122 Meta::List { 123 path: Path { 124 segments: [ 125 PathSegment { 126 ident: "derive", 127 }, 128 ], 129 }, 130 delimiter: MacroDelimiter::Paren, 131 tokens: TokenStream(`Debug , Clone`), 132 } 133 "#); 134 } 135 136 #[test] 137 fn test_union() { 138 let input = quote! { 139 union MaybeUninit { 140 uninit: (), 141 value: T 142 } 143 }; 144 145 snapshot!(input as DeriveInput, @r#" 146 DeriveInput { 147 vis: Visibility::Inherited, 148 ident: "MaybeUninit", 149 generics: Generics { 150 lt_token: Some, 151 params: [ 152 GenericParam::Type(TypeParam { 153 ident: "T", 154 }), 155 ], 156 gt_token: Some, 157 }, 158 data: Data::Union { 159 fields: FieldsNamed { 160 named: [ 161 Field { 162 vis: Visibility::Inherited, 163 modifiers: FieldModifiers, 164 ident: Some("uninit"), 165 colon_token: Some, 166 ty: Type::Tuple, 167 }, 168 Token![,], 169 Field { 170 vis: Visibility::Inherited, 171 modifiers: FieldModifiers, 172 ident: Some("value"), 173 colon_token: Some, 174 ty: Type::Path { 175 path: Path { 176 segments: [ 177 PathSegment { 178 ident: "T", 179 }, 180 ], 181 }, 182 }, 183 }, 184 ], 185 }, 186 }, 187 } 188 "#); 189 } 190 191 #[test] 192 #[cfg(feature = "full")] 193 fn test_enum() { 194 let input = quote! { 195 /// See the std::result module documentation for details. 196 #[must_use] 197 pub enum Result { 198 Ok(T), 199 Err(E), 200 Surprise = 0isize, 201 202 // Smuggling data into a proc_macro_derive, 203 // in the style of https://github.com/dtolnay/proc-macro-hack 204 ProcMacroHack = (0, "data").0 205 } 206 }; 207 208 snapshot!(input as DeriveInput, @r#" 209 DeriveInput { 210 attrs: [ 211 Attribute { 212 style: AttrStyle::Outer, 213 meta: Meta::NameValue { 214 path: Path { 215 segments: [ 216 PathSegment { 217 ident: "doc", 218 }, 219 ], 220 }, 221 value: Expr::Lit { 222 lit: " See the std::result module documentation for details.", 223 }, 224 }, 225 }, 226 Attribute { 227 style: AttrStyle::Outer, 228 meta: Meta::Path { 229 segments: [ 230 PathSegment { 231 ident: "must_use", 232 }, 233 ], 234 }, 235 }, 236 ], 237 vis: Visibility::Public, 238 ident: "Result", 239 generics: Generics { 240 lt_token: Some, 241 params: [ 242 GenericParam::Type(TypeParam { 243 ident: "T", 244 }), 245 Token![,], 246 GenericParam::Type(TypeParam { 247 ident: "E", 248 }), 249 ], 250 gt_token: Some, 251 }, 252 data: Data::Enum { 253 variants: [ 254 Variant { 255 ident: "Ok", 256 fields: Fields::Unnamed { 257 unnamed: [ 258 Field { 259 vis: Visibility::Inherited, 260 modifiers: FieldModifiers, 261 ty: Type::Path { 262 path: Path { 263 segments: [ 264 PathSegment { 265 ident: "T", 266 }, 267 ], 268 }, 269 }, 270 }, 271 ], 272 }, 273 }, 274 Token![,], 275 Variant { 276 ident: "Err", 277 fields: Fields::Unnamed { 278 unnamed: [ 279 Field { 280 vis: Visibility::Inherited, 281 modifiers: FieldModifiers, 282 ty: Type::Path { 283 path: Path { 284 segments: [ 285 PathSegment { 286 ident: "E", 287 }, 288 ], 289 }, 290 }, 291 }, 292 ], 293 }, 294 }, 295 Token![,], 296 Variant { 297 ident: "Surprise", 298 fields: Fields::Unit, 299 discriminant: Some(Expr::Lit { 300 lit: 0isize, 301 }), 302 }, 303 Token![,], 304 Variant { 305 ident: "ProcMacroHack", 306 fields: Fields::Unit, 307 discriminant: Some(Expr::Field { 308 base: Expr::Tuple { 309 elems: [ 310 Expr::Lit { 311 lit: 0, 312 }, 313 Token![,], 314 Expr::Lit { 315 lit: "data", 316 }, 317 ], 318 }, 319 member: Member::Unnamed(Index { 320 index: 0, 321 }), 322 }), 323 }, 324 ], 325 }, 326 } 327 "#); 328 329 let meta_items: Vec<_> = input.attrs.into_iter().map(|attr| attr.meta).collect(); 330 331 snapshot!(meta_items, @r#" 332 [ 333 Meta::NameValue { 334 path: Path { 335 segments: [ 336 PathSegment { 337 ident: "doc", 338 }, 339 ], 340 }, 341 value: Expr::Lit { 342 lit: " See the std::result module documentation for details.", 343 }, 344 }, 345 Meta::Path { 346 segments: [ 347 PathSegment { 348 ident: "must_use", 349 }, 350 ], 351 }, 352 ] 353 "#); 354 } 355 356 #[test] 357 fn test_attr_with_non_mod_style_path() { 358 let input = quote! { 359 #[inert ] 360 struct S; 361 }; 362 363 syn::parse2::(input).unwrap_err(); 364 } 365 366 #[test] 367 fn test_attr_with_mod_style_path_with_self() { 368 let input = quote! { 369 #[foo::self] 370 struct S; 371 }; 372 373 snapshot!(input as DeriveInput, @r#" 374 DeriveInput { 375 attrs: [ 376 Attribute { 377 style: AttrStyle::Outer, 378 meta: Meta::Path { 379 segments: [ 380 PathSegment { 381 ident: "foo", 382 }, 383 Token![::], 384 PathSegment { 385 ident: "self", 386 }, 387 ], 388 }, 389 }, 390 ], 391 vis: Visibility::Inherited, 392 ident: "S", 393 generics: Generics, 394 data: Data::Struct { 395 fields: Fields::Unit, 396 semi_token: Some, 397 }, 398 } 399 "#); 400 401 snapshot!(&input.attrs[0].meta, @r#" 402 Meta::Path { 403 segments: [ 404 PathSegment { 405 ident: "foo", 406 }, 407 Token![::], 408 PathSegment { 409 ident: "self", 410 }, 411 ], 412 } 413 "#); 414 } 415 416 #[test] 417 fn test_pub_restricted() { 418 // Taken from tests/rust/src/test/ui/resolve/auxiliary/privacy-struct-ctor.rs 419 let input = quote! { 420 pub(in m) struct Z(pub(in m::n) u8); 421 }; 422 423 snapshot!(input as DeriveInput, @r#" 424 DeriveInput { 425 vis: Visibility::Restricted { 426 in_token: Some, 427 path: Path { 428 segments: [ 429 PathSegment { 430 ident: "m", 431 }, 432 ], 433 }, 434 }, 435 ident: "Z", 436 generics: Generics, 437 data: Data::Struct { 438 fields: Fields::Unnamed { 439 unnamed: [ 440 Field { 441 vis: Visibility::Restricted { 442 in_token: Some, 443 path: Path { 444 segments: [ 445 PathSegment { 446 ident: "m", 447 }, 448 Token![::], 449 PathSegment { 450 ident: "n", 451 }, 452 ], 453 }, 454 }, 455 modifiers: FieldModifiers, 456 ty: Type::Path { 457 path: Path { 458 segments: [ 459 PathSegment { 460 ident: "u8", 461 }, 462 ], 463 }, 464 }, 465 }, 466 ], 467 }, 468 semi_token: Some, 469 }, 470 } 471 "#); 472 } 473 474 #[test] 475 fn test_pub_restricted_crate() { 476 let input = quote! { 477 pub(crate) struct S; 478 }; 479 480 snapshot!(input as DeriveInput, @r#" 481 DeriveInput { 482 vis: Visibility::Restricted { 483 path: Path { 484 segments: [ 485 PathSegment { 486 ident: "crate", 487 }, 488 ], 489 }, 490 }, 491 ident: "S", 492 generics: Generics, 493 data: Data::Struct { 494 fields: Fields::Unit, 495 semi_token: Some, 496 }, 497 } 498 "#); 499 } 500 501 #[test] 502 fn test_pub_restricted_super() { 503 let input = quote! { 504 pub(super) struct S; 505 }; 506 507 snapshot!(input as DeriveInput, @r#" 508 DeriveInput { 509 vis: Visibility::Restricted { 510 path: Path { 511 segments: [ 512 PathSegment { 513 ident: "super", 514 }, 515 ], 516 }, 517 }, 518 ident: "S", 519 generics: Generics, 520 data: Data::Struct { 521 fields: Fields::Unit, 522 semi_token: Some, 523 }, 524 } 525 "#); 526 } 527 528 #[test] 529 fn test_pub_restricted_in_super() { 530 let input = quote! { 531 pub(in super) struct S; 532 }; 533 534 snapshot!(input as DeriveInput, @r#" 535 DeriveInput { 536 vis: Visibility::Restricted { 537 in_token: Some, 538 path: Path { 539 segments: [ 540 PathSegment { 541 ident: "super", 542 }, 543 ], 544 }, 545 }, 546 ident: "S", 547 generics: Generics, 548 data: Data::Struct { 549 fields: Fields::Unit, 550 semi_token: Some, 551 }, 552 } 553 "#); 554 } 555 556 #[test] 557 fn test_fields_on_unit_struct() { 558 let input = quote! { 559 struct S; 560 }; 561 562 snapshot!(input as DeriveInput, @r#" 563 DeriveInput { 564 vis: Visibility::Inherited, 565 ident: "S", 566 generics: Generics, 567 data: Data::Struct { 568 fields: Fields::Unit, 569 semi_token: Some, 570 }, 571 } 572 "#); 573 574 let data = match input.data { 575 Data::Struct(data) => data, 576 _ => panic!("expected a struct"), 577 }; 578 579 assert_eq!(0, data.fields.iter().count()); 580 } 581 582 #[test] 583 fn test_fields_on_named_struct() { 584 let input = quote! { 585 struct S { 586 foo: i32, 587 pub bar: String, 588 } 589 }; 590 591 snapshot!(input as DeriveInput, @r#" 592 DeriveInput { 593 vis: Visibility::Inherited, 594 ident: "S", 595 generics: Generics, 596 data: Data::Struct { 597 fields: Fields::Named { 598 named: [ 599 Field { 600 vis: Visibility::Inherited, 601 modifiers: FieldModifiers, 602 ident: Some("foo"), 603 colon_token: Some, 604 ty: Type::Path { 605 path: Path { 606 segments: [ 607 PathSegment { 608 ident: "i32", 609 }, 610 ], 611 }, 612 }, 613 }, 614 Token![,], 615 Field { 616 vis: Visibility::Public, 617 modifiers: FieldModifiers, 618 ident: Some("bar"), 619 colon_token: Some, 620 ty: Type::Path { 621 path: Path { 622 segments: [ 623 PathSegment { 624 ident: "String", 625 }, 626 ], 627 }, 628 }, 629 }, 630 Token![,], 631 ], 632 }, 633 }, 634 } 635 "#); 636 637 let data = match input.data { 638 Data::Struct(data) => data, 639 _ => panic!("expected a struct"), 640 }; 641 642 snapshot!(data.fields.into_iter().collect::>(), @r#" 643 [ 644 Field { 645 vis: Visibility::Inherited, 646 modifiers: FieldModifiers, 647 ident: Some("foo"), 648 colon_token: Some, 649 ty: Type::Path { 650 path: Path { 651 segments: [ 652 PathSegment { 653 ident: "i32", 654 }, 655 ], 656 }, 657 }, 658 }, 659 Field { 660 vis: Visibility::Public, 661 modifiers: FieldModifiers, 662 ident: Some("bar"), 663 colon_token: Some, 664 ty: Type::Path { 665 path: Path { 666 segments: [ 667 PathSegment { 668 ident: "String", 669 }, 670 ], 671 }, 672 }, 673 }, 674 ] 675 "#); 676 } 677 678 #[test] 679 fn test_fields_on_tuple_struct() { 680 let input = quote! { 681 struct S(i32, pub String); 682 }; 683 684 snapshot!(input as DeriveInput, @r#" 685 DeriveInput { 686 vis: Visibility::Inherited, 687 ident: "S", 688 generics: Generics, 689 data: Data::Struct { 690 fields: Fields::Unnamed { 691 unnamed: [ 692 Field { 693 vis: Visibility::Inherited, 694 modifiers: FieldModifiers, 695 ty: Type::Path { 696 path: Path { 697 segments: [ 698 PathSegment { 699 ident: "i32", 700 }, 701 ], 702 }, 703 }, 704 }, 705 Token![,], 706 Field { 707 vis: Visibility::Public, 708 modifiers: FieldModifiers, 709 ty: Type::Path { 710 path: Path { 711 segments: [ 712 PathSegment { 713 ident: "String", 714 }, 715 ], 716 }, 717 }, 718 }, 719 ], 720 }, 721 semi_token: Some, 722 }, 723 } 724 "#); 725 726 let data = match input.data { 727 Data::Struct(data) => data, 728 _ => panic!("expected a struct"), 729 }; 730 731 snapshot!(data.fields.iter().collect::>(), @r#" 732 [ 733 Field { 734 vis: Visibility::Inherited, 735 modifiers: FieldModifiers, 736 ty: Type::Path { 737 path: Path { 738 segments: [ 739 PathSegment { 740 ident: "i32", 741 }, 742 ], 743 }, 744 }, 745 }, 746 Field { 747 vis: Visibility::Public, 748 modifiers: FieldModifiers, 749 ty: Type::Path { 750 path: Path { 751 segments: [ 752 PathSegment { 753 ident: "String", 754 }, 755 ], 756 }, 757 }, 758 }, 759 ] 760 "#); 761 } 762 763 #[test] 764 fn test_ambiguous_crate() { 765 let input = quote! { 766 // The field type is `(crate::X)` not `crate (::X)`. 767 struct S(crate::X); 768 }; 769 770 snapshot!(input as DeriveInput, @r#" 771 DeriveInput { 772 vis: Visibility::Inherited, 773 ident: "S", 774 generics: Generics, 775 data: Data::Struct { 776 fields: Fields::Unnamed { 777 unnamed: [ 778 Field { 779 vis: Visibility::Inherited, 780 modifiers: FieldModifiers, 781 ty: Type::Path { 782 path: Path { 783 segments: [ 784 PathSegment { 785 ident: "crate", 786 }, 787 Token![::], 788 PathSegment { 789 ident: "X", 790 }, 791 ], 792 }, 793 }, 794 }, 795 ], 796 }, 797 semi_token: Some, 798 }, 799 } 800 "#); 801 }