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