test_derive_input.rs 24 KB

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