Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/vendor/syn/tests/test_item.rs # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / vendor / syn / tests / test_item.rs` 374 lines ยท 11067 bytes Raw Blame History 1 #![recursion_limit = "256"] 2 #![feature(negative_impls)] 3 #![allow( 4 clippy::elidable_lifetime_names, 5 clippy::needless_lifetimes, 6 clippy::uninlined_format_args 7 )] 8 9 #[macro_use] 10 mod snapshot; 11 12 mod debug; 13 14 use proc_macro2::{Delimiter, Group, Ident, Span, TokenStream, TokenTree}; 15 use quote::quote; 16 use syn::{Item, ItemTrait}; 17 18 #[test] 19 fn test_macro_variable_attr() { 20 // mimics the token stream corresponding to `$attr fn f() {}` 21 let tokens = TokenStream::from_iter([ 22 TokenTree::Group(Group::new(Delimiter::None, quote! { #[test] })), 23 TokenTree::Ident(Ident::new("fn", Span::call_site())), 24 TokenTree::Ident(Ident::new("f", Span::call_site())), 25 TokenTree::Group(Group::new(Delimiter::Parenthesis, TokenStream::new())), 26 TokenTree::Group(Group::new(Delimiter::Brace, TokenStream::new())), 27 ]); 28 29 snapshot!(tokens as Item, @r#" 30 Item::Fn { 31 attrs: [ 32 Attribute { 33 style: AttrStyle::Outer, 34 meta: Meta::Path { 35 segments: [ 36 PathSegment { 37 ident: "test", 38 }, 39 ], 40 }, 41 }, 42 ], 43 vis: Visibility::Inherited, 44 modifiers: FnModifiers, 45 sig: Signature { 46 safety: Safety::Default, 47 ident: "f", 48 generics: Generics, 49 output: ReturnType::Default, 50 }, 51 block: Block { 52 stmts: [], 53 }, 54 } 55 "#); 56 } 57 58 #[test] 59 fn test_negative_impl() { 60 #[cfg(any())] 61 impl ! {} 62 let tokens = quote! { 63 impl ! {} 64 }; 65 snapshot!(tokens as Item, @" 66 Item::Impl { 67 modifiers: ImplModifiers, 68 generics: Generics, 69 self_ty: Type::Never, 70 } 71 "); 72 73 let tokens = quote! { 74 impl !Trait {} 75 }; 76 let err = syn::parse2::(tokens).unwrap_err(); 77 assert_eq!(err.to_string(), "inherent impls cannot be negative"); 78 79 #[cfg(any())] 80 impl !Trait for T {} 81 let tokens = quote! { 82 impl !Trait for T {} 83 }; 84 snapshot!(tokens as Item, @r#" 85 Item::Impl { 86 modifiers: ImplModifiers { 87 polarity: Some, 88 }, 89 generics: Generics, 90 trait_: Some(Path { 91 segments: [ 92 PathSegment { 93 ident: "Trait", 94 }, 95 ], 96 }), 97 self_ty: Type::Path { 98 path: Path { 99 segments: [ 100 PathSegment { 101 ident: "T", 102 }, 103 ], 104 }, 105 }, 106 } 107 "#); 108 } 109 110 #[test] 111 fn test_macro_variable_impl() { 112 // mimics the token stream corresponding to `impl $trait for $ty {}` 113 let tokens = TokenStream::from_iter([ 114 TokenTree::Ident(Ident::new("impl", Span::call_site())), 115 TokenTree::Group(Group::new(Delimiter::None, quote!(Trait))), 116 TokenTree::Ident(Ident::new("for", Span::call_site())), 117 TokenTree::Group(Group::new(Delimiter::None, quote!(Type))), 118 TokenTree::Group(Group::new(Delimiter::Brace, TokenStream::new())), 119 ]); 120 121 snapshot!(tokens as Item, @r#" 122 Item::Impl { 123 modifiers: ImplModifiers, 124 generics: Generics, 125 trait_: Some(Path { 126 segments: [ 127 PathSegment { 128 ident: "Trait", 129 }, 130 ], 131 }), 132 self_ty: Type::Group { 133 elem: Type::Path { 134 path: Path { 135 segments: [ 136 PathSegment { 137 ident: "Type", 138 }, 139 ], 140 }, 141 }, 142 }, 143 } 144 "#); 145 } 146 147 #[test] 148 fn test_supertraits() { 149 // Rustc parses all of the following. 150 151 #[rustfmt::skip] 152 let tokens = quote!(trait Trait where {}); 153 snapshot!(tokens as ItemTrait, @r#" 154 ItemTrait { 155 vis: Visibility::Inherited, 156 modifiers: TraitModifiers, 157 ident: "Trait", 158 generics: Generics { 159 where_clause: Some(WhereClause), 160 }, 161 } 162 "#); 163 164 #[rustfmt::skip] 165 let tokens = quote!(trait Trait: where {}); 166 snapshot!(tokens as ItemTrait, @r#" 167 ItemTrait { 168 vis: Visibility::Inherited, 169 modifiers: TraitModifiers, 170 ident: "Trait", 171 generics: Generics { 172 where_clause: Some(WhereClause), 173 }, 174 colon_token: Some, 175 } 176 "#); 177 178 #[rustfmt::skip] 179 let tokens = quote!(trait Trait: Sized where {}); 180 snapshot!(tokens as ItemTrait, @r#" 181 ItemTrait { 182 vis: Visibility::Inherited, 183 modifiers: TraitModifiers, 184 ident: "Trait", 185 generics: Generics { 186 where_clause: Some(WhereClause), 187 }, 188 colon_token: Some, 189 supertraits: [ 190 TypeParamBound::Trait(TraitBound { 191 modifiers: TraitBoundModifiers, 192 path: Path { 193 segments: [ 194 PathSegment { 195 ident: "Sized", 196 }, 197 ], 198 }, 199 }), 200 ], 201 } 202 "#); 203 204 #[rustfmt::skip] 205 let tokens = quote!(trait Trait: Sized + where {}); 206 snapshot!(tokens as ItemTrait, @r#" 207 ItemTrait { 208 vis: Visibility::Inherited, 209 modifiers: TraitModifiers, 210 ident: "Trait", 211 generics: Generics { 212 where_clause: Some(WhereClause), 213 }, 214 colon_token: Some, 215 supertraits: [ 216 TypeParamBound::Trait(TraitBound { 217 modifiers: TraitBoundModifiers, 218 path: Path { 219 segments: [ 220 PathSegment { 221 ident: "Sized", 222 }, 223 ], 224 }, 225 }), 226 Token![+], 227 ], 228 } 229 "#); 230 } 231 232 #[test] 233 fn test_type_empty_bounds() { 234 #[rustfmt::skip] 235 let tokens = quote! { 236 trait Foo { 237 type Bar: ; 238 } 239 }; 240 241 snapshot!(tokens as ItemTrait, @r#" 242 ItemTrait { 243 vis: Visibility::Inherited, 244 modifiers: TraitModifiers, 245 ident: "Foo", 246 generics: Generics, 247 items: [ 248 TraitItem::Type { 249 modifiers: TypeModifiers, 250 ident: "Bar", 251 generics: Generics, 252 colon_token: Some, 253 }, 254 ], 255 } 256 "#); 257 } 258 259 #[test] 260 fn test_impl_type_parameter_defaults() { 261 #[cfg(any())] 262 impl () {} 263 let tokens = quote! { 264 impl () {} 265 }; 266 snapshot!(tokens as Item, @r#" 267 Item::Impl { 268 modifiers: ImplModifiers, 269 generics: Generics { 270 lt_token: Some, 271 params: [ 272 GenericParam::Type(TypeParam { 273 ident: "T", 274 default: Some(Type::Tuple), 275 }), 276 ], 277 gt_token: Some, 278 }, 279 self_ty: Type::Tuple, 280 } 281 "#); 282 } 283 284 #[test] 285 fn test_impl_trait_trailing_plus() { 286 let tokens = quote! { 287 fn f() -> impl Sized + {} 288 }; 289 290 snapshot!(tokens as Item, @r#" 291 Item::Fn { 292 vis: Visibility::Inherited, 293 modifiers: FnModifiers, 294 sig: Signature { 295 safety: Safety::Default, 296 ident: "f", 297 generics: Generics, 298 output: ReturnType::Type( 299 Type::ImplTrait { 300 bounds: [ 301 TypeParamBound::Trait(TraitBound { 302 modifiers: TraitBoundModifiers, 303 path: Path { 304 segments: [ 305 PathSegment { 306 ident: "Sized", 307 }, 308 ], 309 }, 310 }), 311 Token![+], 312 ], 313 }, 314 ), 315 }, 316 block: Block { 317 stmts: [], 318 }, 319 } 320 "#); 321 } 322 323 // Regression test for issue https://github.com/dtolnay/syn/issues/1967 324 #[test] 325 fn test_nested_receiver_classification() { 326 let tokens = quote! { 327 fn foo( 328 self: foo<{ fn foo( 329 self: foo<{ fn foo( 330 self: foo<{ fn foo( 331 self: foo<{ fn foo( 332 self: foo<{ fn foo( 333 self: foo<{ fn foo( 334 self: foo<{ fn foo( 335 self: foo<{ fn foo( 336 self: foo<{ fn foo( 337 self: foo<{ fn foo( 338 self: foo<{ fn foo( 339 self: foo<{ fn foo( 340 self: foo<{ fn foo( 341 self: foo<{ fn foo( 342 self: foo<{ fn foo( 343 self: foo<{ fn foo( 344 self: foo<{ fn foo( 345 self: foo<{ fn foo( 346 self: foo<{ fn foo( 347 self: foo<{ fn foo( 348 self: foo<{ fn foo( 349 )}> 350 )}> 351 )}> 352 )}> 353 )}> 354 )}> 355 )}> 356 )}> 357 )}> 358 )}> 359 )}> 360 )}> 361 )}> 362 )}> 363 )}> 364 )}> 365 )}> 366 )}> 367 )}> 368 )}> 369 )}> 370 ) {} 371 }; 372 373 let _ = syn::parse2::(tokens); 374 }