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 / tests / test_ty.rs

479 lines · 14511 bytes Raw Blame History
1#![allow(
2 clippy::elidable_lifetime_names,
3 clippy::needless_lifetimes,
4 clippy::uninlined_format_args
5)]
6
7#[macro_use]
8mod snapshot;
9
10mod debug;
11
12use proc_macro2::{Delimiter, Group, Ident, Punct, Spacing, Span, TokenStream, TokenTree};
13use quote::{quote, ToTokens as _};
14use syn::punctuated::Punctuated;
15use syn::{parse_quote, token, Token, Type, TypeTuple};
16
17#[test]
18fn test_mut_self() {
19 syn::parse_str::<Type>("fn(mut self)").unwrap();
20 syn::parse_str::<Type>("fn(mut self,)").unwrap();
21 syn::parse_str::<Type>("fn(mut self: ())").unwrap();
22 syn::parse_str::<Type>("fn(mut self: ...)").unwrap_err();
23 syn::parse_str::<Type>("fn(mut self: mut self)").unwrap_err();
24 syn::parse_str::<Type>("fn(mut self::T)").unwrap_err();
25}
26
27#[test]
28fn test_macro_variable_type() {
29 // mimics the token stream corresponding to `$ty<T>`
30 let tokens = TokenStream::from_iter([
31 TokenTree::Group(Group::new(Delimiter::None, quote! { ty })),
32 TokenTree::Punct(Punct::new('<', Spacing::Alone)),
33 TokenTree::Ident(Ident::new("T", Span::call_site())),
34 TokenTree::Punct(Punct::new('>', Spacing::Alone)),
35 ]);
36
37 snapshot!(tokens as Type, @r#"
38 Type::Path {
39 path: Path {
40 segments: [
41 PathSegment {
42 ident: "ty",
43 arguments: PathArguments::AngleBracketed {
44 args: [
45 GenericArgument::Type(Type::Path {
46 path: Path {
47 segments: [
48 PathSegment {
49 ident: "T",
50 },
51 ],
52 },
53 }),
54 ],
55 },
56 },
57 ],
58 },
59 }
60 "#);
61
62 // mimics the token stream corresponding to `$ty::<T>`
63 let tokens = TokenStream::from_iter([
64 TokenTree::Group(Group::new(Delimiter::None, quote! { ty })),
65 TokenTree::Punct(Punct::new(':', Spacing::Joint)),
66 TokenTree::Punct(Punct::new(':', Spacing::Alone)),
67 TokenTree::Punct(Punct::new('<', Spacing::Alone)),
68 TokenTree::Ident(Ident::new("T", Span::call_site())),
69 TokenTree::Punct(Punct::new('>', Spacing::Alone)),
70 ]);
71
72 snapshot!(tokens as Type, @r#"
73 Type::Path {
74 path: Path {
75 segments: [
76 PathSegment {
77 ident: "ty",
78 arguments: PathArguments::AngleBracketed {
79 colon2_token: Some,
80 args: [
81 GenericArgument::Type(Type::Path {
82 path: Path {
83 segments: [
84 PathSegment {
85 ident: "T",
86 },
87 ],
88 },
89 }),
90 ],
91 },
92 },
93 ],
94 },
95 }
96 "#);
97}
98
99#[test]
100fn test_group_angle_brackets() {
101 // mimics the token stream corresponding to `Option<$ty>`
102 let tokens = TokenStream::from_iter([
103 TokenTree::Ident(Ident::new("Option", Span::call_site())),
104 TokenTree::Punct(Punct::new('<', Spacing::Alone)),
105 TokenTree::Group(Group::new(Delimiter::None, quote! { Vec<u8> })),
106 TokenTree::Punct(Punct::new('>', Spacing::Alone)),
107 ]);
108
109 snapshot!(tokens as Type, @r#"
110 Type::Path {
111 path: Path {
112 segments: [
113 PathSegment {
114 ident: "Option",
115 arguments: PathArguments::AngleBracketed {
116 args: [
117 GenericArgument::Type(Type::Group {
118 elem: Type::Path {
119 path: Path {
120 segments: [
121 PathSegment {
122 ident: "Vec",
123 arguments: PathArguments::AngleBracketed {
124 args: [
125 GenericArgument::Type(Type::Path {
126 path: Path {
127 segments: [
128 PathSegment {
129 ident: "u8",
130 },
131 ],
132 },
133 }),
134 ],
135 },
136 },
137 ],
138 },
139 },
140 }),
141 ],
142 },
143 },
144 ],
145 },
146 }
147 "#);
148}
149
150#[test]
151fn test_group_colons() {
152 // mimics the token stream corresponding to `$ty::Item`
153 let tokens = TokenStream::from_iter([
154 TokenTree::Group(Group::new(Delimiter::None, quote! { Vec<u8> })),
155 TokenTree::Punct(Punct::new(':', Spacing::Joint)),
156 TokenTree::Punct(Punct::new(':', Spacing::Alone)),
157 TokenTree::Ident(Ident::new("Item", Span::call_site())),
158 ]);
159
160 snapshot!(tokens as Type, @r#"
161 Type::Path {
162 path: Path {
163 segments: [
164 PathSegment {
165 ident: "Vec",
166 arguments: PathArguments::AngleBracketed {
167 args: [
168 GenericArgument::Type(Type::Path {
169 path: Path {
170 segments: [
171 PathSegment {
172 ident: "u8",
173 },
174 ],
175 },
176 }),
177 ],
178 },
179 },
180 Token![::],
181 PathSegment {
182 ident: "Item",
183 },
184 ],
185 },
186 }
187 "#);
188
189 let tokens = TokenStream::from_iter([
190 TokenTree::Group(Group::new(Delimiter::None, quote! { [T] })),
191 TokenTree::Punct(Punct::new(':', Spacing::Joint)),
192 TokenTree::Punct(Punct::new(':', Spacing::Alone)),
193 TokenTree::Ident(Ident::new("Element", Span::call_site())),
194 ]);
195
196 snapshot!(tokens as Type, @r#"
197 Type::Path {
198 qself: Some(QSelf {
199 ty: Type::Slice {
200 elem: Type::Path {
201 path: Path {
202 segments: [
203 PathSegment {
204 ident: "T",
205 },
206 ],
207 },
208 },
209 },
210 position: 0,
211 }),
212 path: Path {
213 leading_colon: Some,
214 segments: [
215 PathSegment {
216 ident: "Element",
217 },
218 ],
219 },
220 }
221 "#);
222}
223
224#[test]
225fn test_trait_object() {
226 let tokens = quote!(dyn for<'a> Trait<'a> + 'static);
227 snapshot!(tokens as Type, @r#"
228 Type::TraitObject {
229 dyn_token: Some,
230 bounds: [
231 TypeParamBound::Trait(TraitBound {
232 lifetimes: Some(BoundLifetimes {
233 lifetimes: [
234 GenericParam::Lifetime(LifetimeParam {
235 lifetime: Lifetime {
236 ident: "a",
237 },
238 }),
239 ],
240 }),
241 modifiers: TraitBoundModifiers,
242 path: Path {
243 segments: [
244 PathSegment {
245 ident: "Trait",
246 arguments: PathArguments::AngleBracketed {
247 args: [
248 GenericArgument::Lifetime(Lifetime {
249 ident: "a",
250 }),
251 ],
252 },
253 },
254 ],
255 },
256 }),
257 Token![+],
258 TypeParamBound::Lifetime {
259 ident: "static",
260 },
261 ],
262 }
263 "#);
264
265 let tokens = quote!(dyn 'a + Trait);
266 snapshot!(tokens as Type, @r#"
267 Type::TraitObject {
268 dyn_token: Some,
269 bounds: [
270 TypeParamBound::Lifetime {
271 ident: "a",
272 },
273 Token![+],
274 TypeParamBound::Trait(TraitBound {
275 modifiers: TraitBoundModifiers,
276 path: Path {
277 segments: [
278 PathSegment {
279 ident: "Trait",
280 },
281 ],
282 },
283 }),
284 ],
285 }
286 "#);
287
288 // None of the following are valid Rust types.
289 syn::parse_str::<Type>("for<'a> dyn Trait<'a>").unwrap_err();
290 syn::parse_str::<Type>("dyn for<'a> 'a + Trait").unwrap_err();
291}
292
293#[test]
294fn test_trailing_plus() {
295 #[rustfmt::skip]
296 let tokens = quote!(impl Trait +);
297 snapshot!(tokens as Type, @r#"
298 Type::ImplTrait {
299 bounds: [
300 TypeParamBound::Trait(TraitBound {
301 modifiers: TraitBoundModifiers,
302 path: Path {
303 segments: [
304 PathSegment {
305 ident: "Trait",
306 },
307 ],
308 },
309 }),
310 Token![+],
311 ],
312 }
313 "#);
314
315 #[rustfmt::skip]
316 let tokens = quote!(dyn Trait +);
317 snapshot!(tokens as Type, @r#"
318 Type::TraitObject {
319 dyn_token: Some,
320 bounds: [
321 TypeParamBound::Trait(TraitBound {
322 modifiers: TraitBoundModifiers,
323 path: Path {
324 segments: [
325 PathSegment {
326 ident: "Trait",
327 },
328 ],
329 },
330 }),
331 Token![+],
332 ],
333 }
334 "#);
335
336 #[rustfmt::skip]
337 let tokens = quote!(Trait +);
338 snapshot!(tokens as Type, @r#"
339 Type::TraitObject {
340 bounds: [
341 TypeParamBound::Trait(TraitBound {
342 modifiers: TraitBoundModifiers,
343 path: Path {
344 segments: [
345 PathSegment {
346 ident: "Trait",
347 },
348 ],
349 },
350 }),
351 Token![+],
352 ],
353 }
354 "#);
355}
356
357#[test]
358fn test_tuple_comma() {
359 let mut expr = TypeTuple {
360 attrs: Vec::new(),
361 paren_token: token::Paren::default(),
362 elems: Punctuated::new(),
363 };
364 snapshot!(expr.to_token_stream() as Type, @"Type::Tuple");
365
366 expr.elems.push_value(parse_quote!(_));
367 // Must not parse to Type::Paren
368 snapshot!(expr.to_token_stream() as Type, @"
369 Type::Tuple {
370 elems: [
371 Type::Infer,
372 Token![,],
373 ],
374 }
375 ");
376
377 expr.elems.push_punct(<Token![,]>::default());
378 snapshot!(expr.to_token_stream() as Type, @"
379 Type::Tuple {
380 elems: [
381 Type::Infer,
382 Token![,],
383 ],
384 }
385 ");
386
387 expr.elems.push_value(parse_quote!(_));
388 snapshot!(expr.to_token_stream() as Type, @"
389 Type::Tuple {
390 elems: [
391 Type::Infer,
392 Token![,],
393 Type::Infer,
394 ],
395 }
396 ");
397
398 expr.elems.push_punct(<Token![,]>::default());
399 snapshot!(expr.to_token_stream() as Type, @"
400 Type::Tuple {
401 elems: [
402 Type::Infer,
403 Token![,],
404 Type::Infer,
405 Token![,],
406 ],
407 }
408 ");
409}
410
411#[test]
412fn test_impl_trait_use() {
413 let tokens = quote! {
414 impl Sized + use<'_, 'a, A, Test>
415 };
416
417 snapshot!(tokens as Type, @r#"
418 Type::ImplTrait {
419 bounds: [
420 TypeParamBound::Trait(TraitBound {
421 modifiers: TraitBoundModifiers,
422 path: Path {
423 segments: [
424 PathSegment {
425 ident: "Sized",
426 },
427 ],
428 },
429 }),
430 Token![+],
431 TypeParamBound::PreciseCapture(PreciseCapture {
432 params: [
433 CapturedParam::Lifetime(Lifetime {
434 ident: "_",
435 }),
436 Token![,],
437 CapturedParam::Lifetime(Lifetime {
438 ident: "a",
439 }),
440 Token![,],
441 CapturedParam::Ident("A"),
442 Token![,],
443 CapturedParam::Ident("Test"),
444 ],
445 }),
446 ],
447 }
448 "#);
449
450 let trailing = quote! {
451 impl Sized + use<'_,>
452 };
453
454 snapshot!(trailing as Type, @r#"
455 Type::ImplTrait {
456 bounds: [
457 TypeParamBound::Trait(TraitBound {
458 modifiers: TraitBoundModifiers,
459 path: Path {
460 segments: [
461 PathSegment {
462 ident: "Sized",
463 },
464 ],
465 },
466 }),
467 Token![+],
468 TypeParamBound::PreciseCapture(PreciseCapture {
469 params: [
470 CapturedParam::Lifetime(Lifetime {
471 ident: "_",
472 }),
473 Token![,],
474 ],
475 }),
476 ],
477 }
478 "#);
479}