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