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