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(), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 52 | paren_token: Default::default(), |
| 53 | }.into(), |
| 54 | ), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 55 | colon_token: Some(Default::default()), |
| 56 | eq_token: Default::default(), |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 57 | }), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 58 | ].into(), |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 59 | where_clause: WhereClause { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 60 | where_token: Some(Default::default()), |
| 61 | predicates: vec![ |
| 62 | WherePredicate::BoundPredicate(WhereBoundPredicate { |
| 63 | bound_lifetimes: None, |
| 64 | colon_token: Default::default(), |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 65 | bounded_ty: TypePath { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 66 | qself: None, |
| 67 | path: "T".into(), |
| 68 | }.into(), |
| 69 | bounds: vec![ |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 70 | TypeParamBound::Trait( |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 71 | PolyTraitRef { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 72 | bound_lifetimes: None, |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 73 | trait_ref: "Debug".into(), |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 74 | }, |
| 75 | TraitBoundModifier::None, |
| 76 | ), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 77 | ].into(), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 78 | }), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 79 | ].into(), |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 80 | }, |
| 81 | }; |
| 82 | |
| 83 | let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 84 | let tokens = quote! { |
| 85 | impl #impl_generics MyTrait for Test #ty_generics #where_clause {} |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 86 | }; |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 87 | let expected = concat!( |
| 88 | "impl < 'a , 'b : 'a , # [ may_dangle ] T : 'a > ", |
| 89 | "MyTrait for Test < 'a , 'b , T > ", |
| 90 | "where T : Debug { }" |
| 91 | ); |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 92 | assert_eq!(expected, tokens.to_string()); |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 93 | |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 94 | let turbofish = ty_generics.as_turbofish(); |
| 95 | let tokens = quote! { |
| 96 | Test #turbofish |
| 97 | }; |
| 98 | let expected = "Test :: < 'a , 'b , T >"; |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 99 | assert_eq!(expected, tokens.to_string()); |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 100 | } |
David Tolnay | 23d83f9 | 2017-01-25 15:41:47 -0800 | [diff] [blame] | 101 | |
| 102 | #[test] |
| 103 | fn test_ty_param_bound() { |
| 104 | let tokens = quote!('a); |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 105 | let expected = TypeParamBound::Region(Lifetime::new(Term::intern("'a"), Span::default())); |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 106 | assert_eq!( |
| 107 | expected, |
| 108 | common::parse::syn::<TypeParamBound>(tokens.into()) |
| 109 | ); |
David Tolnay | 23d83f9 | 2017-01-25 15:41:47 -0800 | [diff] [blame] | 110 | |
| 111 | let tokens = quote!(Debug); |
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: "Debug".into(), |
| 116 | }, |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 117 | TraitBoundModifier::None, |
| 118 | ); |
| 119 | assert_eq!( |
| 120 | expected, |
| 121 | common::parse::syn::<TypeParamBound>(tokens.into()) |
| 122 | ); |
David Tolnay | 23d83f9 | 2017-01-25 15:41:47 -0800 | [diff] [blame] | 123 | |
| 124 | let tokens = quote!(?Sized); |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 125 | let expected = TypeParamBound::Trait( |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 126 | PolyTraitRef { |
| 127 | bound_lifetimes: None, |
| 128 | trait_ref: "Sized".into(), |
| 129 | }, |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 130 | TraitBoundModifier::Maybe(Default::default()), |
| 131 | ); |
| 132 | assert_eq!( |
| 133 | expected, |
| 134 | common::parse::syn::<TypeParamBound>(tokens.into()) |
| 135 | ); |
David Tolnay | 23d83f9 | 2017-01-25 15:41:47 -0800 | [diff] [blame] | 136 | } |