blob: 841d6c633011989cc88ad16d5d5d54874f7b7907 [file] [log] [blame]
David Tolnayecd024d2018-07-21 09:07:56 -07001#![recursion_limit = "1024"]
Nika Layzella2a1a4a2017-11-19 11:33:17 -05002
David Tolnayb153dbc2016-10-04 23:39:10 -07003extern crate syn;
4use syn::*;
5
David Tolnaye7678922016-10-13 20:44:03 -07006#[macro_use]
7extern crate quote;
David Tolnayb153dbc2016-10-04 23:39:10 -07008
Alex Crichton605643b2017-07-05 18:35:14 -07009extern crate proc_macro2;
David Tolnay65fb5662018-05-20 20:02:28 -070010use proc_macro2::{Ident, Span, TokenStream};
Alex Crichton605643b2017-07-05 18:35:14 -070011
David Tolnaydd125562017-12-31 02:16:22 -050012#[macro_use]
13mod macros;
14
David Tolnayc3f98562018-11-02 08:55:05 -070015mod features;
David Tolnayc7a5d3d2017-06-04 12:11:05 -070016
Alex Crichtoneed4bc72018-05-17 10:59:15 -070017fn ident(s: &str) -> Ident {
18 Ident::new(s, Span::call_site())
19}
20
David Tolnayb153dbc2016-10-04 23:39:10 -070021#[test]
22fn test_split_for_impl() {
David Tolnaye7678922016-10-13 20:44:03 -070023 // <'a, 'b: 'a, #[may_dangle] T: 'a = ()> where T: Debug
David Tolnayc2f1aba2017-11-12 20:29:22 -080024 let generics = Generics {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070025 gt_token: Some(Default::default()),
26 lt_token: Some(Default::default()),
David Tolnayf2cfd722017-12-31 18:02:51 -050027 params: punctuated![
David Tolnayc2f1aba2017-11-12 20:29:22 -080028 GenericParam::Lifetime(LifetimeDef {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070029 attrs: Default::default(),
David Tolnaye2099f32018-03-31 18:42:43 +020030 lifetime: Lifetime::new("'a", Span::call_site()),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070031 bounds: Default::default(),
32 colon_token: None,
David Tolnayc2f1aba2017-11-12 20:29:22 -080033 }),
34 GenericParam::Lifetime(LifetimeDef {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070035 attrs: Default::default(),
David Tolnaye2099f32018-03-31 18:42:43 +020036 lifetime: Lifetime::new("'b", Span::call_site()),
37 bounds: punctuated![Lifetime::new("'a", Span::call_site())],
David Tolnay42eaae12017-12-26 23:05:18 -050038 colon_token: Some(token::Colon::default()),
David Tolnayc2f1aba2017-11-12 20:29:22 -080039 }),
40 GenericParam::Type(TypeParam {
David Tolnay94d2b792018-04-29 12:26:10 -070041 attrs: vec![Attribute {
42 bracket_token: Default::default(),
43 pound_token: Default::default(),
44 style: AttrStyle::Outer,
Alex Crichtoneed4bc72018-05-17 10:59:15 -070045 path: ident("may_dangle").into(),
hcplaa511792018-05-29 07:13:01 +030046 tts: TokenStream::new(),
David Tolnay94d2b792018-04-29 12:26:10 -070047 }],
Alex Crichtoneed4bc72018-05-17 10:59:15 -070048 ident: ident("T"),
David Tolnay94d2b792018-04-29 12:26:10 -070049 bounds: punctuated![TypeParamBound::Lifetime(Lifetime::new(
50 "'a",
51 Span::call_site()
52 )),],
David Tolnay51382052017-12-27 13:46:21 -050053 default: Some(
54 TypeTuple {
David Tolnayeadbda32017-12-29 02:33:47 -050055 elems: Default::default(),
David Tolnay51382052017-12-27 13:46:21 -050056 paren_token: Default::default(),
David Tolnayfb84fc02018-10-02 21:01:30 -070057 }
58 .into(),
David Tolnay51382052017-12-27 13:46:21 -050059 ),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070060 colon_token: Some(Default::default()),
61 eq_token: Default::default(),
David Tolnayc2f1aba2017-11-12 20:29:22 -080062 }),
David Tolnaydd125562017-12-31 02:16:22 -050063 ],
David Tolnayac997dd2017-12-27 23:18:22 -050064 where_clause: Some(WhereClause {
65 where_token: Default::default(),
David Tolnay94d2b792018-04-29 12:26:10 -070066 predicates: punctuated![WherePredicate::Type(PredicateType {
67 lifetimes: None,
68 colon_token: Default::default(),
69 bounded_ty: TypePath {
70 qself: None,
Alex Crichtoneed4bc72018-05-17 10:59:15 -070071 path: ident("T").into(),
David Tolnayfb84fc02018-10-02 21:01:30 -070072 }
73 .into(),
David Tolnay94d2b792018-04-29 12:26:10 -070074 bounds: punctuated![TypeParamBound::Trait(TraitBound {
75 paren_token: None,
76 modifier: TraitBoundModifier::None,
David Tolnay40fb8ce2018-01-02 10:53:46 -080077 lifetimes: None,
Alex Crichtoneed4bc72018-05-17 10:59:15 -070078 path: ident("Debug").into(),
David Tolnay94d2b792018-04-29 12:26:10 -070079 }),],
80 }),],
David Tolnayac997dd2017-12-27 23:18:22 -050081 }),
David Tolnayb153dbc2016-10-04 23:39:10 -070082 };
83
84 let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
David Tolnaye7678922016-10-13 20:44:03 -070085 let tokens = quote! {
86 impl #impl_generics MyTrait for Test #ty_generics #where_clause {}
David Tolnayb153dbc2016-10-04 23:39:10 -070087 };
David Tolnay51382052017-12-27 13:46:21 -050088 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 Tolnayc879a502017-01-25 15:51:32 -080093 assert_eq!(expected, tokens.to_string());
David Tolnayb153dbc2016-10-04 23:39:10 -070094
David Tolnayc879a502017-01-25 15:51:32 -080095 let turbofish = ty_generics.as_turbofish();
96 let tokens = quote! {
97 Test #turbofish
98 };
99 let expected = "Test :: < 'a , 'b , T >";
David Tolnaye7678922016-10-13 20:44:03 -0700100 assert_eq!(expected, tokens.to_string());
David Tolnayb153dbc2016-10-04 23:39:10 -0700101}
David Tolnay23d83f92017-01-25 15:41:47 -0800102
103#[test]
104fn test_ty_param_bound() {
105 let tokens = quote!('a);
David Tolnaye2099f32018-03-31 18:42:43 +0200106 let expected = TypeParamBound::Lifetime(Lifetime::new("'a", Span::call_site()));
David Tolnay73b7ca12018-08-30 21:05:13 -0700107 assert_eq!(expected, syn::parse2::<TypeParamBound>(tokens).unwrap());
David Tolnay23d83f92017-01-25 15:41:47 -0800108
Bastien Orivel340553a2018-02-15 23:57:38 +0100109 let tokens = quote!('_);
Alex Crichtoneed4bc72018-05-17 10:59:15 -0700110 println!("{:?}", tokens);
David Tolnaye2099f32018-03-31 18:42:43 +0200111 let expected = TypeParamBound::Lifetime(Lifetime::new("'_", Span::call_site()));
David Tolnay73b7ca12018-08-30 21:05:13 -0700112 assert_eq!(expected, syn::parse2::<TypeParamBound>(tokens).unwrap());
Bastien Orivel340553a2018-02-15 23:57:38 +0100113
David Tolnay23d83f92017-01-25 15:41:47 -0800114 let tokens = quote!(Debug);
David Tolnay40fb8ce2018-01-02 10:53:46 -0800115 let expected = TypeParamBound::Trait(TraitBound {
David Tolnayc1f5d5d2018-03-31 22:17:56 +0200116 paren_token: None,
David Tolnay40fb8ce2018-01-02 10:53:46 -0800117 modifier: TraitBoundModifier::None,
118 lifetimes: None,
Alex Crichtoneed4bc72018-05-17 10:59:15 -0700119 path: ident("Debug").into(),
David Tolnay40fb8ce2018-01-02 10:53:46 -0800120 });
David Tolnay73b7ca12018-08-30 21:05:13 -0700121 assert_eq!(expected, syn::parse2::<TypeParamBound>(tokens).unwrap());
David Tolnay23d83f92017-01-25 15:41:47 -0800122
123 let tokens = quote!(?Sized);
David Tolnay40fb8ce2018-01-02 10:53:46 -0800124 let expected = TypeParamBound::Trait(TraitBound {
David Tolnayc1f5d5d2018-03-31 22:17:56 +0200125 paren_token: None,
David Tolnay40fb8ce2018-01-02 10:53:46 -0800126 modifier: TraitBoundModifier::Maybe(Default::default()),
127 lifetimes: None,
Alex Crichtoneed4bc72018-05-17 10:59:15 -0700128 path: ident("Sized").into(),
David Tolnay40fb8ce2018-01-02 10:53:46 -0800129 });
David Tolnay73b7ca12018-08-30 21:05:13 -0700130 assert_eq!(expected, syn::parse2::<TypeParamBound>(tokens).unwrap());
David Tolnay23d83f92017-01-25 15:41:47 -0800131}
Geoffry Songac02b182018-05-19 22:11:31 -0700132
133#[test]
134fn test_fn_precedence_in_where_clause() {
David Tolnayc8b0e0f2019-03-07 22:46:32 -0800135 // This should parse as two separate bounds, `FnOnce() -> i32` and `Send` - not
136 // `FnOnce() -> (i32 + Send)`.
David Tolnay65fb5662018-05-20 20:02:28 -0700137 let sig = quote! {
138 fn f<G>()
139 where
140 G: FnOnce() -> i32 + Send,
141 {
142 }
143 };
David Tolnay59ffad92018-08-30 21:01:04 -0700144 let fun = syn::parse2::<ItemFn>(sig).unwrap();
Geoffry Songac02b182018-05-19 22:11:31 -0700145 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 Tolnay38012de2018-09-02 13:32:47 -0700157
158#[test]
159fn 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}