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