interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / vendor / syn-2.0.119 / tests / test_item.rs

371 lines · 10754 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]
10mod snapshot;
11
12mod debug;
13
14use proc_macro2::{Delimiter, Group, Ident, Span, TokenStream, TokenTree};
15use quote::quote;
16use syn::{Item, ItemTrait};
17
18#[test]
19fn 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 sig: Signature {
45 ident: "f",
46 generics: Generics,
47 output: ReturnType::Default,
48 },
49 block: Block {
50 stmts: [],
51 },
52 }
53 "#);
54}
55
56#[test]
57fn test_negative_impl() {
58 #[cfg(any())]
59 impl ! {}
60 let tokens = quote! {
61 impl ! {}
62 };
63 snapshot!(tokens as Item, @r#"
64 Item::Impl {
65 generics: Generics,
66 self_ty: Type::Never,
67 }
68 "#);
69
70 let tokens = quote! {
71 impl !Trait {}
72 };
73 let err = syn::parse2::<Item>(tokens).unwrap_err();
74 assert_eq!(err.to_string(), "inherent impls cannot be negative");
75
76 #[cfg(any())]
77 impl !Trait for T {}
78 let tokens = quote! {
79 impl !Trait for T {}
80 };
81 snapshot!(tokens as Item, @r#"
82 Item::Impl {
83 generics: Generics,
84 trait_: Some((
85 Some,
86 Path {
87 segments: [
88 PathSegment {
89 ident: "Trait",
90 },
91 ],
92 },
93 )),
94 self_ty: Type::Path {
95 path: Path {
96 segments: [
97 PathSegment {
98 ident: "T",
99 },
100 ],
101 },
102 },
103 }
104 "#);
105}
106
107#[test]
108fn test_macro_variable_impl() {
109 // mimics the token stream corresponding to `impl $trait for $ty {}`
110 let tokens = TokenStream::from_iter([
111 TokenTree::Ident(Ident::new("impl", Span::call_site())),
112 TokenTree::Group(Group::new(Delimiter::None, quote!(Trait))),
113 TokenTree::Ident(Ident::new("for", Span::call_site())),
114 TokenTree::Group(Group::new(Delimiter::None, quote!(Type))),
115 TokenTree::Group(Group::new(Delimiter::Brace, TokenStream::new())),
116 ]);
117
118 snapshot!(tokens as Item, @r#"
119 Item::Impl {
120 generics: Generics,
121 trait_: Some((
122 None,
123 Path {
124 segments: [
125 PathSegment {
126 ident: "Trait",
127 },
128 ],
129 },
130 )),
131 self_ty: Type::Group {
132 elem: Type::Path {
133 path: Path {
134 segments: [
135 PathSegment {
136 ident: "Type",
137 },
138 ],
139 },
140 },
141 },
142 }
143 "#);
144}
145
146#[test]
147fn test_supertraits() {
148 // Rustc parses all of the following.
149
150 #[rustfmt::skip]
151 let tokens = quote!(trait Trait where {});
152 snapshot!(tokens as ItemTrait, @r#"
153 ItemTrait {
154 vis: Visibility::Inherited,
155 ident: "Trait",
156 generics: Generics {
157 where_clause: Some(WhereClause),
158 },
159 }
160 "#);
161
162 #[rustfmt::skip]
163 let tokens = quote!(trait Trait: where {});
164 snapshot!(tokens as ItemTrait, @r#"
165 ItemTrait {
166 vis: Visibility::Inherited,
167 ident: "Trait",
168 generics: Generics {
169 where_clause: Some(WhereClause),
170 },
171 colon_token: Some,
172 }
173 "#);
174
175 #[rustfmt::skip]
176 let tokens = quote!(trait Trait: Sized where {});
177 snapshot!(tokens as ItemTrait, @r#"
178 ItemTrait {
179 vis: Visibility::Inherited,
180 ident: "Trait",
181 generics: Generics {
182 where_clause: Some(WhereClause),
183 },
184 colon_token: Some,
185 supertraits: [
186 TypeParamBound::Trait(TraitBound {
187 path: Path {
188 segments: [
189 PathSegment {
190 ident: "Sized",
191 },
192 ],
193 },
194 }),
195 ],
196 }
197 "#);
198
199 #[rustfmt::skip]
200 let tokens = quote!(trait Trait: Sized + where {});
201 snapshot!(tokens as ItemTrait, @r#"
202 ItemTrait {
203 vis: Visibility::Inherited,
204 ident: "Trait",
205 generics: Generics {
206 where_clause: Some(WhereClause),
207 },
208 colon_token: Some,
209 supertraits: [
210 TypeParamBound::Trait(TraitBound {
211 path: Path {
212 segments: [
213 PathSegment {
214 ident: "Sized",
215 },
216 ],
217 },
218 }),
219 Token![+],
220 ],
221 }
222 "#);
223}
224
225#[test]
226fn test_type_empty_bounds() {
227 #[rustfmt::skip]
228 let tokens = quote! {
229 trait Foo {
230 type Bar: ;
231 }
232 };
233
234 snapshot!(tokens as ItemTrait, @r#"
235 ItemTrait {
236 vis: Visibility::Inherited,
237 ident: "Foo",
238 generics: Generics,
239 items: [
240 TraitItem::Type {
241 ident: "Bar",
242 generics: Generics,
243 colon_token: Some,
244 },
245 ],
246 }
247 "#);
248}
249
250#[test]
251fn test_impl_visibility() {
252 let tokens = quote! {
253 pub default unsafe impl union {}
254 };
255
256 snapshot!(tokens as Item, @"Item::Verbatim(`pub default unsafe impl union { }`)");
257}
258
259#[test]
260fn test_impl_type_parameter_defaults() {
261 #[cfg(any())]
262 impl<T = ()> () {}
263 let tokens = quote! {
264 impl<T = ()> () {}
265 };
266 snapshot!(tokens as Item, @r#"
267 Item::Impl {
268 generics: Generics {
269 lt_token: Some,
270 params: [
271 GenericParam::Type(TypeParam {
272 ident: "T",
273 eq_token: Some,
274 default: Some(Type::Tuple),
275 }),
276 ],
277 gt_token: Some,
278 },
279 self_ty: Type::Tuple,
280 }
281 "#);
282}
283
284#[test]
285fn 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 sig: Signature {
294 ident: "f",
295 generics: Generics,
296 output: ReturnType::Type(
297 Type::ImplTrait {
298 bounds: [
299 TypeParamBound::Trait(TraitBound {
300 path: Path {
301 segments: [
302 PathSegment {
303 ident: "Sized",
304 },
305 ],
306 },
307 }),
308 Token![+],
309 ],
310 },
311 ),
312 },
313 block: Block {
314 stmts: [],
315 },
316 }
317 "#);
318}
319
320// Regression test for issue https://github.com/dtolnay/syn/issues/1967
321#[test]
322fn test_nested_receiver_classification() {
323 let tokens = quote! {
324 fn foo(
325 self: foo<{ fn foo(
326 self: foo<{ fn foo(
327 self: foo<{ 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 )}>
347 )}>
348 )}>
349 )}>
350 )}>
351 )}>
352 )}>
353 )}>
354 )}>
355 )}>
356 )}>
357 )}>
358 )}>
359 )}>
360 )}>
361 )}>
362 )}>
363 )}>
364 )}>
365 )}>
366 )}>
367 ) {}
368 };
369
370 let _ = syn::parse2::<syn::File>(tokens);
371}