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