David Tolnay | ecd024d | 2018-07-21 09:07:56 -0700 | [diff] [blame] | 1 | #![recursion_limit = "1024"] |
Nika Layzell | a2a1a4a | 2017-11-19 11:33:17 -0500 | [diff] [blame] | 2 | |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 3 | extern crate syn; |
| 4 | use syn::*; |
| 5 | |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 6 | #[macro_use] |
| 7 | extern crate quote; |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 8 | |
Alex Crichton | 605643b | 2017-07-05 18:35:14 -0700 | [diff] [blame] | 9 | extern crate proc_macro2; |
David Tolnay | 65fb566 | 2018-05-20 20:02:28 -0700 | [diff] [blame] | 10 | use proc_macro2::{Ident, Span, TokenStream}; |
Alex Crichton | 605643b | 2017-07-05 18:35:14 -0700 | [diff] [blame] | 11 | |
David Tolnay | dd12556 | 2017-12-31 02:16:22 -0500 | [diff] [blame] | 12 | #[macro_use] |
| 13 | mod macros; |
| 14 | |
David Tolnay | c3f9856 | 2018-11-02 08:55:05 -0700 | [diff] [blame] | 15 | mod features; |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 16 | |
Alex Crichton | eed4bc7 | 2018-05-17 10:59:15 -0700 | [diff] [blame] | 17 | fn ident(s: &str) -> Ident { |
| 18 | Ident::new(s, Span::call_site()) |
| 19 | } |
| 20 | |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 21 | #[test] |
| 22 | fn test_split_for_impl() { |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 23 | // <'a, 'b: 'a, #[may_dangle] T: 'a = ()> where T: Debug |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 24 | let generics = Generics { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 25 | gt_token: Some(Default::default()), |
| 26 | lt_token: Some(Default::default()), |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 27 | params: punctuated![ |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 28 | GenericParam::Lifetime(LifetimeDef { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 29 | attrs: Default::default(), |
David Tolnay | e2099f3 | 2018-03-31 18:42:43 +0200 | [diff] [blame] | 30 | lifetime: Lifetime::new("'a", Span::call_site()), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 31 | bounds: Default::default(), |
| 32 | colon_token: None, |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 33 | }), |
| 34 | GenericParam::Lifetime(LifetimeDef { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 35 | attrs: Default::default(), |
David Tolnay | e2099f3 | 2018-03-31 18:42:43 +0200 | [diff] [blame] | 36 | lifetime: Lifetime::new("'b", Span::call_site()), |
| 37 | bounds: punctuated![Lifetime::new("'a", Span::call_site())], |
David Tolnay | 42eaae1 | 2017-12-26 23:05:18 -0500 | [diff] [blame] | 38 | colon_token: Some(token::Colon::default()), |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 39 | }), |
| 40 | GenericParam::Type(TypeParam { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 41 | attrs: vec![Attribute { |
| 42 | bracket_token: Default::default(), |
| 43 | pound_token: Default::default(), |
| 44 | style: AttrStyle::Outer, |
Alex Crichton | eed4bc7 | 2018-05-17 10:59:15 -0700 | [diff] [blame] | 45 | path: ident("may_dangle").into(), |
hcpl | aa51179 | 2018-05-29 07:13:01 +0300 | [diff] [blame] | 46 | tts: TokenStream::new(), |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 47 | }], |
Alex Crichton | eed4bc7 | 2018-05-17 10:59:15 -0700 | [diff] [blame] | 48 | ident: ident("T"), |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 49 | bounds: punctuated![TypeParamBound::Lifetime(Lifetime::new( |
| 50 | "'a", |
| 51 | Span::call_site() |
| 52 | )),], |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 53 | default: Some( |
| 54 | TypeTuple { |
David Tolnay | eadbda3 | 2017-12-29 02:33:47 -0500 | [diff] [blame] | 55 | elems: Default::default(), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 56 | paren_token: Default::default(), |
David Tolnay | fb84fc0 | 2018-10-02 21:01:30 -0700 | [diff] [blame] | 57 | } |
| 58 | .into(), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 59 | ), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 60 | colon_token: Some(Default::default()), |
| 61 | eq_token: Default::default(), |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 62 | }), |
David Tolnay | dd12556 | 2017-12-31 02:16:22 -0500 | [diff] [blame] | 63 | ], |
David Tolnay | ac997dd | 2017-12-27 23:18:22 -0500 | [diff] [blame] | 64 | where_clause: Some(WhereClause { |
| 65 | where_token: Default::default(), |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 66 | predicates: punctuated![WherePredicate::Type(PredicateType { |
| 67 | lifetimes: None, |
| 68 | colon_token: Default::default(), |
| 69 | bounded_ty: TypePath { |
| 70 | qself: None, |
Alex Crichton | eed4bc7 | 2018-05-17 10:59:15 -0700 | [diff] [blame] | 71 | path: ident("T").into(), |
David Tolnay | fb84fc0 | 2018-10-02 21:01:30 -0700 | [diff] [blame] | 72 | } |
| 73 | .into(), |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 74 | bounds: punctuated![TypeParamBound::Trait(TraitBound { |
| 75 | paren_token: None, |
| 76 | modifier: TraitBoundModifier::None, |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 77 | lifetimes: None, |
Alex Crichton | eed4bc7 | 2018-05-17 10:59:15 -0700 | [diff] [blame] | 78 | path: ident("Debug").into(), |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 79 | }),], |
| 80 | }),], |
David Tolnay | ac997dd | 2017-12-27 23:18:22 -0500 | [diff] [blame] | 81 | }), |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 85 | let tokens = quote! { |
| 86 | impl #impl_generics MyTrait for Test #ty_generics #where_clause {} |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 87 | }; |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 88 | let expected = concat!( |
| 89 | "impl < 'a , 'b : 'a , # [ may_dangle ] T : 'a > ", |
| 90 | "MyTrait for Test < 'a , 'b , T > ", |
| 91 | "where T : Debug { }" |
| 92 | ); |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 93 | assert_eq!(expected, tokens.to_string()); |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 94 | |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 95 | let turbofish = ty_generics.as_turbofish(); |
| 96 | let tokens = quote! { |
| 97 | Test #turbofish |
| 98 | }; |
| 99 | let expected = "Test :: < 'a , 'b , T >"; |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 100 | assert_eq!(expected, tokens.to_string()); |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 101 | } |
David Tolnay | 23d83f9 | 2017-01-25 15:41:47 -0800 | [diff] [blame] | 102 | |
| 103 | #[test] |
| 104 | fn test_ty_param_bound() { |
| 105 | let tokens = quote!('a); |
David Tolnay | e2099f3 | 2018-03-31 18:42:43 +0200 | [diff] [blame] | 106 | let expected = TypeParamBound::Lifetime(Lifetime::new("'a", Span::call_site())); |
David Tolnay | 73b7ca1 | 2018-08-30 21:05:13 -0700 | [diff] [blame] | 107 | assert_eq!(expected, syn::parse2::<TypeParamBound>(tokens).unwrap()); |
David Tolnay | 23d83f9 | 2017-01-25 15:41:47 -0800 | [diff] [blame] | 108 | |
Bastien Orivel | 340553a | 2018-02-15 23:57:38 +0100 | [diff] [blame] | 109 | let tokens = quote!('_); |
Alex Crichton | eed4bc7 | 2018-05-17 10:59:15 -0700 | [diff] [blame] | 110 | println!("{:?}", tokens); |
David Tolnay | e2099f3 | 2018-03-31 18:42:43 +0200 | [diff] [blame] | 111 | let expected = TypeParamBound::Lifetime(Lifetime::new("'_", Span::call_site())); |
David Tolnay | 73b7ca1 | 2018-08-30 21:05:13 -0700 | [diff] [blame] | 112 | assert_eq!(expected, syn::parse2::<TypeParamBound>(tokens).unwrap()); |
Bastien Orivel | 340553a | 2018-02-15 23:57:38 +0100 | [diff] [blame] | 113 | |
David Tolnay | 23d83f9 | 2017-01-25 15:41:47 -0800 | [diff] [blame] | 114 | let tokens = quote!(Debug); |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 115 | let expected = TypeParamBound::Trait(TraitBound { |
David Tolnay | c1f5d5d | 2018-03-31 22:17:56 +0200 | [diff] [blame] | 116 | paren_token: None, |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 117 | modifier: TraitBoundModifier::None, |
| 118 | lifetimes: None, |
Alex Crichton | eed4bc7 | 2018-05-17 10:59:15 -0700 | [diff] [blame] | 119 | path: ident("Debug").into(), |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 120 | }); |
David Tolnay | 73b7ca1 | 2018-08-30 21:05:13 -0700 | [diff] [blame] | 121 | assert_eq!(expected, syn::parse2::<TypeParamBound>(tokens).unwrap()); |
David Tolnay | 23d83f9 | 2017-01-25 15:41:47 -0800 | [diff] [blame] | 122 | |
| 123 | let tokens = quote!(?Sized); |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 124 | let expected = TypeParamBound::Trait(TraitBound { |
David Tolnay | c1f5d5d | 2018-03-31 22:17:56 +0200 | [diff] [blame] | 125 | paren_token: None, |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 126 | modifier: TraitBoundModifier::Maybe(Default::default()), |
| 127 | lifetimes: None, |
Alex Crichton | eed4bc7 | 2018-05-17 10:59:15 -0700 | [diff] [blame] | 128 | path: ident("Sized").into(), |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 129 | }); |
David Tolnay | 73b7ca1 | 2018-08-30 21:05:13 -0700 | [diff] [blame] | 130 | assert_eq!(expected, syn::parse2::<TypeParamBound>(tokens).unwrap()); |
David Tolnay | 23d83f9 | 2017-01-25 15:41:47 -0800 | [diff] [blame] | 131 | } |
Geoffry Song | ac02b18 | 2018-05-19 22:11:31 -0700 | [diff] [blame] | 132 | |
| 133 | #[test] |
| 134 | fn test_fn_precedence_in_where_clause() { |
David Tolnay | c8b0e0f | 2019-03-07 22:46:32 -0800 | [diff] [blame] | 135 | // This should parse as two separate bounds, `FnOnce() -> i32` and `Send` - not |
| 136 | // `FnOnce() -> (i32 + Send)`. |
David Tolnay | 65fb566 | 2018-05-20 20:02:28 -0700 | [diff] [blame] | 137 | let sig = quote! { |
| 138 | fn f<G>() |
| 139 | where |
| 140 | G: FnOnce() -> i32 + Send, |
| 141 | { |
| 142 | } |
| 143 | }; |
David Tolnay | 59ffad9 | 2018-08-30 21:01:04 -0700 | [diff] [blame] | 144 | let fun = syn::parse2::<ItemFn>(sig).unwrap(); |
Geoffry Song | ac02b18 | 2018-05-19 22:11:31 -0700 | [diff] [blame] | 145 | let where_clause = fun.decl.generics.where_clause.as_ref().unwrap(); |
| 146 | assert_eq!(where_clause.predicates.len(), 1); |
| 147 | let predicate = match where_clause.predicates[0] { |
| 148 | WherePredicate::Type(ref pred) => pred, |
| 149 | _ => panic!("wrong predicate kind"), |
| 150 | }; |
| 151 | assert_eq!(predicate.bounds.len(), 2, "{:#?}", predicate.bounds); |
| 152 | let first_bound = &predicate.bounds[0]; |
| 153 | assert_eq!(quote!(#first_bound).to_string(), "FnOnce ( ) -> i32"); |
| 154 | let second_bound = &predicate.bounds[1]; |
| 155 | assert_eq!(quote!(#second_bound).to_string(), "Send"); |
| 156 | } |
David Tolnay | 38012de | 2018-09-02 13:32:47 -0700 | [diff] [blame] | 157 | |
| 158 | #[test] |
| 159 | fn test_where_clause_at_end_of_input() { |
| 160 | let tokens = quote! { |
| 161 | where |
| 162 | }; |
| 163 | let where_clause = syn::parse2::<WhereClause>(tokens).unwrap(); |
| 164 | assert_eq!(where_clause.predicates.len(), 0); |
| 165 | } |