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 | e2099f3 | 2018-03-31 18:42:43 +0200 | [diff] [blame] | 19 | use proc_macro2::{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 | |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 26 | #[test] |
| 27 | fn test_split_for_impl() { |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 28 | // <'a, 'b: 'a, #[may_dangle] T: 'a = ()> where T: Debug |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 29 | let generics = Generics { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 30 | gt_token: Some(Default::default()), |
| 31 | lt_token: Some(Default::default()), |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 32 | params: punctuated![ |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 33 | GenericParam::Lifetime(LifetimeDef { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 34 | attrs: Default::default(), |
David Tolnay | e2099f3 | 2018-03-31 18:42:43 +0200 | [diff] [blame] | 35 | lifetime: Lifetime::new("'a", Span::call_site()), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 36 | bounds: Default::default(), |
| 37 | colon_token: None, |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 38 | }), |
| 39 | GenericParam::Lifetime(LifetimeDef { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 40 | attrs: Default::default(), |
David Tolnay | e2099f3 | 2018-03-31 18:42:43 +0200 | [diff] [blame] | 41 | lifetime: Lifetime::new("'b", Span::call_site()), |
| 42 | bounds: punctuated![Lifetime::new("'a", Span::call_site())], |
David Tolnay | 42eaae1 | 2017-12-26 23:05:18 -0500 | [diff] [blame] | 43 | colon_token: Some(token::Colon::default()), |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 44 | }), |
| 45 | GenericParam::Type(TypeParam { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame^] | 46 | attrs: vec![Attribute { |
| 47 | bracket_token: Default::default(), |
| 48 | pound_token: Default::default(), |
| 49 | style: AttrStyle::Outer, |
| 50 | path: "may_dangle".into(), |
| 51 | tts: TokenStream::empty(), |
| 52 | is_sugared_doc: false, |
| 53 | }], |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 54 | ident: "T".into(), |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame^] | 55 | bounds: punctuated![TypeParamBound::Lifetime(Lifetime::new( |
| 56 | "'a", |
| 57 | Span::call_site() |
| 58 | )),], |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 59 | default: Some( |
| 60 | TypeTuple { |
David Tolnay | eadbda3 | 2017-12-29 02:33:47 -0500 | [diff] [blame] | 61 | elems: Default::default(), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 62 | paren_token: Default::default(), |
| 63 | }.into(), |
| 64 | ), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 65 | colon_token: Some(Default::default()), |
| 66 | eq_token: Default::default(), |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 67 | }), |
David Tolnay | dd12556 | 2017-12-31 02:16:22 -0500 | [diff] [blame] | 68 | ], |
David Tolnay | ac997dd | 2017-12-27 23:18:22 -0500 | [diff] [blame] | 69 | where_clause: Some(WhereClause { |
| 70 | where_token: Default::default(), |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame^] | 71 | predicates: punctuated![WherePredicate::Type(PredicateType { |
| 72 | lifetimes: None, |
| 73 | colon_token: Default::default(), |
| 74 | bounded_ty: TypePath { |
| 75 | qself: None, |
| 76 | path: "T".into(), |
| 77 | }.into(), |
| 78 | bounds: punctuated![TypeParamBound::Trait(TraitBound { |
| 79 | paren_token: None, |
| 80 | modifier: TraitBoundModifier::None, |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 81 | lifetimes: None, |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame^] | 82 | path: "Debug".into(), |
| 83 | }),], |
| 84 | }),], |
David Tolnay | ac997dd | 2017-12-27 23:18:22 -0500 | [diff] [blame] | 85 | }), |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 89 | let tokens = quote! { |
| 90 | impl #impl_generics MyTrait for Test #ty_generics #where_clause {} |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 91 | }; |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 92 | let expected = concat!( |
| 93 | "impl < 'a , 'b : 'a , # [ may_dangle ] T : 'a > ", |
| 94 | "MyTrait for Test < 'a , 'b , T > ", |
| 95 | "where T : Debug { }" |
| 96 | ); |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 97 | assert_eq!(expected, tokens.to_string()); |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 98 | |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 99 | let turbofish = ty_generics.as_turbofish(); |
| 100 | let tokens = quote! { |
| 101 | Test #turbofish |
| 102 | }; |
| 103 | let expected = "Test :: < 'a , 'b , T >"; |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 104 | assert_eq!(expected, tokens.to_string()); |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 105 | } |
David Tolnay | 23d83f9 | 2017-01-25 15:41:47 -0800 | [diff] [blame] | 106 | |
| 107 | #[test] |
| 108 | fn test_ty_param_bound() { |
| 109 | let tokens = quote!('a); |
David Tolnay | e2099f3 | 2018-03-31 18:42:43 +0200 | [diff] [blame] | 110 | let expected = TypeParamBound::Lifetime(Lifetime::new("'a", Span::call_site())); |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 111 | assert_eq!( |
| 112 | expected, |
| 113 | common::parse::syn::<TypeParamBound>(tokens.into()) |
| 114 | ); |
David Tolnay | 23d83f9 | 2017-01-25 15:41:47 -0800 | [diff] [blame] | 115 | |
Bastien Orivel | 340553a | 2018-02-15 23:57:38 +0100 | [diff] [blame] | 116 | let tokens = quote!('_); |
David Tolnay | e2099f3 | 2018-03-31 18:42:43 +0200 | [diff] [blame] | 117 | let expected = TypeParamBound::Lifetime(Lifetime::new("'_", Span::call_site())); |
Bastien Orivel | 340553a | 2018-02-15 23:57:38 +0100 | [diff] [blame] | 118 | assert_eq!( |
| 119 | expected, |
| 120 | common::parse::syn::<TypeParamBound>(tokens.into()) |
| 121 | ); |
| 122 | |
David Tolnay | 23d83f9 | 2017-01-25 15:41:47 -0800 | [diff] [blame] | 123 | let tokens = quote!(Debug); |
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::None, |
| 127 | lifetimes: None, |
| 128 | path: "Debug".into(), |
| 129 | }); |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 130 | assert_eq!( |
| 131 | expected, |
| 132 | common::parse::syn::<TypeParamBound>(tokens.into()) |
| 133 | ); |
David Tolnay | 23d83f9 | 2017-01-25 15:41:47 -0800 | [diff] [blame] | 134 | |
| 135 | let tokens = quote!(?Sized); |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 136 | let expected = TypeParamBound::Trait(TraitBound { |
David Tolnay | c1f5d5d | 2018-03-31 22:17:56 +0200 | [diff] [blame] | 137 | paren_token: None, |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 138 | modifier: TraitBoundModifier::Maybe(Default::default()), |
| 139 | lifetimes: None, |
| 140 | path: "Sized".into(), |
| 141 | }); |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 142 | assert_eq!( |
| 143 | expected, |
| 144 | common::parse::syn::<TypeParamBound>(tokens.into()) |
| 145 | ); |
David Tolnay | 23d83f9 | 2017-01-25 15:41:47 -0800 | [diff] [blame] | 146 | } |