David Tolnay | 5553501 | 2018-01-05 16:39:23 -0800 | [diff] [blame^] | 1 | // Copyright 2018 Syn Developers |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 4 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 5 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 6 | // option. This file may not be copied, modified, or distributed |
| 7 | // except according to those terms. |
| 8 | |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 9 | #![cfg(feature = "extra-traits")] |
Nika Layzell | a2a1a4a | 2017-11-19 11:33:17 -0500 | [diff] [blame] | 10 | #![feature(rustc_private)] |
| 11 | |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 12 | extern crate syn; |
| 13 | use syn::*; |
| 14 | |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 15 | #[macro_use] |
| 16 | extern crate quote; |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 17 | |
Alex Crichton | 605643b | 2017-07-05 18:35:14 -0700 | [diff] [blame] | 18 | extern crate proc_macro2; |
David Tolnay | c5f1a65 | 2017-12-27 02:10:10 -0500 | [diff] [blame] | 19 | use proc_macro2::{Span, Term, TokenStream}; |
Alex Crichton | 605643b | 2017-07-05 18:35:14 -0700 | [diff] [blame] | 20 | |
David Tolnay | dd12556 | 2017-12-31 02:16:22 -0500 | [diff] [blame] | 21 | #[macro_use] |
| 22 | mod macros; |
| 23 | |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 24 | mod common; |
| 25 | |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 26 | #[test] |
| 27 | fn test_split_for_impl() { |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 28 | // <'a, 'b: 'a, #[may_dangle] T: 'a = ()> where T: Debug |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 29 | let generics = Generics { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 30 | gt_token: Some(Default::default()), |
| 31 | lt_token: Some(Default::default()), |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 32 | params: punctuated![ |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 33 | GenericParam::Lifetime(LifetimeDef { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 34 | attrs: Default::default(), |
Alex Crichton | 605643b | 2017-07-05 18:35:14 -0700 | [diff] [blame] | 35 | lifetime: Lifetime::new(Term::intern("'a"), Span::default()), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 36 | bounds: Default::default(), |
| 37 | colon_token: None, |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 38 | }), |
| 39 | GenericParam::Lifetime(LifetimeDef { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 40 | attrs: Default::default(), |
Alex Crichton | 605643b | 2017-07-05 18:35:14 -0700 | [diff] [blame] | 41 | lifetime: Lifetime::new(Term::intern("'b"), Span::default()), |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 42 | bounds: punctuated![Lifetime::new(Term::intern("'a"), Span::default())], |
David Tolnay | 42eaae1 | 2017-12-26 23:05:18 -0500 | [diff] [blame] | 43 | colon_token: Some(token::Colon::default()), |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 44 | }), |
| 45 | GenericParam::Type(TypeParam { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 46 | attrs: vec![ |
| 47 | Attribute { |
| 48 | bracket_token: Default::default(), |
| 49 | pound_token: Default::default(), |
| 50 | style: AttrStyle::Outer, |
| 51 | path: "may_dangle".into(), |
| 52 | tts: TokenStream::empty(), |
| 53 | is_sugared_doc: false, |
| 54 | }, |
| 55 | ], |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 56 | ident: "T".into(), |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 57 | bounds: punctuated![ |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 58 | TypeParamBound::Lifetime(Lifetime::new(Term::intern("'a"), Span::default())), |
David Tolnay | dd12556 | 2017-12-31 02:16:22 -0500 | [diff] [blame] | 59 | ], |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 60 | default: Some( |
| 61 | TypeTuple { |
David Tolnay | eadbda3 | 2017-12-29 02:33:47 -0500 | [diff] [blame] | 62 | elems: Default::default(), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 63 | paren_token: Default::default(), |
| 64 | }.into(), |
| 65 | ), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 66 | colon_token: Some(Default::default()), |
| 67 | eq_token: Default::default(), |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 68 | }), |
David Tolnay | dd12556 | 2017-12-31 02:16:22 -0500 | [diff] [blame] | 69 | ], |
David Tolnay | ac997dd | 2017-12-27 23:18:22 -0500 | [diff] [blame] | 70 | where_clause: Some(WhereClause { |
| 71 | where_token: Default::default(), |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 72 | predicates: punctuated![ |
David Tolnay | d4add85 | 2018-01-01 20:13:24 -0800 | [diff] [blame] | 73 | WherePredicate::Type(PredicateType { |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 74 | lifetimes: None, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 75 | colon_token: Default::default(), |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 76 | bounded_ty: TypePath { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 77 | qself: None, |
| 78 | path: "T".into(), |
| 79 | }.into(), |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 80 | bounds: punctuated![ |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 81 | TypeParamBound::Trait(TraitBound { |
| 82 | modifier: TraitBoundModifier::None, |
| 83 | lifetimes: None, |
| 84 | path: "Debug".into(), |
| 85 | }), |
David Tolnay | dd12556 | 2017-12-31 02:16:22 -0500 | [diff] [blame] | 86 | ], |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 87 | }), |
David Tolnay | dd12556 | 2017-12-31 02:16:22 -0500 | [diff] [blame] | 88 | ], |
David Tolnay | ac997dd | 2017-12-27 23:18:22 -0500 | [diff] [blame] | 89 | }), |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 93 | let tokens = quote! { |
| 94 | impl #impl_generics MyTrait for Test #ty_generics #where_clause {} |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 95 | }; |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 96 | let expected = concat!( |
| 97 | "impl < 'a , 'b : 'a , # [ may_dangle ] T : 'a > ", |
| 98 | "MyTrait for Test < 'a , 'b , T > ", |
| 99 | "where T : Debug { }" |
| 100 | ); |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 101 | assert_eq!(expected, tokens.to_string()); |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 102 | |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 103 | let turbofish = ty_generics.as_turbofish(); |
| 104 | let tokens = quote! { |
| 105 | Test #turbofish |
| 106 | }; |
| 107 | let expected = "Test :: < 'a , 'b , T >"; |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 108 | assert_eq!(expected, tokens.to_string()); |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 109 | } |
David Tolnay | 23d83f9 | 2017-01-25 15:41:47 -0800 | [diff] [blame] | 110 | |
| 111 | #[test] |
| 112 | fn test_ty_param_bound() { |
| 113 | let tokens = quote!('a); |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 114 | let expected = TypeParamBound::Lifetime(Lifetime::new(Term::intern("'a"), Span::default())); |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 115 | assert_eq!( |
| 116 | expected, |
| 117 | common::parse::syn::<TypeParamBound>(tokens.into()) |
| 118 | ); |
David Tolnay | 23d83f9 | 2017-01-25 15:41:47 -0800 | [diff] [blame] | 119 | |
| 120 | let tokens = quote!(Debug); |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 121 | let expected = TypeParamBound::Trait(TraitBound { |
| 122 | modifier: TraitBoundModifier::None, |
| 123 | lifetimes: None, |
| 124 | path: "Debug".into(), |
| 125 | }); |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 126 | assert_eq!( |
| 127 | expected, |
| 128 | common::parse::syn::<TypeParamBound>(tokens.into()) |
| 129 | ); |
David Tolnay | 23d83f9 | 2017-01-25 15:41:47 -0800 | [diff] [blame] | 130 | |
| 131 | let tokens = quote!(?Sized); |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 132 | let expected = TypeParamBound::Trait(TraitBound { |
| 133 | modifier: TraitBoundModifier::Maybe(Default::default()), |
| 134 | lifetimes: None, |
| 135 | path: "Sized".into(), |
| 136 | }); |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 137 | assert_eq!( |
| 138 | expected, |
| 139 | common::parse::syn::<TypeParamBound>(tokens.into()) |
| 140 | ); |
David Tolnay | 23d83f9 | 2017-01-25 15:41:47 -0800 | [diff] [blame] | 141 | } |