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