aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / vendor / syn / tests / test_generics.rs
| 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 | default: Some(Type::Tuple), |
| 72 | }), |
| 73 | ], |
| 74 | gt_token: Some, |
| 75 | where_clause: Some(WhereClause { |
| 76 | predicates: [ |
| 77 | WherePredicate::Type(PredicateType { |
| 78 | bounded_ty: Type::Path { |
| 79 | path: Path { |
| 80 | segments: [ |
| 81 | PathSegment { |
| 82 | ident: "T", |
| 83 | }, |
| 84 | ], |
| 85 | }, |
| 86 | }, |
| 87 | bounds: [ |
| 88 | TypeParamBound::Trait(TraitBound { |
| 89 | modifiers: TraitBoundModifiers, |
| 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 | modifiers: TraitBoundModifiers, |
| 155 | path: Path { |
| 156 | segments: [ |
| 157 | PathSegment { |
| 158 | ident: "Debug", |
| 159 | }, |
| 160 | ], |
| 161 | }, |
| 162 | }) |
| 163 | "#); |
| 164 | |
| 165 | let tokens = quote!(?Sized); |
| 166 | snapshot!(tokens as TypeParamBound, @r#" |
| 167 | TypeParamBound::Trait(TraitBound { |
| 168 | modifiers: TraitBoundModifiers, |
| 169 | maybe: Some, |
| 170 | path: Path { |
| 171 | segments: [ |
| 172 | PathSegment { |
| 173 | ident: "Sized", |
| 174 | }, |
| 175 | ], |
| 176 | }, |
| 177 | }) |
| 178 | "#); |
| 179 | |
| 180 | let tokens = quote!(for<'a> Trait); |
| 181 | snapshot!(tokens as TypeParamBound, @r#" |
| 182 | TypeParamBound::Trait(TraitBound { |
| 183 | lifetimes: Some(BoundLifetimes { |
| 184 | lifetimes: [ |
| 185 | GenericParam::Lifetime(LifetimeParam { |
| 186 | lifetime: Lifetime { |
| 187 | ident: "a", |
| 188 | }, |
| 189 | }), |
| 190 | ], |
| 191 | }), |
| 192 | modifiers: TraitBoundModifiers, |
| 193 | path: Path { |
| 194 | segments: [ |
| 195 | PathSegment { |
| 196 | ident: "Trait", |
| 197 | }, |
| 198 | ], |
| 199 | }, |
| 200 | }) |
| 201 | "#); |
| 202 | |
| 203 | let tokens = quote!(for<> ?Trait); |
| 204 | let err = syn::parse2::<TypeParamBound>(tokens).unwrap_err(); |
| 205 | assert_eq!( |
| 206 | "`for<...>` binder not allowed with `?` trait polarity modifier", |
| 207 | err.to_string(), |
| 208 | ); |
| 209 | |
| 210 | let tokens = quote!(?for<> Trait); |
| 211 | let err = syn::parse2::<TypeParamBound>(tokens).unwrap_err(); |
| 212 | assert_eq!( |
| 213 | "`for<...>` binder not allowed with `?` trait polarity modifier", |
| 214 | err.to_string(), |
| 215 | ); |
| 216 | } |
| 217 | |
| 218 | #[test] |
| 219 | fn test_fn_precedence_in_where_clause() { |
| 220 | // This should parse as two separate bounds, `FnOnce() -> i32` and `Send` - not |
| 221 | // `FnOnce() -> (i32 + Send)`. |
| 222 | let input = quote! { |
| 223 | fn f<G>() |
| 224 | where |
| 225 | G: FnOnce() -> i32 + Send, |
| 226 | { |
| 227 | } |
| 228 | }; |
| 229 | |
| 230 | snapshot!(input as ItemFn, @r#" |
| 231 | ItemFn { |
| 232 | vis: Visibility::Inherited, |
| 233 | modifiers: FnModifiers, |
| 234 | sig: Signature { |
| 235 | safety: Safety::Default, |
| 236 | ident: "f", |
| 237 | generics: Generics { |
| 238 | lt_token: Some, |
| 239 | params: [ |
| 240 | GenericParam::Type(TypeParam { |
| 241 | ident: "G", |
| 242 | }), |
| 243 | ], |
| 244 | gt_token: Some, |
| 245 | where_clause: Some(WhereClause { |
| 246 | predicates: [ |
| 247 | WherePredicate::Type(PredicateType { |
| 248 | bounded_ty: Type::Path { |
| 249 | path: Path { |
| 250 | segments: [ |
| 251 | PathSegment { |
| 252 | ident: "G", |
| 253 | }, |
| 254 | ], |
| 255 | }, |
| 256 | }, |
| 257 | bounds: [ |
| 258 | TypeParamBound::Trait(TraitBound { |
| 259 | modifiers: TraitBoundModifiers, |
| 260 | path: Path { |
| 261 | segments: [ |
| 262 | PathSegment { |
| 263 | ident: "FnOnce", |
| 264 | arguments: PathArguments::Parenthesized { |
| 265 | output: ReturnType::Type( |
| 266 | Type::Path { |
| 267 | path: Path { |
| 268 | segments: [ |
| 269 | PathSegment { |
| 270 | ident: "i32", |
| 271 | }, |
| 272 | ], |
| 273 | }, |
| 274 | }, |
| 275 | ), |
| 276 | }, |
| 277 | }, |
| 278 | ], |
| 279 | }, |
| 280 | }), |
| 281 | Token![+], |
| 282 | TypeParamBound::Trait(TraitBound { |
| 283 | modifiers: TraitBoundModifiers, |
| 284 | path: Path { |
| 285 | segments: [ |
| 286 | PathSegment { |
| 287 | ident: "Send", |
| 288 | }, |
| 289 | ], |
| 290 | }, |
| 291 | }), |
| 292 | ], |
| 293 | }), |
| 294 | Token![,], |
| 295 | ], |
| 296 | }), |
| 297 | }, |
| 298 | output: ReturnType::Default, |
| 299 | }, |
| 300 | block: Block { |
| 301 | stmts: [], |
| 302 | }, |
| 303 | } |
| 304 | "#); |
| 305 | |
| 306 | let where_clause = input.sig.generics.where_clause.as_ref().unwrap(); |
| 307 | assert_eq!(where_clause.predicates.len(), 1); |
| 308 | |
| 309 | let predicate = match &where_clause.predicates[0] { |
| 310 | WherePredicate::Type(pred) => pred, |
| 311 | _ => panic!("wrong predicate kind"), |
| 312 | }; |
| 313 | |
| 314 | assert_eq!(predicate.bounds.len(), 2, "{:#?}", predicate.bounds); |
| 315 | |
| 316 | let first_bound = &predicate.bounds[0]; |
| 317 | assert_eq!(quote!(#first_bound).to_string(), "FnOnce () -> i32"); |
| 318 | |
| 319 | let second_bound = &predicate.bounds[1]; |
| 320 | assert_eq!(quote!(#second_bound).to_string(), "Send"); |
| 321 | } |
| 322 | |
| 323 | #[test] |
| 324 | fn test_where_clause_at_end_of_input() { |
| 325 | let input = quote! { |
| 326 | where |
| 327 | }; |
| 328 | |
| 329 | snapshot!(input as WhereClause, @"WhereClause"); |
| 330 | |
| 331 | assert_eq!(input.predicates.len(), 0); |
| 332 | } |
| 333 | |
| 334 | // Regression test for https://github.com/dtolnay/syn/issues/1718 |
| 335 | #[test] |
| 336 | #[allow(clippy::map_unwrap_or)] |
| 337 | fn no_opaque_drop() { |
| 338 | let mut generics = Generics::default(); |
| 339 | |
| 340 | let _ = generics |
| 341 | .lifetimes() |
| 342 | .next() |
| 343 | .map(|param| param.lifetime.clone()) |
| 344 | .unwrap_or_else(|| { |
| 345 | let lifetime: Lifetime = parse_quote!('a); |
| 346 | generics.params.insert( |
| 347 | 0, |
| 348 | GenericParam::Lifetime(LifetimeParam::new(lifetime.clone())), |
| 349 | ); |
| 350 | lifetime |
| 351 | }); |
| 352 | } |
| 353 | |
| 354 | #[test] |
| 355 | fn type_param_with_colon_and_no_bounds() { |
| 356 | let tokens = quote!(T:); |
| 357 | snapshot!(tokens as GenericParam, @r#" |
| 358 | GenericParam::Type(TypeParam { |
| 359 | ident: "T", |
| 360 | colon_token: Some, |
| 361 | }) |
| 362 | "#); |
| 363 | } |