| 1 |
#![allow( |
| 2 |
clippy::manual_let_else, |
| 3 |
clippy::needless_lifetimes, |
| 4 |
clippy::too_many_lines, |
| 5 |
clippy::uninlined_format_args |
| 6 |
)] |
| 7 |
|
| 8 |
#[macro_use] |
| 9 |
mod macros; |
| 10 |
|
| 11 |
use quote::quote; |
| 12 |
use syn::{DeriveInput, ItemFn, TypeParamBound, WhereClause, WherePredicate}; |
| 13 |
|
| 14 |
#[test] |
| 15 |
fn test_split_for_impl() { |
| 16 |
let input = quote! { |
| 17 |
struct S<'a, 'b: 'a, #[may_dangle] T: 'a = ()> where T: Debug; |
| 18 |
}; |
| 19 |
|
| 20 |
snapshot!(input as DeriveInput, @r#" |
| 21 |
DeriveInput { |
| 22 |
vis: Visibility::Inherited, |
| 23 |
ident: "S", |
| 24 |
generics: Generics { |
| 25 |
lt_token: Some, |
| 26 |
params: [ |
| 27 |
GenericParam::Lifetime(LifetimeParam { |
| 28 |
lifetime: Lifetime { |
| 29 |
ident: "a", |
| 30 |
}, |
| 31 |
}), |
| 32 |
Token![,], |
| 33 |
GenericParam::Lifetime(LifetimeParam { |
| 34 |
lifetime: Lifetime { |
| 35 |
ident: "b", |
| 36 |
}, |
| 37 |
colon_token: Some, |
| 38 |
bounds: [ |
| 39 |
Lifetime { |
| 40 |
ident: "a", |
| 41 |
}, |
| 42 |
], |
| 43 |
}), |
| 44 |
Token![,], |
| 45 |
GenericParam::Type(TypeParam { |
| 46 |
attrs: [ |
| 47 |
Attribute { |
| 48 |
style: AttrStyle::Outer, |
| 49 |
meta: Meta::Path { |
| 50 |
segments: [ |
| 51 |
PathSegment { |
| 52 |
ident: "may_dangle", |
| 53 |
}, |
| 54 |
], |
| 55 |
}, |
| 56 |
}, |
| 57 |
], |
| 58 |
ident: "T", |
| 59 |
colon_token: Some, |
| 60 |
bounds: [ |
| 61 |
TypeParamBound::Lifetime { |
| 62 |
ident: "a", |
| 63 |
}, |
| 64 |
], |
| 65 |
eq_token: Some, |
| 66 |
default: Some(Type::Tuple), |
| 67 |
}), |
| 68 |
], |
| 69 |
gt_token: Some, |
| 70 |
where_clause: Some(WhereClause { |
| 71 |
predicates: [ |
| 72 |
WherePredicate::Type(PredicateType { |
| 73 |
bounded_ty: Type::Path { |
| 74 |
path: Path { |
| 75 |
segments: [ |
| 76 |
PathSegment { |
| 77 |
ident: "T", |
| 78 |
}, |
| 79 |
], |
| 80 |
}, |
| 81 |
}, |
| 82 |
bounds: [ |
| 83 |
TypeParamBound::Trait(TraitBound { |
| 84 |
path: Path { |
| 85 |
segments: [ |
| 86 |
PathSegment { |
| 87 |
ident: "Debug", |
| 88 |
}, |
| 89 |
], |
| 90 |
}, |
| 91 |
}), |
| 92 |
], |
| 93 |
}), |
| 94 |
], |
| 95 |
}), |
| 96 |
}, |
| 97 |
data: Data::Struct { |
| 98 |
fields: Fields::Unit, |
| 99 |
semi_token: Some, |
| 100 |
}, |
| 101 |
} |
| 102 |
"#); |
| 103 |
|
| 104 |
let generics = input.generics; |
| 105 |
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); |
| 106 |
|
| 107 |
let generated = quote! { |
| 108 |
impl #impl_generics MyTrait for Test #ty_generics #where_clause {} |
| 109 |
}; |
| 110 |
let expected = quote! { |
| 111 |
impl<'a, 'b: 'a, #[may_dangle] T: 'a> MyTrait |
| 112 |
for Test<'a, 'b, T> |
| 113 |
where |
| 114 |
T: Debug |
| 115 |
{} |
| 116 |
}; |
| 117 |
assert_eq!(generated.to_string(), expected.to_string()); |
| 118 |
|
| 119 |
let turbofish = ty_generics.as_turbofish(); |
| 120 |
let generated = quote! { |
| 121 |
Test #turbofish |
| 122 |
}; |
| 123 |
let expected = quote! { |
| 124 |
Test::<'a, 'b, T> |
| 125 |
}; |
| 126 |
assert_eq!(generated.to_string(), expected.to_string()); |
| 127 |
} |
| 128 |
|
| 129 |
#[test] |
| 130 |
fn test_ty_param_bound() { |
| 131 |
let tokens = quote!('a); |
| 132 |
snapshot!(tokens as TypeParamBound, @r#" |
| 133 |
TypeParamBound::Lifetime { |
| 134 |
ident: "a", |
| 135 |
} |
| 136 |
"#); |
| 137 |
|
| 138 |
let tokens = quote!('_); |
| 139 |
snapshot!(tokens as TypeParamBound, @r#" |
| 140 |
TypeParamBound::Lifetime { |
| 141 |
ident: "_", |
| 142 |
} |
| 143 |
"#); |
| 144 |
|
| 145 |
let tokens = quote!(Debug); |
| 146 |
snapshot!(tokens as TypeParamBound, @r#" |
| 147 |
TypeParamBound::Trait(TraitBound { |
| 148 |
path: Path { |
| 149 |
segments: [ |
| 150 |
PathSegment { |
| 151 |
ident: "Debug", |
| 152 |
}, |
| 153 |
], |
| 154 |
}, |
| 155 |
}) |
| 156 |
"#); |
| 157 |
|
| 158 |
let tokens = quote!(?Sized); |
| 159 |
snapshot!(tokens as TypeParamBound, @r#" |
| 160 |
TypeParamBound::Trait(TraitBound { |
| 161 |
modifier: TraitBoundModifier::Maybe, |
| 162 |
path: Path { |
| 163 |
segments: [ |
| 164 |
PathSegment { |
| 165 |
ident: "Sized", |
| 166 |
}, |
| 167 |
], |
| 168 |
}, |
| 169 |
}) |
| 170 |
"#); |
| 171 |
} |
| 172 |
|
| 173 |
#[test] |
| 174 |
fn test_fn_precedence_in_where_clause() { |
| 175 |
// This should parse as two separate bounds, `FnOnce() -> i32` and `Send` - not |
| 176 |
// `FnOnce() -> (i32 + Send)`. |
| 177 |
let input = quote! { |
| 178 |
fn f<G>() |
| 179 |
where |
| 180 |
G: FnOnce() -> i32 + Send, |
| 181 |
{ |
| 182 |
} |
| 183 |
}; |
| 184 |
|
| 185 |
snapshot!(input as ItemFn, @r#" |
| 186 |
ItemFn { |
| 187 |
vis: Visibility::Inherited, |
| 188 |
sig: Signature { |
| 189 |
ident: "f", |
| 190 |
generics: Generics { |
| 191 |
lt_token: Some, |
| 192 |
params: [ |
| 193 |
GenericParam::Type(TypeParam { |
| 194 |
ident: "G", |
| 195 |
}), |
| 196 |
], |
| 197 |
gt_token: Some, |
| 198 |
where_clause: Some(WhereClause { |
| 199 |
predicates: [ |
| 200 |
WherePredicate::Type(PredicateType { |
| 201 |
bounded_ty: Type::Path { |
| 202 |
path: Path { |
| 203 |
segments: [ |
| 204 |
PathSegment { |
| 205 |
ident: "G", |
| 206 |
}, |
| 207 |
], |
| 208 |
}, |
| 209 |
}, |
| 210 |
bounds: [ |
| 211 |
TypeParamBound::Trait(TraitBound { |
| 212 |
path: Path { |
| 213 |
segments: [ |
| 214 |
PathSegment { |
| 215 |
ident: "FnOnce", |
| 216 |
arguments: PathArguments::Parenthesized { |
| 217 |
output: ReturnType::Type( |
| 218 |
Type::Path { |
| 219 |
path: Path { |
| 220 |
segments: [ |
| 221 |
PathSegment { |
| 222 |
ident: "i32", |
| 223 |
}, |
| 224 |
], |
| 225 |
}, |
| 226 |
}, |
| 227 |
), |
| 228 |
}, |
| 229 |
}, |
| 230 |
], |
| 231 |
}, |
| 232 |
}), |
| 233 |
Token![+], |
| 234 |
TypeParamBound::Trait(TraitBound { |
| 235 |
path: Path { |
| 236 |
segments: [ |
| 237 |
PathSegment { |
| 238 |
ident: "Send", |
| 239 |
}, |
| 240 |
], |
| 241 |
}, |
| 242 |
}), |
| 243 |
], |
| 244 |
}), |
| 245 |
Token![,], |
| 246 |
], |
| 247 |
}), |
| 248 |
}, |
| 249 |
output: ReturnType::Default, |
| 250 |
}, |
| 251 |
block: Block { |
| 252 |
stmts: [], |
| 253 |
}, |
| 254 |
} |
| 255 |
"#); |
| 256 |
|
| 257 |
let where_clause = input.sig.generics.where_clause.as_ref().unwrap(); |
| 258 |
assert_eq!(where_clause.predicates.len(), 1); |
| 259 |
|
| 260 |
let predicate = match &where_clause.predicates[0] { |
| 261 |
WherePredicate::Type(pred) => pred, |
| 262 |
_ => panic!("wrong predicate kind"), |
| 263 |
}; |
| 264 |
|
| 265 |
assert_eq!(predicate.bounds.len(), 2, "{:#?}", predicate.bounds); |
| 266 |
|
| 267 |
let first_bound = &predicate.bounds[0]; |
| 268 |
assert_eq!(quote!(#first_bound).to_string(), "FnOnce () -> i32"); |
| 269 |
|
| 270 |
let second_bound = &predicate.bounds[1]; |
| 271 |
assert_eq!(quote!(#second_bound).to_string(), "Send"); |
| 272 |
} |
| 273 |
|
| 274 |
#[test] |
| 275 |
fn test_where_clause_at_end_of_input() { |
| 276 |
let input = quote! { |
| 277 |
where |
| 278 |
}; |
| 279 |
|
| 280 |
snapshot!(input as WhereClause, @"WhereClause"); |
| 281 |
|
| 282 |
assert_eq!(input.predicates.len(), 0); |
| 283 |
} |