Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 1 | #![cfg(feature = "extra-traits")] |
| 2 | |
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 | c5f1a65 | 2017-12-27 02:10:10 -0500 | [diff] [blame^] | 12 | use proc_macro2::{Span, Term, TokenStream}; |
Alex Crichton | 605643b | 2017-07-05 18:35:14 -0700 | [diff] [blame] | 13 | |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 14 | mod common; |
| 15 | |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 16 | #[test] |
| 17 | fn test_split_for_impl() { |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 18 | // <'a, 'b: 'a, #[may_dangle] T: 'a = ()> where T: Debug |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 19 | let generics = Generics { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 20 | gt_token: Some(Default::default()), |
| 21 | lt_token: Some(Default::default()), |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 22 | params: vec![ |
| 23 | GenericParam::Lifetime(LifetimeDef { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 24 | attrs: Default::default(), |
Alex Crichton | 605643b | 2017-07-05 18:35:14 -0700 | [diff] [blame] | 25 | lifetime: Lifetime::new(Term::intern("'a"), Span::default()), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 26 | bounds: Default::default(), |
| 27 | colon_token: None, |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 28 | }), |
| 29 | GenericParam::Lifetime(LifetimeDef { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 30 | attrs: Default::default(), |
Alex Crichton | 605643b | 2017-07-05 18:35:14 -0700 | [diff] [blame] | 31 | lifetime: Lifetime::new(Term::intern("'b"), Span::default()), |
| 32 | bounds: vec![Lifetime::new(Term::intern("'a"), Span::default())].into(), |
David Tolnay | 42eaae1 | 2017-12-26 23:05:18 -0500 | [diff] [blame] | 33 | colon_token: Some(token::Colon::default()), |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 34 | }), |
| 35 | GenericParam::Type(TypeParam { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 36 | attrs: vec![Attribute { |
| 37 | bracket_token: Default::default(), |
| 38 | pound_token: Default::default(), |
| 39 | style: AttrStyle::Outer, |
| 40 | path: "may_dangle".into(), |
David Tolnay | c5f1a65 | 2017-12-27 02:10:10 -0500 | [diff] [blame^] | 41 | tts: TokenStream::empty(), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 42 | is_sugared_doc: false, |
| 43 | }], |
| 44 | ident: "T".into(), |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 45 | bounds: vec![TypeParamBound::Region(Lifetime::new(Term::intern("'a"), Span::default()))].into(), |
David Tolnay | d079773 | 2017-12-26 01:46:12 -0500 | [diff] [blame] | 46 | default: Some(TypeTuple { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 47 | tys: Default::default(), |
| 48 | lone_comma: None, |
| 49 | paren_token: Default::default(), |
| 50 | }.into()), |
| 51 | colon_token: Some(Default::default()), |
| 52 | eq_token: Default::default(), |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 53 | }), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 54 | ].into(), |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 55 | where_clause: WhereClause { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 56 | where_token: Some(Default::default()), |
| 57 | predicates: vec![ |
| 58 | WherePredicate::BoundPredicate(WhereBoundPredicate { |
| 59 | bound_lifetimes: None, |
| 60 | colon_token: Default::default(), |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 61 | bounded_ty: TypePath { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 62 | qself: None, |
| 63 | path: "T".into(), |
| 64 | }.into(), |
| 65 | bounds: vec![ |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 66 | TypeParamBound::Trait( |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 67 | PolyTraitRef { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 68 | bound_lifetimes: None, |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 69 | trait_ref: "Debug".into(), |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 70 | }, |
| 71 | TraitBoundModifier::None, |
| 72 | ), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 73 | ].into(), |
| 74 | }) |
| 75 | ].into(), |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 76 | }, |
| 77 | }; |
| 78 | |
| 79 | let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 80 | let tokens = quote! { |
| 81 | impl #impl_generics MyTrait for Test #ty_generics #where_clause {} |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 82 | }; |
David Tolnay | 93413a5 | 2016-10-24 13:02:36 -0700 | [diff] [blame] | 83 | let expected = concat!("impl < 'a , 'b : 'a , # [ may_dangle ] T : 'a > ", |
| 84 | "MyTrait for Test < 'a , 'b , T > ", |
| 85 | "where T : Debug { }"); |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 86 | assert_eq!(expected, tokens.to_string()); |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 87 | |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 88 | let turbofish = ty_generics.as_turbofish(); |
| 89 | let tokens = quote! { |
| 90 | Test #turbofish |
| 91 | }; |
| 92 | let expected = "Test :: < 'a , 'b , T >"; |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 93 | assert_eq!(expected, tokens.to_string()); |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 94 | } |
David Tolnay | 23d83f9 | 2017-01-25 15:41:47 -0800 | [diff] [blame] | 95 | |
| 96 | #[test] |
| 97 | fn test_ty_param_bound() { |
| 98 | let tokens = quote!('a); |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 99 | let expected = TypeParamBound::Region(Lifetime::new(Term::intern("'a"), Span::default())); |
| 100 | assert_eq!(expected, common::parse::syn::<TypeParamBound>(tokens.into())); |
David Tolnay | 23d83f9 | 2017-01-25 15:41:47 -0800 | [diff] [blame] | 101 | |
| 102 | let tokens = quote!(Debug); |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 103 | let expected = TypeParamBound::Trait( |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 104 | PolyTraitRef { |
| 105 | bound_lifetimes: None, |
| 106 | trait_ref: "Debug".into(), |
| 107 | }, |
| 108 | TraitBoundModifier::None); |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 109 | assert_eq!(expected, common::parse::syn::<TypeParamBound>(tokens.into())); |
David Tolnay | 23d83f9 | 2017-01-25 15:41:47 -0800 | [diff] [blame] | 110 | |
| 111 | let tokens = quote!(?Sized); |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 112 | let expected = TypeParamBound::Trait( |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 113 | PolyTraitRef { |
| 114 | bound_lifetimes: None, |
| 115 | trait_ref: "Sized".into(), |
| 116 | }, |
| 117 | TraitBoundModifier::Maybe(Default::default())); |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 118 | assert_eq!(expected, common::parse::syn::<TypeParamBound>(tokens.into())); |
David Tolnay | 23d83f9 | 2017-01-25 15:41:47 -0800 | [diff] [blame] | 119 | } |