David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 1 | extern crate syn; |
| 2 | use syn::*; |
| 3 | |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 4 | #[macro_use] |
| 5 | extern crate quote; |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 6 | |
| 7 | #[test] |
| 8 | fn test_split_for_impl() { |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 9 | // <'a, 'b: 'a, #[may_dangle] T: 'a = ()> where T: Debug |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 10 | let generics = Generics { |
| 11 | lifetimes: vec![ |
| 12 | LifetimeDef { |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 13 | attrs: Vec::new(), |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 14 | lifetime: Lifetime::new("'a"), |
| 15 | bounds: Vec::new(), |
| 16 | }, |
| 17 | LifetimeDef { |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 18 | attrs: Vec::new(), |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 19 | lifetime: Lifetime::new("'b"), |
| 20 | bounds: vec![ |
| 21 | Lifetime::new("'a"), |
| 22 | ], |
| 23 | }, |
| 24 | ], |
| 25 | ty_params: vec![ |
| 26 | TyParam { |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 27 | attrs: vec![ |
| 28 | Attribute { |
| 29 | style: AttrStyle::Outer, |
| 30 | value: MetaItem::Word("may_dangle".into()), |
| 31 | is_sugared_doc: false, |
| 32 | }, |
| 33 | ], |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 34 | ident: Ident::new("T"), |
| 35 | bounds: vec![ |
| 36 | TyParamBound::Region(Lifetime::new("'a")), |
| 37 | ], |
| 38 | default: Some(Ty::Tup(Vec::new())), |
| 39 | }, |
| 40 | ], |
| 41 | where_clause: WhereClause { |
| 42 | predicates: vec![ |
| 43 | WherePredicate::BoundPredicate(WhereBoundPredicate { |
| 44 | bound_lifetimes: Vec::new(), |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 45 | bounded_ty: Ty::Path(None, "T".into()), |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 46 | bounds: vec![ |
| 47 | TyParamBound::Trait( |
| 48 | PolyTraitRef { |
| 49 | bound_lifetimes: Vec::new(), |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 50 | trait_ref: "Debug".into(), |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 51 | }, |
| 52 | TraitBoundModifier::None, |
| 53 | ), |
| 54 | ], |
| 55 | }), |
| 56 | ], |
| 57 | }, |
| 58 | }; |
| 59 | |
| 60 | let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); |
| 61 | |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 62 | let tokens = quote! { |
| 63 | impl #impl_generics MyTrait for Test #ty_generics #where_clause {} |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
David Tolnay | 93413a5 | 2016-10-24 13:02:36 -0700 | [diff] [blame] | 66 | let expected = concat!("impl < 'a , 'b : 'a , # [ may_dangle ] T : 'a > ", |
| 67 | "MyTrait for Test < 'a , 'b , T > ", |
| 68 | "where T : Debug { }"); |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 69 | |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 70 | assert_eq!(expected, tokens.to_string()); |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 71 | } |