Debian Codegraph @

third_party/rust/syn/tests/test_derive_input.rs

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