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